1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #undef G_DISABLE_DEPRECATED /* for GCompletion */
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n-lib.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-keyring.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-request-util.h>
46 #include <libempathy/empathy-chatroom-manager.h>
48 #include "empathy-chat.h"
49 #include "empathy-spell.h"
50 #include "empathy-contact-dialogs.h"
51 #include "empathy-contact-list-store.h"
52 #include "empathy-contact-list-view.h"
53 #include "empathy-contact-menu.h"
54 #include "empathy-gtk-marshal.h"
55 #include "empathy-input-text-view.h"
56 #include "empathy-search-bar.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-smiley-manager.h"
59 #include "empathy-ui-utils.h"
60 #include "empathy-string-parser.h"
61 #include "extensions/extensions.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
64 #include <libempathy/empathy-debug.h>
66 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
67 #define COMPOSING_STOP_TIMEOUT 5
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
70 struct _EmpathyChatPriv {
71 EmpathyTpChat *tp_chat;
76 EmpathyContact *remote_contact;
77 gboolean show_contacts;
79 GSettings *gsettings_chat;
80 GSettings *gsettings_ui;
82 TplLogManager *log_manager;
83 TpAccountManager *account_manager;
85 GList *input_history_current;
87 GCompletion *completion;
88 guint composing_stop_timeout_id;
89 guint block_events_timeout_id;
90 TpHandleType handle_type;
92 gboolean has_input_vscroll;
94 /* TRUE if spell checking is enabled, FALSE otherwise.
95 * This is to keep track of the last state of spell checking
97 gboolean spell_checking_enabled;
99 /* These store the signal handler ids for the enclosed text entry. */
100 gulong insert_text_id;
101 gulong delete_range_id;
102 gulong notify_cursor_position_id;
104 /* Source func ID for update_misspelled_words () */
105 guint update_misspelled_words_id;
106 /* Source func ID for save_paned_pos_timeout () */
107 guint save_paned_pos_id;
111 GtkWidget *vbox_left;
112 GtkWidget *scrolled_window_chat;
113 GtkWidget *scrolled_window_input;
114 GtkWidget *scrolled_window_contacts;
115 GtkWidget *hbox_topic;
116 GtkWidget *expander_topic;
117 GtkWidget *label_topic;
118 GtkWidget *contact_list_view;
119 GtkWidget *info_bar_vbox;
120 GtkWidget *search_bar;
122 guint unread_messages;
123 /* TRUE if the pending messages can be displayed. This is to avoid to show
124 * pending messages *before* messages from logs. (#603980) */
125 gboolean can_show_pending;
127 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
128 * be differently handled since it introduces another race condition, which
129 * is really hard to occur, but still possible.
131 * With the current workaround (which has the race above), we need to be
132 * sure to ACK any pending messages only when the retrieval of backlogs is
133 * finished, that's why using retrieving_backlogs flag.
134 * empathy_chat_messages_read () will check this variable and not ACK
135 * anything when TRUE. It will be set TRUE at chat_constructed () and set
136 * back to FALSE when the backlog has been retrieved and the pending
137 * messages actually showed to the user.
139 * Race condition introduced with this workaround:
140 * Scenario: a message is pending, the user is notified and selects the tab.
141 * the tab with a pending message is focused before the messages are properly
142 * shown (since the preparation of the window is slower AND async WRT the
143 * tab showing), which means the user won't see any new messages (rare but
144 * possible), if he/she will change tab focus before the messages are
145 * properly shown, the tab will be set as 'seen' and the user won't be
146 * notified again about the already notified pending messages when the
147 * messages in tab will be properly shown */
148 gboolean retrieving_backlogs;
149 gboolean sms_channel;
153 gchar *text; /* Original message that was specified
154 * upon entry creation. */
155 gchar *modified_text; /* Message that was modified by user.
156 * When no modifications were made, it is NULL */
162 PART_COMMAND_ENTERED,
176 PROP_N_MESSAGES_SENDING,
179 static guint signals[LAST_SIGNAL] = { 0 };
181 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
183 static gboolean update_misspelled_words (gpointer data);
186 chat_get_property (GObject *object,
191 EmpathyChat *chat = EMPATHY_CHAT (object);
192 EmpathyChatPriv *priv = GET_PRIV (object);
196 g_value_set_object (value, priv->tp_chat);
199 g_value_set_object (value, priv->account);
202 g_value_take_string (value, empathy_chat_dup_name (chat));
205 g_value_set_string (value, priv->id);
208 g_value_set_string (value, priv->subject);
210 case PROP_REMOTE_CONTACT:
211 g_value_set_object (value, priv->remote_contact);
213 case PROP_SHOW_CONTACTS:
214 g_value_set_boolean (value, priv->show_contacts);
216 case PROP_SMS_CHANNEL:
217 g_value_set_boolean (value, priv->sms_channel);
219 case PROP_N_MESSAGES_SENDING:
220 g_value_set_uint (value,
221 empathy_chat_get_n_messages_sending (chat));
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
230 chat_set_property (GObject *object,
235 EmpathyChat *chat = EMPATHY_CHAT (object);
239 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
241 case PROP_SHOW_CONTACTS:
242 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
251 account_reconnected (EmpathyChat *chat,
254 EmpathyChatPriv *priv = GET_PRIV (chat);
256 DEBUG ("Account reconnected, request a new Text channel");
258 /* FIXME: Ideally we should ask to handle ourself the channel so we can
259 * report the error if any but this is blocked by
260 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
261 switch (priv->handle_type) {
262 case TP_HANDLE_TYPE_CONTACT:
263 if (priv->sms_channel)
264 empathy_sms_contact_id (
266 TP_USER_ACTION_TIME_NOT_USER_ACTION);
268 empathy_chat_with_contact_id (
270 TP_USER_ACTION_TIME_NOT_USER_ACTION);
272 case TP_HANDLE_TYPE_ROOM:
273 empathy_join_muc (account, priv->id,
274 TP_USER_ACTION_TIME_NOT_USER_ACTION);
276 case TP_HANDLE_TYPE_NONE:
277 case TP_HANDLE_TYPE_LIST:
278 case TP_HANDLE_TYPE_GROUP:
280 g_assert_not_reached ();
284 g_object_unref (chat);
288 chat_new_connection_cb (TpAccount *account,
292 gchar *dbus_error_name,
296 EmpathyChatPriv *priv = GET_PRIV (chat);
298 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
301 if (priv->tp_chat != NULL || account != priv->account ||
302 priv->handle_type == TP_HANDLE_TYPE_NONE ||
303 EMP_STR_EMPTY (priv->id))
308 account_reconnected (chat, account);
312 chat_composing_remove_timeout (EmpathyChat *chat)
314 EmpathyChatPriv *priv;
316 priv = GET_PRIV (chat);
318 if (priv->composing_stop_timeout_id) {
319 g_source_remove (priv->composing_stop_timeout_id);
320 priv->composing_stop_timeout_id = 0;
325 chat_composing_stop_timeout_cb (EmpathyChat *chat)
327 EmpathyChatPriv *priv;
329 priv = GET_PRIV (chat);
331 priv->composing_stop_timeout_id = 0;
332 empathy_tp_chat_set_state (priv->tp_chat,
333 TP_CHANNEL_CHAT_STATE_PAUSED);
339 chat_composing_start (EmpathyChat *chat)
341 EmpathyChatPriv *priv;
343 priv = GET_PRIV (chat);
345 if (priv->composing_stop_timeout_id) {
346 /* Just restart the timeout */
347 chat_composing_remove_timeout (chat);
349 empathy_tp_chat_set_state (priv->tp_chat,
350 TP_CHANNEL_CHAT_STATE_COMPOSING);
353 priv->composing_stop_timeout_id = g_timeout_add_seconds (
354 COMPOSING_STOP_TIMEOUT,
355 (GSourceFunc) chat_composing_stop_timeout_cb,
360 chat_composing_stop (EmpathyChat *chat)
362 EmpathyChatPriv *priv;
364 priv = GET_PRIV (chat);
366 chat_composing_remove_timeout (chat);
367 empathy_tp_chat_set_state (priv->tp_chat,
368 TP_CHANNEL_CHAT_STATE_ACTIVE);
372 chat_input_history_entry_cmp (InputHistoryEntry *entry,
375 if (!tp_strdiff (entry->text, text)) {
376 if (entry->modified_text != NULL) {
377 /* Modified entry and single string cannot be equal. */
385 static InputHistoryEntry *
386 chat_input_history_entry_new_with_text (const gchar *text)
388 InputHistoryEntry *entry;
389 entry = g_slice_new0 (InputHistoryEntry);
390 entry->text = g_strdup (text);
396 chat_input_history_entry_free (InputHistoryEntry *entry)
398 g_free (entry->text);
399 g_free (entry->modified_text);
400 g_slice_free (InputHistoryEntry, entry);
404 chat_input_history_entry_revert (InputHistoryEntry *entry)
406 g_free (entry->modified_text);
407 entry->modified_text = NULL;
411 chat_input_history_entry_update_text (InputHistoryEntry *entry,
416 if (!tp_strdiff (text, entry->text)) {
417 g_free (entry->modified_text);
418 entry->modified_text = NULL;
422 old = entry->modified_text;
423 entry->modified_text = g_strdup (text);
428 chat_input_history_entry_get_text (InputHistoryEntry *entry)
434 if (entry->modified_text != NULL) {
435 return entry->modified_text;
441 chat_input_history_remove_item (GList *list,
444 list = g_list_remove_link (list, item);
445 chat_input_history_entry_free (item->data);
446 g_list_free_1 (item);
451 chat_input_history_revert (EmpathyChat *chat)
453 EmpathyChatPriv *priv;
457 InputHistoryEntry *entry;
459 priv = GET_PRIV (chat);
460 list = priv->input_history;
463 DEBUG ("No input history");
467 /* Delete temporary entry */
468 if (priv->input_history_current != NULL) {
470 list = chat_input_history_remove_item (list, item1);
471 if (priv->input_history_current == item1) {
472 /* Removed temporary entry was current entry */
473 priv->input_history = list;
474 priv->input_history_current = NULL;
479 /* There is no entry to revert */
483 /* Restore the current history entry to original value */
484 item1 = priv->input_history_current;
486 chat_input_history_entry_revert (entry);
488 /* Remove restored entry if there is other occurance before this entry */
489 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
490 (GCompareFunc) chat_input_history_entry_cmp);
491 if (item2 != item1) {
492 list = chat_input_history_remove_item (list, item1);
495 /* Remove other occurance of the restored entry */
496 item2 = g_list_find_custom (item1->next,
497 chat_input_history_entry_get_text (entry),
498 (GCompareFunc) chat_input_history_entry_cmp);
500 list = chat_input_history_remove_item (list, item2);
504 priv->input_history_current = NULL;
505 priv->input_history = list;
509 chat_input_history_add (EmpathyChat *chat,
513 EmpathyChatPriv *priv;
516 InputHistoryEntry *entry;
518 priv = GET_PRIV (chat);
520 list = priv->input_history;
522 /* Remove any other occurances of this entry, if not temporary */
524 while ((item = g_list_find_custom (list, str,
525 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
526 list = chat_input_history_remove_item (list, item);
529 /* Trim the list to the last 10 items */
530 while (g_list_length (list) > 10) {
531 item = g_list_last (list);
533 list = chat_input_history_remove_item (list, item);
541 entry = chat_input_history_entry_new_with_text (str);
542 list = g_list_prepend (list, entry);
544 /* Set the list and the current item pointer */
545 priv->input_history = list;
547 priv->input_history_current = list;
550 priv->input_history_current = NULL;
555 chat_input_history_get_next (EmpathyChat *chat)
557 EmpathyChatPriv *priv;
561 priv = GET_PRIV (chat);
563 if (priv->input_history == NULL) {
564 DEBUG ("No input history, next entry is NULL");
567 g_assert (priv->input_history_current != NULL);
569 if ((item = g_list_next (priv->input_history_current)) == NULL)
571 item = priv->input_history_current;
574 msg = chat_input_history_entry_get_text (item->data);
576 DEBUG ("Returning next entry: '%s'", msg);
578 priv->input_history_current = item;
584 chat_input_history_get_prev (EmpathyChat *chat)
586 EmpathyChatPriv *priv;
590 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
592 priv = GET_PRIV (chat);
594 if (priv->input_history == NULL) {
595 DEBUG ("No input history, previous entry is NULL");
599 if (priv->input_history_current == NULL)
603 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
605 item = priv->input_history_current;
608 msg = chat_input_history_entry_get_text (item->data);
610 DEBUG ("Returning previous entry: '%s'", msg);
612 priv->input_history_current = item;
618 chat_input_history_update (EmpathyChat *chat,
619 GtkTextBuffer *buffer)
621 EmpathyChatPriv *priv;
622 GtkTextIter start, end;
624 InputHistoryEntry *entry;
626 priv = GET_PRIV (chat);
628 gtk_text_buffer_get_bounds (buffer, &start, &end);
629 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
631 if (priv->input_history_current == NULL) {
632 /* Add the current text temporarily to the history */
633 chat_input_history_add (chat, text, TRUE);
638 /* Save the changes in the history */
639 entry = priv->input_history_current->data;
640 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
641 chat_input_history_entry_update_text (entry, text);
650 } ChatCommandMsgData;
653 chat_command_msg_cb (GObject *source,
654 GAsyncResult *result,
657 ChatCommandMsgData *data = user_data;
658 GError *error = NULL;
661 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
662 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
664 if (channel == NULL) {
665 DEBUG ("Failed to get channel: %s", error->message);
666 g_error_free (error);
668 empathy_chat_view_append_event (data->chat->view,
669 _("Failed to open private chat"));
673 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
674 TpTextChannel *text = (TpTextChannel *) channel;
677 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
680 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
682 g_object_unref (msg);
685 g_object_unref (channel);
688 g_free (data->message);
689 g_slice_free (ChatCommandMsgData, data);
693 nick_command_supported (EmpathyChat *chat)
695 EmpathyChatPriv * priv = GET_PRIV (chat);
696 TpConnection *connection;
698 connection = empathy_tp_chat_get_connection (priv->tp_chat);
699 return tp_proxy_has_interface_by_id (connection,
700 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
704 part_command_supported (EmpathyChat *chat)
706 EmpathyChatPriv * priv = GET_PRIV (chat);
709 channel = empathy_tp_chat_get_channel (priv->tp_chat);
710 return tp_proxy_has_interface_by_id (channel,
711 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
715 chat_command_clear (EmpathyChat *chat,
718 empathy_chat_view_clear (chat->view);
722 chat_command_topic (EmpathyChat *chat,
725 EmpathyChatPriv *priv = GET_PRIV (chat);
726 EmpathyTpChatProperty *property;
727 GValue value = {0, };
729 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
730 if (property == NULL) {
731 empathy_chat_view_append_event (chat->view,
732 _("Topic not supported on this conversation"));
736 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
737 empathy_chat_view_append_event (chat->view,
738 _("You are not allowed to change the topic"));
742 g_value_init (&value, G_TYPE_STRING);
743 g_value_set_string (&value, strv[1]);
744 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
745 g_value_unset (&value);
749 empathy_chat_join_muc (EmpathyChat *chat,
752 EmpathyChatPriv *priv = GET_PRIV (chat);
754 empathy_join_muc (priv->account, room,
755 empathy_get_current_action_time ());
759 chat_command_join (EmpathyChat *chat,
764 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
766 /* FIXME: Ideally we should ask to handle ourself the channel so we can
767 * report the error if any but this is blocked by
768 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
769 while (rooms[i] != NULL) {
770 /* ignore empty strings */
771 if (!EMP_STR_EMPTY (rooms[i])) {
772 empathy_chat_join_muc (chat, rooms[i]);
780 chat_command_part (EmpathyChat *chat,
783 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
787 chat_command_msg_internal (EmpathyChat *chat,
788 const gchar *contact_id,
789 const gchar *message)
791 EmpathyChatPriv *priv = GET_PRIV (chat);
792 ChatCommandMsgData *data;
793 TpAccountChannelRequest *req;
796 request = tp_asv_new (
797 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
798 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
799 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
802 req = tp_account_channel_request_new (priv->account, request,
803 empathy_get_current_action_time ());
805 /* FIXME: We should probably search in members alias. But this
806 * is enough for IRC */
807 data = g_slice_new (ChatCommandMsgData);
809 data->message = g_strdup (message);
811 tp_account_channel_request_ensure_and_observe_channel_async (req,
812 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
814 g_object_unref (req);
815 g_hash_table_unref (request);
819 chat_command_query (EmpathyChat *chat,
822 /* If <message> part is not defined,
823 * strv[2] will be the terminal NULL */
824 chat_command_msg_internal (chat, strv[1], strv[2]);
828 chat_command_msg (EmpathyChat *chat,
831 chat_command_msg_internal (chat, strv[1], strv[2]);
835 callback_for_request_rename (TpProxy *proxy,
838 GObject *weak_object)
841 DEBUG ("Call to RequestRename method failed: %s",error->message);
846 chat_command_nick (EmpathyChat *chat,
849 EmpathyChatPriv *priv = GET_PRIV (chat);
852 proxy = TP_PROXY (tp_account_get_connection (priv->account));
854 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
855 strv[1], callback_for_request_rename, NULL, NULL, NULL);
859 chat_command_me (EmpathyChat *chat,
862 EmpathyChatPriv *priv = GET_PRIV (chat);
866 channel = empathy_tp_chat_get_channel (priv->tp_chat);
868 /* Strictly speaking we don't depend yet on Messages so best to check that
869 * the channel is actually a TpTextChannel before casting it. */
870 if (TP_IS_TEXT_CHANNEL (channel) &&
871 !tp_text_channel_supports_message_type (TP_TEXT_CHANNEL (channel),
872 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
873 /* Action message are not supported, 'simulate' the action */
874 EmpathyContact *self_contact;
877 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
878 /* The TpChat can't be ready if it doesn't have the self contact */
879 g_assert (self_contact != NULL);
881 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
883 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
888 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
892 empathy_tp_chat_send (priv->tp_chat, message);
893 g_object_unref (message);
897 chat_command_say (EmpathyChat *chat,
900 EmpathyChatPriv *priv = GET_PRIV (chat);
903 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
905 empathy_tp_chat_send (priv->tp_chat, message);
906 g_object_unref (message);
910 whois_got_contact_cb (TpConnection *connection,
912 TpContact * const *contacts,
913 const gchar * const *requested_ids,
914 GHashTable *failed_id_errors,
917 GObject *weak_object)
919 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
921 g_return_if_fail (n_contacts <= 1);
923 if (n_contacts == 0) {
925 gpointer key = NULL, value = NULL;
929 /* tp-glib guarantees that the contacts you requested will be
930 * in failed_id_errors regardless of whether the individual
931 * contact was invalid or the whole operation failed.
933 g_hash_table_iter_init (&iter, failed_id_errors);
934 g_hash_table_iter_next (&iter, &key, &value);
938 DEBUG ("Error getting TpContact for '%s': %s %u %s",
939 id, g_quark_to_string (id_error->domain),
940 id_error->code, id_error->message);
943 /* The specific ID failed. */
944 gchar *event = g_strdup_printf (
945 _("“%s” is not a valid contact ID"), id);
946 empathy_chat_view_append_event (chat->view, event);
949 /* Otherwise we're disconnected or something; so the window
950 * will already say ‘Disconnected’, so let's not show anything.
953 EmpathyContact *empathy_contact;
956 g_return_if_fail (contacts[0] != NULL);
957 empathy_contact = empathy_contact_dup_from_tp_contact (
960 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
961 /* If we're alive and this command is running, we'd better be
963 g_return_if_fail (window != NULL);
964 g_return_if_fail (gtk_widget_is_toplevel (window));
965 empathy_contact_information_dialog_show (empathy_contact,
966 GTK_WINDOW (window));
967 g_object_unref (empathy_contact);
972 chat_command_whois (EmpathyChat *chat,
975 EmpathyChatPriv *priv = GET_PRIV (chat);
978 conn = empathy_tp_chat_get_connection (priv->tp_chat);
979 tp_connection_get_contacts_by_id (conn,
980 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
981 * entered by the user (including spaces, if any). */
982 1, (const gchar * const *) strv + 1,
984 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
987 static void chat_command_help (EmpathyChat *chat, GStrv strv);
989 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
995 ChatCommandFunc func;
996 gboolean (*is_supported)(EmpathyChat *chat);
1000 static ChatCommandItem commands[] = {
1001 {"clear", 1, 1, chat_command_clear, NULL,
1002 N_("/clear: clear all messages from the current conversation")},
1004 {"topic", 2, 2, chat_command_topic, NULL,
1005 N_("/topic <topic>: set the topic of the current conversation")},
1007 {"join", 2, 2, chat_command_join, NULL,
1008 N_("/join <chat room ID>: join a new chat room")},
1010 {"j", 2, 2, chat_command_join, NULL,
1011 N_("/j <chat room ID>: join a new chat room")},
1013 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1014 {"part", 1, 3, chat_command_part, part_command_supported,
1015 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1016 "by default the current one")},
1018 {"query", 2, 3, chat_command_query, NULL,
1019 N_("/query <contact ID> [<message>]: open a private chat")},
1021 {"msg", 3, 3, chat_command_msg, NULL,
1022 N_("/msg <contact ID> <message>: open a private chat")},
1024 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1025 N_("/nick <nickname>: change your nickname on the current server")},
1027 {"me", 2, 2, chat_command_me, NULL,
1028 N_("/me <message>: send an ACTION message to the current conversation")},
1030 {"say", 2, 2, chat_command_say, NULL,
1031 N_("/say <message>: send <message> to the current conversation. "
1032 "This is used to send a message starting with a '/'. For example: "
1033 "\"/say /join is used to join a new chat room\"")},
1035 {"whois", 2, 2, chat_command_whois, NULL,
1036 N_("/whois <contact ID>: display information about a contact")},
1038 {"help", 1, 2, chat_command_help, NULL,
1039 N_("/help [<command>]: show all supported commands. "
1040 "If <command> is defined, show its usage.")},
1044 chat_command_show_help (EmpathyChat *chat,
1045 ChatCommandItem *item)
1049 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1050 empathy_chat_view_append_event (chat->view, str);
1055 chat_command_help (EmpathyChat *chat,
1060 /* If <command> part is not defined,
1061 * strv[1] will be the terminal NULL */
1062 if (strv[1] == NULL) {
1063 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1064 if (commands[i].is_supported != NULL) {
1065 if (!commands[i].is_supported (chat)) {
1069 empathy_chat_view_append_event (chat->view,
1070 _(commands[i].help));
1075 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1076 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1077 if (commands[i].is_supported != NULL) {
1078 if (!commands[i].is_supported (chat)) {
1082 chat_command_show_help (chat, &commands[i]);
1087 empathy_chat_view_append_event (chat->view,
1088 _("Unknown command"));
1092 chat_command_parse (const gchar *text, guint max_parts)
1097 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1099 array = g_ptr_array_sized_new (max_parts + 1);
1100 while (max_parts > 1) {
1103 /* Skip white spaces */
1104 while (g_ascii_isspace (*text)) {
1108 /* Search the end of this part, until first space. */
1109 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1115 item = g_strndup (text, end - text);
1116 g_ptr_array_add (array, item);
1117 DEBUG ("\tITEM: \"%s\"", item);
1123 /* Append last part if not empty */
1124 item = g_strstrip (g_strdup (text));
1125 if (!EMP_STR_EMPTY (item)) {
1126 g_ptr_array_add (array, item);
1127 DEBUG ("\tITEM: \"%s\"", item);
1132 /* Make the array NULL-terminated */
1133 g_ptr_array_add (array, NULL);
1135 return (GStrv) g_ptr_array_free (array, FALSE);
1139 has_prefix_case (const gchar *s,
1140 const gchar *prefix)
1142 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1146 chat_send (EmpathyChat *chat,
1149 EmpathyChatPriv *priv;
1153 if (EMP_STR_EMPTY (msg)) {
1157 priv = GET_PRIV (chat);
1159 chat_input_history_add (chat, msg, FALSE);
1161 if (msg[0] == '/') {
1162 gboolean second_slash = FALSE;
1163 const gchar *iter = msg + 1;
1165 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1170 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1173 c = *(msg + 1 + strlen (commands[i].prefix));
1174 if (c != '\0' && !g_ascii_isspace (c)) {
1177 if (commands[i].is_supported != NULL) {
1178 if (!commands[i].is_supported (chat)) {
1183 /* We can't use g_strsplit here because it does
1184 * not deal correctly if we have more than one space
1186 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1188 strv_len = g_strv_length (strv);
1189 if (strv_len < commands[i].min_parts ||
1190 strv_len > commands[i].max_parts) {
1191 chat_command_show_help (chat, &commands[i]);
1196 commands[i].func (chat, strv);
1201 /* Also allow messages with two slashes before the
1202 * first space, so it is possible to send a /unix/path.
1203 * This heuristic is kind of crap. */
1204 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1206 second_slash = TRUE;
1212 if (!second_slash) {
1213 empathy_chat_view_append_event (chat->view,
1214 _("Unknown command; see /help for the available"
1220 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1222 empathy_tp_chat_send (priv->tp_chat, message);
1223 g_object_unref (message);
1227 chat_input_text_view_send (EmpathyChat *chat)
1229 GtkTextBuffer *buffer;
1230 GtkTextIter start, end;
1233 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1235 gtk_text_buffer_get_bounds (buffer, &start, &end);
1236 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1238 /* clear the input field */
1239 gtk_text_buffer_set_text (buffer, "", -1);
1240 /* delete input history modifications */
1241 chat_input_history_revert (chat);
1243 chat_send (chat, msg);
1248 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1249 EmpathyContact *contact,
1250 TpChannelChatState state,
1253 EmpathyChatPriv *priv;
1255 gboolean was_composing;
1257 priv = GET_PRIV (chat);
1259 if (empathy_contact_is_user (contact)) {
1260 /* We don't care about our own chat state */
1264 was_composing = (priv->compositors != NULL);
1266 /* Find the contact in the list. After that l is the list elem or NULL */
1267 for (l = priv->compositors; l; l = l->next) {
1268 if (contact == l->data) {
1274 case TP_CHANNEL_CHAT_STATE_GONE:
1275 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1276 case TP_CHANNEL_CHAT_STATE_PAUSED:
1277 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1278 /* Contact is not composing */
1280 priv->compositors = g_list_remove_link (priv->compositors, l);
1281 g_object_unref (l->data);
1285 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1286 /* Contact is composing */
1288 priv->compositors = g_list_prepend (priv->compositors,
1289 g_object_ref (contact));
1293 g_assert_not_reached ();
1296 DEBUG ("Was composing: %s now composing: %s",
1297 was_composing ? "yes" : "no",
1298 priv->compositors ? "yes" : "no");
1300 if ((was_composing && !priv->compositors) ||
1301 (!was_composing && priv->compositors)) {
1302 /* Composing state changed */
1303 g_signal_emit (chat, signals[COMPOSING], 0,
1304 priv->compositors != NULL);
1309 chat_message_received (EmpathyChat *chat,
1310 EmpathyMessage *message,
1313 EmpathyChatPriv *priv = GET_PRIV (chat);
1314 EmpathyContact *sender;
1316 sender = empathy_message_get_sender (message);
1318 DEBUG ("Appending new message from %s (%d)",
1319 empathy_contact_get_alias (sender),
1320 empathy_contact_get_handle (sender));
1322 empathy_chat_view_append_message (chat->view, message);
1324 /* We received a message so the contact is no longer composing */
1325 chat_state_changed_cb (priv->tp_chat, sender,
1326 TP_CHANNEL_CHAT_STATE_ACTIVE,
1329 priv->unread_messages++;
1330 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1334 chat_message_received_cb (EmpathyTpChat *tp_chat,
1335 EmpathyMessage *message,
1338 chat_message_received (chat, message, FALSE);
1342 chat_send_error_cb (EmpathyTpChat *tp_chat,
1343 const gchar *message_body,
1344 TpChannelTextSendError error_code,
1345 const gchar *dbus_error,
1348 const gchar *error = NULL;
1351 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1352 /* translators: error used when user doesn't have enough credit on his
1353 * account to send the message. */
1354 error = _("insufficient balance to send message");
1355 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1356 error = _("not capable");
1359 if (error == NULL) {
1360 /* if we didn't find a dbus-error, try the old error */
1361 switch (error_code) {
1362 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1363 error = _("offline");
1365 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1366 error = _("invalid contact");
1368 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1369 error = _("permission denied");
1371 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1372 error = _("too long message");
1374 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1375 error = _("not implemented");
1377 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1379 error = _("unknown");
1384 if (message_body != NULL) {
1385 str = g_strdup_printf (_("Error sending message '%s': %s"),
1386 message_body, error);
1389 str = g_strdup_printf (_("Error sending message: %s"), error);
1392 empathy_chat_view_append_event (chat->view, str);
1397 chat_topic_label_size_allocate_cb (GtkLabel *label,
1398 GtkAllocation *allocation,
1401 EmpathyChatPriv *priv = GET_PRIV (chat);
1403 if (!gtk_label_get_line_wrap (label)) {
1404 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1405 gtk_widget_show (priv->expander_topic);
1407 gtk_widget_hide (priv->expander_topic);
1414 chat_topic_expander_activate_cb (GtkExpander *expander,
1415 GParamSpec *param_spec,
1418 EmpathyChatPriv *priv = GET_PRIV (chat);
1420 if (gtk_expander_get_expanded (expander)) {
1421 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1422 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1424 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1425 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1430 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1435 EmpathyChatPriv *priv = GET_PRIV (chat);
1437 if (!tp_strdiff (name, "subject")) {
1438 g_free (priv->subject);
1439 priv->subject = g_value_dup_string (value);
1440 g_object_notify (G_OBJECT (chat), "subject");
1442 if (EMP_STR_EMPTY (priv->subject)) {
1443 gtk_widget_hide (priv->hbox_topic);
1445 gchar *markup_topic;
1448 markup_topic = empathy_add_link_markup (priv->subject);
1449 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1450 _("Topic:"), markup_topic);
1452 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1453 g_free (markup_text);
1454 g_free (markup_topic);
1456 gtk_widget_show (priv->hbox_topic);
1458 if (priv->block_events_timeout_id == 0) {
1461 if (!EMP_STR_EMPTY (priv->subject)) {
1462 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1464 str = g_strdup (_("No topic defined"));
1466 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1470 else if (!tp_strdiff (name, "name")) {
1471 g_free (priv->name);
1472 priv->name = g_value_dup_string (value);
1473 g_object_notify (G_OBJECT (chat), "name");
1478 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1482 GtkTextIter word_start = *iter;
1483 GtkTextIter word_end = *iter;
1486 if (gtk_text_iter_inside_word (&word_end) &&
1487 !gtk_text_iter_ends_word (&word_end)) {
1488 gtk_text_iter_forward_word_end (&word_end);
1493 if (gtk_text_iter_get_char (&tmp) == '\'') {
1494 gtk_text_iter_forward_char (&tmp);
1496 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1497 gtk_text_iter_forward_word_end (&word_end);
1502 if (gtk_text_iter_inside_word (&word_start) ||
1503 gtk_text_iter_ends_word (&word_start)) {
1504 if (!gtk_text_iter_starts_word (&word_start) ||
1505 gtk_text_iter_equal (&word_start, &word_end)) {
1506 gtk_text_iter_backward_word_start (&word_start);
1510 gtk_text_iter_backward_char (&tmp);
1512 if (gtk_text_iter_get_char (&tmp) == '\'') {
1513 gtk_text_iter_backward_char (&tmp);
1515 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1516 gtk_text_iter_backward_word_start (&word_start);
1521 *start = word_start;
1527 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1528 GtkTextIter *location,
1533 GtkTextIter iter, pos;
1535 /* Remove all misspelled tags in the inserted text.
1536 * This happens when text is inserted within a misspelled word. */
1537 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1538 gtk_text_iter_get_offset (location) - len);
1539 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1542 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1545 GtkTextIter start, end;
1548 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1551 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1553 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1554 gtk_text_iter_equal (&pos, &end) ||
1555 empathy_spell_check (str)) {
1556 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1558 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1563 } while (gtk_text_iter_forward_word_end (&iter) &&
1564 gtk_text_iter_compare (&iter, location) <= 0);
1568 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1573 GtkTextIter word_start, word_end;
1575 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1576 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1577 &word_start, &word_end);
1582 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1585 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1586 chat_composing_stop (chat);
1588 chat_composing_start (chat);
1593 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1598 GtkTextIter prev_pos;
1599 GtkTextIter word_start;
1600 GtkTextIter word_end;
1604 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1606 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1607 gtk_text_buffer_get_insert (buffer));
1608 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1610 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1613 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1614 !gtk_text_iter_equal (&pos, &word_end)) {
1615 str = gtk_text_buffer_get_text (buffer,
1616 &word_start, &word_end, FALSE);
1618 if (!empathy_spell_check (str)) {
1619 gtk_text_buffer_apply_tag_by_name (buffer,
1620 "misspelled", &word_start, &word_end);
1622 gtk_text_buffer_remove_tag_by_name (buffer,
1623 "misspelled", &word_start, &word_end);
1630 gtk_text_buffer_move_mark (buffer, mark, &pos);
1634 empathy_isspace_cb (gunichar c,
1637 return g_unichar_isspace (c);
1641 chat_input_key_press_event_cb (GtkWidget *widget,
1645 EmpathyChatPriv *priv;
1648 GtkWidget *text_view_sw;
1650 priv = GET_PRIV (chat);
1652 /* Catch ctrl+up/down so we can traverse messages we sent */
1653 if ((event->state & GDK_CONTROL_MASK) &&
1654 (event->keyval == GDK_KEY_Up ||
1655 event->keyval == GDK_KEY_Down)) {
1656 GtkTextBuffer *buffer;
1659 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1660 chat_input_history_update (chat, buffer);
1662 if (event->keyval == GDK_KEY_Up) {
1663 str = chat_input_history_get_next (chat);
1665 str = chat_input_history_get_prev (chat);
1668 g_signal_handlers_block_by_func (buffer,
1669 chat_input_text_buffer_changed_cb,
1671 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1672 g_signal_handlers_unblock_by_func (buffer,
1673 chat_input_text_buffer_changed_cb,
1679 /* Catch enter but not ctrl/shift-enter */
1680 if (IS_ENTER (event->keyval) &&
1681 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1684 /* This is to make sure that kinput2 gets the enter. And if
1685 * it's handled there we shouldn't send on it. This is because
1686 * kinput2 uses Enter to commit letters. See:
1687 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1690 view = GTK_TEXT_VIEW (chat->input_text_view);
1691 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1692 gtk_text_view_reset_im_context (view);
1696 chat_input_text_view_send (chat);
1700 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1702 if (IS_ENTER (event->keyval) &&
1703 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1704 /* Newline for shift/control-enter. */
1707 if (!(event->state & GDK_CONTROL_MASK) &&
1708 event->keyval == GDK_KEY_Page_Up) {
1709 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1710 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1713 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1714 event->keyval == GDK_KEY_Page_Down) {
1715 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1716 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1717 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1718 gtk_adjustment_set_value (adj, val);
1721 if (event->keyval == GDK_KEY_Escape) {
1722 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1724 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1725 event->keyval == GDK_KEY_Tab) {
1726 GtkTextBuffer *buffer;
1727 GtkTextIter start, current;
1728 gchar *nick, *completed;
1729 GList *list, *completed_list;
1730 gboolean is_start_of_buffer;
1732 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1733 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1735 /* Get the start of the nick to complete. */
1736 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1737 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1738 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1740 is_start_of_buffer = gtk_text_iter_is_start (&start);
1742 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1743 g_completion_add_items (priv->completion, list);
1745 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1746 completed_list = g_completion_complete (priv->completion,
1755 GString *message = NULL;
1758 gtk_text_buffer_delete (buffer, &start, ¤t);
1760 len = g_list_length (completed_list);
1763 /* If we only have one hit, use that text
1764 * instead of the text in completed since the
1765 * completed text will use the typed string
1766 * which might be cased all wrong.
1769 text = empathy_contact_get_alias (completed_list->data);
1773 /* Print all hits to the scrollback view, so the
1774 * user knows what possibilities he has.
1777 message = g_string_new ("");
1778 for (l = completed_list; l != NULL; l = l->next) {
1779 g_string_append (message, empathy_contact_get_alias (l->data));
1780 g_string_append (message, " - ");
1782 empathy_chat_view_append_event (chat->view, message->str);
1783 g_string_free (message, TRUE);
1786 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1788 if (len == 1 && is_start_of_buffer) {
1789 gchar *complete_char;
1791 complete_char = g_settings_get_string (
1792 priv->gsettings_chat,
1793 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1795 if (complete_char != NULL) {
1796 gtk_text_buffer_insert_at_cursor (buffer,
1798 strlen (complete_char));
1799 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1800 g_free (complete_char);
1807 g_completion_clear_items (priv->completion);
1809 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1819 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1823 gtk_widget_grab_focus (chat->input_text_view);
1829 chat_input_realize_cb (GtkWidget *widget,
1832 DEBUG ("Setting focus to the input text view");
1833 if (gtk_widget_is_sensitive (widget)) {
1834 gtk_widget_grab_focus (widget);
1839 chat_input_has_focus_notify_cb (GtkWidget *widget,
1843 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1847 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1848 EmpathySmiley *smiley,
1851 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1852 GtkTextBuffer *buffer;
1855 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1857 gtk_text_buffer_get_end_iter (buffer, &iter);
1858 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1860 gtk_text_buffer_get_end_iter (buffer, &iter);
1861 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1872 static EmpathyChatSpell *
1873 chat_spell_new (EmpathyChat *chat,
1878 EmpathyChatSpell *chat_spell;
1880 chat_spell = g_slice_new0 (EmpathyChatSpell);
1882 chat_spell->chat = g_object_ref (chat);
1883 chat_spell->word = g_strdup (word);
1884 chat_spell->start = start;
1885 chat_spell->end = end;
1891 chat_spell_free (EmpathyChatSpell *chat_spell)
1893 g_object_unref (chat_spell->chat);
1894 g_free (chat_spell->word);
1895 g_slice_free (EmpathyChatSpell, chat_spell);
1899 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1900 EmpathyChatSpell *chat_spell)
1902 empathy_chat_correct_word (chat_spell->chat,
1903 &(chat_spell->start),
1905 gtk_menu_item_get_label (menu_item));
1910 chat_spelling_build_suggestions_menu (const gchar *code,
1911 EmpathyChatSpell *chat_spell)
1913 GList *suggestions, *l;
1914 GtkWidget *menu, *menu_item;
1916 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1917 if (suggestions == NULL)
1920 menu = gtk_menu_new ();
1921 for (l = suggestions; l; l = l->next) {
1922 menu_item = gtk_menu_item_new_with_label (l->data);
1923 g_signal_connect (G_OBJECT (menu_item), "activate",
1924 G_CALLBACK (chat_spelling_menu_activate_cb),
1926 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1928 empathy_spell_free_suggestions (suggestions);
1930 gtk_widget_show_all (menu);
1936 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1938 GtkWidget *menu, *submenu, *item;
1941 codes = empathy_spell_get_enabled_language_codes ();
1942 g_assert (codes != NULL);
1944 if (g_list_length (codes) > 1) {
1945 menu = gtk_menu_new ();
1947 for (l = codes; l; l = l->next) {
1948 const gchar *code, *name;
1951 name = empathy_spell_get_language_name (code);
1955 item = gtk_image_menu_item_new_with_label (name);
1957 submenu = chat_spelling_build_suggestions_menu (
1959 if (submenu == NULL)
1960 gtk_widget_set_sensitive (item, FALSE);
1962 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1964 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1967 menu = chat_spelling_build_suggestions_menu (codes->data,
1970 menu = gtk_menu_new ();
1971 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1972 gtk_widget_set_sensitive (item, FALSE);
1973 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1976 g_list_free (codes);
1978 gtk_widget_show_all (menu);
1989 static EmpathyChatWord *
1990 chat_word_new (EmpathyChat *chat,
1994 EmpathyChatWord *chat_word;
1996 chat_word = g_slice_new0 (EmpathyChatWord);
1998 chat_word->chat = g_object_ref (chat);
1999 chat_word->word = g_strdup (word);
2000 chat_word->code = g_strdup (code);
2006 chat_word_free (EmpathyChatWord *chat_word)
2008 g_object_unref (chat_word->chat);
2009 g_free (chat_word->word);
2010 g_free (chat_word->code);
2011 g_slice_free (EmpathyChatWord, chat_word);
2015 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2016 EmpathyChatWord *chat_word)
2018 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2020 empathy_spell_add_to_dictionary (chat_word->code,
2022 priv->update_misspelled_words_id =
2023 g_idle_add (update_misspelled_words, chat_word->chat);
2027 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2029 GtkWidget *menu, *item, *lang_item, *image;
2032 const gchar *code, *name;
2033 EmpathyChatWord *chat_word;
2035 codes = empathy_spell_get_enabled_language_codes ();
2036 g_assert (codes != NULL);
2037 if (g_list_length (codes) > 1) {
2038 /* translators: %s is the selected word */
2039 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2041 item = gtk_image_menu_item_new_with_mnemonic (label);
2043 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2044 GTK_ICON_SIZE_MENU);
2045 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2048 menu = gtk_menu_new ();
2050 for (l = codes; l; l = l->next) {
2052 name = empathy_spell_get_language_name (code);
2056 lang_item = gtk_image_menu_item_new_with_label (name);
2058 chat_word= chat_word_new (chat_spell->chat,
2059 chat_spell->word, code);
2060 g_object_set_data_full (G_OBJECT (lang_item),
2061 "chat-word", chat_word,
2062 (GDestroyNotify) chat_word_free);
2064 g_signal_connect (G_OBJECT (lang_item), "activate",
2065 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2067 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2069 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2072 name = empathy_spell_get_language_name (code);
2073 g_assert (name != NULL);
2074 /* translators: first %s is the selected word,
2075 * second %s is the language name of the target dictionary */
2076 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2077 chat_spell->word, name);
2078 item = gtk_image_menu_item_new_with_mnemonic (label);
2080 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2081 GTK_ICON_SIZE_MENU);
2082 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2084 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2086 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2087 (GDestroyNotify) chat_word_free);
2089 g_signal_connect (G_OBJECT (item), "activate",
2090 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2093 g_list_free (codes);
2095 gtk_widget_show_all (item);
2101 chat_text_send_cb (GtkMenuItem *menuitem,
2104 chat_input_text_view_send (chat);
2108 chat_input_populate_popup_cb (GtkTextView *view,
2112 GtkTextBuffer *buffer;
2113 GtkTextTagTable *table;
2116 GtkTextIter iter, start, end;
2119 EmpathyChatSpell *chat_spell;
2120 GtkWidget *spell_menu;
2121 GtkWidget *spell_item;
2122 EmpathySmileyManager *smiley_manager;
2123 GtkWidget *smiley_menu;
2126 buffer = gtk_text_view_get_buffer (view);
2128 /* Add the emoticon menu. */
2129 item = gtk_separator_menu_item_new ();
2130 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2131 gtk_widget_show (item);
2133 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2134 image = gtk_image_new_from_icon_name ("face-smile",
2135 GTK_ICON_SIZE_MENU);
2136 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2137 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2138 gtk_widget_show (item);
2140 smiley_manager = empathy_smiley_manager_dup_singleton ();
2141 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2142 chat_insert_smiley_activate_cb,
2144 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2145 g_object_unref (smiley_manager);
2147 /* Add the Send menu item. */
2148 gtk_text_buffer_get_bounds (buffer, &start, &end);
2149 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2150 if (!EMP_STR_EMPTY (str)) {
2151 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2152 g_signal_connect (G_OBJECT (item), "activate",
2153 G_CALLBACK (chat_text_send_cb), chat);
2154 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2155 gtk_widget_show (item);
2159 /* Add the spell check menu item. */
2160 table = gtk_text_buffer_get_tag_table (buffer);
2161 tag = gtk_text_tag_table_lookup (table, "misspelled");
2162 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2163 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2164 GTK_TEXT_WINDOW_WIDGET,
2167 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2169 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2170 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2172 str = gtk_text_buffer_get_text (buffer,
2173 &start, &end, FALSE);
2175 if (!EMP_STR_EMPTY (str)) {
2176 chat_spell = chat_spell_new (chat, str, start, end);
2177 g_object_set_data_full (G_OBJECT (menu),
2178 "chat-spell", chat_spell,
2179 (GDestroyNotify) chat_spell_free);
2181 item = gtk_separator_menu_item_new ();
2182 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2183 gtk_widget_show (item);
2185 /* Spelling suggestions */
2186 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2187 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2188 GTK_ICON_SIZE_MENU);
2189 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2191 spell_menu = chat_spelling_build_menu (chat_spell);
2192 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2195 spell_item = gtk_separator_menu_item_new ();
2196 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2197 gtk_widget_show (spell_item);
2199 /* Add to dictionary */
2200 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2202 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2203 gtk_widget_show (spell_item);
2205 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2206 gtk_widget_show (item);
2212 chat_log_filter (TplEvent *log,
2215 EmpathyChat *chat = user_data;
2216 EmpathyMessage *message;
2217 EmpathyChatPriv *priv = GET_PRIV (chat);
2218 const GList *pending;
2220 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2221 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2223 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2224 message = empathy_message_from_tpl_log_event (log);
2226 for (; pending; pending = g_list_next (pending)) {
2227 if (empathy_message_equal (message, pending->data)) {
2228 g_object_unref (message);
2233 g_object_unref (message);
2239 show_pending_messages (EmpathyChat *chat) {
2240 EmpathyChatPriv *priv = GET_PRIV (chat);
2241 const GList *messages, *l;
2243 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2245 if (chat->view == NULL || priv->tp_chat == NULL)
2248 if (!priv->can_show_pending)
2251 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2253 for (l = messages; l != NULL ; l = g_list_next (l)) {
2254 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2255 chat_message_received (chat, message, TRUE);
2261 got_filtered_messages_cb (GObject *manager,
2262 GAsyncResult *result,
2267 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2268 EmpathyChatPriv *priv = GET_PRIV (chat);
2269 GError *error = NULL;
2271 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2272 result, &messages, &error)) {
2273 DEBUG ("%s. Aborting.", error->message);
2274 empathy_chat_view_append_event (chat->view,
2275 _("Failed to retrieve recent logs"));
2276 g_error_free (error);
2280 for (l = messages; l; l = g_list_next (l)) {
2281 EmpathyMessage *message;
2282 g_assert (TPL_IS_EVENT (l->data));
2284 message = empathy_message_from_tpl_log_event (l->data);
2285 g_object_unref (l->data);
2287 empathy_chat_view_append_message (chat->view, message);
2288 g_object_unref (message);
2290 g_list_free (messages);
2293 /* in case of TPL error, skip backlog and show pending messages */
2294 priv->can_show_pending = TRUE;
2295 show_pending_messages (chat);
2297 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2298 * about it in EmpathyChatPriv definition */
2299 priv->retrieving_backlogs = FALSE;
2300 empathy_chat_messages_read (chat);
2302 /* Turn back on scrolling */
2303 empathy_chat_view_scroll (chat->view, TRUE);
2307 chat_add_logs (EmpathyChat *chat)
2309 EmpathyChatPriv *priv = GET_PRIV (chat);
2316 /* Turn off scrolling temporarily */
2317 empathy_chat_view_scroll (chat->view, FALSE);
2319 /* Add messages from last conversation */
2320 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2321 target = tpl_entity_new_from_room_id (priv->id);
2323 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2325 priv->retrieving_backlogs = TRUE;
2326 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2329 TPL_EVENT_MASK_TEXT,
2333 got_filtered_messages_cb,
2336 g_object_unref (target);
2340 chat_contacts_completion_func (const gchar *s1,
2344 gchar *tmp, *nick1, *nick2;
2351 return s1 ? -1 : +1;
2354 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2355 nick1 = g_utf8_casefold (tmp, -1);
2358 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2359 nick2 = g_utf8_casefold (tmp, -1);
2362 ret = strncmp (nick1, nick2, n);
2371 build_part_message (guint reason,
2373 EmpathyContact *actor,
2374 const gchar *message)
2376 GString *s = g_string_new ("");
2377 const gchar *actor_name = NULL;
2379 if (actor != NULL) {
2380 actor_name = empathy_contact_get_alias (actor);
2383 /* Having an actor only really makes sense for a few actions... */
2385 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2386 g_string_append_printf (s, _("%s has disconnected"), name);
2388 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2389 if (actor_name != NULL) {
2390 /* translators: reverse the order of these arguments
2391 * if the kicked should come before the kicker in your locale.
2393 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2396 g_string_append_printf (s, _("%s was kicked"), name);
2399 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2400 if (actor_name != NULL) {
2401 /* translators: reverse the order of these arguments
2402 * if the banned should come before the banner in your locale.
2404 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2407 g_string_append_printf (s, _("%s was banned"), name);
2411 g_string_append_printf (s, _("%s has left the room"), name);
2414 if (!EMP_STR_EMPTY (message)) {
2415 /* Note to translators: this string is appended to
2416 * notifications like "foo has left the room", with the message
2417 * given by the user living the room. If this poses a problem,
2418 * please let us know. :-)
2420 g_string_append_printf (s, _(" (%s)"), message);
2423 return g_string_free (s, FALSE);
2427 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2428 EmpathyContact *contact,
2429 EmpathyContact *actor,
2435 EmpathyChatPriv *priv = GET_PRIV (chat);
2436 const gchar *name = empathy_contact_get_alias (contact);
2439 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2441 if (priv->block_events_timeout_id != 0)
2445 str = g_strdup_printf (_("%s has joined the room"),
2448 str = build_part_message (reason, name, actor, message);
2451 empathy_chat_view_append_event (chat->view, str);
2456 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2457 EmpathyContact *old_contact,
2458 EmpathyContact *new_contact,
2463 EmpathyChatPriv *priv = GET_PRIV (chat);
2465 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2467 if (priv->block_events_timeout_id == 0) {
2470 str = g_strdup_printf (_("%s is now known as %s"),
2471 empathy_contact_get_alias (old_contact),
2472 empathy_contact_get_alias (new_contact));
2473 empathy_chat_view_append_event (chat->view, str);
2480 chat_reset_size_request (gpointer widget)
2482 gtk_widget_set_size_request (widget, -1, -1);
2488 chat_update_contacts_visibility (EmpathyChat *chat,
2491 EmpathyChatPriv *priv = GET_PRIV (chat);
2492 GtkAllocation allocation;
2494 if (!priv->scrolled_window_contacts) {
2498 if (priv->remote_contact != NULL) {
2502 if (show && priv->contact_list_view == NULL) {
2503 EmpathyContactListStore *store;
2506 /* We are adding the contact list to the chat, we don't want the
2507 * chat view to become too small. If the chat view is already
2508 * smaller than 250 make sure that size won't change. If the
2509 * chat view is bigger the contact list will take some space on
2510 * it but we make sure the chat view don't become smaller than
2511 * 250. Relax the size request once the resize is done */
2512 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2513 min_width = MIN (allocation.width, 250);
2514 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2515 g_idle_add (chat_reset_size_request, priv->vbox_left);
2517 if (priv->contacts_width > 0) {
2518 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2519 priv->contacts_width);
2522 store = empathy_contact_list_store_new (
2523 EMPATHY_CONTACT_LIST (priv->tp_chat));
2524 empathy_contact_list_store_set_show_groups (
2525 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2527 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2528 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2529 EMPATHY_CONTACT_FEATURE_CHAT |
2530 EMPATHY_CONTACT_FEATURE_CALL |
2531 EMPATHY_CONTACT_FEATURE_LOG |
2532 EMPATHY_CONTACT_FEATURE_INFO));
2533 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2534 priv->contact_list_view);
2535 gtk_widget_show (priv->contact_list_view);
2536 gtk_widget_show (priv->scrolled_window_contacts);
2537 g_object_unref (store);
2539 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2540 gtk_widget_hide (priv->scrolled_window_contacts);
2541 if (priv->contact_list_view != NULL) {
2542 gtk_widget_destroy (priv->contact_list_view);
2543 priv->contact_list_view = NULL;
2549 empathy_chat_set_show_contacts (EmpathyChat *chat,
2552 EmpathyChatPriv *priv = GET_PRIV (chat);
2554 priv->show_contacts = show;
2556 chat_update_contacts_visibility (chat, show);
2558 g_object_notify (G_OBJECT (chat), "show-contacts");
2562 chat_remote_contact_changed_cb (EmpathyChat *chat)
2564 EmpathyChatPriv *priv = GET_PRIV (chat);
2566 if (priv->remote_contact != NULL) {
2567 g_object_unref (priv->remote_contact);
2568 priv->remote_contact = NULL;
2573 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2574 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2575 if (priv->remote_contact != NULL) {
2576 g_object_ref (priv->remote_contact);
2577 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2579 else if (priv->tp_chat != NULL) {
2582 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2583 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2586 chat_update_contacts_visibility (chat, priv->show_contacts);
2588 g_object_notify (G_OBJECT (chat), "remote-contact");
2589 g_object_notify (G_OBJECT (chat), "id");
2593 chat_destroy_cb (EmpathyTpChat *tp_chat,
2596 EmpathyChatPriv *priv;
2598 priv = GET_PRIV (chat);
2600 if (!priv->tp_chat) {
2604 chat_composing_remove_timeout (chat);
2605 g_object_unref (priv->tp_chat);
2606 priv->tp_chat = NULL;
2607 g_object_notify (G_OBJECT (chat), "tp-chat");
2609 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2610 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2612 chat_update_contacts_visibility (chat, FALSE);
2616 update_misspelled_words (gpointer data)
2618 EmpathyChat *chat = EMPATHY_CHAT (data);
2619 EmpathyChatPriv *priv = GET_PRIV (chat);
2620 GtkTextBuffer *buffer;
2624 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2626 gtk_text_buffer_get_end_iter (buffer, &iter);
2627 length = gtk_text_iter_get_offset (&iter);
2628 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2629 NULL, length, chat);
2631 priv->update_misspelled_words_id = 0;
2637 conf_spell_checking_cb (GSettings *gsettings_chat,
2641 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2642 EmpathyChatPriv *priv = GET_PRIV (chat);
2643 gboolean spell_checker;
2644 GtkTextBuffer *buffer;
2646 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2649 spell_checker = g_settings_get_boolean (gsettings_chat,
2650 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2652 if (!empathy_spell_supported ()) {
2653 spell_checker = FALSE;
2656 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2658 if (spell_checker == priv->spell_checking_enabled) {
2659 if (spell_checker) {
2660 /* Possibly changed dictionaries,
2661 * update misspelled words. Need to do so in idle
2662 * so the spell checker is updated. */
2663 priv->update_misspelled_words_id =
2664 g_idle_add (update_misspelled_words, chat);
2670 if (spell_checker) {
2673 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2674 buffer, "notify::cursor-position",
2675 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2677 priv->insert_text_id = tp_g_signal_connect_object (
2678 buffer, "insert-text",
2679 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2680 chat, G_CONNECT_AFTER);
2681 priv->delete_range_id = tp_g_signal_connect_object (
2682 buffer, "delete-range",
2683 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2684 chat, G_CONNECT_AFTER);
2686 gtk_text_buffer_create_tag (buffer, "misspelled",
2687 "underline", PANGO_UNDERLINE_ERROR,
2690 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2691 gtk_text_buffer_get_insert (buffer));
2692 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2695 /* Mark misspelled words in the existing buffer.
2696 * Need to do so in idle so the spell checker is updated. */
2697 priv->update_misspelled_words_id =
2698 g_idle_add (update_misspelled_words, chat);
2700 GtkTextTagTable *table;
2703 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2704 priv->notify_cursor_position_id = 0;
2705 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2706 priv->insert_text_id = 0;
2707 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2708 priv->delete_range_id = 0;
2710 table = gtk_text_buffer_get_tag_table (buffer);
2711 tag = gtk_text_tag_table_lookup (table, "misspelled");
2712 gtk_text_tag_table_remove (table, tag);
2714 gtk_text_buffer_delete_mark_by_name (buffer,
2715 "previous-cursor-position");
2718 priv->spell_checking_enabled = spell_checker;
2722 save_paned_pos_timeout (gpointer data)
2724 EmpathyChat *self = data;
2727 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2729 g_settings_set_int (self->priv->gsettings_ui,
2730 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2737 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2741 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2743 if (chat->priv->save_paned_pos_id != 0)
2744 g_source_remove (chat->priv->save_paned_pos_id);
2746 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2747 save_paned_pos_timeout, chat);
2753 chat_create_ui (EmpathyChat *chat)
2755 EmpathyChatPriv *priv = GET_PRIV (chat);
2759 GtkTextBuffer *buffer;
2761 EmpathyThemeManager *theme_mgr;
2763 filename = empathy_file_lookup ("empathy-chat.ui",
2765 gui = empathy_builder_get_file (filename,
2766 "chat_widget", &priv->widget,
2767 "hpaned", &priv->hpaned,
2768 "vbox_left", &priv->vbox_left,
2769 "scrolled_window_chat", &priv->scrolled_window_chat,
2770 "scrolled_window_input", &priv->scrolled_window_input,
2771 "hbox_topic", &priv->hbox_topic,
2772 "expander_topic", &priv->expander_topic,
2773 "label_topic", &priv->label_topic,
2774 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2775 "info_bar_vbox", &priv->info_bar_vbox,
2778 empathy_builder_connect (gui, chat,
2779 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2780 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2785 /* Add message view. */
2786 theme_mgr = empathy_theme_manager_dup_singleton ();
2787 chat->view = empathy_theme_manager_create_view (theme_mgr);
2788 g_object_unref (theme_mgr);
2789 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2790 and other types, even though it's non-editable and doesn't accept any
2791 drags. This steals drag motion for anything inside the scrollbars,
2792 making drag destinations on chat windows far less useful.
2794 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2795 g_signal_connect (chat->view, "focus_in_event",
2796 G_CALLBACK (chat_text_view_focus_in_event_cb),
2798 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2799 GTK_WIDGET (chat->view));
2800 gtk_widget_show (GTK_WIDGET (chat->view));
2802 /* Add input GtkTextView */
2803 chat->input_text_view = empathy_input_text_view_new ();
2804 g_signal_connect (chat->input_text_view, "notify::has-focus",
2805 G_CALLBACK (chat_input_has_focus_notify_cb),
2807 g_signal_connect (chat->input_text_view, "key-press-event",
2808 G_CALLBACK (chat_input_key_press_event_cb),
2810 g_signal_connect (chat->input_text_view, "realize",
2811 G_CALLBACK (chat_input_realize_cb),
2813 g_signal_connect (chat->input_text_view, "populate-popup",
2814 G_CALLBACK (chat_input_populate_popup_cb),
2816 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2817 tp_g_signal_connect_object (buffer, "changed",
2818 G_CALLBACK (chat_input_text_buffer_changed_cb),
2820 tp_g_signal_connect_object (priv->gsettings_chat,
2821 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2822 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2823 conf_spell_checking_cb (priv->gsettings_chat,
2824 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2825 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2826 chat->input_text_view);
2827 gtk_widget_show (chat->input_text_view);
2829 /* Add the (invisible) search bar */
2830 priv->search_bar = empathy_search_bar_new (chat->view);
2831 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2834 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2836 /* Initialy hide the topic, will be shown if not empty */
2837 gtk_widget_hide (priv->hbox_topic);
2839 g_signal_connect (priv->hpaned, "notify::position",
2840 G_CALLBACK (chat_hpaned_pos_changed_cb),
2843 /* Load the paned position */
2844 paned_pos = g_settings_get_int (priv->gsettings_ui,
2845 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2847 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2849 /* Set widget focus order */
2850 list = g_list_append (NULL, priv->search_bar);
2851 list = g_list_append (list, priv->scrolled_window_input);
2852 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2855 list = g_list_append (NULL, priv->vbox_left);
2856 list = g_list_append (list, priv->scrolled_window_contacts);
2857 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2860 list = g_list_append (NULL, priv->hpaned);
2861 list = g_list_append (list, priv->hbox_topic);
2862 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2865 /* Add the main widget in the chat widget */
2866 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2867 g_object_unref (gui);
2871 chat_size_allocate (GtkWidget *widget,
2872 GtkAllocation *allocation)
2874 GtkBin *bin = GTK_BIN (widget);
2875 GtkAllocation child_allocation;
2878 gtk_widget_set_allocation (widget, allocation);
2880 child = gtk_bin_get_child (bin);
2882 if (child && gtk_widget_get_visible (child))
2884 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2885 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2886 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2887 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2889 gtk_widget_size_allocate (child, &child_allocation);
2894 chat_finalize (GObject *object)
2897 EmpathyChatPriv *priv;
2899 chat = EMPATHY_CHAT (object);
2900 priv = GET_PRIV (chat);
2902 DEBUG ("Finalized: %p", object);
2904 if (priv->update_misspelled_words_id != 0)
2905 g_source_remove (priv->update_misspelled_words_id);
2907 if (priv->save_paned_pos_id != 0)
2908 g_source_remove (priv->save_paned_pos_id);
2910 g_object_unref (priv->gsettings_chat);
2911 g_object_unref (priv->gsettings_ui);
2913 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2914 g_list_free (priv->input_history);
2916 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2917 g_list_free (priv->compositors);
2919 chat_composing_remove_timeout (chat);
2921 g_object_unref (priv->account_manager);
2922 g_object_unref (priv->log_manager);
2924 if (priv->tp_chat) {
2925 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2926 chat_destroy_cb, chat);
2927 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2928 chat_message_received_cb, chat);
2929 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2930 chat_send_error_cb, chat);
2931 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2932 chat_state_changed_cb, chat);
2933 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2934 chat_property_changed_cb, chat);
2935 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2936 chat_members_changed_cb, chat);
2937 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2938 chat_remote_contact_changed_cb, chat);
2939 empathy_tp_chat_leave (priv->tp_chat, "");
2940 g_object_unref (priv->tp_chat);
2942 if (priv->account) {
2943 g_object_unref (priv->account);
2945 if (priv->remote_contact) {
2946 g_object_unref (priv->remote_contact);
2949 if (priv->block_events_timeout_id) {
2950 g_source_remove (priv->block_events_timeout_id);
2954 g_free (priv->name);
2955 g_free (priv->subject);
2956 g_completion_free (priv->completion);
2958 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2962 chat_constructed (GObject *object)
2964 EmpathyChat *chat = EMPATHY_CHAT (object);
2965 EmpathyChatPriv *priv = GET_PRIV (chat);
2967 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2968 /* First display logs from the logger and then display pending messages */
2969 chat_add_logs (chat);
2972 /* Just display pending messages for rooms */
2973 priv->can_show_pending = TRUE;
2974 show_pending_messages (chat);
2979 empathy_chat_class_init (EmpathyChatClass *klass)
2981 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2982 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2984 object_class->finalize = chat_finalize;
2985 object_class->get_property = chat_get_property;
2986 object_class->set_property = chat_set_property;
2987 object_class->constructed = chat_constructed;
2989 widget_class->size_allocate = chat_size_allocate;
2991 g_object_class_install_property (object_class,
2993 g_param_spec_object ("tp-chat",
2995 "The tp chat object",
2996 EMPATHY_TYPE_TP_CHAT,
2999 G_PARAM_STATIC_STRINGS));
3000 g_object_class_install_property (object_class,
3002 g_param_spec_object ("account",
3003 "Account of the chat",
3004 "The account of the chat",
3007 G_PARAM_STATIC_STRINGS));
3008 g_object_class_install_property (object_class,
3010 g_param_spec_string ("id",
3012 "The id of the chat",
3015 G_PARAM_STATIC_STRINGS));
3016 g_object_class_install_property (object_class,
3018 g_param_spec_string ("name",
3020 "The name of the chat",
3023 G_PARAM_STATIC_STRINGS));
3024 g_object_class_install_property (object_class,
3026 g_param_spec_string ("subject",
3028 "The subject or topic of the chat",
3031 G_PARAM_STATIC_STRINGS));
3032 g_object_class_install_property (object_class,
3033 PROP_REMOTE_CONTACT,
3034 g_param_spec_object ("remote-contact",
3035 "The remote contact",
3036 "The remote contact is any",
3037 EMPATHY_TYPE_CONTACT,
3039 G_PARAM_STATIC_STRINGS));
3040 g_object_class_install_property (object_class,
3042 g_param_spec_boolean ("show-contacts",
3043 "Contacts' visibility",
3044 "The visibility of the contacts' list",
3047 G_PARAM_STATIC_STRINGS));
3049 g_object_class_install_property (object_class,
3051 g_param_spec_boolean ("sms-channel",
3053 "TRUE if this channel is for sending SMSes",
3055 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3057 g_object_class_install_property (object_class,
3058 PROP_N_MESSAGES_SENDING,
3059 g_param_spec_uint ("n-messages-sending",
3060 "Num Messages Sending",
3061 "The number of messages being sent",
3063 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3065 signals[COMPOSING] =
3066 g_signal_new ("composing",
3067 G_OBJECT_CLASS_TYPE (object_class),
3071 g_cclosure_marshal_VOID__BOOLEAN,
3075 signals[NEW_MESSAGE] =
3076 g_signal_new ("new-message",
3077 G_OBJECT_CLASS_TYPE (object_class),
3081 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3083 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3085 signals[PART_COMMAND_ENTERED] =
3086 g_signal_new ("part-command-entered",
3087 G_OBJECT_CLASS_TYPE (object_class),
3091 g_cclosure_marshal_VOID__POINTER,
3095 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3099 chat_block_events_timeout_cb (gpointer data)
3101 EmpathyChatPriv *priv = GET_PRIV (data);
3103 priv->block_events_timeout_id = 0;
3109 account_manager_prepared_cb (GObject *source_object,
3110 GAsyncResult *result,
3113 GList *accounts, *l;
3114 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3115 EmpathyChat *chat = user_data;
3116 GError *error = NULL;
3118 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3119 DEBUG ("Failed to prepare the account manager: %s", error->message);
3120 g_error_free (error);
3124 accounts = tp_account_manager_get_valid_accounts (account_manager);
3126 for (l = accounts; l != NULL; l = l->next) {
3127 TpAccount *account = l->data;
3128 tp_g_signal_connect_object (account, "status-changed",
3129 G_CALLBACK (chat_new_connection_cb),
3133 g_list_free (accounts);
3137 empathy_chat_init (EmpathyChat *chat)
3139 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3140 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3143 priv->log_manager = tpl_log_manager_dup_singleton ();
3144 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3145 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3147 priv->contacts_width = -1;
3148 priv->input_history = NULL;
3149 priv->input_history_current = NULL;
3150 priv->account_manager = tp_account_manager_dup ();
3152 tp_account_manager_prepare_async (priv->account_manager, NULL,
3153 account_manager_prepared_cb, chat);
3155 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3156 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3158 /* Block events for some time to avoid having "has come online" or
3159 * "joined" messages. */
3160 priv->block_events_timeout_id =
3161 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3163 /* Add nick name completion */
3164 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3165 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3167 chat_create_ui (chat);
3171 empathy_chat_new (EmpathyTpChat *tp_chat)
3173 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3177 empathy_chat_get_tp_chat (EmpathyChat *chat)
3179 EmpathyChatPriv *priv = GET_PRIV (chat);
3181 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3183 return priv->tp_chat;
3189 GtkWidget *info_bar;
3199 passwd_remember_button_cb (GtkButton *button,
3202 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3206 passwd_not_now_button_cb (GtkButton *button,
3209 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3213 remember_password_infobar_response_cb (GtkWidget *info_bar,
3217 EmpathyChatPriv *priv = GET_PRIV (data->self);
3219 if (response_id == GTK_RESPONSE_OK) {
3220 DEBUG ("Saving room password");
3221 empathy_keyring_set_room_password_async (priv->account,
3222 empathy_tp_chat_get_id (priv->tp_chat),
3227 gtk_widget_destroy (info_bar);
3228 g_free (data->password);
3229 g_slice_free (PasswordData, data);
3233 chat_prompt_to_save_password (EmpathyChat *self,
3236 GtkWidget *content_area;
3243 /* save the password in case it needs to be saved */
3244 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3246 /* Remove all previous widgets */
3247 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3248 gtk_container_forall (GTK_CONTAINER (content_area),
3249 (GtkCallback) gtk_widget_destroy, NULL);
3250 data->button = NULL;
3253 data->spinner = NULL;
3255 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3256 GTK_MESSAGE_QUESTION);
3258 hbox = gtk_hbox_new (FALSE, 5);
3259 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3262 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3263 GTK_ICON_SIZE_DIALOG);
3264 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3267 label = gtk_label_new (_("Would you like to store this password?"));
3268 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3270 /* Add 'Remember' button */
3271 alig = gtk_alignment_new (0, 0.5, 1, 0);
3273 button = gtk_button_new_with_label (_("Remember"));
3274 gtk_container_add (GTK_CONTAINER (alig), button);
3275 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3277 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3280 /* Add 'Not now' button */
3281 alig = gtk_alignment_new (0, 0.5, 1, 0);
3283 button = gtk_button_new_with_label (_("Not now"));
3284 gtk_container_add (GTK_CONTAINER (alig), button);
3285 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3287 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3291 g_signal_handler_disconnect (data->info_bar, data->response_id);
3292 g_signal_connect (data->info_bar, "response",
3293 G_CALLBACK (remember_password_infobar_response_cb), data);
3295 gtk_widget_show_all (data->info_bar);
3299 provide_password_cb (GObject *tp_chat,
3303 PasswordData *data = user_data;
3304 EmpathyChat *self = data->self;
3305 EmpathyChatPriv *priv = GET_PRIV (self);
3306 GError *error = NULL;
3308 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3310 DEBUG ("error: %s", error->message);
3311 /* FIXME: what should we do if that's another error? Close the channel?
3312 * Display the raw D-Bus error to the user isn't very useful */
3313 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3315 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3316 gtk_widget_set_sensitive (data->entry, TRUE);
3317 gtk_widget_grab_focus (data->entry);
3320 gtk_info_bar_set_message_type (
3321 GTK_INFO_BAR (data->info_bar),
3325 gtk_widget_set_sensitive (data->button, TRUE);
3326 gtk_button_set_label (GTK_BUTTON (data->button),
3330 gtk_label_set_text (GTK_LABEL (data->label),
3331 _("Wrong password; please try again:"));
3334 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3335 gtk_widget_hide (data->spinner);
3337 g_error_free (error);
3341 if (empathy_keyring_is_available ()) {
3342 /* ask whether they want to save the password */
3343 chat_prompt_to_save_password (self, data);
3345 /* Get rid of the password info bar finally */
3346 gtk_widget_destroy (data->info_bar);
3347 g_slice_free (PasswordData, data);
3351 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3352 gtk_widget_grab_focus (self->input_text_view);
3356 password_infobar_response_cb (GtkWidget *info_bar,
3360 EmpathyChatPriv *priv = GET_PRIV (data->self);
3361 const gchar *password;
3363 if (response_id != GTK_RESPONSE_OK) {
3364 gtk_widget_destroy (info_bar);
3365 g_slice_free (PasswordData, data);
3369 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3371 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3372 provide_password_cb, data);
3374 gtk_widget_set_sensitive (data->button, FALSE);
3375 gtk_widget_set_sensitive (data->entry, FALSE);
3377 gtk_spinner_start (GTK_SPINNER (data->spinner));
3378 gtk_widget_show (data->spinner);
3382 password_entry_activate_cb (GtkWidget *entry,
3385 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3389 passwd_join_button_cb (GtkButton *button,
3392 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3396 clear_icon_released_cb (GtkEntry *entry,
3397 GtkEntryIconPosition icon_pos,
3401 gtk_entry_set_text (entry, "");
3405 password_entry_changed_cb (GtkEditable *entry,
3410 str = gtk_entry_get_text (GTK_ENTRY (entry));
3412 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3413 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3417 chat_invalidated_cb (TpProxy *proxy,
3421 gpointer password_infobar)
3423 /* Destroy the password infobar whenever a channel is invalidated
3424 * so we don't have multiple infobars when the MUC is rejoined */
3425 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3429 display_password_info_bar (EmpathyChat *self)
3431 EmpathyChatPriv *priv = GET_PRIV (self);
3432 GtkWidget *info_bar;
3433 GtkWidget *content_area;
3443 data = g_slice_new0 (PasswordData);
3445 info_bar = gtk_info_bar_new ();
3446 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3447 GTK_MESSAGE_QUESTION);
3449 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3451 hbox = gtk_hbox_new (FALSE, 5);
3452 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3455 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3456 GTK_ICON_SIZE_DIALOG);
3457 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3460 label = gtk_label_new (_("This room is protected by a password:"));
3461 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3463 /* Add password entry */
3464 entry = gtk_entry_new ();
3465 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3466 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3468 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3469 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3470 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3471 GTK_ENTRY_ICON_SECONDARY, FALSE);
3473 g_signal_connect (entry, "icon-release",
3474 G_CALLBACK (clear_icon_released_cb), data);
3475 g_signal_connect (entry, "changed",
3476 G_CALLBACK (password_entry_changed_cb), data);
3478 g_signal_connect (entry, "activate",
3479 G_CALLBACK (password_entry_activate_cb), data);
3481 /* Focus the password entry once it's realized */
3482 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3484 /* Add 'Join' button */
3485 alig = gtk_alignment_new (0, 0.5, 1, 0);
3487 button = gtk_button_new_with_label (_("Join"));
3488 gtk_container_add (GTK_CONTAINER (alig), button);
3489 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3491 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3495 spinner = gtk_spinner_new ();
3496 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3498 /* Save some data for messing around with later */
3500 data->info_bar = info_bar;
3501 data->button = button;
3502 data->label = label;
3503 data->entry = entry;
3504 data->spinner = spinner;
3506 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3508 gtk_widget_show_all (hbox);
3510 tp_g_signal_connect_object (empathy_tp_chat_get_channel (priv->tp_chat),
3511 "invalidated", G_CALLBACK (chat_invalidated_cb),
3514 data->response_id = g_signal_connect (info_bar, "response",
3515 G_CALLBACK (password_infobar_response_cb), data);
3517 gtk_widget_show_all (info_bar);
3518 /* ... but hide the spinner */
3519 gtk_widget_hide (spinner);
3521 /* prevent the user from typing anything */
3522 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3526 provide_saved_password_cb (GObject *tp_chat,
3530 EmpathyChat *self = user_data;
3531 EmpathyChatPriv *priv = GET_PRIV (self);
3532 GError *error = NULL;
3534 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3536 DEBUG ("error: %s", error->message);
3537 /* FIXME: what should we do if that's another error? Close the channel?
3538 * Display the raw D-Bus error to the user isn't very useful */
3539 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3540 display_password_info_bar (self);
3541 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3543 g_error_free (error);
3548 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3549 gtk_widget_grab_focus (self->input_text_view);
3553 chat_room_got_password_cb (GObject *source,
3554 GAsyncResult *result,
3557 EmpathyChat *self = user_data;
3558 EmpathyChatPriv *priv = GET_PRIV (self);
3559 const gchar *password;
3560 GError *error = NULL;
3562 password = empathy_keyring_get_room_password_finish (priv->account,
3565 if (error != NULL) {
3566 DEBUG ("Couldn't get room password: %s\n", error->message);
3567 g_clear_error (&error);
3569 display_password_info_bar (self);
3570 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3574 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3575 provide_saved_password_cb, self);
3579 chat_password_needed_changed_cb (EmpathyChat *self)
3581 EmpathyChatPriv *priv = GET_PRIV (self);
3583 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3584 empathy_keyring_get_room_password_async (priv->account,
3585 empathy_tp_chat_get_id (priv->tp_chat),
3586 chat_room_got_password_cb, self);
3591 chat_sms_channel_changed_cb (EmpathyChat *self)
3593 EmpathyChatPriv *priv = GET_PRIV (self);
3595 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3596 g_object_notify (G_OBJECT (self), "sms-channel");
3600 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3602 g_object_notify (G_OBJECT (self), "n-messages-sending");
3606 empathy_chat_set_tp_chat (EmpathyChat *chat,
3607 EmpathyTpChat *tp_chat)
3609 EmpathyChatPriv *priv = GET_PRIV (chat);
3610 GPtrArray *properties;
3612 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3613 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3614 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3616 if (priv->tp_chat) {
3620 if (priv->account) {
3621 g_object_unref (priv->account);
3624 priv->tp_chat = g_object_ref (tp_chat);
3625 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3627 g_signal_connect (tp_chat, "destroy",
3628 G_CALLBACK (chat_destroy_cb),
3630 g_signal_connect (tp_chat, "message-received",
3631 G_CALLBACK (chat_message_received_cb),
3633 g_signal_connect (tp_chat, "send-error",
3634 G_CALLBACK (chat_send_error_cb),
3636 g_signal_connect (tp_chat, "chat-state-changed",
3637 G_CALLBACK (chat_state_changed_cb),
3639 g_signal_connect (tp_chat, "property-changed",
3640 G_CALLBACK (chat_property_changed_cb),
3642 g_signal_connect (tp_chat, "members-changed",
3643 G_CALLBACK (chat_members_changed_cb),
3645 g_signal_connect (tp_chat, "member-renamed",
3646 G_CALLBACK (chat_member_renamed_cb),
3648 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3649 G_CALLBACK (chat_remote_contact_changed_cb),
3651 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3652 G_CALLBACK (chat_password_needed_changed_cb),
3654 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3655 G_CALLBACK (chat_sms_channel_changed_cb),
3657 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3658 G_CALLBACK (chat_n_messages_sending_changed_cb),
3661 /* Get initial value of properties */
3662 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3663 if (properties != NULL) {
3666 for (i = 0; i < properties->len; i++) {
3667 EmpathyTpChatProperty *property;
3669 property = g_ptr_array_index (properties, i);
3670 if (property->value == NULL)
3673 chat_property_changed_cb (priv->tp_chat,
3680 chat_sms_channel_changed_cb (chat);
3681 chat_remote_contact_changed_cb (chat);
3683 if (chat->input_text_view) {
3684 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3685 if (priv->block_events_timeout_id == 0) {
3686 empathy_chat_view_append_event (chat->view, _("Connected"));
3690 g_object_notify (G_OBJECT (chat), "tp-chat");
3691 g_object_notify (G_OBJECT (chat), "id");
3692 g_object_notify (G_OBJECT (chat), "account");
3694 /* This is a noop when tp-chat is set at object construction time and causes
3695 * the pending messages to be show when it's set on the object after it has
3697 show_pending_messages (chat);
3699 /* check if a password is needed */
3700 chat_password_needed_changed_cb (chat);
3704 empathy_chat_get_account (EmpathyChat *chat)
3706 EmpathyChatPriv *priv = GET_PRIV (chat);
3708 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3710 return priv->account;
3714 empathy_chat_get_id (EmpathyChat *chat)
3716 EmpathyChatPriv *priv = GET_PRIV (chat);
3718 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3724 empathy_chat_dup_name (EmpathyChat *chat)
3726 EmpathyChatPriv *priv = GET_PRIV (chat);
3729 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3733 if (!ret && priv->remote_contact) {
3734 ret = empathy_contact_get_alias (priv->remote_contact);
3741 ret = _("Conversation");
3743 if (priv->sms_channel)
3744 /* Translators: this string is a something like
3745 * "Escher Cat (SMS)" */
3746 return g_strdup_printf (_("%s (SMS)"), ret);
3748 return g_strdup (ret);
3752 empathy_chat_get_subject (EmpathyChat *chat)
3754 EmpathyChatPriv *priv = GET_PRIV (chat);
3756 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3758 return priv->subject;
3762 empathy_chat_get_remote_contact (EmpathyChat *chat)
3764 EmpathyChatPriv *priv = GET_PRIV (chat);
3766 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3768 return priv->remote_contact;
3772 empathy_chat_get_contact_menu (EmpathyChat *chat)
3774 EmpathyChatPriv *priv = GET_PRIV (chat);
3775 GtkWidget *menu = NULL;
3777 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3779 if (priv->remote_contact) {
3780 menu = empathy_contact_menu_new (priv->remote_contact,
3781 EMPATHY_CONTACT_FEATURE_CALL |
3782 EMPATHY_CONTACT_FEATURE_LOG |
3783 EMPATHY_CONTACT_FEATURE_INFO |
3784 EMPATHY_CONTACT_FEATURE_BLOCK);
3791 empathy_chat_clear (EmpathyChat *chat)
3793 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3795 empathy_chat_view_clear (chat->view);
3799 empathy_chat_scroll_down (EmpathyChat *chat)
3801 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3803 empathy_chat_view_scroll_down (chat->view);
3807 empathy_chat_cut (EmpathyChat *chat)
3809 GtkTextBuffer *buffer;
3811 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3813 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3814 if (gtk_text_buffer_get_has_selection (buffer)) {
3815 GtkClipboard *clipboard;
3817 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3819 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3824 empathy_chat_copy (EmpathyChat *chat)
3826 GtkTextBuffer *buffer;
3828 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3830 if (empathy_chat_view_get_has_selection (chat->view)) {
3831 empathy_chat_view_copy_clipboard (chat->view);
3835 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3836 if (gtk_text_buffer_get_has_selection (buffer)) {
3837 GtkClipboard *clipboard;
3839 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3841 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3846 EmpathyChatPriv *priv = GET_PRIV (chat);
3848 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3855 GtkClipboard *clipboard;
3857 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3858 start = g_utf8_offset_to_pointer (topic, start_offset);
3859 end = g_utf8_offset_to_pointer (topic, end_offset);
3860 selection = g_strndup (start, end - start);
3862 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3863 gtk_clipboard_set_text (clipboard, selection, -1);
3871 empathy_chat_paste (EmpathyChat *chat)
3873 GtkTextBuffer *buffer;
3874 GtkClipboard *clipboard;
3875 EmpathyChatPriv *priv;
3877 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3879 priv = GET_PRIV (chat);
3881 if (gtk_widget_get_visible (priv->search_bar)) {
3882 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3886 if (priv->tp_chat == NULL ||
3887 !gtk_widget_is_sensitive (chat->input_text_view))
3890 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3891 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3893 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3897 empathy_chat_find (EmpathyChat *chat)
3899 EmpathyChatPriv *priv;
3901 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3903 priv = GET_PRIV (chat);
3905 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3909 empathy_chat_correct_word (EmpathyChat *chat,
3912 const gchar *new_word)
3914 GtkTextBuffer *buffer;
3916 g_return_if_fail (chat != NULL);
3917 g_return_if_fail (new_word != NULL);
3919 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3921 gtk_text_buffer_delete (buffer, start, end);
3922 gtk_text_buffer_insert (buffer, start,
3928 empathy_chat_is_room (EmpathyChat *chat)
3930 EmpathyChatPriv *priv = GET_PRIV (chat);
3932 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3934 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3938 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3940 EmpathyChatPriv *priv = GET_PRIV (self);
3942 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3944 return priv->unread_messages;
3947 /* called when the messages have been read by user */
3949 empathy_chat_messages_read (EmpathyChat *self)
3951 EmpathyChatPriv *priv = GET_PRIV (self);
3953 g_return_if_fail (EMPATHY_IS_CHAT (self));
3955 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3956 * definition. If we are still retrieving the backlogs, do not ACK */
3957 if (priv->retrieving_backlogs)
3960 if (priv->tp_chat != NULL ) {
3961 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3963 priv->unread_messages = 0;
3965 empathy_chat_view_focus_toggled (self->view, TRUE);
3968 /* Return TRUE if on of the contacts in this chat is composing */
3970 empathy_chat_is_composing (EmpathyChat *chat)
3972 return chat->priv->compositors != NULL;
3976 empathy_chat_is_sms_channel (EmpathyChat *self)
3978 EmpathyChatPriv *priv = GET_PRIV (self);
3980 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3982 return priv->sms_channel;
3986 empathy_chat_get_n_messages_sending (EmpathyChat *self)
3988 EmpathyChatPriv *priv;
3990 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3992 priv = GET_PRIV (self);
3994 if (priv->tp_chat == NULL) {
3999 g_object_get (priv->tp_chat,
4000 "n-messages-sending", &n_messages,