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-input-text-view.h"
56 #include "empathy-search-bar.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-theme-adium.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;
109 /* Source func ID for chat_contacts_visible_timeout_cb () */
110 guint contacts_visible_id;
114 GtkWidget *vbox_left;
115 GtkWidget *scrolled_window_chat;
116 GtkWidget *scrolled_window_input;
117 GtkWidget *scrolled_window_contacts;
118 GtkWidget *hbox_topic;
119 GtkWidget *expander_topic;
120 GtkWidget *label_topic;
121 GtkWidget *contact_list_view;
122 GtkWidget *info_bar_vbox;
123 GtkWidget *search_bar;
125 guint unread_messages;
126 guint unread_messages_when_offline;
127 /* TRUE if the pending messages can be displayed. This is to avoid to show
128 * pending messages *before* messages from logs. (#603980) */
129 gboolean can_show_pending;
131 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
132 * be differently handled since it introduces another race condition, which
133 * is really hard to occur, but still possible.
135 * With the current workaround (which has the race above), we need to be
136 * sure to ACK any pending messages only when the retrieval of backlogs is
137 * finished, that's why using retrieving_backlogs flag.
138 * empathy_chat_messages_read () will check this variable and not ACK
139 * anything when TRUE. It will be set TRUE at chat_constructed () and set
140 * back to FALSE when the backlog has been retrieved and the pending
141 * messages actually showed to the user.
143 * Race condition introduced with this workaround:
144 * Scenario: a message is pending, the user is notified and selects the tab.
145 * the tab with a pending message is focused before the messages are properly
146 * shown (since the preparation of the window is slower AND async WRT the
147 * tab showing), which means the user won't see any new messages (rare but
148 * possible), if he/she will change tab focus before the messages are
149 * properly shown, the tab will be set as 'seen' and the user won't be
150 * notified again about the already notified pending messages when the
151 * messages in tab will be properly shown */
152 gboolean retrieving_backlogs;
153 gboolean sms_channel;
155 /* we need to know whether populate-popup happened in response to
156 * the keyboard or the mouse. We can't ask GTK for the most recent
157 * event, because it will be a notify event. Instead we track it here */
158 GdkEventType most_recent_event_type;
162 gchar *text; /* Original message that was specified
163 * upon entry creation. */
164 gchar *modified_text; /* Message that was modified by user.
165 * When no modifications were made, it is NULL */
171 PART_COMMAND_ENTERED,
185 PROP_N_MESSAGES_SENDING,
186 PROP_NB_UNREAD_MESSAGES,
189 static guint signals[LAST_SIGNAL] = { 0 };
191 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
193 static gboolean update_misspelled_words (gpointer data);
196 chat_get_property (GObject *object,
201 EmpathyChat *chat = EMPATHY_CHAT (object);
202 EmpathyChatPriv *priv = GET_PRIV (object);
206 g_value_set_object (value, priv->tp_chat);
209 g_value_set_object (value, priv->account);
212 g_value_take_string (value, empathy_chat_dup_name (chat));
215 g_value_set_string (value, priv->id);
218 g_value_set_string (value, priv->subject);
220 case PROP_REMOTE_CONTACT:
221 g_value_set_object (value, priv->remote_contact);
223 case PROP_SHOW_CONTACTS:
224 g_value_set_boolean (value, priv->show_contacts);
226 case PROP_SMS_CHANNEL:
227 g_value_set_boolean (value, priv->sms_channel);
229 case PROP_N_MESSAGES_SENDING:
230 g_value_set_uint (value,
231 empathy_chat_get_n_messages_sending (chat));
233 case PROP_NB_UNREAD_MESSAGES:
234 g_value_set_uint (value,
235 empathy_chat_get_nb_unread_messages (chat));
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
244 chat_set_property (GObject *object,
249 EmpathyChat *chat = EMPATHY_CHAT (object);
253 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
255 case PROP_SHOW_CONTACTS:
256 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
265 account_reconnected (EmpathyChat *chat,
268 EmpathyChatPriv *priv = GET_PRIV (chat);
270 DEBUG ("Account reconnected, request a new Text channel");
272 /* FIXME: Ideally we should ask to handle ourself the channel so we can
273 * report the error if any but this is blocked by
274 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
275 switch (priv->handle_type) {
276 case TP_HANDLE_TYPE_CONTACT:
277 if (priv->sms_channel)
278 empathy_sms_contact_id (
280 TP_USER_ACTION_TIME_NOT_USER_ACTION);
282 empathy_chat_with_contact_id (
284 TP_USER_ACTION_TIME_NOT_USER_ACTION);
286 case TP_HANDLE_TYPE_ROOM:
287 empathy_join_muc (account, priv->id,
288 TP_USER_ACTION_TIME_NOT_USER_ACTION);
290 case TP_HANDLE_TYPE_NONE:
291 case TP_HANDLE_TYPE_LIST:
292 case TP_HANDLE_TYPE_GROUP:
294 g_assert_not_reached ();
298 g_object_unref (chat);
302 chat_new_connection_cb (TpAccount *account,
306 gchar *dbus_error_name,
310 EmpathyChatPriv *priv = GET_PRIV (chat);
312 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
315 if (priv->tp_chat != NULL || account != priv->account ||
316 priv->handle_type == TP_HANDLE_TYPE_NONE ||
317 EMP_STR_EMPTY (priv->id))
322 account_reconnected (chat, account);
326 chat_composing_remove_timeout (EmpathyChat *chat)
328 EmpathyChatPriv *priv;
330 priv = GET_PRIV (chat);
332 if (priv->composing_stop_timeout_id) {
333 g_source_remove (priv->composing_stop_timeout_id);
334 priv->composing_stop_timeout_id = 0;
339 set_chate_state_cb (GObject *source,
340 GAsyncResult *result,
343 GError *error = NULL;
345 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
347 DEBUG ("Failed to set chat state: %s", error->message);
348 g_error_free (error);
353 set_chat_state (EmpathyChat *self,
354 TpChannelChatState state)
356 EmpathyChatPriv *priv = GET_PRIV (self);
358 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
359 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
362 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
363 set_chate_state_cb, self);
367 chat_composing_stop_timeout_cb (EmpathyChat *chat)
369 EmpathyChatPriv *priv;
371 priv = GET_PRIV (chat);
373 priv->composing_stop_timeout_id = 0;
374 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
380 chat_composing_start (EmpathyChat *chat)
382 EmpathyChatPriv *priv;
384 priv = GET_PRIV (chat);
386 if (priv->composing_stop_timeout_id) {
387 /* Just restart the timeout */
388 chat_composing_remove_timeout (chat);
390 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
393 priv->composing_stop_timeout_id = g_timeout_add_seconds (
394 COMPOSING_STOP_TIMEOUT,
395 (GSourceFunc) chat_composing_stop_timeout_cb,
400 chat_composing_stop (EmpathyChat *chat)
402 chat_composing_remove_timeout (chat);
403 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
407 chat_input_history_entry_cmp (InputHistoryEntry *entry,
410 if (!tp_strdiff (entry->text, text)) {
411 if (entry->modified_text != NULL) {
412 /* Modified entry and single string cannot be equal. */
420 static InputHistoryEntry *
421 chat_input_history_entry_new_with_text (const gchar *text)
423 InputHistoryEntry *entry;
424 entry = g_slice_new0 (InputHistoryEntry);
425 entry->text = g_strdup (text);
431 chat_input_history_entry_free (InputHistoryEntry *entry)
433 g_free (entry->text);
434 g_free (entry->modified_text);
435 g_slice_free (InputHistoryEntry, entry);
439 chat_input_history_entry_revert (InputHistoryEntry *entry)
441 g_free (entry->modified_text);
442 entry->modified_text = NULL;
446 chat_input_history_entry_update_text (InputHistoryEntry *entry,
451 if (!tp_strdiff (text, entry->text)) {
452 g_free (entry->modified_text);
453 entry->modified_text = NULL;
457 old = entry->modified_text;
458 entry->modified_text = g_strdup (text);
463 chat_input_history_entry_get_text (InputHistoryEntry *entry)
469 if (entry->modified_text != NULL) {
470 return entry->modified_text;
476 chat_input_history_remove_item (GList *list,
479 list = g_list_remove_link (list, item);
480 chat_input_history_entry_free (item->data);
481 g_list_free_1 (item);
486 chat_input_history_revert (EmpathyChat *chat)
488 EmpathyChatPriv *priv;
492 InputHistoryEntry *entry;
494 priv = GET_PRIV (chat);
495 list = priv->input_history;
498 DEBUG ("No input history");
502 /* Delete temporary entry */
503 if (priv->input_history_current != NULL) {
505 list = chat_input_history_remove_item (list, item1);
506 if (priv->input_history_current == item1) {
507 /* Removed temporary entry was current entry */
508 priv->input_history = list;
509 priv->input_history_current = NULL;
514 /* There is no entry to revert */
518 /* Restore the current history entry to original value */
519 item1 = priv->input_history_current;
521 chat_input_history_entry_revert (entry);
523 /* Remove restored entry if there is other occurance before this entry */
524 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
525 (GCompareFunc) chat_input_history_entry_cmp);
526 if (item2 != item1) {
527 list = chat_input_history_remove_item (list, item1);
530 /* Remove other occurance of the restored entry */
531 item2 = g_list_find_custom (item1->next,
532 chat_input_history_entry_get_text (entry),
533 (GCompareFunc) chat_input_history_entry_cmp);
535 list = chat_input_history_remove_item (list, item2);
539 priv->input_history_current = NULL;
540 priv->input_history = list;
544 chat_input_history_add (EmpathyChat *chat,
548 EmpathyChatPriv *priv;
551 InputHistoryEntry *entry;
553 priv = GET_PRIV (chat);
555 list = priv->input_history;
557 /* Remove any other occurances of this entry, if not temporary */
559 while ((item = g_list_find_custom (list, str,
560 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
561 list = chat_input_history_remove_item (list, item);
564 /* Trim the list to the last 10 items */
565 while (g_list_length (list) > 10) {
566 item = g_list_last (list);
568 list = chat_input_history_remove_item (list, item);
576 entry = chat_input_history_entry_new_with_text (str);
577 list = g_list_prepend (list, entry);
579 /* Set the list and the current item pointer */
580 priv->input_history = list;
582 priv->input_history_current = list;
585 priv->input_history_current = NULL;
590 chat_input_history_get_next (EmpathyChat *chat)
592 EmpathyChatPriv *priv;
596 priv = GET_PRIV (chat);
598 if (priv->input_history == NULL) {
599 DEBUG ("No input history, next entry is NULL");
602 g_assert (priv->input_history_current != NULL);
604 if ((item = g_list_next (priv->input_history_current)) == NULL)
606 item = priv->input_history_current;
609 msg = chat_input_history_entry_get_text (item->data);
611 DEBUG ("Returning next entry: '%s'", msg);
613 priv->input_history_current = item;
619 chat_input_history_get_prev (EmpathyChat *chat)
621 EmpathyChatPriv *priv;
625 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
627 priv = GET_PRIV (chat);
629 if (priv->input_history == NULL) {
630 DEBUG ("No input history, previous entry is NULL");
634 if (priv->input_history_current == NULL)
638 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
640 item = priv->input_history_current;
643 msg = chat_input_history_entry_get_text (item->data);
645 DEBUG ("Returning previous entry: '%s'", msg);
647 priv->input_history_current = item;
653 chat_input_history_update (EmpathyChat *chat,
654 GtkTextBuffer *buffer)
656 EmpathyChatPriv *priv;
657 GtkTextIter start, end;
659 InputHistoryEntry *entry;
661 priv = GET_PRIV (chat);
663 gtk_text_buffer_get_bounds (buffer, &start, &end);
664 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
666 if (priv->input_history_current == NULL) {
667 /* Add the current text temporarily to the history */
668 chat_input_history_add (chat, text, TRUE);
673 /* Save the changes in the history */
674 entry = priv->input_history_current->data;
675 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
676 chat_input_history_entry_update_text (entry, text);
685 } ChatCommandMsgData;
688 chat_command_msg_cb (GObject *source,
689 GAsyncResult *result,
692 ChatCommandMsgData *data = user_data;
693 GError *error = NULL;
696 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
697 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
699 if (channel == NULL) {
700 DEBUG ("Failed to get channel: %s", error->message);
701 g_error_free (error);
703 empathy_chat_view_append_event (data->chat->view,
704 _("Failed to open private chat"));
708 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
709 TpTextChannel *text = (TpTextChannel *) channel;
712 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
715 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
717 g_object_unref (msg);
720 g_object_unref (channel);
723 g_free (data->message);
724 g_slice_free (ChatCommandMsgData, data);
728 nick_command_supported (EmpathyChat *chat)
730 EmpathyChatPriv * priv = GET_PRIV (chat);
731 TpConnection *connection;
733 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
734 return tp_proxy_has_interface_by_id (connection,
735 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
739 part_command_supported (EmpathyChat *chat)
741 EmpathyChatPriv * priv = GET_PRIV (chat);
743 return tp_proxy_has_interface_by_id (priv->tp_chat,
744 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
748 chat_command_clear (EmpathyChat *chat,
751 empathy_chat_view_clear (chat->view);
755 chat_command_topic (EmpathyChat *chat,
758 EmpathyChatPriv *priv = GET_PRIV (chat);
759 EmpathyTpChatProperty *property;
760 GValue value = {0, };
762 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
763 if (property == NULL) {
764 empathy_chat_view_append_event (chat->view,
765 _("Topic not supported on this conversation"));
769 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
770 empathy_chat_view_append_event (chat->view,
771 _("You are not allowed to change the topic"));
775 g_value_init (&value, G_TYPE_STRING);
776 g_value_set_string (&value, strv[1]);
777 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
778 g_value_unset (&value);
782 empathy_chat_join_muc (EmpathyChat *chat,
785 EmpathyChatPriv *priv = GET_PRIV (chat);
787 empathy_join_muc (priv->account, room,
788 empathy_get_current_action_time ());
792 chat_command_join (EmpathyChat *chat,
797 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
799 /* FIXME: Ideally we should ask to handle ourself the channel so we can
800 * report the error if any but this is blocked by
801 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
802 while (rooms[i] != NULL) {
803 /* ignore empty strings */
804 if (!EMP_STR_EMPTY (rooms[i])) {
805 empathy_chat_join_muc (chat, rooms[i]);
813 chat_command_part (EmpathyChat *chat,
816 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
820 chat_command_msg_internal (EmpathyChat *chat,
821 const gchar *contact_id,
822 const gchar *message)
824 EmpathyChatPriv *priv = GET_PRIV (chat);
825 ChatCommandMsgData *data;
826 TpAccountChannelRequest *req;
829 request = tp_asv_new (
830 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
831 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
832 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
835 req = tp_account_channel_request_new (priv->account, request,
836 empathy_get_current_action_time ());
838 /* FIXME: We should probably search in members alias. But this
839 * is enough for IRC */
840 data = g_slice_new (ChatCommandMsgData);
842 data->message = g_strdup (message);
844 tp_account_channel_request_ensure_and_observe_channel_async (req,
845 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
847 g_object_unref (req);
848 g_hash_table_unref (request);
852 chat_command_query (EmpathyChat *chat,
855 /* If <message> part is not defined,
856 * strv[2] will be the terminal NULL */
857 chat_command_msg_internal (chat, strv[1], strv[2]);
861 chat_command_msg (EmpathyChat *chat,
864 chat_command_msg_internal (chat, strv[1], strv[2]);
868 callback_for_request_rename (TpProxy *proxy,
871 GObject *weak_object)
874 DEBUG ("Call to RequestRename method failed: %s",error->message);
879 chat_command_nick (EmpathyChat *chat,
882 EmpathyChatPriv *priv = GET_PRIV (chat);
885 proxy = TP_PROXY (tp_account_get_connection (priv->account));
887 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
888 strv[1], callback_for_request_rename, NULL, NULL, NULL);
892 chat_command_me (EmpathyChat *chat,
895 EmpathyChatPriv *priv = GET_PRIV (chat);
897 TpTextChannel *channel;
899 channel = (TpTextChannel *) (priv->tp_chat);
901 if (!tp_text_channel_supports_message_type (channel,
902 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
903 /* Action message are not supported, 'simulate' the action */
904 EmpathyContact *self_contact;
907 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
908 /* The TpChat can't be ready if it doesn't have the self contact */
909 g_assert (self_contact != NULL);
911 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
913 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
918 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
922 empathy_tp_chat_send (priv->tp_chat, message);
923 g_object_unref (message);
927 chat_command_say (EmpathyChat *chat,
930 EmpathyChatPriv *priv = GET_PRIV (chat);
933 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
935 empathy_tp_chat_send (priv->tp_chat, message);
936 g_object_unref (message);
940 whois_got_contact_cb (TpConnection *connection,
942 TpContact * const *contacts,
943 const gchar * const *requested_ids,
944 GHashTable *failed_id_errors,
947 GObject *weak_object)
949 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
951 g_return_if_fail (n_contacts <= 1);
953 if (n_contacts == 0) {
955 gpointer key = NULL, value = NULL;
959 /* tp-glib guarantees that the contacts you requested will be
960 * in failed_id_errors regardless of whether the individual
961 * contact was invalid or the whole operation failed.
963 g_hash_table_iter_init (&iter, failed_id_errors);
964 g_hash_table_iter_next (&iter, &key, &value);
968 DEBUG ("Error getting TpContact for '%s': %s %u %s",
969 id, g_quark_to_string (id_error->domain),
970 id_error->code, id_error->message);
973 /* The specific ID failed. */
974 gchar *event = g_strdup_printf (
975 _("“%s” is not a valid contact ID"), id);
976 empathy_chat_view_append_event (chat->view, event);
979 /* Otherwise we're disconnected or something; so the window
980 * will already say ‘Disconnected’, so let's not show anything.
983 EmpathyContact *empathy_contact;
986 g_return_if_fail (contacts[0] != NULL);
987 empathy_contact = empathy_contact_dup_from_tp_contact (
990 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
991 /* If we're alive and this command is running, we'd better be
993 g_return_if_fail (window != NULL);
994 g_return_if_fail (gtk_widget_is_toplevel (window));
995 empathy_contact_information_dialog_show (empathy_contact,
996 GTK_WINDOW (window));
997 g_object_unref (empathy_contact);
1002 chat_command_whois (EmpathyChat *chat,
1005 EmpathyChatPriv *priv = GET_PRIV (chat);
1008 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1009 tp_connection_get_contacts_by_id (conn,
1010 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1011 * entered by the user (including spaces, if any). */
1012 1, (const gchar * const *) strv + 1,
1014 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1018 chat_command_whale (EmpathyChat *chat,
1021 EmpathyChatPriv *priv = GET_PRIV (chat);
1024 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1026 "•_______________•");
1027 empathy_tp_chat_send (priv->tp_chat, message);
1028 g_object_unref (message);
1032 chat_command_babywhale (EmpathyChat *chat,
1035 EmpathyChatPriv *priv = GET_PRIV (chat);
1038 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1041 empathy_tp_chat_send (priv->tp_chat, message);
1042 g_object_unref (message);
1046 chat_command_inspector (EmpathyChat *chat,
1049 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1050 empathy_theme_adium_show_inspector (
1051 EMPATHY_THEME_ADIUM (chat->view));
1055 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1057 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1060 const gchar *prefix;
1063 ChatCommandFunc func;
1064 gboolean (*is_supported)(EmpathyChat *chat);
1068 static ChatCommandItem commands[] = {
1069 {"clear", 1, 1, chat_command_clear, NULL,
1070 N_("/clear: clear all messages from the current conversation")},
1072 {"topic", 2, 2, chat_command_topic, NULL,
1073 N_("/topic <topic>: set the topic of the current conversation")},
1075 {"join", 2, 2, chat_command_join, NULL,
1076 N_("/join <chat room ID>: join a new chat room")},
1078 {"j", 2, 2, chat_command_join, NULL,
1079 N_("/j <chat room ID>: join a new chat room")},
1081 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1082 {"part", 1, 3, chat_command_part, part_command_supported,
1083 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1084 "by default the current one")},
1086 {"query", 2, 3, chat_command_query, NULL,
1087 N_("/query <contact ID> [<message>]: open a private chat")},
1089 {"msg", 3, 3, chat_command_msg, NULL,
1090 N_("/msg <contact ID> <message>: open a private chat")},
1092 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1093 N_("/nick <nickname>: change your nickname on the current server")},
1095 {"me", 2, 2, chat_command_me, NULL,
1096 N_("/me <message>: send an ACTION message to the current conversation")},
1098 {"say", 2, 2, chat_command_say, NULL,
1099 N_("/say <message>: send <message> to the current conversation. "
1100 "This is used to send a message starting with a '/'. For example: "
1101 "\"/say /join is used to join a new chat room\"")},
1103 {"whois", 2, 2, chat_command_whois, NULL,
1104 N_("/whois <contact ID>: display information about a contact")},
1106 {"help", 1, 2, chat_command_help, NULL,
1107 N_("/help [<command>]: show all supported commands. "
1108 "If <command> is defined, show its usage.")},
1110 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1112 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1113 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1117 chat_command_show_help (EmpathyChat *chat,
1118 ChatCommandItem *item)
1122 if (item->help == NULL) {
1126 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1127 empathy_chat_view_append_event (chat->view, str);
1132 chat_command_help (EmpathyChat *chat,
1137 /* If <command> part is not defined,
1138 * strv[1] will be the terminal NULL */
1139 if (strv[1] == NULL) {
1140 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1141 if (commands[i].is_supported != NULL) {
1142 if (!commands[i].is_supported (chat)) {
1146 if (commands[i].help == NULL) {
1149 empathy_chat_view_append_event (chat->view,
1150 _(commands[i].help));
1155 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1156 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1157 if (commands[i].is_supported != NULL) {
1158 if (!commands[i].is_supported (chat)) {
1162 if (commands[i].help == NULL) {
1165 chat_command_show_help (chat, &commands[i]);
1170 empathy_chat_view_append_event (chat->view,
1171 _("Unknown command"));
1175 chat_command_parse (const gchar *text, guint max_parts)
1180 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1182 array = g_ptr_array_sized_new (max_parts + 1);
1183 while (max_parts > 1) {
1186 /* Skip white spaces */
1187 while (g_ascii_isspace (*text)) {
1191 /* Search the end of this part, until first space. */
1192 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1198 item = g_strndup (text, end - text);
1199 g_ptr_array_add (array, item);
1200 DEBUG ("\tITEM: \"%s\"", item);
1206 /* Append last part if not empty */
1207 item = g_strstrip (g_strdup (text));
1208 if (!EMP_STR_EMPTY (item)) {
1209 g_ptr_array_add (array, item);
1210 DEBUG ("\tITEM: \"%s\"", item);
1215 /* Make the array NULL-terminated */
1216 g_ptr_array_add (array, NULL);
1218 return (GStrv) g_ptr_array_free (array, FALSE);
1222 has_prefix_case (const gchar *s,
1223 const gchar *prefix)
1225 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1229 chat_send (EmpathyChat *chat,
1232 EmpathyChatPriv *priv;
1236 if (EMP_STR_EMPTY (msg)) {
1240 priv = GET_PRIV (chat);
1242 chat_input_history_add (chat, msg, FALSE);
1244 if (msg[0] == '/') {
1245 gboolean second_slash = FALSE;
1246 const gchar *iter = msg + 1;
1248 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1253 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1256 c = *(msg + 1 + strlen (commands[i].prefix));
1257 if (c != '\0' && !g_ascii_isspace (c)) {
1260 if (commands[i].is_supported != NULL) {
1261 if (!commands[i].is_supported (chat)) {
1266 /* We can't use g_strsplit here because it does
1267 * not deal correctly if we have more than one space
1269 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1271 strv_len = g_strv_length (strv);
1272 if (strv_len < commands[i].min_parts ||
1273 strv_len > commands[i].max_parts) {
1274 chat_command_show_help (chat, &commands[i]);
1279 commands[i].func (chat, strv);
1284 /* Also allow messages with two slashes before the
1285 * first space, so it is possible to send a /unix/path.
1286 * This heuristic is kind of crap. */
1287 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1289 second_slash = TRUE;
1295 if (!second_slash) {
1296 empathy_chat_view_append_event (chat->view,
1297 _("Unknown command; see /help for the available"
1303 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1305 empathy_tp_chat_send (priv->tp_chat, message);
1306 g_object_unref (message);
1310 chat_input_text_view_send (EmpathyChat *chat)
1312 GtkTextBuffer *buffer;
1313 GtkTextIter start, end;
1316 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1318 gtk_text_buffer_get_bounds (buffer, &start, &end);
1319 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1321 /* clear the input field */
1322 gtk_text_buffer_set_text (buffer, "", -1);
1323 /* delete input history modifications */
1324 chat_input_history_revert (chat);
1326 chat_send (chat, msg);
1331 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1332 EmpathyContact *contact,
1333 TpChannelChatState state,
1336 EmpathyChatPriv *priv;
1338 gboolean was_composing;
1340 priv = GET_PRIV (chat);
1342 if (empathy_contact_is_user (contact)) {
1343 /* We don't care about our own chat state */
1347 was_composing = (priv->compositors != NULL);
1349 /* Find the contact in the list. After that l is the list elem or NULL */
1350 for (l = priv->compositors; l; l = l->next) {
1351 if (contact == l->data) {
1357 case TP_CHANNEL_CHAT_STATE_GONE:
1358 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1359 case TP_CHANNEL_CHAT_STATE_PAUSED:
1360 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1361 /* Contact is not composing */
1363 priv->compositors = g_list_remove_link (priv->compositors, l);
1364 g_object_unref (l->data);
1368 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1369 /* Contact is composing */
1371 priv->compositors = g_list_prepend (priv->compositors,
1372 g_object_ref (contact));
1376 g_assert_not_reached ();
1379 DEBUG ("Was composing: %s now composing: %s",
1380 was_composing ? "yes" : "no",
1381 priv->compositors ? "yes" : "no");
1383 if ((was_composing && !priv->compositors) ||
1384 (!was_composing && priv->compositors)) {
1385 /* Composing state changed */
1386 g_signal_emit (chat, signals[COMPOSING], 0,
1387 priv->compositors != NULL);
1392 chat_message_received (EmpathyChat *chat,
1393 EmpathyMessage *message,
1396 EmpathyChatPriv *priv = GET_PRIV (chat);
1397 EmpathyContact *sender;
1399 sender = empathy_message_get_sender (message);
1401 if (empathy_message_is_edit (message)) {
1402 DEBUG ("Editing message '%s' to '%s'",
1403 empathy_message_get_supersedes (message),
1404 empathy_message_get_body (message));
1406 empathy_chat_view_edit_message (chat->view, message);
1408 DEBUG ("Appending new message '%s' from %s (%d)",
1409 empathy_message_get_token (message),
1410 empathy_contact_get_alias (sender),
1411 empathy_contact_get_handle (sender));
1413 empathy_chat_view_append_message (chat->view, message);
1415 if (empathy_message_is_incoming (message)) {
1416 priv->unread_messages++;
1417 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1420 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1423 /* We received a message so the contact is no longer
1425 chat_state_changed_cb (priv->tp_chat, sender,
1426 TP_CHANNEL_CHAT_STATE_ACTIVE,
1431 chat_message_received_cb (EmpathyTpChat *tp_chat,
1432 EmpathyMessage *message,
1435 chat_message_received (chat, message, FALSE);
1439 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1440 EmpathyMessage *message,
1443 EmpathyChatPriv *priv = GET_PRIV (chat);
1445 empathy_chat_view_message_acknowledged (chat->view,
1448 if (!empathy_message_is_edit (message)) {
1449 priv->unread_messages--;
1450 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1455 chat_send_error_cb (EmpathyTpChat *tp_chat,
1456 const gchar *message_body,
1457 TpChannelTextSendError error_code,
1458 const gchar *dbus_error,
1461 const gchar *error = NULL;
1464 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1465 /* translators: error used when user doesn't have enough credit on his
1466 * account to send the message. */
1467 error = _("insufficient balance to send message");
1468 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1469 error = _("not capable");
1472 if (error == NULL) {
1473 /* if we didn't find a dbus-error, try the old error */
1474 switch (error_code) {
1475 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1476 error = _("offline");
1478 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1479 error = _("invalid contact");
1481 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1482 error = _("permission denied");
1484 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1485 error = _("too long message");
1487 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1488 error = _("not implemented");
1490 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1492 error = _("unknown");
1497 if (message_body != NULL) {
1498 str = g_strdup_printf (_("Error sending message '%s': %s"),
1499 message_body, error);
1502 str = g_strdup_printf (_("Error sending message: %s"), error);
1505 empathy_chat_view_append_event (chat->view, str);
1510 chat_topic_label_size_allocate_cb (GtkLabel *label,
1511 GtkAllocation *allocation,
1514 EmpathyChatPriv *priv = GET_PRIV (chat);
1516 if (!gtk_label_get_line_wrap (label)) {
1517 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1518 gtk_widget_show (priv->expander_topic);
1520 gtk_widget_hide (priv->expander_topic);
1527 chat_topic_expander_activate_cb (GtkExpander *expander,
1528 GParamSpec *param_spec,
1531 EmpathyChatPriv *priv = GET_PRIV (chat);
1533 if (gtk_expander_get_expanded (expander)) {
1534 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1535 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1537 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1538 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1543 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1548 EmpathyChatPriv *priv = GET_PRIV (chat);
1550 if (!tp_strdiff (name, "subject")) {
1551 g_free (priv->subject);
1552 priv->subject = g_value_dup_string (value);
1553 g_object_notify (G_OBJECT (chat), "subject");
1555 if (EMP_STR_EMPTY (priv->subject)) {
1556 gtk_widget_hide (priv->hbox_topic);
1558 gchar *markup_topic;
1561 markup_topic = empathy_add_link_markup (priv->subject);
1562 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1563 _("Topic:"), markup_topic);
1565 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1566 g_free (markup_text);
1567 g_free (markup_topic);
1569 gtk_widget_show (priv->hbox_topic);
1571 if (priv->block_events_timeout_id == 0) {
1574 if (!EMP_STR_EMPTY (priv->subject)) {
1575 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1577 str = g_strdup (_("No topic defined"));
1579 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1583 else if (!tp_strdiff (name, "name")) {
1584 g_free (priv->name);
1585 priv->name = g_value_dup_string (value);
1586 g_object_notify (G_OBJECT (chat), "name");
1591 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1595 GtkTextIter word_start = *iter;
1596 GtkTextIter word_end = *iter;
1599 if (gtk_text_iter_inside_word (&word_end) &&
1600 !gtk_text_iter_ends_word (&word_end)) {
1601 gtk_text_iter_forward_word_end (&word_end);
1606 if (gtk_text_iter_get_char (&tmp) == '\'') {
1607 gtk_text_iter_forward_char (&tmp);
1609 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1610 gtk_text_iter_forward_word_end (&word_end);
1615 if (gtk_text_iter_inside_word (&word_start) ||
1616 gtk_text_iter_ends_word (&word_start)) {
1617 if (!gtk_text_iter_starts_word (&word_start) ||
1618 gtk_text_iter_equal (&word_start, &word_end)) {
1619 gtk_text_iter_backward_word_start (&word_start);
1623 gtk_text_iter_backward_char (&tmp);
1625 if (gtk_text_iter_get_char (&tmp) == '\'') {
1626 gtk_text_iter_backward_char (&tmp);
1628 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1629 gtk_text_iter_backward_word_start (&word_start);
1634 *start = word_start;
1640 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1641 GtkTextIter *location,
1646 GtkTextIter iter, pos;
1648 /* Remove all misspelled tags in the inserted text.
1649 * This happens when text is inserted within a misspelled word. */
1650 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1651 gtk_text_iter_get_offset (location) - len);
1652 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1655 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1658 GtkTextIter start, end;
1661 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1664 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1666 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1667 gtk_text_iter_equal (&pos, &end) ||
1668 empathy_spell_check (str)) {
1669 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1671 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1676 } while (gtk_text_iter_forward_word_end (&iter) &&
1677 gtk_text_iter_compare (&iter, location) <= 0);
1681 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1686 GtkTextIter word_start, word_end;
1688 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1689 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1690 &word_start, &word_end);
1695 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1698 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1699 chat_composing_stop (chat);
1701 chat_composing_start (chat);
1706 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1711 GtkTextIter prev_pos;
1712 GtkTextIter word_start;
1713 GtkTextIter word_end;
1717 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1719 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1720 gtk_text_buffer_get_insert (buffer));
1721 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1723 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1726 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1727 !gtk_text_iter_equal (&pos, &word_end)) {
1728 str = gtk_text_buffer_get_text (buffer,
1729 &word_start, &word_end, FALSE);
1731 if (!empathy_spell_check (str)) {
1732 gtk_text_buffer_apply_tag_by_name (buffer,
1733 "misspelled", &word_start, &word_end);
1735 gtk_text_buffer_remove_tag_by_name (buffer,
1736 "misspelled", &word_start, &word_end);
1743 gtk_text_buffer_move_mark (buffer, mark, &pos);
1747 empathy_isspace_cb (gunichar c,
1750 return g_unichar_isspace (c);
1754 chat_input_key_press_event_cb (GtkWidget *widget,
1758 EmpathyChatPriv *priv;
1761 GtkWidget *text_view_sw;
1763 priv = GET_PRIV (chat);
1765 priv->most_recent_event_type = event->type;
1767 /* Catch ctrl+up/down so we can traverse messages we sent */
1768 if ((event->state & GDK_CONTROL_MASK) &&
1769 (event->keyval == GDK_KEY_Up ||
1770 event->keyval == GDK_KEY_Down)) {
1771 GtkTextBuffer *buffer;
1774 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1775 chat_input_history_update (chat, buffer);
1777 if (event->keyval == GDK_KEY_Up) {
1778 str = chat_input_history_get_next (chat);
1780 str = chat_input_history_get_prev (chat);
1783 g_signal_handlers_block_by_func (buffer,
1784 chat_input_text_buffer_changed_cb,
1786 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1787 g_signal_handlers_unblock_by_func (buffer,
1788 chat_input_text_buffer_changed_cb,
1794 /* Catch enter but not ctrl/shift-enter */
1795 if (IS_ENTER (event->keyval) &&
1796 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1799 /* This is to make sure that kinput2 gets the enter. And if
1800 * it's handled there we shouldn't send on it. This is because
1801 * kinput2 uses Enter to commit letters. See:
1802 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1805 view = GTK_TEXT_VIEW (chat->input_text_view);
1806 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1807 gtk_text_view_reset_im_context (view);
1811 chat_input_text_view_send (chat);
1815 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1817 if (IS_ENTER (event->keyval) &&
1818 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1819 /* Newline for shift/control-enter. */
1822 if (!(event->state & GDK_CONTROL_MASK) &&
1823 event->keyval == GDK_KEY_Page_Up) {
1824 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1825 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1828 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1829 event->keyval == GDK_KEY_Page_Down) {
1830 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1831 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1832 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1833 gtk_adjustment_set_value (adj, val);
1836 if (event->keyval == GDK_KEY_Escape) {
1837 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1839 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1840 event->keyval == GDK_KEY_Tab) {
1841 GtkTextBuffer *buffer;
1842 GtkTextIter start, current;
1843 gchar *nick, *completed;
1844 GList *list, *completed_list;
1845 gboolean is_start_of_buffer;
1847 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1848 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1850 /* Get the start of the nick to complete. */
1851 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1852 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1853 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1855 is_start_of_buffer = gtk_text_iter_is_start (&start);
1857 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1858 g_completion_add_items (priv->completion, list);
1860 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1861 completed_list = g_completion_complete (priv->completion,
1870 GString *message = NULL;
1873 gtk_text_buffer_delete (buffer, &start, ¤t);
1875 len = g_list_length (completed_list);
1878 /* If we only have one hit, use that text
1879 * instead of the text in completed since the
1880 * completed text will use the typed string
1881 * which might be cased all wrong.
1884 text = empathy_contact_get_alias (completed_list->data);
1888 /* Print all hits to the scrollback view, so the
1889 * user knows what possibilities he has.
1892 message = g_string_new ("");
1893 for (l = completed_list; l != NULL; l = l->next) {
1894 g_string_append (message, empathy_contact_get_alias (l->data));
1895 g_string_append (message, " - ");
1897 empathy_chat_view_append_event (chat->view, message->str);
1898 g_string_free (message, TRUE);
1901 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1903 if (len == 1 && is_start_of_buffer) {
1904 gchar *complete_char;
1906 complete_char = g_settings_get_string (
1907 priv->gsettings_chat,
1908 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1910 if (complete_char != NULL) {
1911 gtk_text_buffer_insert_at_cursor (buffer,
1913 strlen (complete_char));
1914 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1915 g_free (complete_char);
1922 g_completion_clear_items (priv->completion);
1924 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1934 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1938 gtk_widget_grab_focus (chat->input_text_view);
1944 chat_input_realize_cb (GtkWidget *widget,
1947 DEBUG ("Setting focus to the input text view");
1948 if (gtk_widget_is_sensitive (widget)) {
1949 gtk_widget_grab_focus (widget);
1954 chat_input_has_focus_notify_cb (GtkWidget *widget,
1958 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1962 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1963 EmpathySmiley *smiley,
1966 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1967 GtkTextBuffer *buffer;
1970 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1972 gtk_text_buffer_get_end_iter (buffer, &iter);
1973 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1975 gtk_text_buffer_get_end_iter (buffer, &iter);
1976 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1987 static EmpathyChatSpell *
1988 chat_spell_new (EmpathyChat *chat,
1993 EmpathyChatSpell *chat_spell;
1995 chat_spell = g_slice_new0 (EmpathyChatSpell);
1997 chat_spell->chat = g_object_ref (chat);
1998 chat_spell->word = g_strdup (word);
1999 chat_spell->start = start;
2000 chat_spell->end = end;
2006 chat_spell_free (EmpathyChatSpell *chat_spell)
2008 g_object_unref (chat_spell->chat);
2009 g_free (chat_spell->word);
2010 g_slice_free (EmpathyChatSpell, chat_spell);
2014 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2015 EmpathyChatSpell *chat_spell)
2017 empathy_chat_correct_word (chat_spell->chat,
2018 &(chat_spell->start),
2020 gtk_menu_item_get_label (menu_item));
2025 chat_spelling_build_suggestions_menu (const gchar *code,
2026 EmpathyChatSpell *chat_spell)
2028 GList *suggestions, *l;
2029 GtkWidget *menu, *menu_item;
2031 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2032 if (suggestions == NULL)
2035 menu = gtk_menu_new ();
2036 for (l = suggestions; l; l = l->next) {
2037 menu_item = gtk_menu_item_new_with_label (l->data);
2038 g_signal_connect (G_OBJECT (menu_item), "activate",
2039 G_CALLBACK (chat_spelling_menu_activate_cb),
2041 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2043 empathy_spell_free_suggestions (suggestions);
2045 gtk_widget_show_all (menu);
2051 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2053 GtkWidget *menu, *submenu, *item;
2056 codes = empathy_spell_get_enabled_language_codes ();
2057 g_assert (codes != NULL);
2059 if (g_list_length (codes) > 1) {
2060 menu = gtk_menu_new ();
2062 for (l = codes; l; l = l->next) {
2063 const gchar *code, *name;
2066 name = empathy_spell_get_language_name (code);
2070 item = gtk_image_menu_item_new_with_label (name);
2072 submenu = chat_spelling_build_suggestions_menu (
2074 if (submenu == NULL)
2075 gtk_widget_set_sensitive (item, FALSE);
2077 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2079 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2082 menu = chat_spelling_build_suggestions_menu (codes->data,
2085 menu = gtk_menu_new ();
2086 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2087 gtk_widget_set_sensitive (item, FALSE);
2088 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2091 g_list_free (codes);
2093 gtk_widget_show_all (menu);
2104 static EmpathyChatWord *
2105 chat_word_new (EmpathyChat *chat,
2109 EmpathyChatWord *chat_word;
2111 chat_word = g_slice_new0 (EmpathyChatWord);
2113 chat_word->chat = g_object_ref (chat);
2114 chat_word->word = g_strdup (word);
2115 chat_word->code = g_strdup (code);
2121 chat_word_free (EmpathyChatWord *chat_word)
2123 g_object_unref (chat_word->chat);
2124 g_free (chat_word->word);
2125 g_free (chat_word->code);
2126 g_slice_free (EmpathyChatWord, chat_word);
2130 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2131 EmpathyChatWord *chat_word)
2133 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2135 empathy_spell_add_to_dictionary (chat_word->code,
2137 priv->update_misspelled_words_id =
2138 g_idle_add (update_misspelled_words, chat_word->chat);
2142 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2144 GtkWidget *menu, *item, *lang_item, *image;
2147 const gchar *code, *name;
2148 EmpathyChatWord *chat_word;
2150 codes = empathy_spell_get_enabled_language_codes ();
2151 g_assert (codes != NULL);
2152 if (g_list_length (codes) > 1) {
2153 /* translators: %s is the selected word */
2154 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2156 item = gtk_image_menu_item_new_with_mnemonic (label);
2158 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2159 GTK_ICON_SIZE_MENU);
2160 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2163 menu = gtk_menu_new ();
2165 for (l = codes; l; l = l->next) {
2167 name = empathy_spell_get_language_name (code);
2171 lang_item = gtk_image_menu_item_new_with_label (name);
2173 chat_word= chat_word_new (chat_spell->chat,
2174 chat_spell->word, code);
2175 g_object_set_data_full (G_OBJECT (lang_item),
2176 "chat-word", chat_word,
2177 (GDestroyNotify) chat_word_free);
2179 g_signal_connect (G_OBJECT (lang_item), "activate",
2180 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2182 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2184 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2187 name = empathy_spell_get_language_name (code);
2188 g_assert (name != NULL);
2189 /* translators: first %s is the selected word,
2190 * second %s is the language name of the target dictionary */
2191 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2192 chat_spell->word, name);
2193 item = gtk_image_menu_item_new_with_mnemonic (label);
2195 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2196 GTK_ICON_SIZE_MENU);
2197 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2199 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2201 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2202 (GDestroyNotify) chat_word_free);
2204 g_signal_connect (G_OBJECT (item), "activate",
2205 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2208 g_list_free (codes);
2210 gtk_widget_show_all (item);
2216 chat_text_send_cb (GtkMenuItem *menuitem,
2219 chat_input_text_view_send (chat);
2223 chat_input_button_press_event_cb (GtkTextView *view,
2224 GdkEventButton *event,
2227 EmpathyChatPriv *priv = GET_PRIV (chat);
2229 priv->most_recent_event_type = event->type;
2235 chat_input_populate_popup_cb (GtkTextView *view,
2239 EmpathyChatPriv *priv = GET_PRIV (chat);
2240 GtkTextBuffer *buffer;
2241 GtkTextTagTable *table;
2244 GtkTextIter iter, start, end;
2247 EmpathyChatSpell *chat_spell;
2248 GtkWidget *spell_menu;
2249 GtkWidget *spell_item;
2250 EmpathySmileyManager *smiley_manager;
2251 GtkWidget *smiley_menu;
2254 buffer = gtk_text_view_get_buffer (view);
2256 /* Add the emoticon menu. */
2257 item = gtk_separator_menu_item_new ();
2258 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2259 gtk_widget_show (item);
2261 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2262 image = gtk_image_new_from_icon_name ("face-smile",
2263 GTK_ICON_SIZE_MENU);
2264 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2265 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2266 gtk_widget_show (item);
2268 smiley_manager = empathy_smiley_manager_dup_singleton ();
2269 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2270 chat_insert_smiley_activate_cb,
2272 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2273 g_object_unref (smiley_manager);
2275 /* Add the Send menu item. */
2276 gtk_text_buffer_get_bounds (buffer, &start, &end);
2277 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2278 if (!EMP_STR_EMPTY (str)) {
2279 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2280 g_signal_connect (G_OBJECT (item), "activate",
2281 G_CALLBACK (chat_text_send_cb), chat);
2282 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2283 gtk_widget_show (item);
2287 /* Add the spell check menu item. */
2288 table = gtk_text_buffer_get_tag_table (buffer);
2289 tag = gtk_text_tag_table_lookup (table, "misspelled");
2291 switch (priv->most_recent_event_type) {
2292 case GDK_BUTTON_PRESS:
2293 /* get the location from the pointer */
2294 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2295 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2296 GTK_TEXT_WINDOW_WIDGET,
2299 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2304 g_warn_if_reached ();
2305 /* assume the KEY_PRESS case */
2308 /* get the location from the cursor */
2309 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2310 gtk_text_buffer_get_insert (buffer));
2316 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2317 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2319 str = gtk_text_buffer_get_text (buffer,
2320 &start, &end, FALSE);
2322 if (!EMP_STR_EMPTY (str)) {
2323 chat_spell = chat_spell_new (chat, str, start, end);
2324 g_object_set_data_full (G_OBJECT (menu),
2325 "chat-spell", chat_spell,
2326 (GDestroyNotify) chat_spell_free);
2328 item = gtk_separator_menu_item_new ();
2329 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2330 gtk_widget_show (item);
2332 /* Spelling suggestions */
2333 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2334 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2335 GTK_ICON_SIZE_MENU);
2336 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2338 spell_menu = chat_spelling_build_menu (chat_spell);
2339 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2342 spell_item = gtk_separator_menu_item_new ();
2343 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2344 gtk_widget_show (spell_item);
2346 /* Add to dictionary */
2347 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2349 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2350 gtk_widget_show (spell_item);
2352 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2353 gtk_widget_show (item);
2359 chat_log_filter (TplEvent *event,
2362 EmpathyChat *chat = user_data;
2363 EmpathyMessage *message;
2364 EmpathyChatPriv *priv = GET_PRIV (chat);
2365 const GList *pending;
2367 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2368 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2370 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2371 message = empathy_message_from_tpl_log_event (event);
2373 for (; pending; pending = g_list_next (pending)) {
2374 if (empathy_message_equal (message, pending->data)) {
2375 g_object_unref (message);
2380 g_object_unref (message);
2386 show_pending_messages (EmpathyChat *chat) {
2387 EmpathyChatPriv *priv = GET_PRIV (chat);
2388 const GList *messages, *l;
2390 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2392 if (chat->view == NULL || priv->tp_chat == NULL)
2395 if (!priv->can_show_pending)
2398 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2400 for (l = messages; l != NULL ; l = g_list_next (l)) {
2401 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2402 chat_message_received (chat, message, TRUE);
2408 got_filtered_messages_cb (GObject *manager,
2409 GAsyncResult *result,
2414 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2415 EmpathyChatPriv *priv = GET_PRIV (chat);
2416 GError *error = NULL;
2418 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2419 result, &messages, &error)) {
2420 DEBUG ("%s. Aborting.", error->message);
2421 empathy_chat_view_append_event (chat->view,
2422 _("Failed to retrieve recent logs"));
2423 g_error_free (error);
2427 for (l = messages; l; l = g_list_next (l)) {
2428 EmpathyMessage *message;
2430 g_assert (TPL_IS_EVENT (l->data));
2432 message = empathy_message_from_tpl_log_event (l->data);
2433 g_object_unref (l->data);
2435 if (empathy_message_is_edit (message)) {
2436 /* this is an edited message, create a synthetic event
2437 * using the supersedes token and
2438 * original-message-sent timestamp, that we can then
2440 EmpathyMessage *syn_msg = g_object_new (
2441 EMPATHY_TYPE_MESSAGE,
2443 "token", empathy_message_get_supersedes (message),
2444 "type", empathy_message_get_tptype (message),
2445 "timestamp", empathy_message_get_original_timestamp (message),
2446 "incoming", empathy_message_is_incoming (message),
2448 "receiver", empathy_message_get_receiver (message),
2449 "sender", empathy_message_get_sender (message),
2452 empathy_chat_view_append_message (chat->view, syn_msg);
2453 empathy_chat_view_edit_message (chat->view, message);
2455 g_object_unref (syn_msg);
2457 /* append the latest message */
2458 empathy_chat_view_append_message (chat->view, message);
2461 g_object_unref (message);
2463 g_list_free (messages);
2466 /* in case of TPL error, skip backlog and show pending messages */
2467 priv->can_show_pending = TRUE;
2468 show_pending_messages (chat);
2470 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2471 * about it in EmpathyChatPriv definition */
2472 priv->retrieving_backlogs = FALSE;
2473 empathy_chat_messages_read (chat);
2475 /* Turn back on scrolling */
2476 empathy_chat_view_scroll (chat->view, TRUE);
2480 chat_add_logs (EmpathyChat *chat)
2482 EmpathyChatPriv *priv = GET_PRIV (chat);
2489 /* Turn off scrolling temporarily */
2490 empathy_chat_view_scroll (chat->view, FALSE);
2492 /* Add messages from last conversation */
2493 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2494 target = tpl_entity_new_from_room_id (priv->id);
2496 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2498 priv->retrieving_backlogs = TRUE;
2499 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2502 TPL_EVENT_MASK_TEXT,
2506 got_filtered_messages_cb,
2509 g_object_unref (target);
2513 chat_contacts_completion_func (const gchar *s1,
2517 gchar *tmp, *nick1, *nick2;
2524 return s1 ? -1 : +1;
2527 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2528 nick1 = g_utf8_casefold (tmp, -1);
2531 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2532 nick2 = g_utf8_casefold (tmp, -1);
2535 ret = strncmp (nick1, nick2, n);
2544 build_part_message (guint reason,
2546 EmpathyContact *actor,
2547 const gchar *message)
2549 GString *s = g_string_new ("");
2550 const gchar *actor_name = NULL;
2552 if (actor != NULL) {
2553 actor_name = empathy_contact_get_alias (actor);
2556 /* Having an actor only really makes sense for a few actions... */
2558 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2559 g_string_append_printf (s, _("%s has disconnected"), name);
2561 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2562 if (actor_name != NULL) {
2563 /* translators: reverse the order of these arguments
2564 * if the kicked should come before the kicker in your locale.
2566 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2569 g_string_append_printf (s, _("%s was kicked"), name);
2572 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2573 if (actor_name != NULL) {
2574 /* translators: reverse the order of these arguments
2575 * if the banned should come before the banner in your locale.
2577 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2580 g_string_append_printf (s, _("%s was banned"), name);
2584 g_string_append_printf (s, _("%s has left the room"), name);
2587 if (!EMP_STR_EMPTY (message)) {
2588 /* Note to translators: this string is appended to
2589 * notifications like "foo has left the room", with the message
2590 * given by the user living the room. If this poses a problem,
2591 * please let us know. :-)
2593 g_string_append_printf (s, _(" (%s)"), message);
2596 return g_string_free (s, FALSE);
2600 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2601 EmpathyContact *contact,
2602 EmpathyContact *actor,
2608 EmpathyChatPriv *priv = GET_PRIV (chat);
2609 const gchar *name = empathy_contact_get_alias (contact);
2612 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2614 if (priv->block_events_timeout_id != 0)
2618 str = g_strdup_printf (_("%s has joined the room"),
2621 str = build_part_message (reason, name, actor, message);
2624 empathy_chat_view_append_event (chat->view, str);
2629 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2630 EmpathyContact *old_contact,
2631 EmpathyContact *new_contact,
2636 EmpathyChatPriv *priv = GET_PRIV (chat);
2638 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2640 if (priv->block_events_timeout_id == 0) {
2643 str = g_strdup_printf (_("%s is now known as %s"),
2644 empathy_contact_get_alias (old_contact),
2645 empathy_contact_get_alias (new_contact));
2646 empathy_chat_view_append_event (chat->view, str);
2653 chat_contacts_visible_timeout_cb (gpointer chat)
2655 EmpathyChatPriv *priv = GET_PRIV (chat);
2657 /* Relax the size request */
2658 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2660 /* Set the position of the slider. This must be done here because
2661 * GtkPaned need to know its size allocation and it will be settled only
2662 * after the gtk_window_resize () tough effect. */
2663 if (priv->contacts_width > 0) {
2664 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2665 priv->contacts_width);
2668 priv->contacts_visible_id = 0;
2674 chat_update_contacts_visibility (EmpathyChat *chat,
2677 EmpathyChatPriv *priv = GET_PRIV (chat);
2679 if (!priv->scrolled_window_contacts) {
2683 if (priv->remote_contact != NULL) {
2687 if (show && priv->contact_list_view == NULL) {
2688 EmpathyContactListStore *store;
2690 GtkAllocation allocation;
2692 /* We are adding the contact list to the chat, we don't want the
2693 * chat view to become too small. If the chat view is already
2694 * smaller than 250 make sure that size won't change. If the
2695 * chat view is bigger the contact list will take some space on
2696 * it but we make sure the chat view don't become smaller than
2697 * 250. Relax the size request once the resize is done */
2698 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2699 min_width = MIN (allocation.width, 250);
2700 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2702 /* There is no way to know when the window resize will happen
2703 * since it is WM's decision. Let's hope it won't be longer. */
2704 if (priv->contacts_visible_id != 0)
2705 g_source_remove (priv->contacts_visible_id);
2706 priv->contacts_visible_id = g_timeout_add (500,
2707 chat_contacts_visible_timeout_cb, chat);
2709 store = empathy_contact_list_store_new (
2710 EMPATHY_CONTACT_LIST (priv->tp_chat));
2711 empathy_contact_list_store_set_show_groups (
2712 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2714 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2715 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2716 EMPATHY_CONTACT_FEATURE_CHAT |
2717 EMPATHY_CONTACT_FEATURE_CALL |
2718 EMPATHY_CONTACT_FEATURE_LOG |
2719 EMPATHY_CONTACT_FEATURE_INFO));
2720 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2721 priv->contact_list_view);
2722 gtk_widget_show (priv->contact_list_view);
2723 gtk_widget_show (priv->scrolled_window_contacts);
2724 g_object_unref (store);
2726 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2727 gtk_widget_hide (priv->scrolled_window_contacts);
2728 if (priv->contact_list_view != NULL) {
2729 gtk_widget_destroy (priv->contact_list_view);
2730 priv->contact_list_view = NULL;
2736 empathy_chat_set_show_contacts (EmpathyChat *chat,
2739 EmpathyChatPriv *priv = GET_PRIV (chat);
2741 priv->show_contacts = show;
2743 chat_update_contacts_visibility (chat, show);
2745 g_object_notify (G_OBJECT (chat), "show-contacts");
2749 chat_remote_contact_changed_cb (EmpathyChat *chat)
2751 EmpathyChatPriv *priv = GET_PRIV (chat);
2753 if (priv->remote_contact != NULL) {
2754 g_object_unref (priv->remote_contact);
2755 priv->remote_contact = NULL;
2760 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2761 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2762 if (priv->remote_contact != NULL) {
2763 g_object_ref (priv->remote_contact);
2764 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2766 else if (priv->tp_chat != NULL) {
2767 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2770 chat_update_contacts_visibility (chat, priv->show_contacts);
2772 g_object_notify (G_OBJECT (chat), "remote-contact");
2773 g_object_notify (G_OBJECT (chat), "id");
2777 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2783 EmpathyChatPriv *priv;
2785 priv = GET_PRIV (chat);
2787 if (!priv->tp_chat) {
2791 chat_composing_remove_timeout (chat);
2792 g_object_unref (priv->tp_chat);
2793 priv->tp_chat = NULL;
2794 g_object_notify (G_OBJECT (chat), "tp-chat");
2796 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2797 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2799 chat_update_contacts_visibility (chat, FALSE);
2801 priv->unread_messages_when_offline = priv->unread_messages;
2805 update_misspelled_words (gpointer data)
2807 EmpathyChat *chat = EMPATHY_CHAT (data);
2808 EmpathyChatPriv *priv = GET_PRIV (chat);
2809 GtkTextBuffer *buffer;
2813 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2815 gtk_text_buffer_get_end_iter (buffer, &iter);
2816 length = gtk_text_iter_get_offset (&iter);
2817 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2818 NULL, length, chat);
2820 priv->update_misspelled_words_id = 0;
2826 conf_spell_checking_cb (GSettings *gsettings_chat,
2830 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2831 EmpathyChatPriv *priv = GET_PRIV (chat);
2832 gboolean spell_checker;
2833 GtkTextBuffer *buffer;
2835 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2838 spell_checker = g_settings_get_boolean (gsettings_chat,
2839 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2841 if (!empathy_spell_supported ()) {
2842 spell_checker = FALSE;
2845 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2847 if (spell_checker == priv->spell_checking_enabled) {
2848 if (spell_checker) {
2849 /* Possibly changed dictionaries,
2850 * update misspelled words. Need to do so in idle
2851 * so the spell checker is updated. */
2852 priv->update_misspelled_words_id =
2853 g_idle_add (update_misspelled_words, chat);
2859 if (spell_checker) {
2862 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2863 buffer, "notify::cursor-position",
2864 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2866 priv->insert_text_id = tp_g_signal_connect_object (
2867 buffer, "insert-text",
2868 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2869 chat, G_CONNECT_AFTER);
2870 priv->delete_range_id = tp_g_signal_connect_object (
2871 buffer, "delete-range",
2872 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2873 chat, G_CONNECT_AFTER);
2875 gtk_text_buffer_create_tag (buffer, "misspelled",
2876 "underline", PANGO_UNDERLINE_ERROR,
2879 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2880 gtk_text_buffer_get_insert (buffer));
2881 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2884 /* Mark misspelled words in the existing buffer.
2885 * Need to do so in idle so the spell checker is updated. */
2886 priv->update_misspelled_words_id =
2887 g_idle_add (update_misspelled_words, chat);
2889 GtkTextTagTable *table;
2892 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2893 priv->notify_cursor_position_id = 0;
2894 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2895 priv->insert_text_id = 0;
2896 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2897 priv->delete_range_id = 0;
2899 table = gtk_text_buffer_get_tag_table (buffer);
2900 tag = gtk_text_tag_table_lookup (table, "misspelled");
2901 gtk_text_tag_table_remove (table, tag);
2903 gtk_text_buffer_delete_mark_by_name (buffer,
2904 "previous-cursor-position");
2907 priv->spell_checking_enabled = spell_checker;
2911 save_paned_pos_timeout (gpointer data)
2913 EmpathyChat *self = data;
2916 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2918 g_settings_set_int (self->priv->gsettings_ui,
2919 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2926 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2930 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2932 if (chat->priv->save_paned_pos_id != 0)
2933 g_source_remove (chat->priv->save_paned_pos_id);
2935 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2936 save_paned_pos_timeout, chat);
2942 chat_create_ui (EmpathyChat *chat)
2944 EmpathyChatPriv *priv = GET_PRIV (chat);
2948 GtkTextBuffer *buffer;
2949 EmpathyThemeManager *theme_mgr;
2951 filename = empathy_file_lookup ("empathy-chat.ui",
2953 gui = empathy_builder_get_file (filename,
2954 "chat_widget", &priv->widget,
2955 "hpaned", &priv->hpaned,
2956 "vbox_left", &priv->vbox_left,
2957 "scrolled_window_chat", &priv->scrolled_window_chat,
2958 "scrolled_window_input", &priv->scrolled_window_input,
2959 "hbox_topic", &priv->hbox_topic,
2960 "expander_topic", &priv->expander_topic,
2961 "label_topic", &priv->label_topic,
2962 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2963 "info_bar_vbox", &priv->info_bar_vbox,
2966 empathy_builder_connect (gui, chat,
2967 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2968 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2973 /* Add message view. */
2974 theme_mgr = empathy_theme_manager_dup_singleton ();
2975 chat->view = empathy_theme_manager_create_view (theme_mgr);
2976 g_object_unref (theme_mgr);
2977 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2978 and other types, even though it's non-editable and doesn't accept any
2979 drags. This steals drag motion for anything inside the scrollbars,
2980 making drag destinations on chat windows far less useful.
2982 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2983 g_signal_connect (chat->view, "focus_in_event",
2984 G_CALLBACK (chat_text_view_focus_in_event_cb),
2986 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2987 GTK_WIDGET (chat->view));
2988 gtk_widget_show (GTK_WIDGET (chat->view));
2990 /* Add input GtkTextView */
2991 chat->input_text_view = empathy_input_text_view_new ();
2992 g_signal_connect (chat->input_text_view, "notify::has-focus",
2993 G_CALLBACK (chat_input_has_focus_notify_cb),
2995 g_signal_connect (chat->input_text_view, "key-press-event",
2996 G_CALLBACK (chat_input_key_press_event_cb),
2998 g_signal_connect (chat->input_text_view, "realize",
2999 G_CALLBACK (chat_input_realize_cb),
3001 g_signal_connect (chat->input_text_view, "button-press-event",
3002 G_CALLBACK (chat_input_button_press_event_cb),
3004 g_signal_connect (chat->input_text_view, "populate-popup",
3005 G_CALLBACK (chat_input_populate_popup_cb),
3007 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3008 tp_g_signal_connect_object (buffer, "changed",
3009 G_CALLBACK (chat_input_text_buffer_changed_cb),
3011 tp_g_signal_connect_object (priv->gsettings_chat,
3012 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3013 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3014 conf_spell_checking_cb (priv->gsettings_chat,
3015 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3016 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3017 chat->input_text_view);
3018 gtk_widget_show (chat->input_text_view);
3020 /* Add the (invisible) search bar */
3021 priv->search_bar = empathy_search_bar_new (chat->view);
3022 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3025 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3027 /* Initialy hide the topic, will be shown if not empty */
3028 gtk_widget_hide (priv->hbox_topic);
3030 g_signal_connect (priv->hpaned, "notify::position",
3031 G_CALLBACK (chat_hpaned_pos_changed_cb),
3034 /* Set widget focus order */
3035 list = g_list_append (NULL, priv->search_bar);
3036 list = g_list_append (list, priv->scrolled_window_input);
3037 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3040 list = g_list_append (NULL, priv->vbox_left);
3041 list = g_list_append (list, priv->scrolled_window_contacts);
3042 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3045 list = g_list_append (NULL, priv->hpaned);
3046 list = g_list_append (list, priv->hbox_topic);
3047 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3050 /* Add the main widget in the chat widget */
3051 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3052 g_object_unref (gui);
3056 chat_finalize (GObject *object)
3059 EmpathyChatPriv *priv;
3061 chat = EMPATHY_CHAT (object);
3062 priv = GET_PRIV (chat);
3064 DEBUG ("Finalized: %p", object);
3066 if (priv->update_misspelled_words_id != 0)
3067 g_source_remove (priv->update_misspelled_words_id);
3069 if (priv->save_paned_pos_id != 0)
3070 g_source_remove (priv->save_paned_pos_id);
3072 if (priv->contacts_visible_id != 0)
3073 g_source_remove (priv->contacts_visible_id);
3075 g_object_unref (priv->gsettings_chat);
3076 g_object_unref (priv->gsettings_ui);
3078 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3079 g_list_free (priv->input_history);
3081 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3082 g_list_free (priv->compositors);
3084 chat_composing_remove_timeout (chat);
3086 g_object_unref (priv->account_manager);
3087 g_object_unref (priv->log_manager);
3089 if (priv->tp_chat) {
3090 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3091 chat_invalidated_cb, chat);
3092 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3093 chat_message_received_cb, chat);
3094 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3095 chat_message_acknowledged_cb, chat);
3096 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3097 chat_send_error_cb, chat);
3098 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3099 chat_state_changed_cb, chat);
3100 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3101 chat_property_changed_cb, chat);
3102 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3103 chat_members_changed_cb, chat);
3104 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3105 chat_remote_contact_changed_cb, chat);
3106 empathy_tp_chat_leave (priv->tp_chat, "");
3107 g_object_unref (priv->tp_chat);
3109 if (priv->account) {
3110 g_object_unref (priv->account);
3112 if (priv->remote_contact) {
3113 g_object_unref (priv->remote_contact);
3116 if (priv->block_events_timeout_id) {
3117 g_source_remove (priv->block_events_timeout_id);
3121 g_free (priv->name);
3122 g_free (priv->subject);
3123 g_completion_free (priv->completion);
3125 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3129 chat_constructed (GObject *object)
3131 EmpathyChat *chat = EMPATHY_CHAT (object);
3132 EmpathyChatPriv *priv = GET_PRIV (chat);
3134 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3135 /* First display logs from the logger and then display pending messages */
3136 chat_add_logs (chat);
3139 /* Just display pending messages for rooms */
3140 priv->can_show_pending = TRUE;
3141 show_pending_messages (chat);
3146 empathy_chat_class_init (EmpathyChatClass *klass)
3148 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3150 object_class->finalize = chat_finalize;
3151 object_class->get_property = chat_get_property;
3152 object_class->set_property = chat_set_property;
3153 object_class->constructed = chat_constructed;
3155 g_object_class_install_property (object_class,
3157 g_param_spec_object ("tp-chat",
3159 "The tp chat object",
3160 EMPATHY_TYPE_TP_CHAT,
3163 G_PARAM_STATIC_STRINGS));
3164 g_object_class_install_property (object_class,
3166 g_param_spec_object ("account",
3167 "Account of the chat",
3168 "The account of the chat",
3171 G_PARAM_STATIC_STRINGS));
3172 g_object_class_install_property (object_class,
3174 g_param_spec_string ("id",
3176 "The id of the chat",
3179 G_PARAM_STATIC_STRINGS));
3180 g_object_class_install_property (object_class,
3182 g_param_spec_string ("name",
3184 "The name of the chat",
3187 G_PARAM_STATIC_STRINGS));
3188 g_object_class_install_property (object_class,
3190 g_param_spec_string ("subject",
3192 "The subject or topic of the chat",
3195 G_PARAM_STATIC_STRINGS));
3196 g_object_class_install_property (object_class,
3197 PROP_REMOTE_CONTACT,
3198 g_param_spec_object ("remote-contact",
3199 "The remote contact",
3200 "The remote contact is any",
3201 EMPATHY_TYPE_CONTACT,
3203 G_PARAM_STATIC_STRINGS));
3204 g_object_class_install_property (object_class,
3206 g_param_spec_boolean ("show-contacts",
3207 "Contacts' visibility",
3208 "The visibility of the contacts' list",
3211 G_PARAM_STATIC_STRINGS));
3213 g_object_class_install_property (object_class,
3215 g_param_spec_boolean ("sms-channel",
3217 "TRUE if this channel is for sending SMSes",
3219 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3221 g_object_class_install_property (object_class,
3222 PROP_N_MESSAGES_SENDING,
3223 g_param_spec_uint ("n-messages-sending",
3224 "Num Messages Sending",
3225 "The number of messages being sent",
3227 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3229 g_object_class_install_property (object_class,
3230 PROP_NB_UNREAD_MESSAGES,
3231 g_param_spec_uint ("nb-unread-messages",
3232 "Num Unread Messages",
3233 "The number of unread messages",
3235 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3237 signals[COMPOSING] =
3238 g_signal_new ("composing",
3239 G_OBJECT_CLASS_TYPE (object_class),
3243 g_cclosure_marshal_generic,
3247 signals[NEW_MESSAGE] =
3248 g_signal_new ("new-message",
3249 G_OBJECT_CLASS_TYPE (object_class),
3253 g_cclosure_marshal_generic,
3255 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3257 signals[PART_COMMAND_ENTERED] =
3258 g_signal_new ("part-command-entered",
3259 G_OBJECT_CLASS_TYPE (object_class),
3263 g_cclosure_marshal_generic,
3267 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3271 chat_block_events_timeout_cb (gpointer data)
3273 EmpathyChatPriv *priv = GET_PRIV (data);
3275 priv->block_events_timeout_id = 0;
3281 account_manager_prepared_cb (GObject *source_object,
3282 GAsyncResult *result,
3285 GList *accounts, *l;
3286 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3287 EmpathyChat *chat = user_data;
3288 GError *error = NULL;
3290 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3291 DEBUG ("Failed to prepare the account manager: %s", error->message);
3292 g_error_free (error);
3296 accounts = tp_account_manager_get_valid_accounts (account_manager);
3298 for (l = accounts; l != NULL; l = l->next) {
3299 TpAccount *account = l->data;
3300 tp_g_signal_connect_object (account, "status-changed",
3301 G_CALLBACK (chat_new_connection_cb),
3305 g_list_free (accounts);
3309 empathy_chat_init (EmpathyChat *chat)
3311 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3312 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3315 priv->log_manager = tpl_log_manager_dup_singleton ();
3316 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3317 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3319 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3320 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3321 priv->input_history = NULL;
3322 priv->input_history_current = NULL;
3323 priv->account_manager = tp_account_manager_dup ();
3325 tp_proxy_prepare_async (priv->account_manager, NULL,
3326 account_manager_prepared_cb, chat);
3328 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3329 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3331 /* Block events for some time to avoid having "has come online" or
3332 * "joined" messages. */
3333 priv->block_events_timeout_id =
3334 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3336 /* Add nick name completion */
3337 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3338 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3340 chat_create_ui (chat);
3344 empathy_chat_new (EmpathyTpChat *tp_chat)
3346 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3350 empathy_chat_get_tp_chat (EmpathyChat *chat)
3352 EmpathyChatPriv *priv = GET_PRIV (chat);
3354 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3356 return priv->tp_chat;
3362 GtkWidget *info_bar;
3372 passwd_remember_button_cb (GtkButton *button,
3375 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3379 passwd_not_now_button_cb (GtkButton *button,
3382 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3386 remember_password_infobar_response_cb (GtkWidget *info_bar,
3390 EmpathyChatPriv *priv = GET_PRIV (data->self);
3392 if (response_id == GTK_RESPONSE_OK) {
3393 DEBUG ("Saving room password");
3394 empathy_keyring_set_room_password_async (priv->account,
3395 empathy_tp_chat_get_id (priv->tp_chat),
3400 gtk_widget_destroy (info_bar);
3401 g_free (data->password);
3402 g_slice_free (PasswordData, data);
3406 chat_prompt_to_save_password (EmpathyChat *self,
3409 GtkWidget *content_area;
3416 /* save the password in case it needs to be saved */
3417 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3419 /* Remove all previous widgets */
3420 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3421 gtk_container_forall (GTK_CONTAINER (content_area),
3422 (GtkCallback) gtk_widget_destroy, NULL);
3423 data->button = NULL;
3426 data->spinner = NULL;
3428 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3429 GTK_MESSAGE_QUESTION);
3431 hbox = gtk_hbox_new (FALSE, 5);
3432 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3435 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3436 GTK_ICON_SIZE_DIALOG);
3437 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3440 label = gtk_label_new (_("Would you like to store this password?"));
3441 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3443 /* Add 'Remember' button */
3444 alig = gtk_alignment_new (0, 0.5, 1, 0);
3446 button = gtk_button_new_with_label (_("Remember"));
3447 gtk_container_add (GTK_CONTAINER (alig), button);
3448 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3450 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3453 /* Add 'Not now' button */
3454 alig = gtk_alignment_new (0, 0.5, 1, 0);
3456 button = gtk_button_new_with_label (_("Not now"));
3457 gtk_container_add (GTK_CONTAINER (alig), button);
3458 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3460 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3464 g_signal_handler_disconnect (data->info_bar, data->response_id);
3465 g_signal_connect (data->info_bar, "response",
3466 G_CALLBACK (remember_password_infobar_response_cb), data);
3468 gtk_widget_show_all (data->info_bar);
3472 provide_password_cb (GObject *tp_chat,
3476 PasswordData *data = user_data;
3477 EmpathyChat *self = data->self;
3478 EmpathyChatPriv *priv = GET_PRIV (self);
3479 GError *error = NULL;
3481 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3483 DEBUG ("error: %s", error->message);
3484 /* FIXME: what should we do if that's another error? Close the channel?
3485 * Display the raw D-Bus error to the user isn't very useful */
3486 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3488 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3489 gtk_widget_set_sensitive (data->entry, TRUE);
3490 gtk_widget_grab_focus (data->entry);
3493 gtk_info_bar_set_message_type (
3494 GTK_INFO_BAR (data->info_bar),
3498 gtk_widget_set_sensitive (data->button, TRUE);
3499 gtk_button_set_label (GTK_BUTTON (data->button),
3503 gtk_label_set_text (GTK_LABEL (data->label),
3504 _("Wrong password; please try again:"));
3507 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3508 gtk_widget_hide (data->spinner);
3510 g_error_free (error);
3514 if (empathy_keyring_is_available ()) {
3515 /* ask whether they want to save the password */
3516 chat_prompt_to_save_password (self, data);
3518 /* Get rid of the password info bar finally */
3519 gtk_widget_destroy (data->info_bar);
3520 g_slice_free (PasswordData, data);
3524 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3525 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3526 gtk_widget_grab_focus (self->input_text_view);
3530 password_infobar_response_cb (GtkWidget *info_bar,
3534 EmpathyChatPriv *priv = GET_PRIV (data->self);
3535 const gchar *password;
3537 if (response_id != GTK_RESPONSE_OK) {
3538 gtk_widget_destroy (info_bar);
3539 g_slice_free (PasswordData, data);
3543 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3545 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3546 provide_password_cb, data);
3548 gtk_widget_set_sensitive (data->button, FALSE);
3549 gtk_widget_set_sensitive (data->entry, FALSE);
3551 gtk_spinner_start (GTK_SPINNER (data->spinner));
3552 gtk_widget_show (data->spinner);
3556 password_entry_activate_cb (GtkWidget *entry,
3559 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3563 passwd_join_button_cb (GtkButton *button,
3566 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3570 clear_icon_released_cb (GtkEntry *entry,
3571 GtkEntryIconPosition icon_pos,
3575 gtk_entry_set_text (entry, "");
3579 password_entry_changed_cb (GtkEditable *entry,
3584 str = gtk_entry_get_text (GTK_ENTRY (entry));
3586 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3587 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3591 infobar_chat_invalidated_cb (TpProxy *proxy,
3595 gpointer password_infobar)
3597 /* Destroy the password infobar whenever a channel is invalidated
3598 * so we don't have multiple infobars when the MUC is rejoined */
3599 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3603 display_password_info_bar (EmpathyChat *self)
3605 EmpathyChatPriv *priv = GET_PRIV (self);
3606 GtkWidget *info_bar;
3607 GtkWidget *content_area;
3617 data = g_slice_new0 (PasswordData);
3619 info_bar = gtk_info_bar_new ();
3620 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3621 GTK_MESSAGE_QUESTION);
3623 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3625 hbox = gtk_hbox_new (FALSE, 5);
3626 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3629 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3630 GTK_ICON_SIZE_DIALOG);
3631 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3634 label = gtk_label_new (_("This room is protected by a password:"));
3635 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3637 /* Add password entry */
3638 entry = gtk_entry_new ();
3639 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3640 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3642 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3643 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3644 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3645 GTK_ENTRY_ICON_SECONDARY, FALSE);
3647 g_signal_connect (entry, "icon-release",
3648 G_CALLBACK (clear_icon_released_cb), data);
3649 g_signal_connect (entry, "changed",
3650 G_CALLBACK (password_entry_changed_cb), data);
3652 g_signal_connect (entry, "activate",
3653 G_CALLBACK (password_entry_activate_cb), data);
3655 /* Focus the password entry once it's realized */
3656 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3658 /* Add 'Join' button */
3659 alig = gtk_alignment_new (0, 0.5, 1, 0);
3661 button = gtk_button_new_with_label (_("Join"));
3662 gtk_container_add (GTK_CONTAINER (alig), button);
3663 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3665 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3669 spinner = gtk_spinner_new ();
3670 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3672 /* Save some data for messing around with later */
3674 data->info_bar = info_bar;
3675 data->button = button;
3676 data->label = label;
3677 data->entry = entry;
3678 data->spinner = spinner;
3680 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3682 gtk_widget_show_all (hbox);
3684 tp_g_signal_connect_object (priv->tp_chat,
3685 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3688 data->response_id = g_signal_connect (info_bar, "response",
3689 G_CALLBACK (password_infobar_response_cb), data);
3691 gtk_widget_show_all (info_bar);
3692 /* ... but hide the spinner */
3693 gtk_widget_hide (spinner);
3695 /* prevent the user from typing anything */
3696 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3700 provide_saved_password_cb (GObject *tp_chat,
3704 EmpathyChat *self = user_data;
3705 EmpathyChatPriv *priv = GET_PRIV (self);
3706 GError *error = NULL;
3708 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3710 DEBUG ("error: %s", error->message);
3711 /* FIXME: what should we do if that's another error? Close the channel?
3712 * Display the raw D-Bus error to the user isn't very useful */
3713 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3714 display_password_info_bar (self);
3715 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3717 g_error_free (error);
3722 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3723 gtk_widget_grab_focus (self->input_text_view);
3727 chat_room_got_password_cb (GObject *source,
3728 GAsyncResult *result,
3731 EmpathyChat *self = user_data;
3732 EmpathyChatPriv *priv = GET_PRIV (self);
3733 const gchar *password;
3734 GError *error = NULL;
3736 password = empathy_keyring_get_room_password_finish (priv->account,
3739 if (error != NULL) {
3740 DEBUG ("Couldn't get room password: %s\n", error->message);
3741 g_clear_error (&error);
3743 display_password_info_bar (self);
3744 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3748 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3749 provide_saved_password_cb, self);
3753 chat_password_needed_changed_cb (EmpathyChat *self)
3755 EmpathyChatPriv *priv = GET_PRIV (self);
3757 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3758 empathy_keyring_get_room_password_async (priv->account,
3759 empathy_tp_chat_get_id (priv->tp_chat),
3760 chat_room_got_password_cb, self);
3765 chat_sms_channel_changed_cb (EmpathyChat *self)
3767 EmpathyChatPriv *priv = GET_PRIV (self);
3769 priv->sms_channel = tp_text_channel_is_sms_channel (
3770 (TpTextChannel *) priv->tp_chat);
3771 g_object_notify (G_OBJECT (self), "sms-channel");
3775 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3777 g_object_notify (G_OBJECT (self), "n-messages-sending");
3781 empathy_chat_set_tp_chat (EmpathyChat *chat,
3782 EmpathyTpChat *tp_chat)
3784 EmpathyChatPriv *priv = GET_PRIV (chat);
3785 GPtrArray *properties;
3787 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3788 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3790 if (priv->tp_chat) {
3794 if (priv->account) {
3795 g_object_unref (priv->account);
3798 priv->tp_chat = g_object_ref (tp_chat);
3799 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3801 g_signal_connect (tp_chat, "invalidated",
3802 G_CALLBACK (chat_invalidated_cb),
3804 g_signal_connect (tp_chat, "message-received-empathy",
3805 G_CALLBACK (chat_message_received_cb),
3807 g_signal_connect (tp_chat, "message_acknowledged",
3808 G_CALLBACK (chat_message_acknowledged_cb),
3810 g_signal_connect (tp_chat, "send-error",
3811 G_CALLBACK (chat_send_error_cb),
3813 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3814 G_CALLBACK (chat_state_changed_cb),
3816 g_signal_connect (tp_chat, "property-changed",
3817 G_CALLBACK (chat_property_changed_cb),
3819 g_signal_connect (tp_chat, "members-changed",
3820 G_CALLBACK (chat_members_changed_cb),
3822 g_signal_connect (tp_chat, "member-renamed",
3823 G_CALLBACK (chat_member_renamed_cb),
3825 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3826 G_CALLBACK (chat_remote_contact_changed_cb),
3828 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3829 G_CALLBACK (chat_password_needed_changed_cb),
3831 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
3832 G_CALLBACK (chat_sms_channel_changed_cb),
3834 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3835 G_CALLBACK (chat_n_messages_sending_changed_cb),
3838 /* Get initial value of properties */
3839 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3840 if (properties != NULL) {
3843 for (i = 0; i < properties->len; i++) {
3844 EmpathyTpChatProperty *property;
3846 property = g_ptr_array_index (properties, i);
3847 if (property->value == NULL)
3850 chat_property_changed_cb (priv->tp_chat,
3857 chat_sms_channel_changed_cb (chat);
3858 chat_remote_contact_changed_cb (chat);
3860 if (chat->input_text_view) {
3861 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3862 if (priv->block_events_timeout_id == 0) {
3863 empathy_chat_view_append_event (chat->view, _("Connected"));
3867 g_object_notify (G_OBJECT (chat), "tp-chat");
3868 g_object_notify (G_OBJECT (chat), "id");
3869 g_object_notify (G_OBJECT (chat), "account");
3871 /* This is a noop when tp-chat is set at object construction time and causes
3872 * the pending messages to be show when it's set on the object after it has
3874 show_pending_messages (chat);
3876 /* check if a password is needed */
3877 chat_password_needed_changed_cb (chat);
3881 empathy_chat_get_account (EmpathyChat *chat)
3883 EmpathyChatPriv *priv = GET_PRIV (chat);
3885 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3887 return priv->account;
3891 empathy_chat_get_id (EmpathyChat *chat)
3893 EmpathyChatPriv *priv = GET_PRIV (chat);
3895 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3901 empathy_chat_dup_name (EmpathyChat *chat)
3903 EmpathyChatPriv *priv = GET_PRIV (chat);
3906 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3910 if (!ret && priv->remote_contact) {
3911 ret = empathy_contact_get_alias (priv->remote_contact);
3918 ret = _("Conversation");
3920 if (priv->sms_channel)
3921 /* Translators: this string is a something like
3922 * "Escher Cat (SMS)" */
3923 return g_strdup_printf (_("%s (SMS)"), ret);
3925 return g_strdup (ret);
3929 empathy_chat_get_subject (EmpathyChat *chat)
3931 EmpathyChatPriv *priv = GET_PRIV (chat);
3933 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3935 return priv->subject;
3939 empathy_chat_get_remote_contact (EmpathyChat *chat)
3941 EmpathyChatPriv *priv = GET_PRIV (chat);
3943 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3945 return priv->remote_contact;
3949 empathy_chat_get_contact_menu (EmpathyChat *chat)
3951 EmpathyChatPriv *priv = GET_PRIV (chat);
3952 GtkWidget *menu = NULL;
3954 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3956 if (priv->remote_contact) {
3957 menu = empathy_contact_menu_new (priv->remote_contact,
3958 EMPATHY_CONTACT_FEATURE_CALL |
3959 EMPATHY_CONTACT_FEATURE_LOG |
3960 EMPATHY_CONTACT_FEATURE_INFO |
3961 EMPATHY_CONTACT_FEATURE_BLOCK);
3968 empathy_chat_clear (EmpathyChat *chat)
3970 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3972 empathy_chat_view_clear (chat->view);
3976 empathy_chat_scroll_down (EmpathyChat *chat)
3978 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3980 empathy_chat_view_scroll_down (chat->view);
3984 empathy_chat_cut (EmpathyChat *chat)
3986 GtkTextBuffer *buffer;
3988 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3990 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3991 if (gtk_text_buffer_get_has_selection (buffer)) {
3992 GtkClipboard *clipboard;
3994 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3996 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4001 empathy_chat_copy (EmpathyChat *chat)
4003 GtkTextBuffer *buffer;
4005 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4007 if (empathy_chat_view_get_has_selection (chat->view)) {
4008 empathy_chat_view_copy_clipboard (chat->view);
4012 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4013 if (gtk_text_buffer_get_has_selection (buffer)) {
4014 GtkClipboard *clipboard;
4016 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4018 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4023 EmpathyChatPriv *priv = GET_PRIV (chat);
4025 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4032 GtkClipboard *clipboard;
4034 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4035 start = g_utf8_offset_to_pointer (topic, start_offset);
4036 end = g_utf8_offset_to_pointer (topic, end_offset);
4037 selection = g_strndup (start, end - start);
4039 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4040 gtk_clipboard_set_text (clipboard, selection, -1);
4048 empathy_chat_paste (EmpathyChat *chat)
4050 GtkTextBuffer *buffer;
4051 GtkClipboard *clipboard;
4052 EmpathyChatPriv *priv;
4054 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4056 priv = GET_PRIV (chat);
4058 if (gtk_widget_get_visible (priv->search_bar)) {
4059 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4063 if (priv->tp_chat == NULL ||
4064 !gtk_widget_is_sensitive (chat->input_text_view))
4067 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4068 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4070 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4074 empathy_chat_find (EmpathyChat *chat)
4076 EmpathyChatPriv *priv;
4078 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4080 priv = GET_PRIV (chat);
4082 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4086 empathy_chat_correct_word (EmpathyChat *chat,
4089 const gchar *new_word)
4091 GtkTextBuffer *buffer;
4093 g_return_if_fail (chat != NULL);
4094 g_return_if_fail (new_word != NULL);
4096 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4098 gtk_text_buffer_delete (buffer, start, end);
4099 gtk_text_buffer_insert (buffer, start,
4105 empathy_chat_is_room (EmpathyChat *chat)
4107 EmpathyChatPriv *priv = GET_PRIV (chat);
4109 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4111 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4115 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4117 EmpathyChatPriv *priv = GET_PRIV (self);
4119 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4121 return priv->unread_messages;
4124 /* called when the messages have been read by user */
4126 empathy_chat_messages_read (EmpathyChat *self)
4128 EmpathyChatPriv *priv = GET_PRIV (self);
4130 g_return_if_fail (EMPATHY_IS_CHAT (self));
4132 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4133 * definition. If we are still retrieving the backlogs, do not ACK */
4134 if (priv->retrieving_backlogs)
4137 if (priv->tp_chat != NULL) {
4138 tp_text_channel_ack_all_pending_messages_async (
4139 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4142 if (priv->unread_messages_when_offline > 0) {
4143 /* We can't ack those as the connection has gone away so just consider
4145 priv->unread_messages -= priv->unread_messages_when_offline;
4146 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4147 priv->unread_messages_when_offline = 0;
4151 /* Return TRUE if on of the contacts in this chat is composing */
4153 empathy_chat_is_composing (EmpathyChat *chat)
4155 return chat->priv->compositors != NULL;
4159 empathy_chat_is_sms_channel (EmpathyChat *self)
4161 EmpathyChatPriv *priv = GET_PRIV (self);
4163 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4165 return priv->sms_channel;
4169 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4171 EmpathyChatPriv *priv;
4173 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4175 priv = GET_PRIV (self);
4177 if (priv->tp_chat == NULL) {
4182 g_object_get (priv->tp_chat,
4183 "n-messages-sending", &n_messages,
4191 empathy_chat_dup_text (EmpathyChat *self)
4193 GtkTextBuffer *buffer;
4194 GtkTextIter start, end;
4196 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4198 gtk_text_buffer_get_bounds (buffer, &start, &end);
4199 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4203 empathy_chat_set_text (EmpathyChat *self,
4206 GtkTextBuffer *buffer;
4208 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4210 gtk_text_buffer_set_text (buffer, text, -1);