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 <telepathy-logger/text-event.h>
42 #include <libempathy/empathy-contact-list.h>
43 #include <libempathy/empathy-gsettings.h>
44 #include <libempathy/empathy-keyring.h>
45 #include <libempathy/empathy-utils.h>
46 #include <libempathy/empathy-request-util.h>
47 #include <libempathy/empathy-chatroom-manager.h>
49 #include "empathy-chat.h"
50 #include "empathy-spell.h"
51 #include "empathy-contact-dialogs.h"
52 #include "empathy-contact-list-store.h"
53 #include "empathy-contact-list-view.h"
54 #include "empathy-contact-menu.h"
55 #include "empathy-gtk-marshal.h"
56 #include "empathy-input-text-view.h"
57 #include "empathy-search-bar.h"
58 #include "empathy-theme-manager.h"
59 #include "empathy-smiley-manager.h"
60 #include "empathy-ui-utils.h"
61 #include "empathy-string-parser.h"
62 #include "extensions/extensions.h"
64 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
65 #include <libempathy/empathy-debug.h>
67 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
68 #define COMPOSING_STOP_TIMEOUT 5
70 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
71 struct _EmpathyChatPriv {
72 EmpathyTpChat *tp_chat;
77 EmpathyContact *remote_contact;
78 gboolean show_contacts;
80 GSettings *gsettings_chat;
81 GSettings *gsettings_ui;
83 TplLogManager *log_manager;
84 TpAccountManager *account_manager;
86 GList *input_history_current;
88 GCompletion *completion;
89 guint composing_stop_timeout_id;
90 guint block_events_timeout_id;
91 TpHandleType handle_type;
93 gboolean has_input_vscroll;
95 /* TRUE if spell checking is enabled, FALSE otherwise.
96 * This is to keep track of the last state of spell checking
98 gboolean spell_checking_enabled;
100 /* These store the signal handler ids for the enclosed text entry. */
101 gulong insert_text_id;
102 gulong delete_range_id;
103 gulong notify_cursor_position_id;
105 /* Source func ID for update_misspelled_words () */
106 guint update_misspelled_words_id;
107 /* Source func ID for save_paned_pos_timeout () */
108 guint save_paned_pos_id;
112 GtkWidget *vbox_left;
113 GtkWidget *scrolled_window_chat;
114 GtkWidget *scrolled_window_input;
115 GtkWidget *scrolled_window_contacts;
116 GtkWidget *hbox_topic;
117 GtkWidget *expander_topic;
118 GtkWidget *label_topic;
119 GtkWidget *contact_list_view;
120 GtkWidget *info_bar_vbox;
121 GtkWidget *search_bar;
123 guint unread_messages;
124 /* TRUE if the pending messages can be displayed. This is to avoid to show
125 * pending messages *before* messages from logs. (#603980) */
126 gboolean can_show_pending;
128 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
129 * be differently handled since it introduces another race condition, which
130 * is really hard to occur, but still possible.
132 * With the current workaround (which has the race above), we need to be
133 * sure to ACK any pending messages only when the retrieval of backlogs is
134 * finished, that's why using retrieving_backlogs flag.
135 * empathy_chat_messages_read () will check this variable and not ACK
136 * anything when TRUE. It will be set TRUE at chat_constructed () and set
137 * back to FALSE when the backlog has been retrieved and the pending
138 * messages actually showed to the user.
140 * Race condition introduced with this workaround:
141 * Scenario: a message is pending, the user is notified and selects the tab.
142 * the tab with a pending message is focused before the messages are properly
143 * shown (since the preparation of the window is slower AND async WRT the
144 * tab showing), which means the user won't see any new messages (rare but
145 * possible), if he/she will change tab focus before the messages are
146 * properly shown, the tab will be set as 'seen' and the user won't be
147 * notified again about the already notified pending messages when the
148 * messages in tab will be properly shown */
149 gboolean retrieving_backlogs;
150 gboolean sms_channel;
154 gchar *text; /* Original message that was specified
155 * upon entry creation. */
156 gchar *modified_text; /* Message that was modified by user.
157 * When no modifications were made, it is NULL */
163 PART_COMMAND_ENTERED,
177 PROP_N_MESSAGES_SENDING,
178 PROP_NB_UNREAD_MESSAGES,
181 static guint signals[LAST_SIGNAL] = { 0 };
183 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
185 static gboolean update_misspelled_words (gpointer data);
188 chat_get_property (GObject *object,
193 EmpathyChat *chat = EMPATHY_CHAT (object);
194 EmpathyChatPriv *priv = GET_PRIV (object);
198 g_value_set_object (value, priv->tp_chat);
201 g_value_set_object (value, priv->account);
204 g_value_take_string (value, empathy_chat_dup_name (chat));
207 g_value_set_string (value, priv->id);
210 g_value_set_string (value, priv->subject);
212 case PROP_REMOTE_CONTACT:
213 g_value_set_object (value, priv->remote_contact);
215 case PROP_SHOW_CONTACTS:
216 g_value_set_boolean (value, priv->show_contacts);
218 case PROP_SMS_CHANNEL:
219 g_value_set_boolean (value, priv->sms_channel);
221 case PROP_N_MESSAGES_SENDING:
222 g_value_set_uint (value,
223 empathy_chat_get_n_messages_sending (chat));
225 case PROP_NB_UNREAD_MESSAGES:
226 g_value_set_uint (value,
227 empathy_chat_get_nb_unread_messages (chat));
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
236 chat_set_property (GObject *object,
241 EmpathyChat *chat = EMPATHY_CHAT (object);
245 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
247 case PROP_SHOW_CONTACTS:
248 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
257 account_reconnected (EmpathyChat *chat,
260 EmpathyChatPriv *priv = GET_PRIV (chat);
262 DEBUG ("Account reconnected, request a new Text channel");
264 /* FIXME: Ideally we should ask to handle ourself the channel so we can
265 * report the error if any but this is blocked by
266 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
267 switch (priv->handle_type) {
268 case TP_HANDLE_TYPE_CONTACT:
269 if (priv->sms_channel)
270 empathy_sms_contact_id (
272 TP_USER_ACTION_TIME_NOT_USER_ACTION);
274 empathy_chat_with_contact_id (
276 TP_USER_ACTION_TIME_NOT_USER_ACTION);
278 case TP_HANDLE_TYPE_ROOM:
279 empathy_join_muc (account, priv->id,
280 TP_USER_ACTION_TIME_NOT_USER_ACTION);
282 case TP_HANDLE_TYPE_NONE:
283 case TP_HANDLE_TYPE_LIST:
284 case TP_HANDLE_TYPE_GROUP:
286 g_assert_not_reached ();
290 g_object_unref (chat);
294 chat_new_connection_cb (TpAccount *account,
298 gchar *dbus_error_name,
302 EmpathyChatPriv *priv = GET_PRIV (chat);
304 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
307 if (priv->tp_chat != NULL || account != priv->account ||
308 priv->handle_type == TP_HANDLE_TYPE_NONE ||
309 EMP_STR_EMPTY (priv->id))
314 account_reconnected (chat, account);
318 chat_composing_remove_timeout (EmpathyChat *chat)
320 EmpathyChatPriv *priv;
322 priv = GET_PRIV (chat);
324 if (priv->composing_stop_timeout_id) {
325 g_source_remove (priv->composing_stop_timeout_id);
326 priv->composing_stop_timeout_id = 0;
331 set_chate_state_cb (GObject *source,
332 GAsyncResult *result,
335 GError *error = NULL;
337 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
339 DEBUG ("Failed to set chat state: %s", error->message);
340 g_error_free (error);
345 set_chat_state (EmpathyChat *self,
346 TpChannelChatState state)
348 EmpathyChatPriv *priv = GET_PRIV (self);
350 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
351 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
354 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
355 set_chate_state_cb, self);
359 chat_composing_stop_timeout_cb (EmpathyChat *chat)
361 EmpathyChatPriv *priv;
363 priv = GET_PRIV (chat);
365 priv->composing_stop_timeout_id = 0;
366 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
372 chat_composing_start (EmpathyChat *chat)
374 EmpathyChatPriv *priv;
376 priv = GET_PRIV (chat);
378 if (priv->composing_stop_timeout_id) {
379 /* Just restart the timeout */
380 chat_composing_remove_timeout (chat);
382 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
385 priv->composing_stop_timeout_id = g_timeout_add_seconds (
386 COMPOSING_STOP_TIMEOUT,
387 (GSourceFunc) chat_composing_stop_timeout_cb,
392 chat_composing_stop (EmpathyChat *chat)
394 chat_composing_remove_timeout (chat);
395 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
399 chat_input_history_entry_cmp (InputHistoryEntry *entry,
402 if (!tp_strdiff (entry->text, text)) {
403 if (entry->modified_text != NULL) {
404 /* Modified entry and single string cannot be equal. */
412 static InputHistoryEntry *
413 chat_input_history_entry_new_with_text (const gchar *text)
415 InputHistoryEntry *entry;
416 entry = g_slice_new0 (InputHistoryEntry);
417 entry->text = g_strdup (text);
423 chat_input_history_entry_free (InputHistoryEntry *entry)
425 g_free (entry->text);
426 g_free (entry->modified_text);
427 g_slice_free (InputHistoryEntry, entry);
431 chat_input_history_entry_revert (InputHistoryEntry *entry)
433 g_free (entry->modified_text);
434 entry->modified_text = NULL;
438 chat_input_history_entry_update_text (InputHistoryEntry *entry,
443 if (!tp_strdiff (text, entry->text)) {
444 g_free (entry->modified_text);
445 entry->modified_text = NULL;
449 old = entry->modified_text;
450 entry->modified_text = g_strdup (text);
455 chat_input_history_entry_get_text (InputHistoryEntry *entry)
461 if (entry->modified_text != NULL) {
462 return entry->modified_text;
468 chat_input_history_remove_item (GList *list,
471 list = g_list_remove_link (list, item);
472 chat_input_history_entry_free (item->data);
473 g_list_free_1 (item);
478 chat_input_history_revert (EmpathyChat *chat)
480 EmpathyChatPriv *priv;
484 InputHistoryEntry *entry;
486 priv = GET_PRIV (chat);
487 list = priv->input_history;
490 DEBUG ("No input history");
494 /* Delete temporary entry */
495 if (priv->input_history_current != NULL) {
497 list = chat_input_history_remove_item (list, item1);
498 if (priv->input_history_current == item1) {
499 /* Removed temporary entry was current entry */
500 priv->input_history = list;
501 priv->input_history_current = NULL;
506 /* There is no entry to revert */
510 /* Restore the current history entry to original value */
511 item1 = priv->input_history_current;
513 chat_input_history_entry_revert (entry);
515 /* Remove restored entry if there is other occurance before this entry */
516 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
517 (GCompareFunc) chat_input_history_entry_cmp);
518 if (item2 != item1) {
519 list = chat_input_history_remove_item (list, item1);
522 /* Remove other occurance of the restored entry */
523 item2 = g_list_find_custom (item1->next,
524 chat_input_history_entry_get_text (entry),
525 (GCompareFunc) chat_input_history_entry_cmp);
527 list = chat_input_history_remove_item (list, item2);
531 priv->input_history_current = NULL;
532 priv->input_history = list;
536 chat_input_history_add (EmpathyChat *chat,
540 EmpathyChatPriv *priv;
543 InputHistoryEntry *entry;
545 priv = GET_PRIV (chat);
547 list = priv->input_history;
549 /* Remove any other occurances of this entry, if not temporary */
551 while ((item = g_list_find_custom (list, str,
552 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
553 list = chat_input_history_remove_item (list, item);
556 /* Trim the list to the last 10 items */
557 while (g_list_length (list) > 10) {
558 item = g_list_last (list);
560 list = chat_input_history_remove_item (list, item);
568 entry = chat_input_history_entry_new_with_text (str);
569 list = g_list_prepend (list, entry);
571 /* Set the list and the current item pointer */
572 priv->input_history = list;
574 priv->input_history_current = list;
577 priv->input_history_current = NULL;
582 chat_input_history_get_next (EmpathyChat *chat)
584 EmpathyChatPriv *priv;
588 priv = GET_PRIV (chat);
590 if (priv->input_history == NULL) {
591 DEBUG ("No input history, next entry is NULL");
594 g_assert (priv->input_history_current != NULL);
596 if ((item = g_list_next (priv->input_history_current)) == NULL)
598 item = priv->input_history_current;
601 msg = chat_input_history_entry_get_text (item->data);
603 DEBUG ("Returning next entry: '%s'", msg);
605 priv->input_history_current = item;
611 chat_input_history_get_prev (EmpathyChat *chat)
613 EmpathyChatPriv *priv;
617 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
619 priv = GET_PRIV (chat);
621 if (priv->input_history == NULL) {
622 DEBUG ("No input history, previous entry is NULL");
626 if (priv->input_history_current == NULL)
630 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
632 item = priv->input_history_current;
635 msg = chat_input_history_entry_get_text (item->data);
637 DEBUG ("Returning previous entry: '%s'", msg);
639 priv->input_history_current = item;
645 chat_input_history_update (EmpathyChat *chat,
646 GtkTextBuffer *buffer)
648 EmpathyChatPriv *priv;
649 GtkTextIter start, end;
651 InputHistoryEntry *entry;
653 priv = GET_PRIV (chat);
655 gtk_text_buffer_get_bounds (buffer, &start, &end);
656 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
658 if (priv->input_history_current == NULL) {
659 /* Add the current text temporarily to the history */
660 chat_input_history_add (chat, text, TRUE);
665 /* Save the changes in the history */
666 entry = priv->input_history_current->data;
667 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
668 chat_input_history_entry_update_text (entry, text);
677 } ChatCommandMsgData;
680 chat_command_msg_cb (GObject *source,
681 GAsyncResult *result,
684 ChatCommandMsgData *data = user_data;
685 GError *error = NULL;
688 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
689 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
691 if (channel == NULL) {
692 DEBUG ("Failed to get channel: %s", error->message);
693 g_error_free (error);
695 empathy_chat_view_append_event (data->chat->view,
696 _("Failed to open private chat"));
700 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
701 TpTextChannel *text = (TpTextChannel *) channel;
704 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
707 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
709 g_object_unref (msg);
712 g_object_unref (channel);
715 g_free (data->message);
716 g_slice_free (ChatCommandMsgData, data);
720 nick_command_supported (EmpathyChat *chat)
722 EmpathyChatPriv * priv = GET_PRIV (chat);
723 TpConnection *connection;
725 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
726 return tp_proxy_has_interface_by_id (connection,
727 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
731 part_command_supported (EmpathyChat *chat)
733 EmpathyChatPriv * priv = GET_PRIV (chat);
735 return tp_proxy_has_interface_by_id (priv->tp_chat,
736 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
740 chat_command_clear (EmpathyChat *chat,
743 empathy_chat_view_clear (chat->view);
747 chat_command_topic (EmpathyChat *chat,
750 EmpathyChatPriv *priv = GET_PRIV (chat);
751 EmpathyTpChatProperty *property;
752 GValue value = {0, };
754 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
755 if (property == NULL) {
756 empathy_chat_view_append_event (chat->view,
757 _("Topic not supported on this conversation"));
761 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
762 empathy_chat_view_append_event (chat->view,
763 _("You are not allowed to change the topic"));
767 g_value_init (&value, G_TYPE_STRING);
768 g_value_set_string (&value, strv[1]);
769 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
770 g_value_unset (&value);
774 empathy_chat_join_muc (EmpathyChat *chat,
777 EmpathyChatPriv *priv = GET_PRIV (chat);
779 empathy_join_muc (priv->account, room,
780 empathy_get_current_action_time ());
784 chat_command_join (EmpathyChat *chat,
789 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
791 /* FIXME: Ideally we should ask to handle ourself the channel so we can
792 * report the error if any but this is blocked by
793 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
794 while (rooms[i] != NULL) {
795 /* ignore empty strings */
796 if (!EMP_STR_EMPTY (rooms[i])) {
797 empathy_chat_join_muc (chat, rooms[i]);
805 chat_command_part (EmpathyChat *chat,
808 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
812 chat_command_msg_internal (EmpathyChat *chat,
813 const gchar *contact_id,
814 const gchar *message)
816 EmpathyChatPriv *priv = GET_PRIV (chat);
817 ChatCommandMsgData *data;
818 TpAccountChannelRequest *req;
821 request = tp_asv_new (
822 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
823 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
824 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
827 req = tp_account_channel_request_new (priv->account, request,
828 empathy_get_current_action_time ());
830 /* FIXME: We should probably search in members alias. But this
831 * is enough for IRC */
832 data = g_slice_new (ChatCommandMsgData);
834 data->message = g_strdup (message);
836 tp_account_channel_request_ensure_and_observe_channel_async (req,
837 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
839 g_object_unref (req);
840 g_hash_table_unref (request);
844 chat_command_query (EmpathyChat *chat,
847 /* If <message> part is not defined,
848 * strv[2] will be the terminal NULL */
849 chat_command_msg_internal (chat, strv[1], strv[2]);
853 chat_command_msg (EmpathyChat *chat,
856 chat_command_msg_internal (chat, strv[1], strv[2]);
860 callback_for_request_rename (TpProxy *proxy,
863 GObject *weak_object)
866 DEBUG ("Call to RequestRename method failed: %s",error->message);
871 chat_command_nick (EmpathyChat *chat,
874 EmpathyChatPriv *priv = GET_PRIV (chat);
877 proxy = TP_PROXY (tp_account_get_connection (priv->account));
879 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
880 strv[1], callback_for_request_rename, NULL, NULL, NULL);
884 chat_command_me (EmpathyChat *chat,
887 EmpathyChatPriv *priv = GET_PRIV (chat);
889 TpTextChannel *channel;
891 channel = (TpTextChannel *) (priv->tp_chat);
893 if (!tp_text_channel_supports_message_type (channel,
894 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
895 /* Action message are not supported, 'simulate' the action */
896 EmpathyContact *self_contact;
899 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
900 /* The TpChat can't be ready if it doesn't have the self contact */
901 g_assert (self_contact != NULL);
903 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
905 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
910 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
914 empathy_tp_chat_send (priv->tp_chat, message);
915 g_object_unref (message);
919 chat_command_say (EmpathyChat *chat,
922 EmpathyChatPriv *priv = GET_PRIV (chat);
925 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
927 empathy_tp_chat_send (priv->tp_chat, message);
928 g_object_unref (message);
932 whois_got_contact_cb (TpConnection *connection,
934 TpContact * const *contacts,
935 const gchar * const *requested_ids,
936 GHashTable *failed_id_errors,
939 GObject *weak_object)
941 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
943 g_return_if_fail (n_contacts <= 1);
945 if (n_contacts == 0) {
947 gpointer key = NULL, value = NULL;
951 /* tp-glib guarantees that the contacts you requested will be
952 * in failed_id_errors regardless of whether the individual
953 * contact was invalid or the whole operation failed.
955 g_hash_table_iter_init (&iter, failed_id_errors);
956 g_hash_table_iter_next (&iter, &key, &value);
960 DEBUG ("Error getting TpContact for '%s': %s %u %s",
961 id, g_quark_to_string (id_error->domain),
962 id_error->code, id_error->message);
965 /* The specific ID failed. */
966 gchar *event = g_strdup_printf (
967 _("“%s” is not a valid contact ID"), id);
968 empathy_chat_view_append_event (chat->view, event);
971 /* Otherwise we're disconnected or something; so the window
972 * will already say ‘Disconnected’, so let's not show anything.
975 EmpathyContact *empathy_contact;
978 g_return_if_fail (contacts[0] != NULL);
979 empathy_contact = empathy_contact_dup_from_tp_contact (
982 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
983 /* If we're alive and this command is running, we'd better be
985 g_return_if_fail (window != NULL);
986 g_return_if_fail (gtk_widget_is_toplevel (window));
987 empathy_contact_information_dialog_show (empathy_contact,
988 GTK_WINDOW (window));
989 g_object_unref (empathy_contact);
994 chat_command_whois (EmpathyChat *chat,
997 EmpathyChatPriv *priv = GET_PRIV (chat);
1000 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1001 tp_connection_get_contacts_by_id (conn,
1002 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1003 * entered by the user (including spaces, if any). */
1004 1, (const gchar * const *) strv + 1,
1006 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1009 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1011 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1014 const gchar *prefix;
1017 ChatCommandFunc func;
1018 gboolean (*is_supported)(EmpathyChat *chat);
1022 static ChatCommandItem commands[] = {
1023 {"clear", 1, 1, chat_command_clear, NULL,
1024 N_("/clear: clear all messages from the current conversation")},
1026 {"topic", 2, 2, chat_command_topic, NULL,
1027 N_("/topic <topic>: set the topic of the current conversation")},
1029 {"join", 2, 2, chat_command_join, NULL,
1030 N_("/join <chat room ID>: join a new chat room")},
1032 {"j", 2, 2, chat_command_join, NULL,
1033 N_("/j <chat room ID>: join a new chat room")},
1035 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1036 {"part", 1, 3, chat_command_part, part_command_supported,
1037 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1038 "by default the current one")},
1040 {"query", 2, 3, chat_command_query, NULL,
1041 N_("/query <contact ID> [<message>]: open a private chat")},
1043 {"msg", 3, 3, chat_command_msg, NULL,
1044 N_("/msg <contact ID> <message>: open a private chat")},
1046 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1047 N_("/nick <nickname>: change your nickname on the current server")},
1049 {"me", 2, 2, chat_command_me, NULL,
1050 N_("/me <message>: send an ACTION message to the current conversation")},
1052 {"say", 2, 2, chat_command_say, NULL,
1053 N_("/say <message>: send <message> to the current conversation. "
1054 "This is used to send a message starting with a '/'. For example: "
1055 "\"/say /join is used to join a new chat room\"")},
1057 {"whois", 2, 2, chat_command_whois, NULL,
1058 N_("/whois <contact ID>: display information about a contact")},
1060 {"help", 1, 2, chat_command_help, NULL,
1061 N_("/help [<command>]: show all supported commands. "
1062 "If <command> is defined, show its usage.")},
1066 chat_command_show_help (EmpathyChat *chat,
1067 ChatCommandItem *item)
1071 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1072 empathy_chat_view_append_event (chat->view, str);
1077 chat_command_help (EmpathyChat *chat,
1082 /* If <command> part is not defined,
1083 * strv[1] will be the terminal NULL */
1084 if (strv[1] == NULL) {
1085 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1086 if (commands[i].is_supported != NULL) {
1087 if (!commands[i].is_supported (chat)) {
1091 empathy_chat_view_append_event (chat->view,
1092 _(commands[i].help));
1097 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1098 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1099 if (commands[i].is_supported != NULL) {
1100 if (!commands[i].is_supported (chat)) {
1104 chat_command_show_help (chat, &commands[i]);
1109 empathy_chat_view_append_event (chat->view,
1110 _("Unknown command"));
1114 chat_command_parse (const gchar *text, guint max_parts)
1119 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1121 array = g_ptr_array_sized_new (max_parts + 1);
1122 while (max_parts > 1) {
1125 /* Skip white spaces */
1126 while (g_ascii_isspace (*text)) {
1130 /* Search the end of this part, until first space. */
1131 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1137 item = g_strndup (text, end - text);
1138 g_ptr_array_add (array, item);
1139 DEBUG ("\tITEM: \"%s\"", item);
1145 /* Append last part if not empty */
1146 item = g_strstrip (g_strdup (text));
1147 if (!EMP_STR_EMPTY (item)) {
1148 g_ptr_array_add (array, item);
1149 DEBUG ("\tITEM: \"%s\"", item);
1154 /* Make the array NULL-terminated */
1155 g_ptr_array_add (array, NULL);
1157 return (GStrv) g_ptr_array_free (array, FALSE);
1161 has_prefix_case (const gchar *s,
1162 const gchar *prefix)
1164 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1168 chat_send (EmpathyChat *chat,
1171 EmpathyChatPriv *priv;
1175 if (EMP_STR_EMPTY (msg)) {
1179 priv = GET_PRIV (chat);
1181 chat_input_history_add (chat, msg, FALSE);
1183 if (msg[0] == '/') {
1184 gboolean second_slash = FALSE;
1185 const gchar *iter = msg + 1;
1187 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1192 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1195 c = *(msg + 1 + strlen (commands[i].prefix));
1196 if (c != '\0' && !g_ascii_isspace (c)) {
1199 if (commands[i].is_supported != NULL) {
1200 if (!commands[i].is_supported (chat)) {
1205 /* We can't use g_strsplit here because it does
1206 * not deal correctly if we have more than one space
1208 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1210 strv_len = g_strv_length (strv);
1211 if (strv_len < commands[i].min_parts ||
1212 strv_len > commands[i].max_parts) {
1213 chat_command_show_help (chat, &commands[i]);
1218 commands[i].func (chat, strv);
1223 /* Also allow messages with two slashes before the
1224 * first space, so it is possible to send a /unix/path.
1225 * This heuristic is kind of crap. */
1226 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1228 second_slash = TRUE;
1234 if (!second_slash) {
1235 empathy_chat_view_append_event (chat->view,
1236 _("Unknown command; see /help for the available"
1242 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1244 empathy_tp_chat_send (priv->tp_chat, message);
1245 g_object_unref (message);
1249 chat_input_text_view_send (EmpathyChat *chat)
1251 GtkTextBuffer *buffer;
1252 GtkTextIter start, end;
1255 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1257 gtk_text_buffer_get_bounds (buffer, &start, &end);
1258 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1260 /* clear the input field */
1261 gtk_text_buffer_set_text (buffer, "", -1);
1262 /* delete input history modifications */
1263 chat_input_history_revert (chat);
1265 chat_send (chat, msg);
1270 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1271 EmpathyContact *contact,
1272 TpChannelChatState state,
1275 EmpathyChatPriv *priv;
1277 gboolean was_composing;
1279 priv = GET_PRIV (chat);
1281 if (empathy_contact_is_user (contact)) {
1282 /* We don't care about our own chat state */
1286 was_composing = (priv->compositors != NULL);
1288 /* Find the contact in the list. After that l is the list elem or NULL */
1289 for (l = priv->compositors; l; l = l->next) {
1290 if (contact == l->data) {
1296 case TP_CHANNEL_CHAT_STATE_GONE:
1297 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1298 case TP_CHANNEL_CHAT_STATE_PAUSED:
1299 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1300 /* Contact is not composing */
1302 priv->compositors = g_list_remove_link (priv->compositors, l);
1303 g_object_unref (l->data);
1307 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1308 /* Contact is composing */
1310 priv->compositors = g_list_prepend (priv->compositors,
1311 g_object_ref (contact));
1315 g_assert_not_reached ();
1318 DEBUG ("Was composing: %s now composing: %s",
1319 was_composing ? "yes" : "no",
1320 priv->compositors ? "yes" : "no");
1322 if ((was_composing && !priv->compositors) ||
1323 (!was_composing && priv->compositors)) {
1324 /* Composing state changed */
1325 g_signal_emit (chat, signals[COMPOSING], 0,
1326 priv->compositors != NULL);
1331 chat_message_received (EmpathyChat *chat,
1332 EmpathyMessage *message,
1335 EmpathyChatPriv *priv = GET_PRIV (chat);
1336 EmpathyContact *sender;
1338 sender = empathy_message_get_sender (message);
1340 if (empathy_message_is_edit (message)) {
1341 DEBUG ("Editing message '%s' to '%s'",
1342 empathy_message_get_supersedes (message),
1343 empathy_message_get_body (message));
1345 empathy_chat_view_edit_message (chat->view, message);
1347 DEBUG ("Appending new message '%s' from %s (%d)",
1348 empathy_message_get_token (message),
1349 empathy_contact_get_alias (sender),
1350 empathy_contact_get_handle (sender));
1352 empathy_chat_view_append_message (chat->view, message);
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);
1362 /* We received a message so the contact is no longer
1364 chat_state_changed_cb (priv->tp_chat, sender,
1365 TP_CHANNEL_CHAT_STATE_ACTIVE,
1370 chat_message_received_cb (EmpathyTpChat *tp_chat,
1371 EmpathyMessage *message,
1374 chat_message_received (chat, message, FALSE);
1378 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1379 EmpathyMessage *message,
1382 EmpathyChatPriv *priv = GET_PRIV (chat);
1384 empathy_chat_view_message_acknowledged (chat->view,
1387 if (!empathy_message_is_edit (message)) {
1388 priv->unread_messages--;
1389 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1394 chat_send_error_cb (EmpathyTpChat *tp_chat,
1395 const gchar *message_body,
1396 TpChannelTextSendError error_code,
1397 const gchar *dbus_error,
1400 const gchar *error = NULL;
1403 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1404 /* translators: error used when user doesn't have enough credit on his
1405 * account to send the message. */
1406 error = _("insufficient balance to send message");
1407 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1408 error = _("not capable");
1411 if (error == NULL) {
1412 /* if we didn't find a dbus-error, try the old error */
1413 switch (error_code) {
1414 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1415 error = _("offline");
1417 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1418 error = _("invalid contact");
1420 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1421 error = _("permission denied");
1423 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1424 error = _("too long message");
1426 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1427 error = _("not implemented");
1429 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1431 error = _("unknown");
1436 if (message_body != NULL) {
1437 str = g_strdup_printf (_("Error sending message '%s': %s"),
1438 message_body, error);
1441 str = g_strdup_printf (_("Error sending message: %s"), error);
1444 empathy_chat_view_append_event (chat->view, str);
1449 chat_topic_label_size_allocate_cb (GtkLabel *label,
1450 GtkAllocation *allocation,
1453 EmpathyChatPriv *priv = GET_PRIV (chat);
1455 if (!gtk_label_get_line_wrap (label)) {
1456 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1457 gtk_widget_show (priv->expander_topic);
1459 gtk_widget_hide (priv->expander_topic);
1466 chat_topic_expander_activate_cb (GtkExpander *expander,
1467 GParamSpec *param_spec,
1470 EmpathyChatPriv *priv = GET_PRIV (chat);
1472 if (gtk_expander_get_expanded (expander)) {
1473 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1474 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1476 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1477 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1482 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1487 EmpathyChatPriv *priv = GET_PRIV (chat);
1489 if (!tp_strdiff (name, "subject")) {
1490 g_free (priv->subject);
1491 priv->subject = g_value_dup_string (value);
1492 g_object_notify (G_OBJECT (chat), "subject");
1494 if (EMP_STR_EMPTY (priv->subject)) {
1495 gtk_widget_hide (priv->hbox_topic);
1497 gchar *markup_topic;
1500 markup_topic = empathy_add_link_markup (priv->subject);
1501 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1502 _("Topic:"), markup_topic);
1504 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1505 g_free (markup_text);
1506 g_free (markup_topic);
1508 gtk_widget_show (priv->hbox_topic);
1510 if (priv->block_events_timeout_id == 0) {
1513 if (!EMP_STR_EMPTY (priv->subject)) {
1514 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1516 str = g_strdup (_("No topic defined"));
1518 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1522 else if (!tp_strdiff (name, "name")) {
1523 g_free (priv->name);
1524 priv->name = g_value_dup_string (value);
1525 g_object_notify (G_OBJECT (chat), "name");
1530 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1534 GtkTextIter word_start = *iter;
1535 GtkTextIter word_end = *iter;
1538 if (gtk_text_iter_inside_word (&word_end) &&
1539 !gtk_text_iter_ends_word (&word_end)) {
1540 gtk_text_iter_forward_word_end (&word_end);
1545 if (gtk_text_iter_get_char (&tmp) == '\'') {
1546 gtk_text_iter_forward_char (&tmp);
1548 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1549 gtk_text_iter_forward_word_end (&word_end);
1554 if (gtk_text_iter_inside_word (&word_start) ||
1555 gtk_text_iter_ends_word (&word_start)) {
1556 if (!gtk_text_iter_starts_word (&word_start) ||
1557 gtk_text_iter_equal (&word_start, &word_end)) {
1558 gtk_text_iter_backward_word_start (&word_start);
1562 gtk_text_iter_backward_char (&tmp);
1564 if (gtk_text_iter_get_char (&tmp) == '\'') {
1565 gtk_text_iter_backward_char (&tmp);
1567 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1568 gtk_text_iter_backward_word_start (&word_start);
1573 *start = word_start;
1579 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1580 GtkTextIter *location,
1585 GtkTextIter iter, pos;
1587 /* Remove all misspelled tags in the inserted text.
1588 * This happens when text is inserted within a misspelled word. */
1589 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1590 gtk_text_iter_get_offset (location) - len);
1591 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1594 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1597 GtkTextIter start, end;
1600 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1603 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1605 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1606 gtk_text_iter_equal (&pos, &end) ||
1607 empathy_spell_check (str)) {
1608 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1610 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1615 } while (gtk_text_iter_forward_word_end (&iter) &&
1616 gtk_text_iter_compare (&iter, location) <= 0);
1620 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1625 GtkTextIter word_start, word_end;
1627 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1628 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1629 &word_start, &word_end);
1634 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1637 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1638 chat_composing_stop (chat);
1640 chat_composing_start (chat);
1645 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1650 GtkTextIter prev_pos;
1651 GtkTextIter word_start;
1652 GtkTextIter word_end;
1656 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1658 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1659 gtk_text_buffer_get_insert (buffer));
1660 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1662 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1665 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1666 !gtk_text_iter_equal (&pos, &word_end)) {
1667 str = gtk_text_buffer_get_text (buffer,
1668 &word_start, &word_end, FALSE);
1670 if (!empathy_spell_check (str)) {
1671 gtk_text_buffer_apply_tag_by_name (buffer,
1672 "misspelled", &word_start, &word_end);
1674 gtk_text_buffer_remove_tag_by_name (buffer,
1675 "misspelled", &word_start, &word_end);
1682 gtk_text_buffer_move_mark (buffer, mark, &pos);
1686 empathy_isspace_cb (gunichar c,
1689 return g_unichar_isspace (c);
1693 chat_input_key_press_event_cb (GtkWidget *widget,
1697 EmpathyChatPriv *priv;
1700 GtkWidget *text_view_sw;
1702 priv = GET_PRIV (chat);
1704 /* Catch ctrl+up/down so we can traverse messages we sent */
1705 if ((event->state & GDK_CONTROL_MASK) &&
1706 (event->keyval == GDK_KEY_Up ||
1707 event->keyval == GDK_KEY_Down)) {
1708 GtkTextBuffer *buffer;
1711 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1712 chat_input_history_update (chat, buffer);
1714 if (event->keyval == GDK_KEY_Up) {
1715 str = chat_input_history_get_next (chat);
1717 str = chat_input_history_get_prev (chat);
1720 g_signal_handlers_block_by_func (buffer,
1721 chat_input_text_buffer_changed_cb,
1723 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1724 g_signal_handlers_unblock_by_func (buffer,
1725 chat_input_text_buffer_changed_cb,
1731 /* Catch enter but not ctrl/shift-enter */
1732 if (IS_ENTER (event->keyval) &&
1733 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1736 /* This is to make sure that kinput2 gets the enter. And if
1737 * it's handled there we shouldn't send on it. This is because
1738 * kinput2 uses Enter to commit letters. See:
1739 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1742 view = GTK_TEXT_VIEW (chat->input_text_view);
1743 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1744 gtk_text_view_reset_im_context (view);
1748 chat_input_text_view_send (chat);
1752 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1754 if (IS_ENTER (event->keyval) &&
1755 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1756 /* Newline for shift/control-enter. */
1759 if (!(event->state & GDK_CONTROL_MASK) &&
1760 event->keyval == GDK_KEY_Page_Up) {
1761 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1762 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1765 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1766 event->keyval == GDK_KEY_Page_Down) {
1767 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1768 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1769 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1770 gtk_adjustment_set_value (adj, val);
1773 if (event->keyval == GDK_KEY_Escape) {
1774 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1776 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1777 event->keyval == GDK_KEY_Tab) {
1778 GtkTextBuffer *buffer;
1779 GtkTextIter start, current;
1780 gchar *nick, *completed;
1781 GList *list, *completed_list;
1782 gboolean is_start_of_buffer;
1784 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1785 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1787 /* Get the start of the nick to complete. */
1788 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1789 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1790 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1792 is_start_of_buffer = gtk_text_iter_is_start (&start);
1794 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1795 g_completion_add_items (priv->completion, list);
1797 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1798 completed_list = g_completion_complete (priv->completion,
1807 GString *message = NULL;
1810 gtk_text_buffer_delete (buffer, &start, ¤t);
1812 len = g_list_length (completed_list);
1815 /* If we only have one hit, use that text
1816 * instead of the text in completed since the
1817 * completed text will use the typed string
1818 * which might be cased all wrong.
1821 text = empathy_contact_get_alias (completed_list->data);
1825 /* Print all hits to the scrollback view, so the
1826 * user knows what possibilities he has.
1829 message = g_string_new ("");
1830 for (l = completed_list; l != NULL; l = l->next) {
1831 g_string_append (message, empathy_contact_get_alias (l->data));
1832 g_string_append (message, " - ");
1834 empathy_chat_view_append_event (chat->view, message->str);
1835 g_string_free (message, TRUE);
1838 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1840 if (len == 1 && is_start_of_buffer) {
1841 gchar *complete_char;
1843 complete_char = g_settings_get_string (
1844 priv->gsettings_chat,
1845 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1847 if (complete_char != NULL) {
1848 gtk_text_buffer_insert_at_cursor (buffer,
1850 strlen (complete_char));
1851 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1852 g_free (complete_char);
1859 g_completion_clear_items (priv->completion);
1861 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1871 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1875 gtk_widget_grab_focus (chat->input_text_view);
1881 chat_input_realize_cb (GtkWidget *widget,
1884 DEBUG ("Setting focus to the input text view");
1885 if (gtk_widget_is_sensitive (widget)) {
1886 gtk_widget_grab_focus (widget);
1891 chat_input_has_focus_notify_cb (GtkWidget *widget,
1895 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1899 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1900 EmpathySmiley *smiley,
1903 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1904 GtkTextBuffer *buffer;
1907 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1909 gtk_text_buffer_get_end_iter (buffer, &iter);
1910 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1912 gtk_text_buffer_get_end_iter (buffer, &iter);
1913 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1924 static EmpathyChatSpell *
1925 chat_spell_new (EmpathyChat *chat,
1930 EmpathyChatSpell *chat_spell;
1932 chat_spell = g_slice_new0 (EmpathyChatSpell);
1934 chat_spell->chat = g_object_ref (chat);
1935 chat_spell->word = g_strdup (word);
1936 chat_spell->start = start;
1937 chat_spell->end = end;
1943 chat_spell_free (EmpathyChatSpell *chat_spell)
1945 g_object_unref (chat_spell->chat);
1946 g_free (chat_spell->word);
1947 g_slice_free (EmpathyChatSpell, chat_spell);
1951 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1952 EmpathyChatSpell *chat_spell)
1954 empathy_chat_correct_word (chat_spell->chat,
1955 &(chat_spell->start),
1957 gtk_menu_item_get_label (menu_item));
1962 chat_spelling_build_suggestions_menu (const gchar *code,
1963 EmpathyChatSpell *chat_spell)
1965 GList *suggestions, *l;
1966 GtkWidget *menu, *menu_item;
1968 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1969 if (suggestions == NULL)
1972 menu = gtk_menu_new ();
1973 for (l = suggestions; l; l = l->next) {
1974 menu_item = gtk_menu_item_new_with_label (l->data);
1975 g_signal_connect (G_OBJECT (menu_item), "activate",
1976 G_CALLBACK (chat_spelling_menu_activate_cb),
1978 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1980 empathy_spell_free_suggestions (suggestions);
1982 gtk_widget_show_all (menu);
1988 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1990 GtkWidget *menu, *submenu, *item;
1993 codes = empathy_spell_get_enabled_language_codes ();
1994 g_assert (codes != NULL);
1996 if (g_list_length (codes) > 1) {
1997 menu = gtk_menu_new ();
1999 for (l = codes; l; l = l->next) {
2000 const gchar *code, *name;
2003 name = empathy_spell_get_language_name (code);
2007 item = gtk_image_menu_item_new_with_label (name);
2009 submenu = chat_spelling_build_suggestions_menu (
2011 if (submenu == NULL)
2012 gtk_widget_set_sensitive (item, FALSE);
2014 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2016 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2019 menu = chat_spelling_build_suggestions_menu (codes->data,
2022 menu = gtk_menu_new ();
2023 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2024 gtk_widget_set_sensitive (item, FALSE);
2025 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2028 g_list_free (codes);
2030 gtk_widget_show_all (menu);
2041 static EmpathyChatWord *
2042 chat_word_new (EmpathyChat *chat,
2046 EmpathyChatWord *chat_word;
2048 chat_word = g_slice_new0 (EmpathyChatWord);
2050 chat_word->chat = g_object_ref (chat);
2051 chat_word->word = g_strdup (word);
2052 chat_word->code = g_strdup (code);
2058 chat_word_free (EmpathyChatWord *chat_word)
2060 g_object_unref (chat_word->chat);
2061 g_free (chat_word->word);
2062 g_free (chat_word->code);
2063 g_slice_free (EmpathyChatWord, chat_word);
2067 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2068 EmpathyChatWord *chat_word)
2070 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2072 empathy_spell_add_to_dictionary (chat_word->code,
2074 priv->update_misspelled_words_id =
2075 g_idle_add (update_misspelled_words, chat_word->chat);
2079 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2081 GtkWidget *menu, *item, *lang_item, *image;
2084 const gchar *code, *name;
2085 EmpathyChatWord *chat_word;
2087 codes = empathy_spell_get_enabled_language_codes ();
2088 g_assert (codes != NULL);
2089 if (g_list_length (codes) > 1) {
2090 /* translators: %s is the selected word */
2091 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2093 item = gtk_image_menu_item_new_with_mnemonic (label);
2095 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2096 GTK_ICON_SIZE_MENU);
2097 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2100 menu = gtk_menu_new ();
2102 for (l = codes; l; l = l->next) {
2104 name = empathy_spell_get_language_name (code);
2108 lang_item = gtk_image_menu_item_new_with_label (name);
2110 chat_word= chat_word_new (chat_spell->chat,
2111 chat_spell->word, code);
2112 g_object_set_data_full (G_OBJECT (lang_item),
2113 "chat-word", chat_word,
2114 (GDestroyNotify) chat_word_free);
2116 g_signal_connect (G_OBJECT (lang_item), "activate",
2117 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2119 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2121 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2124 name = empathy_spell_get_language_name (code);
2125 g_assert (name != NULL);
2126 /* translators: first %s is the selected word,
2127 * second %s is the language name of the target dictionary */
2128 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2129 chat_spell->word, name);
2130 item = gtk_image_menu_item_new_with_mnemonic (label);
2132 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2133 GTK_ICON_SIZE_MENU);
2134 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2136 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2138 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2139 (GDestroyNotify) chat_word_free);
2141 g_signal_connect (G_OBJECT (item), "activate",
2142 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2145 g_list_free (codes);
2147 gtk_widget_show_all (item);
2153 chat_text_send_cb (GtkMenuItem *menuitem,
2156 chat_input_text_view_send (chat);
2160 chat_input_populate_popup_cb (GtkTextView *view,
2164 GtkTextBuffer *buffer;
2165 GtkTextTagTable *table;
2168 GtkTextIter iter, start, end;
2171 EmpathyChatSpell *chat_spell;
2172 GtkWidget *spell_menu;
2173 GtkWidget *spell_item;
2174 EmpathySmileyManager *smiley_manager;
2175 GtkWidget *smiley_menu;
2178 buffer = gtk_text_view_get_buffer (view);
2180 /* Add the emoticon menu. */
2181 item = gtk_separator_menu_item_new ();
2182 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2183 gtk_widget_show (item);
2185 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2186 image = gtk_image_new_from_icon_name ("face-smile",
2187 GTK_ICON_SIZE_MENU);
2188 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2189 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2190 gtk_widget_show (item);
2192 smiley_manager = empathy_smiley_manager_dup_singleton ();
2193 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2194 chat_insert_smiley_activate_cb,
2196 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2197 g_object_unref (smiley_manager);
2199 /* Add the Send menu item. */
2200 gtk_text_buffer_get_bounds (buffer, &start, &end);
2201 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2202 if (!EMP_STR_EMPTY (str)) {
2203 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2204 g_signal_connect (G_OBJECT (item), "activate",
2205 G_CALLBACK (chat_text_send_cb), chat);
2206 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2207 gtk_widget_show (item);
2211 /* Add the spell check menu item. */
2212 table = gtk_text_buffer_get_tag_table (buffer);
2213 tag = gtk_text_tag_table_lookup (table, "misspelled");
2214 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2215 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2216 GTK_TEXT_WINDOW_WIDGET,
2219 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2221 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2222 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2224 str = gtk_text_buffer_get_text (buffer,
2225 &start, &end, FALSE);
2227 if (!EMP_STR_EMPTY (str)) {
2228 chat_spell = chat_spell_new (chat, str, start, end);
2229 g_object_set_data_full (G_OBJECT (menu),
2230 "chat-spell", chat_spell,
2231 (GDestroyNotify) chat_spell_free);
2233 item = gtk_separator_menu_item_new ();
2234 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2235 gtk_widget_show (item);
2237 /* Spelling suggestions */
2238 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2239 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2240 GTK_ICON_SIZE_MENU);
2241 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2243 spell_menu = chat_spelling_build_menu (chat_spell);
2244 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2247 spell_item = gtk_separator_menu_item_new ();
2248 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2249 gtk_widget_show (spell_item);
2251 /* Add to dictionary */
2252 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2254 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2255 gtk_widget_show (spell_item);
2257 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2258 gtk_widget_show (item);
2264 chat_log_filter (TplEvent *event,
2267 EmpathyChat *chat = user_data;
2268 EmpathyMessage *message;
2269 EmpathyChatPriv *priv = GET_PRIV (chat);
2270 const GList *pending;
2272 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2273 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2275 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2276 message = empathy_message_from_tpl_log_event (event);
2278 for (; pending; pending = g_list_next (pending)) {
2279 if (empathy_message_equal (message, pending->data)) {
2280 g_object_unref (message);
2285 g_object_unref (message);
2291 show_pending_messages (EmpathyChat *chat) {
2292 EmpathyChatPriv *priv = GET_PRIV (chat);
2293 const GList *messages, *l;
2295 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2297 if (chat->view == NULL || priv->tp_chat == NULL)
2300 if (!priv->can_show_pending)
2303 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2305 for (l = messages; l != NULL ; l = g_list_next (l)) {
2306 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2307 chat_message_received (chat, message, TRUE);
2313 got_filtered_messages_cb (GObject *manager,
2314 GAsyncResult *result,
2319 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2320 EmpathyChatPriv *priv = GET_PRIV (chat);
2321 GError *error = NULL;
2323 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2324 result, &messages, &error)) {
2325 DEBUG ("%s. Aborting.", error->message);
2326 empathy_chat_view_append_event (chat->view,
2327 _("Failed to retrieve recent logs"));
2328 g_error_free (error);
2332 for (l = messages; l; l = g_list_next (l)) {
2333 EmpathyMessage *message;
2335 g_assert (TPL_IS_EVENT (l->data));
2337 message = empathy_message_from_tpl_log_event (l->data);
2338 g_object_unref (l->data);
2340 if (empathy_message_is_edit (message)) {
2341 /* this is an edited message, create a synthetic event
2342 * using the supersedes token and
2343 * original-message-sent timestamp, that we can then
2345 EmpathyMessage *syn_msg = g_object_new (
2346 EMPATHY_TYPE_MESSAGE,
2348 "token", empathy_message_get_supersedes (message),
2349 "type", empathy_message_get_tptype (message),
2350 "timestamp", empathy_message_get_original_timestamp (message),
2351 "incoming", empathy_message_is_incoming (message),
2353 "receiver", empathy_message_get_receiver (message),
2354 "sender", empathy_message_get_sender (message),
2357 empathy_chat_view_append_message (chat->view, syn_msg);
2358 empathy_chat_view_edit_message (chat->view, message);
2360 g_object_unref (syn_msg);
2362 /* append the latest message */
2363 empathy_chat_view_append_message (chat->view, message);
2366 g_object_unref (message);
2368 g_list_free (messages);
2371 /* in case of TPL error, skip backlog and show pending messages */
2372 priv->can_show_pending = TRUE;
2373 show_pending_messages (chat);
2375 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2376 * about it in EmpathyChatPriv definition */
2377 priv->retrieving_backlogs = FALSE;
2378 empathy_chat_messages_read (chat);
2380 /* Turn back on scrolling */
2381 empathy_chat_view_scroll (chat->view, TRUE);
2385 chat_add_logs (EmpathyChat *chat)
2387 EmpathyChatPriv *priv = GET_PRIV (chat);
2394 /* Turn off scrolling temporarily */
2395 empathy_chat_view_scroll (chat->view, FALSE);
2397 /* Add messages from last conversation */
2398 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2399 target = tpl_entity_new_from_room_id (priv->id);
2401 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2403 priv->retrieving_backlogs = TRUE;
2404 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2407 TPL_EVENT_MASK_TEXT,
2411 got_filtered_messages_cb,
2414 g_object_unref (target);
2418 chat_contacts_completion_func (const gchar *s1,
2422 gchar *tmp, *nick1, *nick2;
2429 return s1 ? -1 : +1;
2432 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2433 nick1 = g_utf8_casefold (tmp, -1);
2436 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2437 nick2 = g_utf8_casefold (tmp, -1);
2440 ret = strncmp (nick1, nick2, n);
2449 build_part_message (guint reason,
2451 EmpathyContact *actor,
2452 const gchar *message)
2454 GString *s = g_string_new ("");
2455 const gchar *actor_name = NULL;
2457 if (actor != NULL) {
2458 actor_name = empathy_contact_get_alias (actor);
2461 /* Having an actor only really makes sense for a few actions... */
2463 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2464 g_string_append_printf (s, _("%s has disconnected"), name);
2466 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2467 if (actor_name != NULL) {
2468 /* translators: reverse the order of these arguments
2469 * if the kicked should come before the kicker in your locale.
2471 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2474 g_string_append_printf (s, _("%s was kicked"), name);
2477 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2478 if (actor_name != NULL) {
2479 /* translators: reverse the order of these arguments
2480 * if the banned should come before the banner in your locale.
2482 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2485 g_string_append_printf (s, _("%s was banned"), name);
2489 g_string_append_printf (s, _("%s has left the room"), name);
2492 if (!EMP_STR_EMPTY (message)) {
2493 /* Note to translators: this string is appended to
2494 * notifications like "foo has left the room", with the message
2495 * given by the user living the room. If this poses a problem,
2496 * please let us know. :-)
2498 g_string_append_printf (s, _(" (%s)"), message);
2501 return g_string_free (s, FALSE);
2505 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2506 EmpathyContact *contact,
2507 EmpathyContact *actor,
2513 EmpathyChatPriv *priv = GET_PRIV (chat);
2514 const gchar *name = empathy_contact_get_alias (contact);
2517 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2519 if (priv->block_events_timeout_id != 0)
2523 str = g_strdup_printf (_("%s has joined the room"),
2526 str = build_part_message (reason, name, actor, message);
2529 empathy_chat_view_append_event (chat->view, str);
2534 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2535 EmpathyContact *old_contact,
2536 EmpathyContact *new_contact,
2541 EmpathyChatPriv *priv = GET_PRIV (chat);
2543 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2545 if (priv->block_events_timeout_id == 0) {
2548 str = g_strdup_printf (_("%s is now known as %s"),
2549 empathy_contact_get_alias (old_contact),
2550 empathy_contact_get_alias (new_contact));
2551 empathy_chat_view_append_event (chat->view, str);
2558 chat_reset_size_request (gpointer widget)
2560 gtk_widget_set_size_request (widget, -1, -1);
2566 chat_update_contacts_visibility (EmpathyChat *chat,
2569 EmpathyChatPriv *priv = GET_PRIV (chat);
2570 GtkAllocation allocation;
2572 if (!priv->scrolled_window_contacts) {
2576 if (priv->remote_contact != NULL) {
2580 if (show && priv->contact_list_view == NULL) {
2581 EmpathyContactListStore *store;
2584 /* We are adding the contact list to the chat, we don't want the
2585 * chat view to become too small. If the chat view is already
2586 * smaller than 250 make sure that size won't change. If the
2587 * chat view is bigger the contact list will take some space on
2588 * it but we make sure the chat view don't become smaller than
2589 * 250. Relax the size request once the resize is done */
2590 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2591 min_width = MIN (allocation.width, 250);
2592 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2593 g_idle_add (chat_reset_size_request, priv->vbox_left);
2595 if (priv->contacts_width > 0) {
2596 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2597 priv->contacts_width);
2600 store = empathy_contact_list_store_new (
2601 EMPATHY_CONTACT_LIST (priv->tp_chat));
2602 empathy_contact_list_store_set_show_groups (
2603 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2605 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2606 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2607 EMPATHY_CONTACT_FEATURE_CHAT |
2608 EMPATHY_CONTACT_FEATURE_CALL |
2609 EMPATHY_CONTACT_FEATURE_LOG |
2610 EMPATHY_CONTACT_FEATURE_INFO));
2611 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2612 priv->contact_list_view);
2613 gtk_widget_show (priv->contact_list_view);
2614 gtk_widget_show (priv->scrolled_window_contacts);
2615 g_object_unref (store);
2617 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2618 gtk_widget_hide (priv->scrolled_window_contacts);
2619 if (priv->contact_list_view != NULL) {
2620 gtk_widget_destroy (priv->contact_list_view);
2621 priv->contact_list_view = NULL;
2627 empathy_chat_set_show_contacts (EmpathyChat *chat,
2630 EmpathyChatPriv *priv = GET_PRIV (chat);
2632 priv->show_contacts = show;
2634 chat_update_contacts_visibility (chat, show);
2636 g_object_notify (G_OBJECT (chat), "show-contacts");
2640 chat_remote_contact_changed_cb (EmpathyChat *chat)
2642 EmpathyChatPriv *priv = GET_PRIV (chat);
2644 if (priv->remote_contact != NULL) {
2645 g_object_unref (priv->remote_contact);
2646 priv->remote_contact = NULL;
2651 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2652 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2653 if (priv->remote_contact != NULL) {
2654 g_object_ref (priv->remote_contact);
2655 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2657 else if (priv->tp_chat != NULL) {
2658 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2661 chat_update_contacts_visibility (chat, priv->show_contacts);
2663 g_object_notify (G_OBJECT (chat), "remote-contact");
2664 g_object_notify (G_OBJECT (chat), "id");
2668 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2674 EmpathyChatPriv *priv;
2676 priv = GET_PRIV (chat);
2678 if (!priv->tp_chat) {
2682 chat_composing_remove_timeout (chat);
2683 g_object_unref (priv->tp_chat);
2684 priv->tp_chat = NULL;
2685 g_object_notify (G_OBJECT (chat), "tp-chat");
2687 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2688 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2690 chat_update_contacts_visibility (chat, FALSE);
2694 update_misspelled_words (gpointer data)
2696 EmpathyChat *chat = EMPATHY_CHAT (data);
2697 EmpathyChatPriv *priv = GET_PRIV (chat);
2698 GtkTextBuffer *buffer;
2702 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2704 gtk_text_buffer_get_end_iter (buffer, &iter);
2705 length = gtk_text_iter_get_offset (&iter);
2706 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2707 NULL, length, chat);
2709 priv->update_misspelled_words_id = 0;
2715 conf_spell_checking_cb (GSettings *gsettings_chat,
2719 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2720 EmpathyChatPriv *priv = GET_PRIV (chat);
2721 gboolean spell_checker;
2722 GtkTextBuffer *buffer;
2724 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2727 spell_checker = g_settings_get_boolean (gsettings_chat,
2728 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2730 if (!empathy_spell_supported ()) {
2731 spell_checker = FALSE;
2734 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2736 if (spell_checker == priv->spell_checking_enabled) {
2737 if (spell_checker) {
2738 /* Possibly changed dictionaries,
2739 * update misspelled words. Need to do so in idle
2740 * so the spell checker is updated. */
2741 priv->update_misspelled_words_id =
2742 g_idle_add (update_misspelled_words, chat);
2748 if (spell_checker) {
2751 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2752 buffer, "notify::cursor-position",
2753 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2755 priv->insert_text_id = tp_g_signal_connect_object (
2756 buffer, "insert-text",
2757 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2758 chat, G_CONNECT_AFTER);
2759 priv->delete_range_id = tp_g_signal_connect_object (
2760 buffer, "delete-range",
2761 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2762 chat, G_CONNECT_AFTER);
2764 gtk_text_buffer_create_tag (buffer, "misspelled",
2765 "underline", PANGO_UNDERLINE_ERROR,
2768 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2769 gtk_text_buffer_get_insert (buffer));
2770 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2773 /* Mark misspelled words in the existing buffer.
2774 * Need to do so in idle so the spell checker is updated. */
2775 priv->update_misspelled_words_id =
2776 g_idle_add (update_misspelled_words, chat);
2778 GtkTextTagTable *table;
2781 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2782 priv->notify_cursor_position_id = 0;
2783 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2784 priv->insert_text_id = 0;
2785 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2786 priv->delete_range_id = 0;
2788 table = gtk_text_buffer_get_tag_table (buffer);
2789 tag = gtk_text_tag_table_lookup (table, "misspelled");
2790 gtk_text_tag_table_remove (table, tag);
2792 gtk_text_buffer_delete_mark_by_name (buffer,
2793 "previous-cursor-position");
2796 priv->spell_checking_enabled = spell_checker;
2800 save_paned_pos_timeout (gpointer data)
2802 EmpathyChat *self = data;
2805 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2807 g_settings_set_int (self->priv->gsettings_ui,
2808 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2815 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2819 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2821 if (chat->priv->save_paned_pos_id != 0)
2822 g_source_remove (chat->priv->save_paned_pos_id);
2824 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2825 save_paned_pos_timeout, chat);
2831 chat_create_ui (EmpathyChat *chat)
2833 EmpathyChatPriv *priv = GET_PRIV (chat);
2837 GtkTextBuffer *buffer;
2839 EmpathyThemeManager *theme_mgr;
2841 filename = empathy_file_lookup ("empathy-chat.ui",
2843 gui = empathy_builder_get_file (filename,
2844 "chat_widget", &priv->widget,
2845 "hpaned", &priv->hpaned,
2846 "vbox_left", &priv->vbox_left,
2847 "scrolled_window_chat", &priv->scrolled_window_chat,
2848 "scrolled_window_input", &priv->scrolled_window_input,
2849 "hbox_topic", &priv->hbox_topic,
2850 "expander_topic", &priv->expander_topic,
2851 "label_topic", &priv->label_topic,
2852 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2853 "info_bar_vbox", &priv->info_bar_vbox,
2856 empathy_builder_connect (gui, chat,
2857 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2858 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2863 /* Add message view. */
2864 theme_mgr = empathy_theme_manager_dup_singleton ();
2865 chat->view = empathy_theme_manager_create_view (theme_mgr);
2866 g_object_unref (theme_mgr);
2867 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2868 and other types, even though it's non-editable and doesn't accept any
2869 drags. This steals drag motion for anything inside the scrollbars,
2870 making drag destinations on chat windows far less useful.
2872 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2873 g_signal_connect (chat->view, "focus_in_event",
2874 G_CALLBACK (chat_text_view_focus_in_event_cb),
2876 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2877 GTK_WIDGET (chat->view));
2878 gtk_widget_show (GTK_WIDGET (chat->view));
2880 /* Add input GtkTextView */
2881 chat->input_text_view = empathy_input_text_view_new ();
2882 g_signal_connect (chat->input_text_view, "notify::has-focus",
2883 G_CALLBACK (chat_input_has_focus_notify_cb),
2885 g_signal_connect (chat->input_text_view, "key-press-event",
2886 G_CALLBACK (chat_input_key_press_event_cb),
2888 g_signal_connect (chat->input_text_view, "realize",
2889 G_CALLBACK (chat_input_realize_cb),
2891 g_signal_connect (chat->input_text_view, "populate-popup",
2892 G_CALLBACK (chat_input_populate_popup_cb),
2894 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2895 tp_g_signal_connect_object (buffer, "changed",
2896 G_CALLBACK (chat_input_text_buffer_changed_cb),
2898 tp_g_signal_connect_object (priv->gsettings_chat,
2899 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2900 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2901 conf_spell_checking_cb (priv->gsettings_chat,
2902 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2903 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2904 chat->input_text_view);
2905 gtk_widget_show (chat->input_text_view);
2907 /* Add the (invisible) search bar */
2908 priv->search_bar = empathy_search_bar_new (chat->view);
2909 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2912 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2914 /* Initialy hide the topic, will be shown if not empty */
2915 gtk_widget_hide (priv->hbox_topic);
2917 g_signal_connect (priv->hpaned, "notify::position",
2918 G_CALLBACK (chat_hpaned_pos_changed_cb),
2921 /* Load the paned position */
2922 paned_pos = g_settings_get_int (priv->gsettings_ui,
2923 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2925 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2927 /* Set widget focus order */
2928 list = g_list_append (NULL, priv->search_bar);
2929 list = g_list_append (list, priv->scrolled_window_input);
2930 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2933 list = g_list_append (NULL, priv->vbox_left);
2934 list = g_list_append (list, priv->scrolled_window_contacts);
2935 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2938 list = g_list_append (NULL, priv->hpaned);
2939 list = g_list_append (list, priv->hbox_topic);
2940 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2943 /* Add the main widget in the chat widget */
2944 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2945 g_object_unref (gui);
2949 chat_size_allocate (GtkWidget *widget,
2950 GtkAllocation *allocation)
2952 GtkBin *bin = GTK_BIN (widget);
2953 GtkAllocation child_allocation;
2956 gtk_widget_set_allocation (widget, allocation);
2958 child = gtk_bin_get_child (bin);
2960 if (child && gtk_widget_get_visible (child))
2962 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2963 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2964 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2965 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2967 gtk_widget_size_allocate (child, &child_allocation);
2972 chat_finalize (GObject *object)
2975 EmpathyChatPriv *priv;
2977 chat = EMPATHY_CHAT (object);
2978 priv = GET_PRIV (chat);
2980 DEBUG ("Finalized: %p", object);
2982 if (priv->update_misspelled_words_id != 0)
2983 g_source_remove (priv->update_misspelled_words_id);
2985 if (priv->save_paned_pos_id != 0)
2986 g_source_remove (priv->save_paned_pos_id);
2988 g_object_unref (priv->gsettings_chat);
2989 g_object_unref (priv->gsettings_ui);
2991 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2992 g_list_free (priv->input_history);
2994 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2995 g_list_free (priv->compositors);
2997 chat_composing_remove_timeout (chat);
2999 g_object_unref (priv->account_manager);
3000 g_object_unref (priv->log_manager);
3002 if (priv->tp_chat) {
3003 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3004 chat_invalidated_cb, chat);
3005 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3006 chat_message_received_cb, chat);
3007 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3008 chat_message_acknowledged_cb, chat);
3009 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3010 chat_send_error_cb, chat);
3011 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3012 chat_state_changed_cb, chat);
3013 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3014 chat_property_changed_cb, chat);
3015 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3016 chat_members_changed_cb, chat);
3017 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3018 chat_remote_contact_changed_cb, chat);
3019 empathy_tp_chat_leave (priv->tp_chat, "");
3020 g_object_unref (priv->tp_chat);
3022 if (priv->account) {
3023 g_object_unref (priv->account);
3025 if (priv->remote_contact) {
3026 g_object_unref (priv->remote_contact);
3029 if (priv->block_events_timeout_id) {
3030 g_source_remove (priv->block_events_timeout_id);
3034 g_free (priv->name);
3035 g_free (priv->subject);
3036 g_completion_free (priv->completion);
3038 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3042 chat_constructed (GObject *object)
3044 EmpathyChat *chat = EMPATHY_CHAT (object);
3045 EmpathyChatPriv *priv = GET_PRIV (chat);
3047 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3048 /* First display logs from the logger and then display pending messages */
3049 chat_add_logs (chat);
3052 /* Just display pending messages for rooms */
3053 priv->can_show_pending = TRUE;
3054 show_pending_messages (chat);
3059 empathy_chat_class_init (EmpathyChatClass *klass)
3061 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
3062 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3064 object_class->finalize = chat_finalize;
3065 object_class->get_property = chat_get_property;
3066 object_class->set_property = chat_set_property;
3067 object_class->constructed = chat_constructed;
3069 widget_class->size_allocate = chat_size_allocate;
3071 g_object_class_install_property (object_class,
3073 g_param_spec_object ("tp-chat",
3075 "The tp chat object",
3076 EMPATHY_TYPE_TP_CHAT,
3079 G_PARAM_STATIC_STRINGS));
3080 g_object_class_install_property (object_class,
3082 g_param_spec_object ("account",
3083 "Account of the chat",
3084 "The account of the chat",
3087 G_PARAM_STATIC_STRINGS));
3088 g_object_class_install_property (object_class,
3090 g_param_spec_string ("id",
3092 "The id of the chat",
3095 G_PARAM_STATIC_STRINGS));
3096 g_object_class_install_property (object_class,
3098 g_param_spec_string ("name",
3100 "The name of the chat",
3103 G_PARAM_STATIC_STRINGS));
3104 g_object_class_install_property (object_class,
3106 g_param_spec_string ("subject",
3108 "The subject or topic of the chat",
3111 G_PARAM_STATIC_STRINGS));
3112 g_object_class_install_property (object_class,
3113 PROP_REMOTE_CONTACT,
3114 g_param_spec_object ("remote-contact",
3115 "The remote contact",
3116 "The remote contact is any",
3117 EMPATHY_TYPE_CONTACT,
3119 G_PARAM_STATIC_STRINGS));
3120 g_object_class_install_property (object_class,
3122 g_param_spec_boolean ("show-contacts",
3123 "Contacts' visibility",
3124 "The visibility of the contacts' list",
3127 G_PARAM_STATIC_STRINGS));
3129 g_object_class_install_property (object_class,
3131 g_param_spec_boolean ("sms-channel",
3133 "TRUE if this channel is for sending SMSes",
3135 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3137 g_object_class_install_property (object_class,
3138 PROP_N_MESSAGES_SENDING,
3139 g_param_spec_uint ("n-messages-sending",
3140 "Num Messages Sending",
3141 "The number of messages being sent",
3143 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3145 g_object_class_install_property (object_class,
3146 PROP_NB_UNREAD_MESSAGES,
3147 g_param_spec_uint ("nb-unread-messages",
3148 "Num Unread Messages",
3149 "The number of unread messages",
3151 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3153 signals[COMPOSING] =
3154 g_signal_new ("composing",
3155 G_OBJECT_CLASS_TYPE (object_class),
3159 g_cclosure_marshal_VOID__BOOLEAN,
3163 signals[NEW_MESSAGE] =
3164 g_signal_new ("new-message",
3165 G_OBJECT_CLASS_TYPE (object_class),
3169 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3171 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3173 signals[PART_COMMAND_ENTERED] =
3174 g_signal_new ("part-command-entered",
3175 G_OBJECT_CLASS_TYPE (object_class),
3179 g_cclosure_marshal_VOID__POINTER,
3183 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3187 chat_block_events_timeout_cb (gpointer data)
3189 EmpathyChatPriv *priv = GET_PRIV (data);
3191 priv->block_events_timeout_id = 0;
3197 account_manager_prepared_cb (GObject *source_object,
3198 GAsyncResult *result,
3201 GList *accounts, *l;
3202 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3203 EmpathyChat *chat = user_data;
3204 GError *error = NULL;
3206 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3207 DEBUG ("Failed to prepare the account manager: %s", error->message);
3208 g_error_free (error);
3212 accounts = tp_account_manager_get_valid_accounts (account_manager);
3214 for (l = accounts; l != NULL; l = l->next) {
3215 TpAccount *account = l->data;
3216 tp_g_signal_connect_object (account, "status-changed",
3217 G_CALLBACK (chat_new_connection_cb),
3221 g_list_free (accounts);
3225 empathy_chat_init (EmpathyChat *chat)
3227 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3228 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3231 priv->log_manager = tpl_log_manager_dup_singleton ();
3232 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3233 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3235 priv->contacts_width = -1;
3236 priv->input_history = NULL;
3237 priv->input_history_current = NULL;
3238 priv->account_manager = tp_account_manager_dup ();
3240 tp_account_manager_prepare_async (priv->account_manager, NULL,
3241 account_manager_prepared_cb, chat);
3243 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3244 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3246 /* Block events for some time to avoid having "has come online" or
3247 * "joined" messages. */
3248 priv->block_events_timeout_id =
3249 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3251 /* Add nick name completion */
3252 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3253 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3255 chat_create_ui (chat);
3259 empathy_chat_new (EmpathyTpChat *tp_chat)
3261 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3265 empathy_chat_get_tp_chat (EmpathyChat *chat)
3267 EmpathyChatPriv *priv = GET_PRIV (chat);
3269 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3271 return priv->tp_chat;
3277 GtkWidget *info_bar;
3287 passwd_remember_button_cb (GtkButton *button,
3290 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3294 passwd_not_now_button_cb (GtkButton *button,
3297 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3301 remember_password_infobar_response_cb (GtkWidget *info_bar,
3305 EmpathyChatPriv *priv = GET_PRIV (data->self);
3307 if (response_id == GTK_RESPONSE_OK) {
3308 DEBUG ("Saving room password");
3309 empathy_keyring_set_room_password_async (priv->account,
3310 empathy_tp_chat_get_id (priv->tp_chat),
3315 gtk_widget_destroy (info_bar);
3316 g_free (data->password);
3317 g_slice_free (PasswordData, data);
3321 chat_prompt_to_save_password (EmpathyChat *self,
3324 GtkWidget *content_area;
3331 /* save the password in case it needs to be saved */
3332 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3334 /* Remove all previous widgets */
3335 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3336 gtk_container_forall (GTK_CONTAINER (content_area),
3337 (GtkCallback) gtk_widget_destroy, NULL);
3338 data->button = NULL;
3341 data->spinner = NULL;
3343 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3344 GTK_MESSAGE_QUESTION);
3346 hbox = gtk_hbox_new (FALSE, 5);
3347 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3350 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3351 GTK_ICON_SIZE_DIALOG);
3352 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3355 label = gtk_label_new (_("Would you like to store this password?"));
3356 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3358 /* Add 'Remember' button */
3359 alig = gtk_alignment_new (0, 0.5, 1, 0);
3361 button = gtk_button_new_with_label (_("Remember"));
3362 gtk_container_add (GTK_CONTAINER (alig), button);
3363 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3365 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3368 /* Add 'Not now' button */
3369 alig = gtk_alignment_new (0, 0.5, 1, 0);
3371 button = gtk_button_new_with_label (_("Not now"));
3372 gtk_container_add (GTK_CONTAINER (alig), button);
3373 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3375 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3379 g_signal_handler_disconnect (data->info_bar, data->response_id);
3380 g_signal_connect (data->info_bar, "response",
3381 G_CALLBACK (remember_password_infobar_response_cb), data);
3383 gtk_widget_show_all (data->info_bar);
3387 provide_password_cb (GObject *tp_chat,
3391 PasswordData *data = user_data;
3392 EmpathyChat *self = data->self;
3393 EmpathyChatPriv *priv = GET_PRIV (self);
3394 GError *error = NULL;
3396 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3398 DEBUG ("error: %s", error->message);
3399 /* FIXME: what should we do if that's another error? Close the channel?
3400 * Display the raw D-Bus error to the user isn't very useful */
3401 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3403 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3404 gtk_widget_set_sensitive (data->entry, TRUE);
3405 gtk_widget_grab_focus (data->entry);
3408 gtk_info_bar_set_message_type (
3409 GTK_INFO_BAR (data->info_bar),
3413 gtk_widget_set_sensitive (data->button, TRUE);
3414 gtk_button_set_label (GTK_BUTTON (data->button),
3418 gtk_label_set_text (GTK_LABEL (data->label),
3419 _("Wrong password; please try again:"));
3422 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3423 gtk_widget_hide (data->spinner);
3425 g_error_free (error);
3429 if (empathy_keyring_is_available ()) {
3430 /* ask whether they want to save the password */
3431 chat_prompt_to_save_password (self, data);
3433 /* Get rid of the password info bar finally */
3434 gtk_widget_destroy (data->info_bar);
3435 g_slice_free (PasswordData, data);
3439 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3440 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3441 gtk_widget_grab_focus (self->input_text_view);
3445 password_infobar_response_cb (GtkWidget *info_bar,
3449 EmpathyChatPriv *priv = GET_PRIV (data->self);
3450 const gchar *password;
3452 if (response_id != GTK_RESPONSE_OK) {
3453 gtk_widget_destroy (info_bar);
3454 g_slice_free (PasswordData, data);
3458 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3460 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3461 provide_password_cb, data);
3463 gtk_widget_set_sensitive (data->button, FALSE);
3464 gtk_widget_set_sensitive (data->entry, FALSE);
3466 gtk_spinner_start (GTK_SPINNER (data->spinner));
3467 gtk_widget_show (data->spinner);
3471 password_entry_activate_cb (GtkWidget *entry,
3474 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3478 passwd_join_button_cb (GtkButton *button,
3481 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3485 clear_icon_released_cb (GtkEntry *entry,
3486 GtkEntryIconPosition icon_pos,
3490 gtk_entry_set_text (entry, "");
3494 password_entry_changed_cb (GtkEditable *entry,
3499 str = gtk_entry_get_text (GTK_ENTRY (entry));
3501 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3502 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3506 infobar_chat_invalidated_cb (TpProxy *proxy,
3510 gpointer password_infobar)
3512 /* Destroy the password infobar whenever a channel is invalidated
3513 * so we don't have multiple infobars when the MUC is rejoined */
3514 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3518 display_password_info_bar (EmpathyChat *self)
3520 EmpathyChatPriv *priv = GET_PRIV (self);
3521 GtkWidget *info_bar;
3522 GtkWidget *content_area;
3532 data = g_slice_new0 (PasswordData);
3534 info_bar = gtk_info_bar_new ();
3535 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3536 GTK_MESSAGE_QUESTION);
3538 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3540 hbox = gtk_hbox_new (FALSE, 5);
3541 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3544 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3545 GTK_ICON_SIZE_DIALOG);
3546 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3549 label = gtk_label_new (_("This room is protected by a password:"));
3550 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3552 /* Add password entry */
3553 entry = gtk_entry_new ();
3554 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3555 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3557 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3558 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3559 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3560 GTK_ENTRY_ICON_SECONDARY, FALSE);
3562 g_signal_connect (entry, "icon-release",
3563 G_CALLBACK (clear_icon_released_cb), data);
3564 g_signal_connect (entry, "changed",
3565 G_CALLBACK (password_entry_changed_cb), data);
3567 g_signal_connect (entry, "activate",
3568 G_CALLBACK (password_entry_activate_cb), data);
3570 /* Focus the password entry once it's realized */
3571 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3573 /* Add 'Join' button */
3574 alig = gtk_alignment_new (0, 0.5, 1, 0);
3576 button = gtk_button_new_with_label (_("Join"));
3577 gtk_container_add (GTK_CONTAINER (alig), button);
3578 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3580 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3584 spinner = gtk_spinner_new ();
3585 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3587 /* Save some data for messing around with later */
3589 data->info_bar = info_bar;
3590 data->button = button;
3591 data->label = label;
3592 data->entry = entry;
3593 data->spinner = spinner;
3595 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3597 gtk_widget_show_all (hbox);
3599 tp_g_signal_connect_object (priv->tp_chat,
3600 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3603 data->response_id = g_signal_connect (info_bar, "response",
3604 G_CALLBACK (password_infobar_response_cb), data);
3606 gtk_widget_show_all (info_bar);
3607 /* ... but hide the spinner */
3608 gtk_widget_hide (spinner);
3610 /* prevent the user from typing anything */
3611 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3615 provide_saved_password_cb (GObject *tp_chat,
3619 EmpathyChat *self = user_data;
3620 EmpathyChatPriv *priv = GET_PRIV (self);
3621 GError *error = NULL;
3623 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3625 DEBUG ("error: %s", error->message);
3626 /* FIXME: what should we do if that's another error? Close the channel?
3627 * Display the raw D-Bus error to the user isn't very useful */
3628 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3629 display_password_info_bar (self);
3630 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3632 g_error_free (error);
3637 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3638 gtk_widget_grab_focus (self->input_text_view);
3642 chat_room_got_password_cb (GObject *source,
3643 GAsyncResult *result,
3646 EmpathyChat *self = user_data;
3647 EmpathyChatPriv *priv = GET_PRIV (self);
3648 const gchar *password;
3649 GError *error = NULL;
3651 password = empathy_keyring_get_room_password_finish (priv->account,
3654 if (error != NULL) {
3655 DEBUG ("Couldn't get room password: %s\n", error->message);
3656 g_clear_error (&error);
3658 display_password_info_bar (self);
3659 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3663 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3664 provide_saved_password_cb, self);
3668 chat_password_needed_changed_cb (EmpathyChat *self)
3670 EmpathyChatPriv *priv = GET_PRIV (self);
3672 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3673 empathy_keyring_get_room_password_async (priv->account,
3674 empathy_tp_chat_get_id (priv->tp_chat),
3675 chat_room_got_password_cb, self);
3680 chat_sms_channel_changed_cb (EmpathyChat *self)
3682 EmpathyChatPriv *priv = GET_PRIV (self);
3684 priv->sms_channel = tp_text_channel_is_sms_channel (
3685 (TpTextChannel *) priv->tp_chat);
3686 g_object_notify (G_OBJECT (self), "sms-channel");
3690 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3692 g_object_notify (G_OBJECT (self), "n-messages-sending");
3696 empathy_chat_set_tp_chat (EmpathyChat *chat,
3697 EmpathyTpChat *tp_chat)
3699 EmpathyChatPriv *priv = GET_PRIV (chat);
3700 GPtrArray *properties;
3702 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3703 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3705 if (priv->tp_chat) {
3709 if (priv->account) {
3710 g_object_unref (priv->account);
3713 priv->tp_chat = g_object_ref (tp_chat);
3714 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3716 g_signal_connect (tp_chat, "invalidated",
3717 G_CALLBACK (chat_invalidated_cb),
3719 g_signal_connect (tp_chat, "message-received-empathy",
3720 G_CALLBACK (chat_message_received_cb),
3722 g_signal_connect (tp_chat, "message_acknowledged",
3723 G_CALLBACK (chat_message_acknowledged_cb),
3725 g_signal_connect (tp_chat, "send-error",
3726 G_CALLBACK (chat_send_error_cb),
3728 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3729 G_CALLBACK (chat_state_changed_cb),
3731 g_signal_connect (tp_chat, "property-changed",
3732 G_CALLBACK (chat_property_changed_cb),
3734 g_signal_connect (tp_chat, "members-changed",
3735 G_CALLBACK (chat_members_changed_cb),
3737 g_signal_connect (tp_chat, "member-renamed",
3738 G_CALLBACK (chat_member_renamed_cb),
3740 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3741 G_CALLBACK (chat_remote_contact_changed_cb),
3743 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3744 G_CALLBACK (chat_password_needed_changed_cb),
3746 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
3747 G_CALLBACK (chat_sms_channel_changed_cb),
3749 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3750 G_CALLBACK (chat_n_messages_sending_changed_cb),
3753 /* Get initial value of properties */
3754 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3755 if (properties != NULL) {
3758 for (i = 0; i < properties->len; i++) {
3759 EmpathyTpChatProperty *property;
3761 property = g_ptr_array_index (properties, i);
3762 if (property->value == NULL)
3765 chat_property_changed_cb (priv->tp_chat,
3772 chat_sms_channel_changed_cb (chat);
3773 chat_remote_contact_changed_cb (chat);
3775 if (chat->input_text_view) {
3776 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3777 if (priv->block_events_timeout_id == 0) {
3778 empathy_chat_view_append_event (chat->view, _("Connected"));
3782 g_object_notify (G_OBJECT (chat), "tp-chat");
3783 g_object_notify (G_OBJECT (chat), "id");
3784 g_object_notify (G_OBJECT (chat), "account");
3786 /* This is a noop when tp-chat is set at object construction time and causes
3787 * the pending messages to be show when it's set on the object after it has
3789 show_pending_messages (chat);
3791 /* check if a password is needed */
3792 chat_password_needed_changed_cb (chat);
3796 empathy_chat_get_account (EmpathyChat *chat)
3798 EmpathyChatPriv *priv = GET_PRIV (chat);
3800 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3802 return priv->account;
3806 empathy_chat_get_id (EmpathyChat *chat)
3808 EmpathyChatPriv *priv = GET_PRIV (chat);
3810 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3816 empathy_chat_dup_name (EmpathyChat *chat)
3818 EmpathyChatPriv *priv = GET_PRIV (chat);
3821 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3825 if (!ret && priv->remote_contact) {
3826 ret = empathy_contact_get_alias (priv->remote_contact);
3833 ret = _("Conversation");
3835 if (priv->sms_channel)
3836 /* Translators: this string is a something like
3837 * "Escher Cat (SMS)" */
3838 return g_strdup_printf (_("%s (SMS)"), ret);
3840 return g_strdup (ret);
3844 empathy_chat_get_subject (EmpathyChat *chat)
3846 EmpathyChatPriv *priv = GET_PRIV (chat);
3848 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3850 return priv->subject;
3854 empathy_chat_get_remote_contact (EmpathyChat *chat)
3856 EmpathyChatPriv *priv = GET_PRIV (chat);
3858 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3860 return priv->remote_contact;
3864 empathy_chat_get_contact_menu (EmpathyChat *chat)
3866 EmpathyChatPriv *priv = GET_PRIV (chat);
3867 GtkWidget *menu = NULL;
3869 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3871 if (priv->remote_contact) {
3872 menu = empathy_contact_menu_new (priv->remote_contact,
3873 EMPATHY_CONTACT_FEATURE_CALL |
3874 EMPATHY_CONTACT_FEATURE_LOG |
3875 EMPATHY_CONTACT_FEATURE_INFO |
3876 EMPATHY_CONTACT_FEATURE_BLOCK);
3883 empathy_chat_clear (EmpathyChat *chat)
3885 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3887 empathy_chat_view_clear (chat->view);
3891 empathy_chat_scroll_down (EmpathyChat *chat)
3893 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3895 empathy_chat_view_scroll_down (chat->view);
3899 empathy_chat_cut (EmpathyChat *chat)
3901 GtkTextBuffer *buffer;
3903 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3905 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3906 if (gtk_text_buffer_get_has_selection (buffer)) {
3907 GtkClipboard *clipboard;
3909 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3911 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3916 empathy_chat_copy (EmpathyChat *chat)
3918 GtkTextBuffer *buffer;
3920 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3922 if (empathy_chat_view_get_has_selection (chat->view)) {
3923 empathy_chat_view_copy_clipboard (chat->view);
3927 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3928 if (gtk_text_buffer_get_has_selection (buffer)) {
3929 GtkClipboard *clipboard;
3931 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3933 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3938 EmpathyChatPriv *priv = GET_PRIV (chat);
3940 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3947 GtkClipboard *clipboard;
3949 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3950 start = g_utf8_offset_to_pointer (topic, start_offset);
3951 end = g_utf8_offset_to_pointer (topic, end_offset);
3952 selection = g_strndup (start, end - start);
3954 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3955 gtk_clipboard_set_text (clipboard, selection, -1);
3963 empathy_chat_paste (EmpathyChat *chat)
3965 GtkTextBuffer *buffer;
3966 GtkClipboard *clipboard;
3967 EmpathyChatPriv *priv;
3969 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3971 priv = GET_PRIV (chat);
3973 if (gtk_widget_get_visible (priv->search_bar)) {
3974 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3978 if (priv->tp_chat == NULL ||
3979 !gtk_widget_is_sensitive (chat->input_text_view))
3982 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3983 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3985 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3989 empathy_chat_find (EmpathyChat *chat)
3991 EmpathyChatPriv *priv;
3993 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3995 priv = GET_PRIV (chat);
3997 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4001 empathy_chat_correct_word (EmpathyChat *chat,
4004 const gchar *new_word)
4006 GtkTextBuffer *buffer;
4008 g_return_if_fail (chat != NULL);
4009 g_return_if_fail (new_word != NULL);
4011 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4013 gtk_text_buffer_delete (buffer, start, end);
4014 gtk_text_buffer_insert (buffer, start,
4020 empathy_chat_is_room (EmpathyChat *chat)
4022 EmpathyChatPriv *priv = GET_PRIV (chat);
4024 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4026 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4030 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4032 EmpathyChatPriv *priv = GET_PRIV (self);
4034 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4036 return priv->unread_messages;
4039 /* called when the messages have been read by user */
4041 empathy_chat_messages_read (EmpathyChat *self)
4043 EmpathyChatPriv *priv = GET_PRIV (self);
4045 g_return_if_fail (EMPATHY_IS_CHAT (self));
4047 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4048 * definition. If we are still retrieving the backlogs, do not ACK */
4049 if (priv->retrieving_backlogs)
4052 if (priv->tp_chat != NULL) {
4053 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
4057 /* Return TRUE if on of the contacts in this chat is composing */
4059 empathy_chat_is_composing (EmpathyChat *chat)
4061 return chat->priv->compositors != NULL;
4065 empathy_chat_is_sms_channel (EmpathyChat *self)
4067 EmpathyChatPriv *priv = GET_PRIV (self);
4069 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4071 return priv->sms_channel;
4075 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4077 EmpathyChatPriv *priv;
4079 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4081 priv = GET_PRIV (self);
4083 if (priv->tp_chat == NULL) {
4088 g_object_get (priv->tp_chat,
4089 "n-messages-sending", &n_messages,
4097 empathy_chat_dup_text (EmpathyChat *self)
4099 GtkTextBuffer *buffer;
4100 GtkTextIter start, end;
4102 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4104 gtk_text_buffer_get_bounds (buffer, &start, &end);
4105 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4109 empathy_chat_set_text (EmpathyChat *self,
4112 GtkTextBuffer *buffer;
4114 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4116 gtk_text_buffer_set_text (buffer, text, -1);