1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #undef G_DISABLE_DEPRECATED /* for GCompletion */
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n-lib.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-keyring.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-request-util.h>
46 #include <libempathy/empathy-chatroom-manager.h>
48 #include "empathy-chat.h"
49 #include "empathy-spell.h"
50 #include "empathy-contact-list-store.h"
51 #include "empathy-contact-list-view.h"
52 #include "empathy-contact-menu.h"
53 #include "empathy-gtk-marshal.h"
54 #include "empathy-input-text-view.h"
55 #include "empathy-search-bar.h"
56 #include "empathy-theme-manager.h"
57 #include "empathy-smiley-manager.h"
58 #include "empathy-ui-utils.h"
59 #include "empathy-string-parser.h"
60 #include "extensions/extensions.h"
62 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
63 #include <libempathy/empathy-debug.h>
65 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
66 #define COMPOSING_STOP_TIMEOUT 5
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
69 struct _EmpathyChatPriv {
70 EmpathyTpChat *tp_chat;
75 EmpathyContact *remote_contact;
76 gboolean show_contacts;
78 GSettings *gsettings_chat;
79 GSettings *gsettings_ui;
81 TplLogManager *log_manager;
82 TpAccountManager *account_manager;
84 GList *input_history_current;
86 GCompletion *completion;
87 guint composing_stop_timeout_id;
88 guint block_events_timeout_id;
89 TpHandleType handle_type;
91 gboolean has_input_vscroll;
93 /* TRUE if spell checking is enabled, FALSE otherwise.
94 * This is to keep track of the last state of spell checking
96 gboolean spell_checking_enabled;
98 /* These store the signal handler ids for the enclosed text entry. */
99 gulong insert_text_id;
100 gulong delete_range_id;
101 gulong notify_cursor_position_id;
103 /* Source func ID for update_misspelled_words () */
104 guint update_misspelled_words_id;
105 /* Source func ID for save_paned_pos_timeout () */
106 guint save_paned_pos_id;
110 GtkWidget *vbox_left;
111 GtkWidget *scrolled_window_chat;
112 GtkWidget *scrolled_window_input;
113 GtkWidget *scrolled_window_contacts;
114 GtkWidget *hbox_topic;
115 GtkWidget *expander_topic;
116 GtkWidget *label_topic;
117 GtkWidget *contact_list_view;
118 GtkWidget *info_bar_vbox;
119 GtkWidget *search_bar;
121 guint unread_messages;
122 /* TRUE if the pending messages can be displayed. This is to avoid to show
123 * pending messages *before* messages from logs. (#603980) */
124 gboolean can_show_pending;
126 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
127 * be differently handled since it introduces another race condition, which
128 * is really hard to occur, but still possible.
130 * With the current workaround (which has the race above), we need to be
131 * sure to ACK any pending messages only when the retrieval of backlogs is
132 * finished, that's why using retrieving_backlogs flag.
133 * empathy_chat_messages_read () will check this variable and not ACK
134 * anything when TRUE. It will be set TRUE at chat_constructed () and set
135 * back to FALSE when the backlog has been retrieved and the pending
136 * messages actually showed to the user.
138 * Race condition introduced with this workaround:
139 * Scenario: a message is pending, the user is notified and selects the tab.
140 * the tab with a pending message is focused before the messages are properly
141 * shown (since the preparation of the window is slower AND async WRT the
142 * tab showing), which means the user won't see any new messages (rare but
143 * possible), if he/she will change tab focus before the messages are
144 * properly shown, the tab will be set as 'seen' and the user won't be
145 * notified again about the already notified pending messages when the
146 * messages in tab will be properly shown */
147 gboolean retrieving_backlogs;
148 gboolean sms_channel;
152 gchar *text; /* Original message that was specified
153 * upon entry creation. */
154 gchar *modified_text; /* Message that was modified by user.
155 * When no modifications were made, it is NULL */
161 PART_COMMAND_ENTERED,
175 PROP_N_MESSAGES_SENDING,
178 static guint signals[LAST_SIGNAL] = { 0 };
180 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
182 static gboolean update_misspelled_words (gpointer data);
185 chat_get_property (GObject *object,
190 EmpathyChat *chat = EMPATHY_CHAT (object);
191 EmpathyChatPriv *priv = GET_PRIV (object);
195 g_value_set_object (value, priv->tp_chat);
198 g_value_set_object (value, priv->account);
201 g_value_take_string (value, empathy_chat_dup_name (chat));
204 g_value_set_string (value, priv->id);
207 g_value_set_string (value, priv->subject);
209 case PROP_REMOTE_CONTACT:
210 g_value_set_object (value, priv->remote_contact);
212 case PROP_SHOW_CONTACTS:
213 g_value_set_boolean (value, priv->show_contacts);
215 case PROP_SMS_CHANNEL:
216 g_value_set_boolean (value, priv->sms_channel);
218 case PROP_N_MESSAGES_SENDING:
219 g_value_set_uint (value,
220 empathy_chat_get_n_messages_sending (chat));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
229 chat_set_property (GObject *object,
234 EmpathyChat *chat = EMPATHY_CHAT (object);
238 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
240 case PROP_SHOW_CONTACTS:
241 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
250 account_reconnected (EmpathyChat *chat,
253 EmpathyChatPriv *priv = GET_PRIV (chat);
255 DEBUG ("Account reconnected, request a new Text channel");
257 /* FIXME: Ideally we should ask to handle ourself the channel so we can
258 * report the error if any but this is blocked by
259 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
260 switch (priv->handle_type) {
261 case TP_HANDLE_TYPE_CONTACT:
262 if (priv->sms_channel)
263 empathy_sms_contact_id (
265 TP_USER_ACTION_TIME_NOT_USER_ACTION);
267 empathy_chat_with_contact_id (
269 TP_USER_ACTION_TIME_NOT_USER_ACTION);
271 case TP_HANDLE_TYPE_ROOM:
272 empathy_join_muc (account, priv->id,
273 TP_USER_ACTION_TIME_NOT_USER_ACTION);
275 case TP_HANDLE_TYPE_NONE:
276 case TP_HANDLE_TYPE_LIST:
277 case TP_HANDLE_TYPE_GROUP:
279 g_assert_not_reached ();
283 g_object_unref (chat);
287 chat_new_connection_cb (TpAccount *account,
291 gchar *dbus_error_name,
295 EmpathyChatPriv *priv = GET_PRIV (chat);
297 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
300 if (priv->tp_chat != NULL || account != priv->account ||
301 priv->handle_type == TP_HANDLE_TYPE_NONE ||
302 EMP_STR_EMPTY (priv->id))
307 account_reconnected (chat, account);
311 chat_composing_remove_timeout (EmpathyChat *chat)
313 EmpathyChatPriv *priv;
315 priv = GET_PRIV (chat);
317 if (priv->composing_stop_timeout_id) {
318 g_source_remove (priv->composing_stop_timeout_id);
319 priv->composing_stop_timeout_id = 0;
324 chat_composing_stop_timeout_cb (EmpathyChat *chat)
326 EmpathyChatPriv *priv;
328 priv = GET_PRIV (chat);
330 priv->composing_stop_timeout_id = 0;
331 empathy_tp_chat_set_state (priv->tp_chat,
332 TP_CHANNEL_CHAT_STATE_PAUSED);
338 chat_composing_start (EmpathyChat *chat)
340 EmpathyChatPriv *priv;
342 priv = GET_PRIV (chat);
344 if (priv->composing_stop_timeout_id) {
345 /* Just restart the timeout */
346 chat_composing_remove_timeout (chat);
348 empathy_tp_chat_set_state (priv->tp_chat,
349 TP_CHANNEL_CHAT_STATE_COMPOSING);
352 priv->composing_stop_timeout_id = g_timeout_add_seconds (
353 COMPOSING_STOP_TIMEOUT,
354 (GSourceFunc) chat_composing_stop_timeout_cb,
359 chat_composing_stop (EmpathyChat *chat)
361 EmpathyChatPriv *priv;
363 priv = GET_PRIV (chat);
365 chat_composing_remove_timeout (chat);
366 empathy_tp_chat_set_state (priv->tp_chat,
367 TP_CHANNEL_CHAT_STATE_ACTIVE);
371 chat_input_history_entry_cmp (InputHistoryEntry *entry,
374 if (!tp_strdiff (entry->text, text)) {
375 if (entry->modified_text != NULL) {
376 /* Modified entry and single string cannot be equal. */
384 static InputHistoryEntry *
385 chat_input_history_entry_new_with_text (const gchar *text)
387 InputHistoryEntry *entry;
388 entry = g_slice_new0 (InputHistoryEntry);
389 entry->text = g_strdup (text);
395 chat_input_history_entry_free (InputHistoryEntry *entry)
397 g_free (entry->text);
398 g_free (entry->modified_text);
399 g_slice_free (InputHistoryEntry, entry);
403 chat_input_history_entry_revert (InputHistoryEntry *entry)
405 g_free (entry->modified_text);
406 entry->modified_text = NULL;
410 chat_input_history_entry_update_text (InputHistoryEntry *entry,
415 if (!tp_strdiff (text, entry->text)) {
416 g_free (entry->modified_text);
417 entry->modified_text = NULL;
421 old = entry->modified_text;
422 entry->modified_text = g_strdup (text);
427 chat_input_history_entry_get_text (InputHistoryEntry *entry)
433 if (entry->modified_text != NULL) {
434 return entry->modified_text;
440 chat_input_history_remove_item (GList *list,
443 list = g_list_remove_link (list, item);
444 chat_input_history_entry_free (item->data);
445 g_list_free_1 (item);
450 chat_input_history_revert (EmpathyChat *chat)
452 EmpathyChatPriv *priv;
456 InputHistoryEntry *entry;
458 priv = GET_PRIV (chat);
459 list = priv->input_history;
462 DEBUG ("No input history");
466 /* Delete temporary entry */
467 if (priv->input_history_current != NULL) {
469 list = chat_input_history_remove_item (list, item1);
470 if (priv->input_history_current == item1) {
471 /* Removed temporary entry was current entry */
472 priv->input_history = list;
473 priv->input_history_current = NULL;
478 /* There is no entry to revert */
482 /* Restore the current history entry to original value */
483 item1 = priv->input_history_current;
485 chat_input_history_entry_revert (entry);
487 /* Remove restored entry if there is other occurance before this entry */
488 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
489 (GCompareFunc) chat_input_history_entry_cmp);
490 if (item2 != item1) {
491 list = chat_input_history_remove_item (list, item1);
494 /* Remove other occurance of the restored entry */
495 item2 = g_list_find_custom (item1->next,
496 chat_input_history_entry_get_text (entry),
497 (GCompareFunc) chat_input_history_entry_cmp);
499 list = chat_input_history_remove_item (list, item2);
503 priv->input_history_current = NULL;
504 priv->input_history = list;
508 chat_input_history_add (EmpathyChat *chat,
512 EmpathyChatPriv *priv;
515 InputHistoryEntry *entry;
517 priv = GET_PRIV (chat);
519 list = priv->input_history;
521 /* Remove any other occurances of this entry, if not temporary */
523 while ((item = g_list_find_custom (list, str,
524 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
525 list = chat_input_history_remove_item (list, item);
528 /* Trim the list to the last 10 items */
529 while (g_list_length (list) > 10) {
530 item = g_list_last (list);
532 list = chat_input_history_remove_item (list, item);
540 entry = chat_input_history_entry_new_with_text (str);
541 list = g_list_prepend (list, entry);
543 /* Set the list and the current item pointer */
544 priv->input_history = list;
546 priv->input_history_current = list;
549 priv->input_history_current = NULL;
554 chat_input_history_get_next (EmpathyChat *chat)
556 EmpathyChatPriv *priv;
560 priv = GET_PRIV (chat);
562 if (priv->input_history == NULL) {
563 DEBUG ("No input history, next entry is NULL");
566 g_assert (priv->input_history_current != NULL);
568 if ((item = g_list_next (priv->input_history_current)) == NULL)
570 item = priv->input_history_current;
573 msg = chat_input_history_entry_get_text (item->data);
575 DEBUG ("Returning next entry: '%s'", msg);
577 priv->input_history_current = item;
583 chat_input_history_get_prev (EmpathyChat *chat)
585 EmpathyChatPriv *priv;
589 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
591 priv = GET_PRIV (chat);
593 if (priv->input_history == NULL) {
594 DEBUG ("No input history, previous entry is NULL");
598 if (priv->input_history_current == NULL)
602 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
604 item = priv->input_history_current;
607 msg = chat_input_history_entry_get_text (item->data);
609 DEBUG ("Returning previous entry: '%s'", msg);
611 priv->input_history_current = item;
617 chat_input_history_update (EmpathyChat *chat,
618 GtkTextBuffer *buffer)
620 EmpathyChatPriv *priv;
621 GtkTextIter start, end;
623 InputHistoryEntry *entry;
625 priv = GET_PRIV (chat);
627 gtk_text_buffer_get_bounds (buffer, &start, &end);
628 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
630 if (priv->input_history_current == NULL) {
631 /* Add the current text temporarily to the history */
632 chat_input_history_add (chat, text, TRUE);
637 /* Save the changes in the history */
638 entry = priv->input_history_current->data;
639 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
640 chat_input_history_entry_update_text (entry, text);
649 } ChatCommandMsgData;
652 chat_command_msg_cb (GObject *source,
653 GAsyncResult *result,
656 ChatCommandMsgData *data = user_data;
657 GError *error = NULL;
660 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
661 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
663 if (channel == NULL) {
664 DEBUG ("Failed to get channel: %s", error->message);
665 g_error_free (error);
667 empathy_chat_view_append_event (data->chat->view,
668 _("Failed to open private chat"));
672 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
673 TpTextChannel *text = (TpTextChannel *) channel;
676 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
679 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
681 g_object_unref (msg);
684 g_object_unref (channel);
687 g_free (data->message);
688 g_slice_free (ChatCommandMsgData, data);
692 nick_command_supported (EmpathyChat *chat)
694 EmpathyChatPriv * priv = GET_PRIV (chat);
695 TpConnection *connection;
697 connection = empathy_tp_chat_get_connection (priv->tp_chat);
698 return tp_proxy_has_interface_by_id (connection,
699 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
703 part_command_supported (EmpathyChat *chat)
705 EmpathyChatPriv * priv = GET_PRIV (chat);
708 channel = empathy_tp_chat_get_channel (priv->tp_chat);
709 return tp_proxy_has_interface_by_id (channel,
710 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
714 chat_command_clear (EmpathyChat *chat,
717 empathy_chat_view_clear (chat->view);
721 chat_command_topic (EmpathyChat *chat,
724 EmpathyChatPriv *priv = GET_PRIV (chat);
725 EmpathyTpChatProperty *property;
726 GValue value = {0, };
728 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
729 if (property == NULL) {
730 empathy_chat_view_append_event (chat->view,
731 _("Topic not supported on this conversation"));
735 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
736 empathy_chat_view_append_event (chat->view,
737 _("You are not allowed to change the topic"));
741 g_value_init (&value, G_TYPE_STRING);
742 g_value_set_string (&value, strv[1]);
743 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
744 g_value_unset (&value);
748 empathy_chat_join_muc (EmpathyChat *chat,
751 EmpathyChatPriv *priv = GET_PRIV (chat);
753 empathy_join_muc (priv->account, room,
754 empathy_get_current_action_time ());
758 chat_command_join (EmpathyChat *chat,
763 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
765 /* FIXME: Ideally we should ask to handle ourself the channel so we can
766 * report the error if any but this is blocked by
767 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
768 while (rooms[i] != NULL) {
769 /* ignore empty strings */
770 if (!EMP_STR_EMPTY (rooms[i])) {
771 empathy_chat_join_muc (chat, rooms[i]);
779 chat_command_part (EmpathyChat *chat,
782 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
786 chat_command_msg_internal (EmpathyChat *chat,
787 const gchar *contact_id,
788 const gchar *message)
790 EmpathyChatPriv *priv = GET_PRIV (chat);
791 ChatCommandMsgData *data;
792 TpAccountChannelRequest *req;
795 request = tp_asv_new (
796 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
797 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
798 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
801 req = tp_account_channel_request_new (priv->account, request,
802 empathy_get_current_action_time ());
804 /* FIXME: We should probably search in members alias. But this
805 * is enough for IRC */
806 data = g_slice_new (ChatCommandMsgData);
808 data->message = g_strdup (message);
810 tp_account_channel_request_ensure_and_observe_channel_async (req,
811 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
813 g_object_unref (req);
814 g_hash_table_unref (request);
818 chat_command_query (EmpathyChat *chat,
821 /* If <message> part is not defined,
822 * strv[2] will be the terminal NULL */
823 chat_command_msg_internal (chat, strv[1], strv[2]);
827 chat_command_msg (EmpathyChat *chat,
830 chat_command_msg_internal (chat, strv[1], strv[2]);
834 callback_for_request_rename (TpProxy *proxy,
837 GObject *weak_object)
840 DEBUG ("Call to RequestRename method failed: %s",error->message);
845 chat_command_nick (EmpathyChat *chat,
848 EmpathyChatPriv *priv = GET_PRIV (chat);
851 proxy = TP_PROXY (tp_account_get_connection (priv->account));
853 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
854 strv[1], callback_for_request_rename, NULL, NULL, NULL);
858 chat_command_me (EmpathyChat *chat,
861 EmpathyChatPriv *priv = GET_PRIV (chat);
865 channel = empathy_tp_chat_get_channel (priv->tp_chat);
867 /* Strictly speaking we don't depend yet on Messages so best to check that
868 * the channel is actually a TpTextChannel before casting it. */
869 if (TP_IS_TEXT_CHANNEL (channel) &&
870 !tp_text_channel_supports_message_type (TP_TEXT_CHANNEL (channel),
871 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
872 /* Action message are not supported, 'simulate' the action */
873 EmpathyContact *self_contact;
876 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
877 /* The TpChat can't be ready if it doesn't have the self contact */
878 g_assert (self_contact != NULL);
880 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
882 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
887 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
891 empathy_tp_chat_send (priv->tp_chat, message);
892 g_object_unref (message);
896 chat_command_say (EmpathyChat *chat,
899 EmpathyChatPriv *priv = GET_PRIV (chat);
902 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
904 empathy_tp_chat_send (priv->tp_chat, message);
905 g_object_unref (message);
908 static void chat_command_help (EmpathyChat *chat, GStrv strv);
910 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
916 ChatCommandFunc func;
917 gboolean (*is_supported)(EmpathyChat *chat);
921 static ChatCommandItem commands[] = {
922 {"clear", 1, 1, chat_command_clear, NULL,
923 N_("/clear: clear all messages from the current conversation")},
925 {"topic", 2, 2, chat_command_topic, NULL,
926 N_("/topic <topic>: set the topic of the current conversation")},
928 {"join", 2, 2, chat_command_join, NULL,
929 N_("/join <chat room ID>: join a new chat room")},
931 {"j", 2, 2, chat_command_join, NULL,
932 N_("/j <chat room ID>: join a new chat room")},
934 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
935 {"part", 1, 3, chat_command_part, part_command_supported,
936 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
937 "by default the current one")},
939 {"query", 2, 3, chat_command_query, NULL,
940 N_("/query <contact ID> [<message>]: open a private chat")},
942 {"msg", 3, 3, chat_command_msg, NULL,
943 N_("/msg <contact ID> <message>: open a private chat")},
945 {"nick", 2, 2, chat_command_nick, nick_command_supported,
946 N_("/nick <nickname>: change your nickname on the current server")},
948 {"me", 2, 2, chat_command_me, NULL,
949 N_("/me <message>: send an ACTION message to the current conversation")},
951 {"say", 2, 2, chat_command_say, NULL,
952 N_("/say <message>: send <message> to the current conversation. "
953 "This is used to send a message starting with a '/'. For example: "
954 "\"/say /join is used to join a new chat room\"")},
956 {"help", 1, 2, chat_command_help, NULL,
957 N_("/help [<command>]: show all supported commands. "
958 "If <command> is defined, show its usage.")},
962 chat_command_show_help (EmpathyChat *chat,
963 ChatCommandItem *item)
967 str = g_strdup_printf (_("Usage: %s"), _(item->help));
968 empathy_chat_view_append_event (chat->view, str);
973 chat_command_help (EmpathyChat *chat,
978 /* If <command> part is not defined,
979 * strv[1] will be the terminal NULL */
980 if (strv[1] == NULL) {
981 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
982 if (commands[i].is_supported != NULL) {
983 if (!commands[i].is_supported (chat)) {
987 empathy_chat_view_append_event (chat->view,
988 _(commands[i].help));
993 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
994 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
995 if (commands[i].is_supported != NULL) {
996 if (!commands[i].is_supported (chat)) {
1000 chat_command_show_help (chat, &commands[i]);
1005 empathy_chat_view_append_event (chat->view,
1006 _("Unknown command"));
1010 chat_command_parse (const gchar *text, guint max_parts)
1015 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1017 array = g_ptr_array_sized_new (max_parts + 1);
1018 while (max_parts > 1) {
1021 /* Skip white spaces */
1022 while (g_ascii_isspace (*text)) {
1026 /* Search the end of this part, until first space. */
1027 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1033 item = g_strndup (text, end - text);
1034 g_ptr_array_add (array, item);
1035 DEBUG ("\tITEM: \"%s\"", item);
1041 /* Append last part if not empty */
1042 item = g_strstrip (g_strdup (text));
1043 if (!EMP_STR_EMPTY (item)) {
1044 g_ptr_array_add (array, item);
1045 DEBUG ("\tITEM: \"%s\"", item);
1050 /* Make the array NULL-terminated */
1051 g_ptr_array_add (array, NULL);
1053 return (GStrv) g_ptr_array_free (array, FALSE);
1057 has_prefix_case (const gchar *s,
1058 const gchar *prefix)
1060 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1064 chat_send (EmpathyChat *chat,
1067 EmpathyChatPriv *priv;
1071 if (EMP_STR_EMPTY (msg)) {
1075 priv = GET_PRIV (chat);
1077 chat_input_history_add (chat, msg, FALSE);
1079 if (msg[0] == '/') {
1080 gboolean second_slash = FALSE;
1081 const gchar *iter = msg + 1;
1083 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1088 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1091 c = *(msg + 1 + strlen (commands[i].prefix));
1092 if (c != '\0' && !g_ascii_isspace (c)) {
1095 if (commands[i].is_supported != NULL) {
1096 if (!commands[i].is_supported (chat)) {
1101 /* We can't use g_strsplit here because it does
1102 * not deal correctly if we have more than one space
1104 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1106 strv_len = g_strv_length (strv);
1107 if (strv_len < commands[i].min_parts ||
1108 strv_len > commands[i].max_parts) {
1109 chat_command_show_help (chat, &commands[i]);
1114 commands[i].func (chat, strv);
1119 /* Also allow messages with two slashes before the
1120 * first space, so it is possible to send a /unix/path.
1121 * This heuristic is kind of crap. */
1122 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1124 second_slash = TRUE;
1130 if (!second_slash) {
1131 empathy_chat_view_append_event (chat->view,
1132 _("Unknown command; see /help for the available"
1138 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1140 empathy_tp_chat_send (priv->tp_chat, message);
1141 g_object_unref (message);
1145 chat_input_text_view_send (EmpathyChat *chat)
1147 GtkTextBuffer *buffer;
1148 GtkTextIter start, end;
1151 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1153 gtk_text_buffer_get_bounds (buffer, &start, &end);
1154 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1156 /* clear the input field */
1157 gtk_text_buffer_set_text (buffer, "", -1);
1158 /* delete input history modifications */
1159 chat_input_history_revert (chat);
1161 chat_send (chat, msg);
1166 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1167 EmpathyContact *contact,
1168 TpChannelChatState state,
1171 EmpathyChatPriv *priv;
1173 gboolean was_composing;
1175 priv = GET_PRIV (chat);
1177 if (empathy_contact_is_user (contact)) {
1178 /* We don't care about our own chat state */
1182 was_composing = (priv->compositors != NULL);
1184 /* Find the contact in the list. After that l is the list elem or NULL */
1185 for (l = priv->compositors; l; l = l->next) {
1186 if (contact == l->data) {
1192 case TP_CHANNEL_CHAT_STATE_GONE:
1193 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1194 case TP_CHANNEL_CHAT_STATE_PAUSED:
1195 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1196 /* Contact is not composing */
1198 priv->compositors = g_list_remove_link (priv->compositors, l);
1199 g_object_unref (l->data);
1203 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1204 /* Contact is composing */
1206 priv->compositors = g_list_prepend (priv->compositors,
1207 g_object_ref (contact));
1211 g_assert_not_reached ();
1214 DEBUG ("Was composing: %s now composing: %s",
1215 was_composing ? "yes" : "no",
1216 priv->compositors ? "yes" : "no");
1218 if ((was_composing && !priv->compositors) ||
1219 (!was_composing && priv->compositors)) {
1220 /* Composing state changed */
1221 g_signal_emit (chat, signals[COMPOSING], 0,
1222 priv->compositors != NULL);
1227 chat_message_received (EmpathyChat *chat,
1228 EmpathyMessage *message,
1231 EmpathyChatPriv *priv = GET_PRIV (chat);
1232 EmpathyContact *sender;
1234 sender = empathy_message_get_sender (message);
1236 DEBUG ("Appending new message from %s (%d)",
1237 empathy_contact_get_alias (sender),
1238 empathy_contact_get_handle (sender));
1240 empathy_chat_view_append_message (chat->view, message);
1242 /* We received a message so the contact is no longer composing */
1243 chat_state_changed_cb (priv->tp_chat, sender,
1244 TP_CHANNEL_CHAT_STATE_ACTIVE,
1247 priv->unread_messages++;
1248 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1252 chat_message_received_cb (EmpathyTpChat *tp_chat,
1253 EmpathyMessage *message,
1256 chat_message_received (chat, message, FALSE);
1260 chat_send_error_cb (EmpathyTpChat *tp_chat,
1261 const gchar *message_body,
1262 TpChannelTextSendError error_code,
1263 const gchar *dbus_error,
1266 const gchar *error = NULL;
1269 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1270 /* translators: error used when user doesn't have enough credit on his
1271 * account to send the message. */
1272 error = _("insufficient balance to send message");
1273 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1274 error = _("not capable");
1277 if (error == NULL) {
1278 /* if we didn't find a dbus-error, try the old error */
1279 switch (error_code) {
1280 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1281 error = _("offline");
1283 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1284 error = _("invalid contact");
1286 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1287 error = _("permission denied");
1289 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1290 error = _("too long message");
1292 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1293 error = _("not implemented");
1295 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1297 error = _("unknown");
1302 if (message_body != NULL) {
1303 str = g_strdup_printf (_("Error sending message '%s': %s"),
1304 message_body, error);
1307 str = g_strdup_printf (_("Error sending message: %s"), error);
1310 empathy_chat_view_append_event (chat->view, str);
1315 chat_topic_label_size_allocate_cb (GtkLabel *label,
1316 GtkAllocation *allocation,
1319 EmpathyChatPriv *priv = GET_PRIV (chat);
1321 if (!gtk_label_get_line_wrap (label)) {
1322 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1323 gtk_widget_show (priv->expander_topic);
1325 gtk_widget_hide (priv->expander_topic);
1332 chat_topic_expander_activate_cb (GtkExpander *expander,
1333 GParamSpec *param_spec,
1336 EmpathyChatPriv *priv = GET_PRIV (chat);
1338 if (gtk_expander_get_expanded (expander)) {
1339 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1340 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1342 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1343 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1348 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1353 EmpathyChatPriv *priv = GET_PRIV (chat);
1355 if (!tp_strdiff (name, "subject")) {
1356 g_free (priv->subject);
1357 priv->subject = g_value_dup_string (value);
1358 g_object_notify (G_OBJECT (chat), "subject");
1360 if (EMP_STR_EMPTY (priv->subject)) {
1361 gtk_widget_hide (priv->hbox_topic);
1363 gchar *markup_topic;
1366 markup_topic = empathy_add_link_markup (priv->subject);
1367 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1368 _("Topic:"), markup_topic);
1370 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1371 g_free (markup_text);
1372 g_free (markup_topic);
1374 gtk_widget_show (priv->hbox_topic);
1376 if (priv->block_events_timeout_id == 0) {
1379 if (!EMP_STR_EMPTY (priv->subject)) {
1380 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1382 str = g_strdup (_("No topic defined"));
1384 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1388 else if (!tp_strdiff (name, "name")) {
1389 g_free (priv->name);
1390 priv->name = g_value_dup_string (value);
1391 g_object_notify (G_OBJECT (chat), "name");
1396 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1400 GtkTextIter word_start = *iter;
1401 GtkTextIter word_end = *iter;
1404 if (gtk_text_iter_inside_word (&word_end) &&
1405 !gtk_text_iter_ends_word (&word_end)) {
1406 gtk_text_iter_forward_word_end (&word_end);
1411 if (gtk_text_iter_get_char (&tmp) == '\'') {
1412 gtk_text_iter_forward_char (&tmp);
1414 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1415 gtk_text_iter_forward_word_end (&word_end);
1420 if (gtk_text_iter_inside_word (&word_start) ||
1421 gtk_text_iter_ends_word (&word_start)) {
1422 if (!gtk_text_iter_starts_word (&word_start) ||
1423 gtk_text_iter_equal (&word_start, &word_end)) {
1424 gtk_text_iter_backward_word_start (&word_start);
1428 gtk_text_iter_backward_char (&tmp);
1430 if (gtk_text_iter_get_char (&tmp) == '\'') {
1431 gtk_text_iter_backward_char (&tmp);
1433 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1434 gtk_text_iter_backward_word_start (&word_start);
1439 *start = word_start;
1445 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1446 GtkTextIter *location,
1451 GtkTextIter iter, pos;
1453 /* Remove all misspelled tags in the inserted text.
1454 * This happens when text is inserted within a misspelled word. */
1455 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1456 gtk_text_iter_get_offset (location) - len);
1457 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1460 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1463 GtkTextIter start, end;
1466 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1469 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1471 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1472 gtk_text_iter_equal (&pos, &end) ||
1473 empathy_spell_check (str)) {
1474 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1476 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1481 } while (gtk_text_iter_forward_word_end (&iter) &&
1482 gtk_text_iter_compare (&iter, location) <= 0);
1486 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1491 GtkTextIter word_start, word_end;
1493 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1494 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1495 &word_start, &word_end);
1500 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1503 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1504 chat_composing_stop (chat);
1506 chat_composing_start (chat);
1511 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1516 GtkTextIter prev_pos;
1517 GtkTextIter word_start;
1518 GtkTextIter word_end;
1522 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1524 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1525 gtk_text_buffer_get_insert (buffer));
1526 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1528 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1531 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1532 !gtk_text_iter_equal (&pos, &word_end)) {
1533 str = gtk_text_buffer_get_text (buffer,
1534 &word_start, &word_end, FALSE);
1536 if (!empathy_spell_check (str)) {
1537 gtk_text_buffer_apply_tag_by_name (buffer,
1538 "misspelled", &word_start, &word_end);
1540 gtk_text_buffer_remove_tag_by_name (buffer,
1541 "misspelled", &word_start, &word_end);
1548 gtk_text_buffer_move_mark (buffer, mark, &pos);
1552 empathy_isspace_cb (gunichar c,
1555 return g_unichar_isspace (c);
1559 chat_input_key_press_event_cb (GtkWidget *widget,
1563 EmpathyChatPriv *priv;
1566 GtkWidget *text_view_sw;
1568 priv = GET_PRIV (chat);
1570 /* Catch ctrl+up/down so we can traverse messages we sent */
1571 if ((event->state & GDK_CONTROL_MASK) &&
1572 (event->keyval == GDK_KEY_Up ||
1573 event->keyval == GDK_KEY_Down)) {
1574 GtkTextBuffer *buffer;
1577 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1578 chat_input_history_update (chat, buffer);
1580 if (event->keyval == GDK_KEY_Up) {
1581 str = chat_input_history_get_next (chat);
1583 str = chat_input_history_get_prev (chat);
1586 g_signal_handlers_block_by_func (buffer,
1587 chat_input_text_buffer_changed_cb,
1589 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1590 g_signal_handlers_unblock_by_func (buffer,
1591 chat_input_text_buffer_changed_cb,
1597 /* Catch enter but not ctrl/shift-enter */
1598 if (IS_ENTER (event->keyval) &&
1599 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1602 /* This is to make sure that kinput2 gets the enter. And if
1603 * it's handled there we shouldn't send on it. This is because
1604 * kinput2 uses Enter to commit letters. See:
1605 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1608 view = GTK_TEXT_VIEW (chat->input_text_view);
1609 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1610 gtk_text_view_reset_im_context (view);
1614 chat_input_text_view_send (chat);
1618 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1620 if (IS_ENTER (event->keyval) &&
1621 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1622 /* Newline for shift/control-enter. */
1625 if (!(event->state & GDK_CONTROL_MASK) &&
1626 event->keyval == GDK_KEY_Page_Up) {
1627 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1628 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1631 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1632 event->keyval == GDK_KEY_Page_Down) {
1633 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1634 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1635 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1636 gtk_adjustment_set_value (adj, val);
1639 if (event->keyval == GDK_KEY_Escape) {
1640 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1642 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1643 event->keyval == GDK_KEY_Tab) {
1644 GtkTextBuffer *buffer;
1645 GtkTextIter start, current;
1646 gchar *nick, *completed;
1647 GList *list, *completed_list;
1648 gboolean is_start_of_buffer;
1650 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1651 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1653 /* Get the start of the nick to complete. */
1654 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1655 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1656 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1658 is_start_of_buffer = gtk_text_iter_is_start (&start);
1660 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1661 g_completion_add_items (priv->completion, list);
1663 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1664 completed_list = g_completion_complete (priv->completion,
1673 GString *message = NULL;
1676 gtk_text_buffer_delete (buffer, &start, ¤t);
1678 len = g_list_length (completed_list);
1681 /* If we only have one hit, use that text
1682 * instead of the text in completed since the
1683 * completed text will use the typed string
1684 * which might be cased all wrong.
1687 text = empathy_contact_get_alias (completed_list->data);
1691 /* Print all hits to the scrollback view, so the
1692 * user knows what possibilities he has.
1695 message = g_string_new ("");
1696 for (l = completed_list; l != NULL; l = l->next) {
1697 g_string_append (message, empathy_contact_get_alias (l->data));
1698 g_string_append (message, " - ");
1700 empathy_chat_view_append_event (chat->view, message->str);
1701 g_string_free (message, TRUE);
1704 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1706 if (len == 1 && is_start_of_buffer) {
1707 gchar *complete_char;
1709 complete_char = g_settings_get_string (
1710 priv->gsettings_chat,
1711 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1713 if (complete_char != NULL) {
1714 gtk_text_buffer_insert_at_cursor (buffer,
1716 strlen (complete_char));
1717 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1718 g_free (complete_char);
1725 g_completion_clear_items (priv->completion);
1727 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1737 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1741 gtk_widget_grab_focus (chat->input_text_view);
1747 chat_input_realize_cb (GtkWidget *widget,
1750 DEBUG ("Setting focus to the input text view");
1751 if (gtk_widget_is_sensitive (widget)) {
1752 gtk_widget_grab_focus (widget);
1757 chat_input_has_focus_notify_cb (GtkWidget *widget,
1761 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1765 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1766 EmpathySmiley *smiley,
1769 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1770 GtkTextBuffer *buffer;
1773 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1775 gtk_text_buffer_get_end_iter (buffer, &iter);
1776 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1778 gtk_text_buffer_get_end_iter (buffer, &iter);
1779 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1790 static EmpathyChatSpell *
1791 chat_spell_new (EmpathyChat *chat,
1796 EmpathyChatSpell *chat_spell;
1798 chat_spell = g_slice_new0 (EmpathyChatSpell);
1800 chat_spell->chat = g_object_ref (chat);
1801 chat_spell->word = g_strdup (word);
1802 chat_spell->start = start;
1803 chat_spell->end = end;
1809 chat_spell_free (EmpathyChatSpell *chat_spell)
1811 g_object_unref (chat_spell->chat);
1812 g_free (chat_spell->word);
1813 g_slice_free (EmpathyChatSpell, chat_spell);
1817 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1818 EmpathyChatSpell *chat_spell)
1820 empathy_chat_correct_word (chat_spell->chat,
1821 &(chat_spell->start),
1823 gtk_menu_item_get_label (menu_item));
1828 chat_spelling_build_suggestions_menu (const gchar *code,
1829 EmpathyChatSpell *chat_spell)
1831 GList *suggestions, *l;
1832 GtkWidget *menu, *menu_item;
1834 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1835 if (suggestions == NULL)
1838 menu = gtk_menu_new ();
1839 for (l = suggestions; l; l = l->next) {
1840 menu_item = gtk_menu_item_new_with_label (l->data);
1841 g_signal_connect (G_OBJECT (menu_item), "activate",
1842 G_CALLBACK (chat_spelling_menu_activate_cb),
1844 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1846 empathy_spell_free_suggestions (suggestions);
1848 gtk_widget_show_all (menu);
1854 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1856 GtkWidget *menu, *submenu, *item;
1859 codes = empathy_spell_get_enabled_language_codes ();
1860 g_assert (codes != NULL);
1862 if (g_list_length (codes) > 1) {
1863 menu = gtk_menu_new ();
1865 for (l = codes; l; l = l->next) {
1866 const gchar *code, *name;
1869 name = empathy_spell_get_language_name (code);
1873 item = gtk_image_menu_item_new_with_label (name);
1875 submenu = chat_spelling_build_suggestions_menu (
1877 if (submenu == NULL)
1878 gtk_widget_set_sensitive (item, FALSE);
1880 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1882 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1885 menu = chat_spelling_build_suggestions_menu (codes->data,
1888 menu = gtk_menu_new ();
1889 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1890 gtk_widget_set_sensitive (item, FALSE);
1891 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1894 g_list_free (codes);
1896 gtk_widget_show_all (menu);
1907 static EmpathyChatWord *
1908 chat_word_new (EmpathyChat *chat,
1912 EmpathyChatWord *chat_word;
1914 chat_word = g_slice_new0 (EmpathyChatWord);
1916 chat_word->chat = g_object_ref (chat);
1917 chat_word->word = g_strdup (word);
1918 chat_word->code = g_strdup (code);
1924 chat_word_free (EmpathyChatWord *chat_word)
1926 g_object_unref (chat_word->chat);
1927 g_free (chat_word->word);
1928 g_free (chat_word->code);
1929 g_slice_free (EmpathyChatWord, chat_word);
1933 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1934 EmpathyChatWord *chat_word)
1936 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1938 empathy_spell_add_to_dictionary (chat_word->code,
1940 priv->update_misspelled_words_id =
1941 g_idle_add (update_misspelled_words, chat_word->chat);
1945 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1947 GtkWidget *menu, *item, *lang_item, *image;
1950 const gchar *code, *name;
1951 EmpathyChatWord *chat_word;
1953 codes = empathy_spell_get_enabled_language_codes ();
1954 g_assert (codes != NULL);
1955 if (g_list_length (codes) > 1) {
1956 /* translators: %s is the selected word */
1957 label = g_strdup_printf (_("Add '%s' to Dictionary"),
1959 item = gtk_image_menu_item_new_with_mnemonic (label);
1961 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1962 GTK_ICON_SIZE_MENU);
1963 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1966 menu = gtk_menu_new ();
1968 for (l = codes; l; l = l->next) {
1970 name = empathy_spell_get_language_name (code);
1974 lang_item = gtk_image_menu_item_new_with_label (name);
1976 chat_word= chat_word_new (chat_spell->chat,
1977 chat_spell->word, code);
1978 g_object_set_data_full (G_OBJECT (lang_item),
1979 "chat-word", chat_word,
1980 (GDestroyNotify) chat_word_free);
1982 g_signal_connect (G_OBJECT (lang_item), "activate",
1983 G_CALLBACK (chat_add_to_dictionary_activate_cb),
1985 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
1987 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
1990 name = empathy_spell_get_language_name (code);
1991 g_assert (name != NULL);
1992 /* translators: first %s is the selected word,
1993 * second %s is the language name of the target dictionary */
1994 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
1995 chat_spell->word, name);
1996 item = gtk_image_menu_item_new_with_mnemonic (label);
1998 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1999 GTK_ICON_SIZE_MENU);
2000 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2002 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2004 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2005 (GDestroyNotify) chat_word_free);
2007 g_signal_connect (G_OBJECT (item), "activate",
2008 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2011 g_list_free (codes);
2013 gtk_widget_show_all (item);
2019 chat_text_send_cb (GtkMenuItem *menuitem,
2022 chat_input_text_view_send (chat);
2026 chat_input_populate_popup_cb (GtkTextView *view,
2030 GtkTextBuffer *buffer;
2031 GtkTextTagTable *table;
2034 GtkTextIter iter, start, end;
2037 EmpathyChatSpell *chat_spell;
2038 GtkWidget *spell_menu;
2039 GtkWidget *spell_item;
2040 EmpathySmileyManager *smiley_manager;
2041 GtkWidget *smiley_menu;
2044 buffer = gtk_text_view_get_buffer (view);
2046 /* Add the emoticon menu. */
2047 item = gtk_separator_menu_item_new ();
2048 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2049 gtk_widget_show (item);
2051 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2052 image = gtk_image_new_from_icon_name ("face-smile",
2053 GTK_ICON_SIZE_MENU);
2054 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2055 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2056 gtk_widget_show (item);
2058 smiley_manager = empathy_smiley_manager_dup_singleton ();
2059 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2060 chat_insert_smiley_activate_cb,
2062 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2063 g_object_unref (smiley_manager);
2065 /* Add the Send menu item. */
2066 gtk_text_buffer_get_bounds (buffer, &start, &end);
2067 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2068 if (!EMP_STR_EMPTY (str)) {
2069 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2070 g_signal_connect (G_OBJECT (item), "activate",
2071 G_CALLBACK (chat_text_send_cb), chat);
2072 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2073 gtk_widget_show (item);
2077 /* Add the spell check menu item. */
2078 table = gtk_text_buffer_get_tag_table (buffer);
2079 tag = gtk_text_tag_table_lookup (table, "misspelled");
2080 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2081 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2082 GTK_TEXT_WINDOW_WIDGET,
2085 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2087 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2088 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2090 str = gtk_text_buffer_get_text (buffer,
2091 &start, &end, FALSE);
2093 if (!EMP_STR_EMPTY (str)) {
2094 chat_spell = chat_spell_new (chat, str, start, end);
2095 g_object_set_data_full (G_OBJECT (menu),
2096 "chat-spell", chat_spell,
2097 (GDestroyNotify) chat_spell_free);
2099 item = gtk_separator_menu_item_new ();
2100 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2101 gtk_widget_show (item);
2103 /* Spelling suggestions */
2104 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2105 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2106 GTK_ICON_SIZE_MENU);
2107 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2109 spell_menu = chat_spelling_build_menu (chat_spell);
2110 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2113 spell_item = gtk_separator_menu_item_new ();
2114 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2115 gtk_widget_show (spell_item);
2117 /* Add to dictionary */
2118 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2120 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2121 gtk_widget_show (spell_item);
2123 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2124 gtk_widget_show (item);
2130 chat_log_filter (TplEvent *log,
2133 EmpathyChat *chat = user_data;
2134 EmpathyMessage *message;
2135 EmpathyChatPriv *priv = GET_PRIV (chat);
2136 const GList *pending;
2138 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2139 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2141 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2142 message = empathy_message_from_tpl_log_event (log);
2144 for (; pending; pending = g_list_next (pending)) {
2145 if (empathy_message_equal (message, pending->data)) {
2146 g_object_unref (message);
2151 g_object_unref (message);
2157 show_pending_messages (EmpathyChat *chat) {
2158 EmpathyChatPriv *priv = GET_PRIV (chat);
2159 const GList *messages, *l;
2161 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2163 if (chat->view == NULL || priv->tp_chat == NULL)
2166 if (!priv->can_show_pending)
2169 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2171 for (l = messages; l != NULL ; l = g_list_next (l)) {
2172 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2173 chat_message_received (chat, message, TRUE);
2179 got_filtered_messages_cb (GObject *manager,
2180 GAsyncResult *result,
2185 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2186 EmpathyChatPriv *priv = GET_PRIV (chat);
2187 GError *error = NULL;
2189 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2190 result, &messages, &error)) {
2191 DEBUG ("%s. Aborting.", error->message);
2192 empathy_chat_view_append_event (chat->view,
2193 _("Failed to retrieve recent logs"));
2194 g_error_free (error);
2198 for (l = messages; l; l = g_list_next (l)) {
2199 EmpathyMessage *message;
2200 g_assert (TPL_IS_EVENT (l->data));
2202 message = empathy_message_from_tpl_log_event (l->data);
2203 g_object_unref (l->data);
2205 empathy_chat_view_append_message (chat->view, message);
2206 g_object_unref (message);
2208 g_list_free (messages);
2211 /* in case of TPL error, skip backlog and show pending messages */
2212 priv->can_show_pending = TRUE;
2213 show_pending_messages (chat);
2215 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2216 * about it in EmpathyChatPriv definition */
2217 priv->retrieving_backlogs = FALSE;
2218 empathy_chat_messages_read (chat);
2220 /* Turn back on scrolling */
2221 empathy_chat_view_scroll (chat->view, TRUE);
2225 chat_add_logs (EmpathyChat *chat)
2227 EmpathyChatPriv *priv = GET_PRIV (chat);
2234 /* Turn off scrolling temporarily */
2235 empathy_chat_view_scroll (chat->view, FALSE);
2237 /* Add messages from last conversation */
2238 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2239 target = tpl_entity_new_from_room_id (priv->id);
2241 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2243 priv->retrieving_backlogs = TRUE;
2244 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2247 TPL_EVENT_MASK_TEXT,
2251 got_filtered_messages_cb,
2254 g_object_unref (target);
2258 chat_contacts_completion_func (const gchar *s1,
2262 gchar *tmp, *nick1, *nick2;
2269 return s1 ? -1 : +1;
2272 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2273 nick1 = g_utf8_casefold (tmp, -1);
2276 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2277 nick2 = g_utf8_casefold (tmp, -1);
2280 ret = strncmp (nick1, nick2, n);
2289 build_part_message (guint reason,
2291 EmpathyContact *actor,
2292 const gchar *message)
2294 GString *s = g_string_new ("");
2295 const gchar *actor_name = NULL;
2297 if (actor != NULL) {
2298 actor_name = empathy_contact_get_alias (actor);
2301 /* Having an actor only really makes sense for a few actions... */
2303 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2304 g_string_append_printf (s, _("%s has disconnected"), name);
2306 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2307 if (actor_name != NULL) {
2308 /* translators: reverse the order of these arguments
2309 * if the kicked should come before the kicker in your locale.
2311 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2314 g_string_append_printf (s, _("%s was kicked"), name);
2317 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2318 if (actor_name != NULL) {
2319 /* translators: reverse the order of these arguments
2320 * if the banned should come before the banner in your locale.
2322 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2325 g_string_append_printf (s, _("%s was banned"), name);
2329 g_string_append_printf (s, _("%s has left the room"), name);
2332 if (!EMP_STR_EMPTY (message)) {
2333 /* Note to translators: this string is appended to
2334 * notifications like "foo has left the room", with the message
2335 * given by the user living the room. If this poses a problem,
2336 * please let us know. :-)
2338 g_string_append_printf (s, _(" (%s)"), message);
2341 return g_string_free (s, FALSE);
2345 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2346 EmpathyContact *contact,
2347 EmpathyContact *actor,
2353 EmpathyChatPriv *priv = GET_PRIV (chat);
2354 const gchar *name = empathy_contact_get_alias (contact);
2357 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2359 if (priv->block_events_timeout_id != 0)
2363 str = g_strdup_printf (_("%s has joined the room"),
2366 str = build_part_message (reason, name, actor, message);
2369 empathy_chat_view_append_event (chat->view, str);
2374 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2375 EmpathyContact *old_contact,
2376 EmpathyContact *new_contact,
2381 EmpathyChatPriv *priv = GET_PRIV (chat);
2383 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2385 if (priv->block_events_timeout_id == 0) {
2388 str = g_strdup_printf (_("%s is now known as %s"),
2389 empathy_contact_get_alias (old_contact),
2390 empathy_contact_get_alias (new_contact));
2391 empathy_chat_view_append_event (chat->view, str);
2398 chat_reset_size_request (gpointer widget)
2400 gtk_widget_set_size_request (widget, -1, -1);
2406 chat_update_contacts_visibility (EmpathyChat *chat,
2409 EmpathyChatPriv *priv = GET_PRIV (chat);
2410 GtkAllocation allocation;
2412 if (!priv->scrolled_window_contacts) {
2416 if (priv->remote_contact != NULL) {
2420 if (show && priv->contact_list_view == NULL) {
2421 EmpathyContactListStore *store;
2424 /* We are adding the contact list to the chat, we don't want the
2425 * chat view to become too small. If the chat view is already
2426 * smaller than 250 make sure that size won't change. If the
2427 * chat view is bigger the contact list will take some space on
2428 * it but we make sure the chat view don't become smaller than
2429 * 250. Relax the size request once the resize is done */
2430 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2431 min_width = MIN (allocation.width, 250);
2432 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2433 g_idle_add (chat_reset_size_request, priv->vbox_left);
2435 if (priv->contacts_width > 0) {
2436 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2437 priv->contacts_width);
2440 store = empathy_contact_list_store_new (
2441 EMPATHY_CONTACT_LIST (priv->tp_chat));
2442 empathy_contact_list_store_set_show_groups (
2443 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2445 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2446 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2447 EMPATHY_CONTACT_FEATURE_CHAT |
2448 EMPATHY_CONTACT_FEATURE_CALL |
2449 EMPATHY_CONTACT_FEATURE_LOG |
2450 EMPATHY_CONTACT_FEATURE_INFO));
2451 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2452 priv->contact_list_view);
2453 gtk_widget_show (priv->contact_list_view);
2454 gtk_widget_show (priv->scrolled_window_contacts);
2455 g_object_unref (store);
2457 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2458 gtk_widget_hide (priv->scrolled_window_contacts);
2459 if (priv->contact_list_view != NULL) {
2460 gtk_widget_destroy (priv->contact_list_view);
2461 priv->contact_list_view = NULL;
2467 empathy_chat_set_show_contacts (EmpathyChat *chat,
2470 EmpathyChatPriv *priv = GET_PRIV (chat);
2472 priv->show_contacts = show;
2474 chat_update_contacts_visibility (chat, show);
2476 g_object_notify (G_OBJECT (chat), "show-contacts");
2480 chat_remote_contact_changed_cb (EmpathyChat *chat)
2482 EmpathyChatPriv *priv = GET_PRIV (chat);
2484 if (priv->remote_contact != NULL) {
2485 g_object_unref (priv->remote_contact);
2486 priv->remote_contact = NULL;
2491 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2492 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2493 if (priv->remote_contact != NULL) {
2494 g_object_ref (priv->remote_contact);
2495 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2497 else if (priv->tp_chat != NULL) {
2500 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2501 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2504 chat_update_contacts_visibility (chat, priv->show_contacts);
2506 g_object_notify (G_OBJECT (chat), "remote-contact");
2507 g_object_notify (G_OBJECT (chat), "id");
2511 chat_destroy_cb (EmpathyTpChat *tp_chat,
2514 EmpathyChatPriv *priv;
2516 priv = GET_PRIV (chat);
2518 if (!priv->tp_chat) {
2522 chat_composing_remove_timeout (chat);
2523 g_object_unref (priv->tp_chat);
2524 priv->tp_chat = NULL;
2525 g_object_notify (G_OBJECT (chat), "tp-chat");
2527 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2528 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2530 chat_update_contacts_visibility (chat, FALSE);
2534 update_misspelled_words (gpointer data)
2536 EmpathyChat *chat = EMPATHY_CHAT (data);
2537 EmpathyChatPriv *priv = GET_PRIV (chat);
2538 GtkTextBuffer *buffer;
2542 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2544 gtk_text_buffer_get_end_iter (buffer, &iter);
2545 length = gtk_text_iter_get_offset (&iter);
2546 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2547 NULL, length, chat);
2549 priv->update_misspelled_words_id = 0;
2555 conf_spell_checking_cb (GSettings *gsettings_chat,
2559 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2560 EmpathyChatPriv *priv = GET_PRIV (chat);
2561 gboolean spell_checker;
2562 GtkTextBuffer *buffer;
2564 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2567 spell_checker = g_settings_get_boolean (gsettings_chat,
2568 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2570 if (!empathy_spell_supported ()) {
2571 spell_checker = FALSE;
2574 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2576 if (spell_checker == priv->spell_checking_enabled) {
2577 if (spell_checker) {
2578 /* Possibly changed dictionaries,
2579 * update misspelled words. Need to do so in idle
2580 * so the spell checker is updated. */
2581 priv->update_misspelled_words_id =
2582 g_idle_add (update_misspelled_words, chat);
2588 if (spell_checker) {
2591 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2592 buffer, "notify::cursor-position",
2593 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2595 priv->insert_text_id = tp_g_signal_connect_object (
2596 buffer, "insert-text",
2597 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2598 chat, G_CONNECT_AFTER);
2599 priv->delete_range_id = tp_g_signal_connect_object (
2600 buffer, "delete-range",
2601 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2602 chat, G_CONNECT_AFTER);
2604 gtk_text_buffer_create_tag (buffer, "misspelled",
2605 "underline", PANGO_UNDERLINE_ERROR,
2608 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2609 gtk_text_buffer_get_insert (buffer));
2610 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2613 /* Mark misspelled words in the existing buffer.
2614 * Need to do so in idle so the spell checker is updated. */
2615 priv->update_misspelled_words_id =
2616 g_idle_add (update_misspelled_words, chat);
2618 GtkTextTagTable *table;
2621 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2622 priv->notify_cursor_position_id = 0;
2623 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2624 priv->insert_text_id = 0;
2625 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2626 priv->delete_range_id = 0;
2628 table = gtk_text_buffer_get_tag_table (buffer);
2629 tag = gtk_text_tag_table_lookup (table, "misspelled");
2630 gtk_text_tag_table_remove (table, tag);
2632 gtk_text_buffer_delete_mark_by_name (buffer,
2633 "previous-cursor-position");
2636 priv->spell_checking_enabled = spell_checker;
2640 save_paned_pos_timeout (gpointer data)
2642 EmpathyChat *self = data;
2645 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2647 g_settings_set_int (self->priv->gsettings_ui,
2648 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2655 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2659 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2661 if (chat->priv->save_paned_pos_id != 0)
2662 g_source_remove (chat->priv->save_paned_pos_id);
2664 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2665 save_paned_pos_timeout, chat);
2671 chat_create_ui (EmpathyChat *chat)
2673 EmpathyChatPriv *priv = GET_PRIV (chat);
2677 GtkTextBuffer *buffer;
2679 EmpathyThemeManager *theme_mgr;
2681 filename = empathy_file_lookup ("empathy-chat.ui",
2683 gui = empathy_builder_get_file (filename,
2684 "chat_widget", &priv->widget,
2685 "hpaned", &priv->hpaned,
2686 "vbox_left", &priv->vbox_left,
2687 "scrolled_window_chat", &priv->scrolled_window_chat,
2688 "scrolled_window_input", &priv->scrolled_window_input,
2689 "hbox_topic", &priv->hbox_topic,
2690 "expander_topic", &priv->expander_topic,
2691 "label_topic", &priv->label_topic,
2692 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2693 "info_bar_vbox", &priv->info_bar_vbox,
2696 empathy_builder_connect (gui, chat,
2697 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2698 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2703 /* Add message view. */
2704 theme_mgr = empathy_theme_manager_dup_singleton ();
2705 chat->view = empathy_theme_manager_create_view (theme_mgr);
2706 g_object_unref (theme_mgr);
2707 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2708 and other types, even though it's non-editable and doesn't accept any
2709 drags. This steals drag motion for anything inside the scrollbars,
2710 making drag destinations on chat windows far less useful.
2712 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2713 g_signal_connect (chat->view, "focus_in_event",
2714 G_CALLBACK (chat_text_view_focus_in_event_cb),
2716 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2717 GTK_WIDGET (chat->view));
2718 gtk_widget_show (GTK_WIDGET (chat->view));
2720 /* Add input GtkTextView */
2721 chat->input_text_view = empathy_input_text_view_new ();
2722 g_signal_connect (chat->input_text_view, "notify::has-focus",
2723 G_CALLBACK (chat_input_has_focus_notify_cb),
2725 g_signal_connect (chat->input_text_view, "key-press-event",
2726 G_CALLBACK (chat_input_key_press_event_cb),
2728 g_signal_connect (chat->input_text_view, "realize",
2729 G_CALLBACK (chat_input_realize_cb),
2731 g_signal_connect (chat->input_text_view, "populate-popup",
2732 G_CALLBACK (chat_input_populate_popup_cb),
2734 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2735 tp_g_signal_connect_object (buffer, "changed",
2736 G_CALLBACK (chat_input_text_buffer_changed_cb),
2738 tp_g_signal_connect_object (priv->gsettings_chat,
2739 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2740 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2741 conf_spell_checking_cb (priv->gsettings_chat,
2742 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2743 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2744 chat->input_text_view);
2745 gtk_widget_show (chat->input_text_view);
2747 /* Add the (invisible) search bar */
2748 priv->search_bar = empathy_search_bar_new (chat->view);
2749 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2752 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2754 /* Initialy hide the topic, will be shown if not empty */
2755 gtk_widget_hide (priv->hbox_topic);
2757 g_signal_connect (priv->hpaned, "notify::position",
2758 G_CALLBACK (chat_hpaned_pos_changed_cb),
2761 /* Load the paned position */
2762 paned_pos = g_settings_get_int (priv->gsettings_ui,
2763 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2765 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2767 /* Set widget focus order */
2768 list = g_list_append (NULL, priv->search_bar);
2769 list = g_list_append (list, priv->scrolled_window_input);
2770 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2773 list = g_list_append (NULL, priv->vbox_left);
2774 list = g_list_append (list, priv->scrolled_window_contacts);
2775 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2778 list = g_list_append (NULL, priv->hpaned);
2779 list = g_list_append (list, priv->hbox_topic);
2780 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2783 /* Add the main widget in the chat widget */
2784 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2785 g_object_unref (gui);
2789 chat_size_allocate (GtkWidget *widget,
2790 GtkAllocation *allocation)
2792 GtkBin *bin = GTK_BIN (widget);
2793 GtkAllocation child_allocation;
2796 gtk_widget_set_allocation (widget, allocation);
2798 child = gtk_bin_get_child (bin);
2800 if (child && gtk_widget_get_visible (child))
2802 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2803 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2804 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2805 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2807 gtk_widget_size_allocate (child, &child_allocation);
2812 chat_finalize (GObject *object)
2815 EmpathyChatPriv *priv;
2817 chat = EMPATHY_CHAT (object);
2818 priv = GET_PRIV (chat);
2820 DEBUG ("Finalized: %p", object);
2822 if (priv->update_misspelled_words_id != 0)
2823 g_source_remove (priv->update_misspelled_words_id);
2825 if (priv->save_paned_pos_id != 0)
2826 g_source_remove (priv->save_paned_pos_id);
2828 g_object_unref (priv->gsettings_chat);
2829 g_object_unref (priv->gsettings_ui);
2831 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2832 g_list_free (priv->input_history);
2834 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2835 g_list_free (priv->compositors);
2837 chat_composing_remove_timeout (chat);
2839 g_object_unref (priv->account_manager);
2840 g_object_unref (priv->log_manager);
2842 if (priv->tp_chat) {
2843 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2844 chat_destroy_cb, chat);
2845 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2846 chat_message_received_cb, chat);
2847 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2848 chat_send_error_cb, chat);
2849 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2850 chat_state_changed_cb, chat);
2851 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2852 chat_property_changed_cb, chat);
2853 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2854 chat_members_changed_cb, chat);
2855 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2856 chat_remote_contact_changed_cb, chat);
2857 empathy_tp_chat_leave (priv->tp_chat, "");
2858 g_object_unref (priv->tp_chat);
2860 if (priv->account) {
2861 g_object_unref (priv->account);
2863 if (priv->remote_contact) {
2864 g_object_unref (priv->remote_contact);
2867 if (priv->block_events_timeout_id) {
2868 g_source_remove (priv->block_events_timeout_id);
2872 g_free (priv->name);
2873 g_free (priv->subject);
2874 g_completion_free (priv->completion);
2876 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2880 chat_constructed (GObject *object)
2882 EmpathyChat *chat = EMPATHY_CHAT (object);
2883 EmpathyChatPriv *priv = GET_PRIV (chat);
2885 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2886 /* First display logs from the logger and then display pending messages */
2887 chat_add_logs (chat);
2890 /* Just display pending messages for rooms */
2891 priv->can_show_pending = TRUE;
2892 show_pending_messages (chat);
2897 empathy_chat_class_init (EmpathyChatClass *klass)
2899 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2900 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2902 object_class->finalize = chat_finalize;
2903 object_class->get_property = chat_get_property;
2904 object_class->set_property = chat_set_property;
2905 object_class->constructed = chat_constructed;
2907 widget_class->size_allocate = chat_size_allocate;
2909 g_object_class_install_property (object_class,
2911 g_param_spec_object ("tp-chat",
2913 "The tp chat object",
2914 EMPATHY_TYPE_TP_CHAT,
2917 G_PARAM_STATIC_STRINGS));
2918 g_object_class_install_property (object_class,
2920 g_param_spec_object ("account",
2921 "Account of the chat",
2922 "The account of the chat",
2925 G_PARAM_STATIC_STRINGS));
2926 g_object_class_install_property (object_class,
2928 g_param_spec_string ("id",
2930 "The id of the chat",
2933 G_PARAM_STATIC_STRINGS));
2934 g_object_class_install_property (object_class,
2936 g_param_spec_string ("name",
2938 "The name of the chat",
2941 G_PARAM_STATIC_STRINGS));
2942 g_object_class_install_property (object_class,
2944 g_param_spec_string ("subject",
2946 "The subject or topic of the chat",
2949 G_PARAM_STATIC_STRINGS));
2950 g_object_class_install_property (object_class,
2951 PROP_REMOTE_CONTACT,
2952 g_param_spec_object ("remote-contact",
2953 "The remote contact",
2954 "The remote contact is any",
2955 EMPATHY_TYPE_CONTACT,
2957 G_PARAM_STATIC_STRINGS));
2958 g_object_class_install_property (object_class,
2960 g_param_spec_boolean ("show-contacts",
2961 "Contacts' visibility",
2962 "The visibility of the contacts' list",
2965 G_PARAM_STATIC_STRINGS));
2967 g_object_class_install_property (object_class,
2969 g_param_spec_boolean ("sms-channel",
2971 "TRUE if this channel is for sending SMSes",
2973 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
2975 g_object_class_install_property (object_class,
2976 PROP_N_MESSAGES_SENDING,
2977 g_param_spec_uint ("n-messages-sending",
2978 "Num Messages Sending",
2979 "The number of messages being sent",
2981 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
2983 signals[COMPOSING] =
2984 g_signal_new ("composing",
2985 G_OBJECT_CLASS_TYPE (object_class),
2989 g_cclosure_marshal_VOID__BOOLEAN,
2993 signals[NEW_MESSAGE] =
2994 g_signal_new ("new-message",
2995 G_OBJECT_CLASS_TYPE (object_class),
2999 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3001 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3003 signals[PART_COMMAND_ENTERED] =
3004 g_signal_new ("part-command-entered",
3005 G_OBJECT_CLASS_TYPE (object_class),
3009 g_cclosure_marshal_VOID__POINTER,
3013 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3017 chat_block_events_timeout_cb (gpointer data)
3019 EmpathyChatPriv *priv = GET_PRIV (data);
3021 priv->block_events_timeout_id = 0;
3027 account_manager_prepared_cb (GObject *source_object,
3028 GAsyncResult *result,
3031 GList *accounts, *l;
3032 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3033 EmpathyChat *chat = user_data;
3034 GError *error = NULL;
3036 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3037 DEBUG ("Failed to prepare the account manager: %s", error->message);
3038 g_error_free (error);
3042 accounts = tp_account_manager_get_valid_accounts (account_manager);
3044 for (l = accounts; l != NULL; l = l->next) {
3045 TpAccount *account = l->data;
3046 tp_g_signal_connect_object (account, "status-changed",
3047 G_CALLBACK (chat_new_connection_cb),
3051 g_list_free (accounts);
3055 empathy_chat_init (EmpathyChat *chat)
3057 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3058 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3061 priv->log_manager = tpl_log_manager_dup_singleton ();
3062 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3063 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3065 priv->contacts_width = -1;
3066 priv->input_history = NULL;
3067 priv->input_history_current = NULL;
3068 priv->account_manager = tp_account_manager_dup ();
3070 tp_account_manager_prepare_async (priv->account_manager, NULL,
3071 account_manager_prepared_cb, chat);
3073 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3074 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3076 /* Block events for some time to avoid having "has come online" or
3077 * "joined" messages. */
3078 priv->block_events_timeout_id =
3079 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3081 /* Add nick name completion */
3082 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3083 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3085 chat_create_ui (chat);
3089 empathy_chat_new (EmpathyTpChat *tp_chat)
3091 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3095 empathy_chat_get_tp_chat (EmpathyChat *chat)
3097 EmpathyChatPriv *priv = GET_PRIV (chat);
3099 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3101 return priv->tp_chat;
3107 GtkWidget *info_bar;
3117 passwd_remember_button_cb (GtkButton *button,
3120 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3124 passwd_not_now_button_cb (GtkButton *button,
3127 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3131 remember_password_infobar_response_cb (GtkWidget *info_bar,
3135 EmpathyChatPriv *priv = GET_PRIV (data->self);
3137 if (response_id == GTK_RESPONSE_OK) {
3138 DEBUG ("Saving room password");
3139 empathy_keyring_set_room_password_async (priv->account,
3140 empathy_tp_chat_get_id (priv->tp_chat),
3145 gtk_widget_destroy (info_bar);
3146 g_free (data->password);
3147 g_slice_free (PasswordData, data);
3151 chat_prompt_to_save_password (EmpathyChat *self,
3154 GtkWidget *content_area;
3161 /* save the password in case it needs to be saved */
3162 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3164 /* Remove all previous widgets */
3165 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3166 gtk_container_forall (GTK_CONTAINER (content_area),
3167 (GtkCallback) gtk_widget_destroy, NULL);
3168 data->button = NULL;
3171 data->spinner = NULL;
3173 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3174 GTK_MESSAGE_QUESTION);
3176 hbox = gtk_hbox_new (FALSE, 5);
3177 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3180 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3181 GTK_ICON_SIZE_DIALOG);
3182 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3185 label = gtk_label_new (_("Would you like to store this password?"));
3186 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3188 /* Add 'Remember' button */
3189 alig = gtk_alignment_new (0, 0.5, 1, 0);
3191 button = gtk_button_new_with_label (_("Remember"));
3192 gtk_container_add (GTK_CONTAINER (alig), button);
3193 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3195 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3198 /* Add 'Not now' button */
3199 alig = gtk_alignment_new (0, 0.5, 1, 0);
3201 button = gtk_button_new_with_label (_("Not now"));
3202 gtk_container_add (GTK_CONTAINER (alig), button);
3203 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3205 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3209 g_signal_handler_disconnect (data->info_bar, data->response_id);
3210 g_signal_connect (data->info_bar, "response",
3211 G_CALLBACK (remember_password_infobar_response_cb), data);
3213 gtk_widget_show_all (data->info_bar);
3217 provide_password_cb (GObject *tp_chat,
3221 PasswordData *data = user_data;
3222 EmpathyChat *self = data->self;
3223 EmpathyChatPriv *priv = GET_PRIV (self);
3224 GError *error = NULL;
3226 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3228 DEBUG ("error: %s", error->message);
3229 /* FIXME: what should we do if that's another error? Close the channel?
3230 * Display the raw D-Bus error to the user isn't very useful */
3231 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3233 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3234 gtk_widget_set_sensitive (data->entry, TRUE);
3235 gtk_widget_grab_focus (data->entry);
3238 gtk_info_bar_set_message_type (
3239 GTK_INFO_BAR (data->info_bar),
3243 gtk_widget_set_sensitive (data->button, TRUE);
3244 gtk_button_set_label (GTK_BUTTON (data->button),
3248 gtk_label_set_text (GTK_LABEL (data->label),
3249 _("Wrong password; please try again:"));
3252 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3253 gtk_widget_hide (data->spinner);
3255 g_error_free (error);
3259 if (empathy_keyring_is_available ()) {
3260 /* ask whether they want to save the password */
3261 chat_prompt_to_save_password (self, data);
3263 /* Get rid of the password info bar finally */
3264 gtk_widget_destroy (data->info_bar);
3265 g_slice_free (PasswordData, data);
3269 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3270 gtk_widget_grab_focus (self->input_text_view);
3274 password_infobar_response_cb (GtkWidget *info_bar,
3278 EmpathyChatPriv *priv = GET_PRIV (data->self);
3279 const gchar *password;
3281 if (response_id != GTK_RESPONSE_OK) {
3282 gtk_widget_destroy (info_bar);
3283 g_slice_free (PasswordData, data);
3287 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3289 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3290 provide_password_cb, data);
3292 gtk_widget_set_sensitive (data->button, FALSE);
3293 gtk_widget_set_sensitive (data->entry, FALSE);
3295 gtk_spinner_start (GTK_SPINNER (data->spinner));
3296 gtk_widget_show (data->spinner);
3300 password_entry_activate_cb (GtkWidget *entry,
3303 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3307 passwd_join_button_cb (GtkButton *button,
3310 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3314 clear_icon_released_cb (GtkEntry *entry,
3315 GtkEntryIconPosition icon_pos,
3319 gtk_entry_set_text (entry, "");
3323 password_entry_changed_cb (GtkEditable *entry,
3328 str = gtk_entry_get_text (GTK_ENTRY (entry));
3330 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3331 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3335 chat_invalidated_cb (TpProxy *proxy,
3339 gpointer password_infobar)
3341 /* Destroy the password infobar whenever a channel is invalidated
3342 * so we don't have multiple infobars when the MUC is rejoined */
3343 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3347 display_password_info_bar (EmpathyChat *self)
3349 EmpathyChatPriv *priv = GET_PRIV (self);
3350 GtkWidget *info_bar;
3351 GtkWidget *content_area;
3361 data = g_slice_new0 (PasswordData);
3363 info_bar = gtk_info_bar_new ();
3364 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3365 GTK_MESSAGE_QUESTION);
3367 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3369 hbox = gtk_hbox_new (FALSE, 5);
3370 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3373 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3374 GTK_ICON_SIZE_DIALOG);
3375 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3378 label = gtk_label_new (_("This room is protected by a password:"));
3379 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3381 /* Add password entry */
3382 entry = gtk_entry_new ();
3383 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3384 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3386 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3387 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3388 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3389 GTK_ENTRY_ICON_SECONDARY, FALSE);
3391 g_signal_connect (entry, "icon-release",
3392 G_CALLBACK (clear_icon_released_cb), data);
3393 g_signal_connect (entry, "changed",
3394 G_CALLBACK (password_entry_changed_cb), data);
3396 g_signal_connect (entry, "activate",
3397 G_CALLBACK (password_entry_activate_cb), data);
3399 /* Focus the password entry once it's realized */
3400 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3402 /* Add 'Join' button */
3403 alig = gtk_alignment_new (0, 0.5, 1, 0);
3405 button = gtk_button_new_with_label (_("Join"));
3406 gtk_container_add (GTK_CONTAINER (alig), button);
3407 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3409 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3413 spinner = gtk_spinner_new ();
3414 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3416 /* Save some data for messing around with later */
3418 data->info_bar = info_bar;
3419 data->button = button;
3420 data->label = label;
3421 data->entry = entry;
3422 data->spinner = spinner;
3424 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3426 gtk_widget_show_all (hbox);
3428 tp_g_signal_connect_object (empathy_tp_chat_get_channel (priv->tp_chat),
3429 "invalidated", G_CALLBACK (chat_invalidated_cb),
3432 data->response_id = g_signal_connect (info_bar, "response",
3433 G_CALLBACK (password_infobar_response_cb), data);
3435 gtk_widget_show_all (info_bar);
3436 /* ... but hide the spinner */
3437 gtk_widget_hide (spinner);
3439 /* prevent the user from typing anything */
3440 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3444 provide_saved_password_cb (GObject *tp_chat,
3448 EmpathyChat *self = user_data;
3449 EmpathyChatPriv *priv = GET_PRIV (self);
3450 GError *error = NULL;
3452 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3454 DEBUG ("error: %s", error->message);
3455 /* FIXME: what should we do if that's another error? Close the channel?
3456 * Display the raw D-Bus error to the user isn't very useful */
3457 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3458 display_password_info_bar (self);
3459 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3461 g_error_free (error);
3466 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3467 gtk_widget_grab_focus (self->input_text_view);
3471 chat_room_got_password_cb (GObject *source,
3472 GAsyncResult *result,
3475 EmpathyChat *self = user_data;
3476 EmpathyChatPriv *priv = GET_PRIV (self);
3477 const gchar *password;
3478 GError *error = NULL;
3480 password = empathy_keyring_get_room_password_finish (priv->account,
3483 if (error != NULL) {
3484 DEBUG ("Couldn't get room password: %s\n", error->message);
3485 g_clear_error (&error);
3487 display_password_info_bar (self);
3488 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3492 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3493 provide_saved_password_cb, self);
3497 chat_password_needed_changed_cb (EmpathyChat *self)
3499 EmpathyChatPriv *priv = GET_PRIV (self);
3501 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3502 empathy_keyring_get_room_password_async (priv->account,
3503 empathy_tp_chat_get_id (priv->tp_chat),
3504 chat_room_got_password_cb, self);
3509 chat_sms_channel_changed_cb (EmpathyChat *self)
3511 EmpathyChatPriv *priv = GET_PRIV (self);
3513 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3514 g_object_notify (G_OBJECT (self), "sms-channel");
3518 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3520 g_object_notify (G_OBJECT (self), "n-messages-sending");
3524 empathy_chat_set_tp_chat (EmpathyChat *chat,
3525 EmpathyTpChat *tp_chat)
3527 EmpathyChatPriv *priv = GET_PRIV (chat);
3528 GPtrArray *properties;
3530 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3531 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3532 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3534 if (priv->tp_chat) {
3538 if (priv->account) {
3539 g_object_unref (priv->account);
3542 priv->tp_chat = g_object_ref (tp_chat);
3543 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3545 g_signal_connect (tp_chat, "destroy",
3546 G_CALLBACK (chat_destroy_cb),
3548 g_signal_connect (tp_chat, "message-received",
3549 G_CALLBACK (chat_message_received_cb),
3551 g_signal_connect (tp_chat, "send-error",
3552 G_CALLBACK (chat_send_error_cb),
3554 g_signal_connect (tp_chat, "chat-state-changed",
3555 G_CALLBACK (chat_state_changed_cb),
3557 g_signal_connect (tp_chat, "property-changed",
3558 G_CALLBACK (chat_property_changed_cb),
3560 g_signal_connect (tp_chat, "members-changed",
3561 G_CALLBACK (chat_members_changed_cb),
3563 g_signal_connect (tp_chat, "member-renamed",
3564 G_CALLBACK (chat_member_renamed_cb),
3566 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3567 G_CALLBACK (chat_remote_contact_changed_cb),
3569 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3570 G_CALLBACK (chat_password_needed_changed_cb),
3572 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3573 G_CALLBACK (chat_sms_channel_changed_cb),
3575 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3576 G_CALLBACK (chat_n_messages_sending_changed_cb),
3579 /* Get initial value of properties */
3580 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3581 if (properties != NULL) {
3584 for (i = 0; i < properties->len; i++) {
3585 EmpathyTpChatProperty *property;
3587 property = g_ptr_array_index (properties, i);
3588 if (property->value == NULL)
3591 chat_property_changed_cb (priv->tp_chat,
3598 chat_sms_channel_changed_cb (chat);
3599 chat_remote_contact_changed_cb (chat);
3601 if (chat->input_text_view) {
3602 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3603 if (priv->block_events_timeout_id == 0) {
3604 empathy_chat_view_append_event (chat->view, _("Connected"));
3608 g_object_notify (G_OBJECT (chat), "tp-chat");
3609 g_object_notify (G_OBJECT (chat), "id");
3610 g_object_notify (G_OBJECT (chat), "account");
3612 /* This is a noop when tp-chat is set at object construction time and causes
3613 * the pending messages to be show when it's set on the object after it has
3615 show_pending_messages (chat);
3617 /* check if a password is needed */
3618 chat_password_needed_changed_cb (chat);
3622 empathy_chat_get_account (EmpathyChat *chat)
3624 EmpathyChatPriv *priv = GET_PRIV (chat);
3626 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3628 return priv->account;
3632 empathy_chat_get_id (EmpathyChat *chat)
3634 EmpathyChatPriv *priv = GET_PRIV (chat);
3636 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3642 empathy_chat_dup_name (EmpathyChat *chat)
3644 EmpathyChatPriv *priv = GET_PRIV (chat);
3647 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3651 if (!ret && priv->remote_contact) {
3652 ret = empathy_contact_get_alias (priv->remote_contact);
3659 ret = _("Conversation");
3661 if (priv->sms_channel)
3662 /* Translators: this string is a something like
3663 * "Escher Cat (SMS)" */
3664 return g_strdup_printf (_("%s (SMS)"), ret);
3666 return g_strdup (ret);
3670 empathy_chat_get_subject (EmpathyChat *chat)
3672 EmpathyChatPriv *priv = GET_PRIV (chat);
3674 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3676 return priv->subject;
3680 empathy_chat_get_remote_contact (EmpathyChat *chat)
3682 EmpathyChatPriv *priv = GET_PRIV (chat);
3684 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3686 return priv->remote_contact;
3690 empathy_chat_get_contact_menu (EmpathyChat *chat)
3692 EmpathyChatPriv *priv = GET_PRIV (chat);
3693 GtkWidget *menu = NULL;
3695 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3697 if (priv->remote_contact) {
3698 menu = empathy_contact_menu_new (priv->remote_contact,
3699 EMPATHY_CONTACT_FEATURE_CALL |
3700 EMPATHY_CONTACT_FEATURE_LOG |
3701 EMPATHY_CONTACT_FEATURE_INFO |
3702 EMPATHY_CONTACT_FEATURE_BLOCK);
3709 empathy_chat_clear (EmpathyChat *chat)
3711 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3713 empathy_chat_view_clear (chat->view);
3717 empathy_chat_scroll_down (EmpathyChat *chat)
3719 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3721 empathy_chat_view_scroll_down (chat->view);
3725 empathy_chat_cut (EmpathyChat *chat)
3727 GtkTextBuffer *buffer;
3729 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3731 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3732 if (gtk_text_buffer_get_has_selection (buffer)) {
3733 GtkClipboard *clipboard;
3735 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3737 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3742 empathy_chat_copy (EmpathyChat *chat)
3744 GtkTextBuffer *buffer;
3746 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3748 if (empathy_chat_view_get_has_selection (chat->view)) {
3749 empathy_chat_view_copy_clipboard (chat->view);
3753 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3754 if (gtk_text_buffer_get_has_selection (buffer)) {
3755 GtkClipboard *clipboard;
3757 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3759 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3764 EmpathyChatPriv *priv = GET_PRIV (chat);
3766 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3773 GtkClipboard *clipboard;
3775 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3776 start = g_utf8_offset_to_pointer (topic, start_offset);
3777 end = g_utf8_offset_to_pointer (topic, end_offset);
3778 selection = g_strndup (start, end - start);
3780 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3781 gtk_clipboard_set_text (clipboard, selection, -1);
3789 empathy_chat_paste (EmpathyChat *chat)
3791 GtkTextBuffer *buffer;
3792 GtkClipboard *clipboard;
3793 EmpathyChatPriv *priv;
3795 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3797 priv = GET_PRIV (chat);
3799 if (gtk_widget_get_visible (priv->search_bar)) {
3800 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3804 if (priv->tp_chat == NULL ||
3805 !gtk_widget_is_sensitive (chat->input_text_view))
3808 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3809 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3811 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3815 empathy_chat_find (EmpathyChat *chat)
3817 EmpathyChatPriv *priv;
3819 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3821 priv = GET_PRIV (chat);
3823 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3827 empathy_chat_correct_word (EmpathyChat *chat,
3830 const gchar *new_word)
3832 GtkTextBuffer *buffer;
3834 g_return_if_fail (chat != NULL);
3835 g_return_if_fail (new_word != NULL);
3837 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3839 gtk_text_buffer_delete (buffer, start, end);
3840 gtk_text_buffer_insert (buffer, start,
3846 empathy_chat_is_room (EmpathyChat *chat)
3848 EmpathyChatPriv *priv = GET_PRIV (chat);
3850 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3852 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3856 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3858 EmpathyChatPriv *priv = GET_PRIV (self);
3860 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3862 return priv->unread_messages;
3865 /* called when the messages have been read by user */
3867 empathy_chat_messages_read (EmpathyChat *self)
3869 EmpathyChatPriv *priv = GET_PRIV (self);
3871 g_return_if_fail (EMPATHY_IS_CHAT (self));
3873 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3874 * definition. If we are still retrieving the backlogs, do not ACK */
3875 if (priv->retrieving_backlogs)
3878 if (priv->tp_chat != NULL ) {
3879 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3881 priv->unread_messages = 0;
3883 empathy_chat_view_focus_toggled (self->view, TRUE);
3886 /* Return TRUE if on of the contacts in this chat is composing */
3888 empathy_chat_is_composing (EmpathyChat *chat)
3890 return chat->priv->compositors != NULL;
3894 empathy_chat_is_sms_channel (EmpathyChat *self)
3896 EmpathyChatPriv *priv = GET_PRIV (self);
3898 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3900 return priv->sms_channel;
3904 empathy_chat_get_n_messages_sending (EmpathyChat *self)
3906 EmpathyChatPriv *priv;
3908 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3910 priv = GET_PRIV (self);
3912 if (priv->tp_chat == NULL) {
3917 g_object_get (priv->tp_chat,
3918 "n-messages-sending", &n_messages,
3926 empathy_chat_dup_text (EmpathyChat *self)
3928 GtkTextBuffer *buffer;
3929 GtkTextIter start, end;
3931 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
3933 gtk_text_buffer_get_bounds (buffer, &start, &end);
3934 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
3938 empathy_chat_set_text (EmpathyChat *self,
3941 GtkTextBuffer *buffer;
3943 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
3945 gtk_text_buffer_set_text (buffer, text, -1);