1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #undef G_DISABLE_DEPRECATED /* for GCompletion */
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n-lib.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-keyring.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-request-util.h>
46 #include <libempathy/empathy-chatroom-manager.h>
48 #include "empathy-chat.h"
49 #include "empathy-spell.h"
50 #include "empathy-contact-dialogs.h"
51 #include "empathy-contact-list-store.h"
52 #include "empathy-contact-list-view.h"
53 #include "empathy-contact-menu.h"
54 #include "empathy-gtk-marshal.h"
55 #include "empathy-input-text-view.h"
56 #include "empathy-search-bar.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-smiley-manager.h"
59 #include "empathy-ui-utils.h"
60 #include "empathy-string-parser.h"
61 #include "extensions/extensions.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
64 #include <libempathy/empathy-debug.h>
66 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
67 #define COMPOSING_STOP_TIMEOUT 5
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
70 struct _EmpathyChatPriv {
71 EmpathyTpChat *tp_chat;
76 EmpathyContact *remote_contact;
77 gboolean show_contacts;
79 GSettings *gsettings_chat;
80 GSettings *gsettings_ui;
82 TplLogManager *log_manager;
83 TpAccountManager *account_manager;
85 GList *input_history_current;
87 GCompletion *completion;
88 guint composing_stop_timeout_id;
89 guint block_events_timeout_id;
90 TpHandleType handle_type;
92 gboolean has_input_vscroll;
94 /* TRUE if spell checking is enabled, FALSE otherwise.
95 * This is to keep track of the last state of spell checking
97 gboolean spell_checking_enabled;
99 /* These store the signal handler ids for the enclosed text entry. */
100 gulong insert_text_id;
101 gulong delete_range_id;
102 gulong notify_cursor_position_id;
104 /* Source func ID for update_misspelled_words () */
105 guint update_misspelled_words_id;
106 /* Source func ID for save_paned_pos_timeout () */
107 guint save_paned_pos_id;
111 GtkWidget *vbox_left;
112 GtkWidget *scrolled_window_chat;
113 GtkWidget *scrolled_window_input;
114 GtkWidget *scrolled_window_contacts;
115 GtkWidget *hbox_topic;
116 GtkWidget *expander_topic;
117 GtkWidget *label_topic;
118 GtkWidget *contact_list_view;
119 GtkWidget *info_bar_vbox;
120 GtkWidget *search_bar;
122 guint unread_messages;
123 /* TRUE if the pending messages can be displayed. This is to avoid to show
124 * pending messages *before* messages from logs. (#603980) */
125 gboolean can_show_pending;
127 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
128 * be differently handled since it introduces another race condition, which
129 * is really hard to occur, but still possible.
131 * With the current workaround (which has the race above), we need to be
132 * sure to ACK any pending messages only when the retrieval of backlogs is
133 * finished, that's why using retrieving_backlogs flag.
134 * empathy_chat_messages_read () will check this variable and not ACK
135 * anything when TRUE. It will be set TRUE at chat_constructed () and set
136 * back to FALSE when the backlog has been retrieved and the pending
137 * messages actually showed to the user.
139 * Race condition introduced with this workaround:
140 * Scenario: a message is pending, the user is notified and selects the tab.
141 * the tab with a pending message is focused before the messages are properly
142 * shown (since the preparation of the window is slower AND async WRT the
143 * tab showing), which means the user won't see any new messages (rare but
144 * possible), if he/she will change tab focus before the messages are
145 * properly shown, the tab will be set as 'seen' and the user won't be
146 * notified again about the already notified pending messages when the
147 * messages in tab will be properly shown */
148 gboolean retrieving_backlogs;
149 gboolean sms_channel;
153 gchar *text; /* Original message that was specified
154 * upon entry creation. */
155 gchar *modified_text; /* Message that was modified by user.
156 * When no modifications were made, it is NULL */
162 PART_COMMAND_ENTERED,
176 PROP_N_MESSAGES_SENDING,
177 PROP_NB_UNREAD_MESSAGES,
180 static guint signals[LAST_SIGNAL] = { 0 };
182 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
184 static gboolean update_misspelled_words (gpointer data);
187 chat_get_property (GObject *object,
192 EmpathyChat *chat = EMPATHY_CHAT (object);
193 EmpathyChatPriv *priv = GET_PRIV (object);
197 g_value_set_object (value, priv->tp_chat);
200 g_value_set_object (value, priv->account);
203 g_value_take_string (value, empathy_chat_dup_name (chat));
206 g_value_set_string (value, priv->id);
209 g_value_set_string (value, priv->subject);
211 case PROP_REMOTE_CONTACT:
212 g_value_set_object (value, priv->remote_contact);
214 case PROP_SHOW_CONTACTS:
215 g_value_set_boolean (value, priv->show_contacts);
217 case PROP_SMS_CHANNEL:
218 g_value_set_boolean (value, priv->sms_channel);
220 case PROP_N_MESSAGES_SENDING:
221 g_value_set_uint (value,
222 empathy_chat_get_n_messages_sending (chat));
224 case PROP_NB_UNREAD_MESSAGES:
225 g_value_set_uint (value,
226 empathy_chat_get_nb_unread_messages (chat));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
235 chat_set_property (GObject *object,
240 EmpathyChat *chat = EMPATHY_CHAT (object);
244 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
246 case PROP_SHOW_CONTACTS:
247 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
256 account_reconnected (EmpathyChat *chat,
259 EmpathyChatPriv *priv = GET_PRIV (chat);
261 DEBUG ("Account reconnected, request a new Text channel");
263 /* FIXME: Ideally we should ask to handle ourself the channel so we can
264 * report the error if any but this is blocked by
265 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
266 switch (priv->handle_type) {
267 case TP_HANDLE_TYPE_CONTACT:
268 if (priv->sms_channel)
269 empathy_sms_contact_id (
271 TP_USER_ACTION_TIME_NOT_USER_ACTION);
273 empathy_chat_with_contact_id (
275 TP_USER_ACTION_TIME_NOT_USER_ACTION);
277 case TP_HANDLE_TYPE_ROOM:
278 empathy_join_muc (account, priv->id,
279 TP_USER_ACTION_TIME_NOT_USER_ACTION);
281 case TP_HANDLE_TYPE_NONE:
282 case TP_HANDLE_TYPE_LIST:
283 case TP_HANDLE_TYPE_GROUP:
285 g_assert_not_reached ();
289 g_object_unref (chat);
293 chat_new_connection_cb (TpAccount *account,
297 gchar *dbus_error_name,
301 EmpathyChatPriv *priv = GET_PRIV (chat);
303 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
306 if (priv->tp_chat != NULL || account != priv->account ||
307 priv->handle_type == TP_HANDLE_TYPE_NONE ||
308 EMP_STR_EMPTY (priv->id))
313 account_reconnected (chat, account);
317 chat_composing_remove_timeout (EmpathyChat *chat)
319 EmpathyChatPriv *priv;
321 priv = GET_PRIV (chat);
323 if (priv->composing_stop_timeout_id) {
324 g_source_remove (priv->composing_stop_timeout_id);
325 priv->composing_stop_timeout_id = 0;
330 chat_composing_stop_timeout_cb (EmpathyChat *chat)
332 EmpathyChatPriv *priv;
334 priv = GET_PRIV (chat);
336 priv->composing_stop_timeout_id = 0;
337 empathy_tp_chat_set_state (priv->tp_chat,
338 TP_CHANNEL_CHAT_STATE_PAUSED);
344 chat_composing_start (EmpathyChat *chat)
346 EmpathyChatPriv *priv;
348 priv = GET_PRIV (chat);
350 if (priv->composing_stop_timeout_id) {
351 /* Just restart the timeout */
352 chat_composing_remove_timeout (chat);
354 empathy_tp_chat_set_state (priv->tp_chat,
355 TP_CHANNEL_CHAT_STATE_COMPOSING);
358 priv->composing_stop_timeout_id = g_timeout_add_seconds (
359 COMPOSING_STOP_TIMEOUT,
360 (GSourceFunc) chat_composing_stop_timeout_cb,
365 chat_composing_stop (EmpathyChat *chat)
367 EmpathyChatPriv *priv;
369 priv = GET_PRIV (chat);
371 chat_composing_remove_timeout (chat);
372 empathy_tp_chat_set_state (priv->tp_chat,
373 TP_CHANNEL_CHAT_STATE_ACTIVE);
377 chat_input_history_entry_cmp (InputHistoryEntry *entry,
380 if (!tp_strdiff (entry->text, text)) {
381 if (entry->modified_text != NULL) {
382 /* Modified entry and single string cannot be equal. */
390 static InputHistoryEntry *
391 chat_input_history_entry_new_with_text (const gchar *text)
393 InputHistoryEntry *entry;
394 entry = g_slice_new0 (InputHistoryEntry);
395 entry->text = g_strdup (text);
401 chat_input_history_entry_free (InputHistoryEntry *entry)
403 g_free (entry->text);
404 g_free (entry->modified_text);
405 g_slice_free (InputHistoryEntry, entry);
409 chat_input_history_entry_revert (InputHistoryEntry *entry)
411 g_free (entry->modified_text);
412 entry->modified_text = NULL;
416 chat_input_history_entry_update_text (InputHistoryEntry *entry,
421 if (!tp_strdiff (text, entry->text)) {
422 g_free (entry->modified_text);
423 entry->modified_text = NULL;
427 old = entry->modified_text;
428 entry->modified_text = g_strdup (text);
433 chat_input_history_entry_get_text (InputHistoryEntry *entry)
439 if (entry->modified_text != NULL) {
440 return entry->modified_text;
446 chat_input_history_remove_item (GList *list,
449 list = g_list_remove_link (list, item);
450 chat_input_history_entry_free (item->data);
451 g_list_free_1 (item);
456 chat_input_history_revert (EmpathyChat *chat)
458 EmpathyChatPriv *priv;
462 InputHistoryEntry *entry;
464 priv = GET_PRIV (chat);
465 list = priv->input_history;
468 DEBUG ("No input history");
472 /* Delete temporary entry */
473 if (priv->input_history_current != NULL) {
475 list = chat_input_history_remove_item (list, item1);
476 if (priv->input_history_current == item1) {
477 /* Removed temporary entry was current entry */
478 priv->input_history = list;
479 priv->input_history_current = NULL;
484 /* There is no entry to revert */
488 /* Restore the current history entry to original value */
489 item1 = priv->input_history_current;
491 chat_input_history_entry_revert (entry);
493 /* Remove restored entry if there is other occurance before this entry */
494 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
495 (GCompareFunc) chat_input_history_entry_cmp);
496 if (item2 != item1) {
497 list = chat_input_history_remove_item (list, item1);
500 /* Remove other occurance of the restored entry */
501 item2 = g_list_find_custom (item1->next,
502 chat_input_history_entry_get_text (entry),
503 (GCompareFunc) chat_input_history_entry_cmp);
505 list = chat_input_history_remove_item (list, item2);
509 priv->input_history_current = NULL;
510 priv->input_history = list;
514 chat_input_history_add (EmpathyChat *chat,
518 EmpathyChatPriv *priv;
521 InputHistoryEntry *entry;
523 priv = GET_PRIV (chat);
525 list = priv->input_history;
527 /* Remove any other occurances of this entry, if not temporary */
529 while ((item = g_list_find_custom (list, str,
530 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
531 list = chat_input_history_remove_item (list, item);
534 /* Trim the list to the last 10 items */
535 while (g_list_length (list) > 10) {
536 item = g_list_last (list);
538 list = chat_input_history_remove_item (list, item);
546 entry = chat_input_history_entry_new_with_text (str);
547 list = g_list_prepend (list, entry);
549 /* Set the list and the current item pointer */
550 priv->input_history = list;
552 priv->input_history_current = list;
555 priv->input_history_current = NULL;
560 chat_input_history_get_next (EmpathyChat *chat)
562 EmpathyChatPriv *priv;
566 priv = GET_PRIV (chat);
568 if (priv->input_history == NULL) {
569 DEBUG ("No input history, next entry is NULL");
572 g_assert (priv->input_history_current != NULL);
574 if ((item = g_list_next (priv->input_history_current)) == NULL)
576 item = priv->input_history_current;
579 msg = chat_input_history_entry_get_text (item->data);
581 DEBUG ("Returning next entry: '%s'", msg);
583 priv->input_history_current = item;
589 chat_input_history_get_prev (EmpathyChat *chat)
591 EmpathyChatPriv *priv;
595 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
597 priv = GET_PRIV (chat);
599 if (priv->input_history == NULL) {
600 DEBUG ("No input history, previous entry is NULL");
604 if (priv->input_history_current == NULL)
608 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
610 item = priv->input_history_current;
613 msg = chat_input_history_entry_get_text (item->data);
615 DEBUG ("Returning previous entry: '%s'", msg);
617 priv->input_history_current = item;
623 chat_input_history_update (EmpathyChat *chat,
624 GtkTextBuffer *buffer)
626 EmpathyChatPriv *priv;
627 GtkTextIter start, end;
629 InputHistoryEntry *entry;
631 priv = GET_PRIV (chat);
633 gtk_text_buffer_get_bounds (buffer, &start, &end);
634 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
636 if (priv->input_history_current == NULL) {
637 /* Add the current text temporarily to the history */
638 chat_input_history_add (chat, text, TRUE);
643 /* Save the changes in the history */
644 entry = priv->input_history_current->data;
645 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
646 chat_input_history_entry_update_text (entry, text);
655 } ChatCommandMsgData;
658 chat_command_msg_cb (GObject *source,
659 GAsyncResult *result,
662 ChatCommandMsgData *data = user_data;
663 GError *error = NULL;
666 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
667 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
669 if (channel == NULL) {
670 DEBUG ("Failed to get channel: %s", error->message);
671 g_error_free (error);
673 empathy_chat_view_append_event (data->chat->view,
674 _("Failed to open private chat"));
678 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
679 TpTextChannel *text = (TpTextChannel *) channel;
682 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
685 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
687 g_object_unref (msg);
690 g_object_unref (channel);
693 g_free (data->message);
694 g_slice_free (ChatCommandMsgData, data);
698 nick_command_supported (EmpathyChat *chat)
700 EmpathyChatPriv * priv = GET_PRIV (chat);
701 TpConnection *connection;
703 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
704 return tp_proxy_has_interface_by_id (connection,
705 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
709 part_command_supported (EmpathyChat *chat)
711 EmpathyChatPriv * priv = GET_PRIV (chat);
713 return tp_proxy_has_interface_by_id (priv->tp_chat,
714 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
718 chat_command_clear (EmpathyChat *chat,
721 empathy_chat_view_clear (chat->view);
725 chat_command_topic (EmpathyChat *chat,
728 EmpathyChatPriv *priv = GET_PRIV (chat);
729 EmpathyTpChatProperty *property;
730 GValue value = {0, };
732 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
733 if (property == NULL) {
734 empathy_chat_view_append_event (chat->view,
735 _("Topic not supported on this conversation"));
739 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
740 empathy_chat_view_append_event (chat->view,
741 _("You are not allowed to change the topic"));
745 g_value_init (&value, G_TYPE_STRING);
746 g_value_set_string (&value, strv[1]);
747 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
748 g_value_unset (&value);
752 empathy_chat_join_muc (EmpathyChat *chat,
755 EmpathyChatPriv *priv = GET_PRIV (chat);
757 empathy_join_muc (priv->account, room,
758 empathy_get_current_action_time ());
762 chat_command_join (EmpathyChat *chat,
767 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
769 /* FIXME: Ideally we should ask to handle ourself the channel so we can
770 * report the error if any but this is blocked by
771 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
772 while (rooms[i] != NULL) {
773 /* ignore empty strings */
774 if (!EMP_STR_EMPTY (rooms[i])) {
775 empathy_chat_join_muc (chat, rooms[i]);
783 chat_command_part (EmpathyChat *chat,
786 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
790 chat_command_msg_internal (EmpathyChat *chat,
791 const gchar *contact_id,
792 const gchar *message)
794 EmpathyChatPriv *priv = GET_PRIV (chat);
795 ChatCommandMsgData *data;
796 TpAccountChannelRequest *req;
799 request = tp_asv_new (
800 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
801 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
802 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
805 req = tp_account_channel_request_new (priv->account, request,
806 empathy_get_current_action_time ());
808 /* FIXME: We should probably search in members alias. But this
809 * is enough for IRC */
810 data = g_slice_new (ChatCommandMsgData);
812 data->message = g_strdup (message);
814 tp_account_channel_request_ensure_and_observe_channel_async (req,
815 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
817 g_object_unref (req);
818 g_hash_table_unref (request);
822 chat_command_query (EmpathyChat *chat,
825 /* If <message> part is not defined,
826 * strv[2] will be the terminal NULL */
827 chat_command_msg_internal (chat, strv[1], strv[2]);
831 chat_command_msg (EmpathyChat *chat,
834 chat_command_msg_internal (chat, strv[1], strv[2]);
838 callback_for_request_rename (TpProxy *proxy,
841 GObject *weak_object)
844 DEBUG ("Call to RequestRename method failed: %s",error->message);
849 chat_command_nick (EmpathyChat *chat,
852 EmpathyChatPriv *priv = GET_PRIV (chat);
855 proxy = TP_PROXY (tp_account_get_connection (priv->account));
857 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
858 strv[1], callback_for_request_rename, NULL, NULL, NULL);
862 chat_command_me (EmpathyChat *chat,
865 EmpathyChatPriv *priv = GET_PRIV (chat);
867 TpTextChannel *channel;
869 channel = (TpTextChannel *) (priv->tp_chat);
871 if (!tp_text_channel_supports_message_type (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 = tp_channel_borrow_connection ((TpChannel *) 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 if (empathy_message_is_incoming (message)) {
1330 priv->unread_messages++;
1331 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1334 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1338 chat_message_received_cb (EmpathyTpChat *tp_chat,
1339 EmpathyMessage *message,
1342 chat_message_received (chat, message, FALSE);
1346 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1347 EmpathyMessage *message,
1350 EmpathyChatPriv *priv = GET_PRIV (chat);
1352 empathy_chat_view_message_acknowledged (chat->view,
1355 priv->unread_messages--;
1356 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1360 chat_send_error_cb (EmpathyTpChat *tp_chat,
1361 const gchar *message_body,
1362 TpChannelTextSendError error_code,
1363 const gchar *dbus_error,
1366 const gchar *error = NULL;
1369 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1370 /* translators: error used when user doesn't have enough credit on his
1371 * account to send the message. */
1372 error = _("insufficient balance to send message");
1373 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1374 error = _("not capable");
1377 if (error == NULL) {
1378 /* if we didn't find a dbus-error, try the old error */
1379 switch (error_code) {
1380 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1381 error = _("offline");
1383 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1384 error = _("invalid contact");
1386 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1387 error = _("permission denied");
1389 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1390 error = _("too long message");
1392 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1393 error = _("not implemented");
1395 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1397 error = _("unknown");
1402 if (message_body != NULL) {
1403 str = g_strdup_printf (_("Error sending message '%s': %s"),
1404 message_body, error);
1407 str = g_strdup_printf (_("Error sending message: %s"), error);
1410 empathy_chat_view_append_event (chat->view, str);
1415 chat_topic_label_size_allocate_cb (GtkLabel *label,
1416 GtkAllocation *allocation,
1419 EmpathyChatPriv *priv = GET_PRIV (chat);
1421 if (!gtk_label_get_line_wrap (label)) {
1422 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1423 gtk_widget_show (priv->expander_topic);
1425 gtk_widget_hide (priv->expander_topic);
1432 chat_topic_expander_activate_cb (GtkExpander *expander,
1433 GParamSpec *param_spec,
1436 EmpathyChatPriv *priv = GET_PRIV (chat);
1438 if (gtk_expander_get_expanded (expander)) {
1439 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1440 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1442 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1443 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1448 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1453 EmpathyChatPriv *priv = GET_PRIV (chat);
1455 if (!tp_strdiff (name, "subject")) {
1456 g_free (priv->subject);
1457 priv->subject = g_value_dup_string (value);
1458 g_object_notify (G_OBJECT (chat), "subject");
1460 if (EMP_STR_EMPTY (priv->subject)) {
1461 gtk_widget_hide (priv->hbox_topic);
1463 gchar *markup_topic;
1466 markup_topic = empathy_add_link_markup (priv->subject);
1467 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1468 _("Topic:"), markup_topic);
1470 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1471 g_free (markup_text);
1472 g_free (markup_topic);
1474 gtk_widget_show (priv->hbox_topic);
1476 if (priv->block_events_timeout_id == 0) {
1479 if (!EMP_STR_EMPTY (priv->subject)) {
1480 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1482 str = g_strdup (_("No topic defined"));
1484 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1488 else if (!tp_strdiff (name, "name")) {
1489 g_free (priv->name);
1490 priv->name = g_value_dup_string (value);
1491 g_object_notify (G_OBJECT (chat), "name");
1496 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1500 GtkTextIter word_start = *iter;
1501 GtkTextIter word_end = *iter;
1504 if (gtk_text_iter_inside_word (&word_end) &&
1505 !gtk_text_iter_ends_word (&word_end)) {
1506 gtk_text_iter_forward_word_end (&word_end);
1511 if (gtk_text_iter_get_char (&tmp) == '\'') {
1512 gtk_text_iter_forward_char (&tmp);
1514 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1515 gtk_text_iter_forward_word_end (&word_end);
1520 if (gtk_text_iter_inside_word (&word_start) ||
1521 gtk_text_iter_ends_word (&word_start)) {
1522 if (!gtk_text_iter_starts_word (&word_start) ||
1523 gtk_text_iter_equal (&word_start, &word_end)) {
1524 gtk_text_iter_backward_word_start (&word_start);
1528 gtk_text_iter_backward_char (&tmp);
1530 if (gtk_text_iter_get_char (&tmp) == '\'') {
1531 gtk_text_iter_backward_char (&tmp);
1533 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1534 gtk_text_iter_backward_word_start (&word_start);
1539 *start = word_start;
1545 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1546 GtkTextIter *location,
1551 GtkTextIter iter, pos;
1553 /* Remove all misspelled tags in the inserted text.
1554 * This happens when text is inserted within a misspelled word. */
1555 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1556 gtk_text_iter_get_offset (location) - len);
1557 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1560 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1563 GtkTextIter start, end;
1566 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1569 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1571 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1572 gtk_text_iter_equal (&pos, &end) ||
1573 empathy_spell_check (str)) {
1574 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1576 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1581 } while (gtk_text_iter_forward_word_end (&iter) &&
1582 gtk_text_iter_compare (&iter, location) <= 0);
1586 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1591 GtkTextIter word_start, word_end;
1593 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1594 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1595 &word_start, &word_end);
1600 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1603 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1604 chat_composing_stop (chat);
1606 chat_composing_start (chat);
1611 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1616 GtkTextIter prev_pos;
1617 GtkTextIter word_start;
1618 GtkTextIter word_end;
1622 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1624 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1625 gtk_text_buffer_get_insert (buffer));
1626 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1628 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1631 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1632 !gtk_text_iter_equal (&pos, &word_end)) {
1633 str = gtk_text_buffer_get_text (buffer,
1634 &word_start, &word_end, FALSE);
1636 if (!empathy_spell_check (str)) {
1637 gtk_text_buffer_apply_tag_by_name (buffer,
1638 "misspelled", &word_start, &word_end);
1640 gtk_text_buffer_remove_tag_by_name (buffer,
1641 "misspelled", &word_start, &word_end);
1648 gtk_text_buffer_move_mark (buffer, mark, &pos);
1652 empathy_isspace_cb (gunichar c,
1655 return g_unichar_isspace (c);
1659 chat_input_key_press_event_cb (GtkWidget *widget,
1663 EmpathyChatPriv *priv;
1666 GtkWidget *text_view_sw;
1668 priv = GET_PRIV (chat);
1670 /* Catch ctrl+up/down so we can traverse messages we sent */
1671 if ((event->state & GDK_CONTROL_MASK) &&
1672 (event->keyval == GDK_KEY_Up ||
1673 event->keyval == GDK_KEY_Down)) {
1674 GtkTextBuffer *buffer;
1677 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1678 chat_input_history_update (chat, buffer);
1680 if (event->keyval == GDK_KEY_Up) {
1681 str = chat_input_history_get_next (chat);
1683 str = chat_input_history_get_prev (chat);
1686 g_signal_handlers_block_by_func (buffer,
1687 chat_input_text_buffer_changed_cb,
1689 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1690 g_signal_handlers_unblock_by_func (buffer,
1691 chat_input_text_buffer_changed_cb,
1697 /* Catch enter but not ctrl/shift-enter */
1698 if (IS_ENTER (event->keyval) &&
1699 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1702 /* This is to make sure that kinput2 gets the enter. And if
1703 * it's handled there we shouldn't send on it. This is because
1704 * kinput2 uses Enter to commit letters. See:
1705 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1708 view = GTK_TEXT_VIEW (chat->input_text_view);
1709 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1710 gtk_text_view_reset_im_context (view);
1714 chat_input_text_view_send (chat);
1718 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1720 if (IS_ENTER (event->keyval) &&
1721 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1722 /* Newline for shift/control-enter. */
1725 if (!(event->state & GDK_CONTROL_MASK) &&
1726 event->keyval == GDK_KEY_Page_Up) {
1727 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1728 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1731 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1732 event->keyval == GDK_KEY_Page_Down) {
1733 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1734 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1735 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1736 gtk_adjustment_set_value (adj, val);
1739 if (event->keyval == GDK_KEY_Escape) {
1740 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1742 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1743 event->keyval == GDK_KEY_Tab) {
1744 GtkTextBuffer *buffer;
1745 GtkTextIter start, current;
1746 gchar *nick, *completed;
1747 GList *list, *completed_list;
1748 gboolean is_start_of_buffer;
1750 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1751 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1753 /* Get the start of the nick to complete. */
1754 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1755 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1756 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1758 is_start_of_buffer = gtk_text_iter_is_start (&start);
1760 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1761 g_completion_add_items (priv->completion, list);
1763 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1764 completed_list = g_completion_complete (priv->completion,
1773 GString *message = NULL;
1776 gtk_text_buffer_delete (buffer, &start, ¤t);
1778 len = g_list_length (completed_list);
1781 /* If we only have one hit, use that text
1782 * instead of the text in completed since the
1783 * completed text will use the typed string
1784 * which might be cased all wrong.
1787 text = empathy_contact_get_alias (completed_list->data);
1791 /* Print all hits to the scrollback view, so the
1792 * user knows what possibilities he has.
1795 message = g_string_new ("");
1796 for (l = completed_list; l != NULL; l = l->next) {
1797 g_string_append (message, empathy_contact_get_alias (l->data));
1798 g_string_append (message, " - ");
1800 empathy_chat_view_append_event (chat->view, message->str);
1801 g_string_free (message, TRUE);
1804 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1806 if (len == 1 && is_start_of_buffer) {
1807 gchar *complete_char;
1809 complete_char = g_settings_get_string (
1810 priv->gsettings_chat,
1811 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1813 if (complete_char != NULL) {
1814 gtk_text_buffer_insert_at_cursor (buffer,
1816 strlen (complete_char));
1817 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1818 g_free (complete_char);
1825 g_completion_clear_items (priv->completion);
1827 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1837 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1841 gtk_widget_grab_focus (chat->input_text_view);
1847 chat_input_realize_cb (GtkWidget *widget,
1850 DEBUG ("Setting focus to the input text view");
1851 if (gtk_widget_is_sensitive (widget)) {
1852 gtk_widget_grab_focus (widget);
1857 chat_input_has_focus_notify_cb (GtkWidget *widget,
1861 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1865 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1866 EmpathySmiley *smiley,
1869 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1870 GtkTextBuffer *buffer;
1873 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1875 gtk_text_buffer_get_end_iter (buffer, &iter);
1876 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1878 gtk_text_buffer_get_end_iter (buffer, &iter);
1879 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1890 static EmpathyChatSpell *
1891 chat_spell_new (EmpathyChat *chat,
1896 EmpathyChatSpell *chat_spell;
1898 chat_spell = g_slice_new0 (EmpathyChatSpell);
1900 chat_spell->chat = g_object_ref (chat);
1901 chat_spell->word = g_strdup (word);
1902 chat_spell->start = start;
1903 chat_spell->end = end;
1909 chat_spell_free (EmpathyChatSpell *chat_spell)
1911 g_object_unref (chat_spell->chat);
1912 g_free (chat_spell->word);
1913 g_slice_free (EmpathyChatSpell, chat_spell);
1917 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1918 EmpathyChatSpell *chat_spell)
1920 empathy_chat_correct_word (chat_spell->chat,
1921 &(chat_spell->start),
1923 gtk_menu_item_get_label (menu_item));
1928 chat_spelling_build_suggestions_menu (const gchar *code,
1929 EmpathyChatSpell *chat_spell)
1931 GList *suggestions, *l;
1932 GtkWidget *menu, *menu_item;
1934 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1935 if (suggestions == NULL)
1938 menu = gtk_menu_new ();
1939 for (l = suggestions; l; l = l->next) {
1940 menu_item = gtk_menu_item_new_with_label (l->data);
1941 g_signal_connect (G_OBJECT (menu_item), "activate",
1942 G_CALLBACK (chat_spelling_menu_activate_cb),
1944 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1946 empathy_spell_free_suggestions (suggestions);
1948 gtk_widget_show_all (menu);
1954 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1956 GtkWidget *menu, *submenu, *item;
1959 codes = empathy_spell_get_enabled_language_codes ();
1960 g_assert (codes != NULL);
1962 if (g_list_length (codes) > 1) {
1963 menu = gtk_menu_new ();
1965 for (l = codes; l; l = l->next) {
1966 const gchar *code, *name;
1969 name = empathy_spell_get_language_name (code);
1973 item = gtk_image_menu_item_new_with_label (name);
1975 submenu = chat_spelling_build_suggestions_menu (
1977 if (submenu == NULL)
1978 gtk_widget_set_sensitive (item, FALSE);
1980 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1982 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1985 menu = chat_spelling_build_suggestions_menu (codes->data,
1988 menu = gtk_menu_new ();
1989 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1990 gtk_widget_set_sensitive (item, FALSE);
1991 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1994 g_list_free (codes);
1996 gtk_widget_show_all (menu);
2007 static EmpathyChatWord *
2008 chat_word_new (EmpathyChat *chat,
2012 EmpathyChatWord *chat_word;
2014 chat_word = g_slice_new0 (EmpathyChatWord);
2016 chat_word->chat = g_object_ref (chat);
2017 chat_word->word = g_strdup (word);
2018 chat_word->code = g_strdup (code);
2024 chat_word_free (EmpathyChatWord *chat_word)
2026 g_object_unref (chat_word->chat);
2027 g_free (chat_word->word);
2028 g_free (chat_word->code);
2029 g_slice_free (EmpathyChatWord, chat_word);
2033 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2034 EmpathyChatWord *chat_word)
2036 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2038 empathy_spell_add_to_dictionary (chat_word->code,
2040 priv->update_misspelled_words_id =
2041 g_idle_add (update_misspelled_words, chat_word->chat);
2045 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2047 GtkWidget *menu, *item, *lang_item, *image;
2050 const gchar *code, *name;
2051 EmpathyChatWord *chat_word;
2053 codes = empathy_spell_get_enabled_language_codes ();
2054 g_assert (codes != NULL);
2055 if (g_list_length (codes) > 1) {
2056 /* translators: %s is the selected word */
2057 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2059 item = gtk_image_menu_item_new_with_mnemonic (label);
2061 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2062 GTK_ICON_SIZE_MENU);
2063 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2066 menu = gtk_menu_new ();
2068 for (l = codes; l; l = l->next) {
2070 name = empathy_spell_get_language_name (code);
2074 lang_item = gtk_image_menu_item_new_with_label (name);
2076 chat_word= chat_word_new (chat_spell->chat,
2077 chat_spell->word, code);
2078 g_object_set_data_full (G_OBJECT (lang_item),
2079 "chat-word", chat_word,
2080 (GDestroyNotify) chat_word_free);
2082 g_signal_connect (G_OBJECT (lang_item), "activate",
2083 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2085 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2087 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2090 name = empathy_spell_get_language_name (code);
2091 g_assert (name != NULL);
2092 /* translators: first %s is the selected word,
2093 * second %s is the language name of the target dictionary */
2094 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2095 chat_spell->word, name);
2096 item = gtk_image_menu_item_new_with_mnemonic (label);
2098 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2099 GTK_ICON_SIZE_MENU);
2100 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2102 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2104 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2105 (GDestroyNotify) chat_word_free);
2107 g_signal_connect (G_OBJECT (item), "activate",
2108 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2111 g_list_free (codes);
2113 gtk_widget_show_all (item);
2119 chat_text_send_cb (GtkMenuItem *menuitem,
2122 chat_input_text_view_send (chat);
2126 chat_input_populate_popup_cb (GtkTextView *view,
2130 GtkTextBuffer *buffer;
2131 GtkTextTagTable *table;
2134 GtkTextIter iter, start, end;
2137 EmpathyChatSpell *chat_spell;
2138 GtkWidget *spell_menu;
2139 GtkWidget *spell_item;
2140 EmpathySmileyManager *smiley_manager;
2141 GtkWidget *smiley_menu;
2144 buffer = gtk_text_view_get_buffer (view);
2146 /* Add the emoticon menu. */
2147 item = gtk_separator_menu_item_new ();
2148 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2149 gtk_widget_show (item);
2151 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2152 image = gtk_image_new_from_icon_name ("face-smile",
2153 GTK_ICON_SIZE_MENU);
2154 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2155 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2156 gtk_widget_show (item);
2158 smiley_manager = empathy_smiley_manager_dup_singleton ();
2159 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2160 chat_insert_smiley_activate_cb,
2162 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2163 g_object_unref (smiley_manager);
2165 /* Add the Send menu item. */
2166 gtk_text_buffer_get_bounds (buffer, &start, &end);
2167 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2168 if (!EMP_STR_EMPTY (str)) {
2169 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2170 g_signal_connect (G_OBJECT (item), "activate",
2171 G_CALLBACK (chat_text_send_cb), chat);
2172 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2173 gtk_widget_show (item);
2177 /* Add the spell check menu item. */
2178 table = gtk_text_buffer_get_tag_table (buffer);
2179 tag = gtk_text_tag_table_lookup (table, "misspelled");
2180 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2181 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2182 GTK_TEXT_WINDOW_WIDGET,
2185 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2187 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2188 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2190 str = gtk_text_buffer_get_text (buffer,
2191 &start, &end, FALSE);
2193 if (!EMP_STR_EMPTY (str)) {
2194 chat_spell = chat_spell_new (chat, str, start, end);
2195 g_object_set_data_full (G_OBJECT (menu),
2196 "chat-spell", chat_spell,
2197 (GDestroyNotify) chat_spell_free);
2199 item = gtk_separator_menu_item_new ();
2200 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2201 gtk_widget_show (item);
2203 /* Spelling suggestions */
2204 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2205 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2206 GTK_ICON_SIZE_MENU);
2207 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2209 spell_menu = chat_spelling_build_menu (chat_spell);
2210 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2213 spell_item = gtk_separator_menu_item_new ();
2214 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2215 gtk_widget_show (spell_item);
2217 /* Add to dictionary */
2218 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2220 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2221 gtk_widget_show (spell_item);
2223 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2224 gtk_widget_show (item);
2230 chat_log_filter (TplEvent *log,
2233 EmpathyChat *chat = user_data;
2234 EmpathyMessage *message;
2235 EmpathyChatPriv *priv = GET_PRIV (chat);
2236 const GList *pending;
2238 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2239 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2241 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2242 message = empathy_message_from_tpl_log_event (log);
2244 for (; pending; pending = g_list_next (pending)) {
2245 if (empathy_message_equal (message, pending->data)) {
2246 g_object_unref (message);
2251 g_object_unref (message);
2257 show_pending_messages (EmpathyChat *chat) {
2258 EmpathyChatPriv *priv = GET_PRIV (chat);
2259 const GList *messages, *l;
2261 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2263 if (chat->view == NULL || priv->tp_chat == NULL)
2266 if (!priv->can_show_pending)
2269 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2271 for (l = messages; l != NULL ; l = g_list_next (l)) {
2272 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2273 chat_message_received (chat, message, TRUE);
2279 got_filtered_messages_cb (GObject *manager,
2280 GAsyncResult *result,
2285 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2286 EmpathyChatPriv *priv = GET_PRIV (chat);
2287 GError *error = NULL;
2289 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2290 result, &messages, &error)) {
2291 DEBUG ("%s. Aborting.", error->message);
2292 empathy_chat_view_append_event (chat->view,
2293 _("Failed to retrieve recent logs"));
2294 g_error_free (error);
2298 for (l = messages; l; l = g_list_next (l)) {
2299 EmpathyMessage *message;
2300 g_assert (TPL_IS_EVENT (l->data));
2302 message = empathy_message_from_tpl_log_event (l->data);
2303 g_object_unref (l->data);
2305 empathy_chat_view_append_message (chat->view, message);
2306 g_object_unref (message);
2308 g_list_free (messages);
2311 /* in case of TPL error, skip backlog and show pending messages */
2312 priv->can_show_pending = TRUE;
2313 show_pending_messages (chat);
2315 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2316 * about it in EmpathyChatPriv definition */
2317 priv->retrieving_backlogs = FALSE;
2318 empathy_chat_messages_read (chat);
2320 /* Turn back on scrolling */
2321 empathy_chat_view_scroll (chat->view, TRUE);
2325 chat_add_logs (EmpathyChat *chat)
2327 EmpathyChatPriv *priv = GET_PRIV (chat);
2334 /* Turn off scrolling temporarily */
2335 empathy_chat_view_scroll (chat->view, FALSE);
2337 /* Add messages from last conversation */
2338 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2339 target = tpl_entity_new_from_room_id (priv->id);
2341 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2343 priv->retrieving_backlogs = TRUE;
2344 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2347 TPL_EVENT_MASK_TEXT,
2351 got_filtered_messages_cb,
2354 g_object_unref (target);
2358 chat_contacts_completion_func (const gchar *s1,
2362 gchar *tmp, *nick1, *nick2;
2369 return s1 ? -1 : +1;
2372 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2373 nick1 = g_utf8_casefold (tmp, -1);
2376 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2377 nick2 = g_utf8_casefold (tmp, -1);
2380 ret = strncmp (nick1, nick2, n);
2389 build_part_message (guint reason,
2391 EmpathyContact *actor,
2392 const gchar *message)
2394 GString *s = g_string_new ("");
2395 const gchar *actor_name = NULL;
2397 if (actor != NULL) {
2398 actor_name = empathy_contact_get_alias (actor);
2401 /* Having an actor only really makes sense for a few actions... */
2403 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2404 g_string_append_printf (s, _("%s has disconnected"), name);
2406 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2407 if (actor_name != NULL) {
2408 /* translators: reverse the order of these arguments
2409 * if the kicked should come before the kicker in your locale.
2411 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2414 g_string_append_printf (s, _("%s was kicked"), name);
2417 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2418 if (actor_name != NULL) {
2419 /* translators: reverse the order of these arguments
2420 * if the banned should come before the banner in your locale.
2422 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2425 g_string_append_printf (s, _("%s was banned"), name);
2429 g_string_append_printf (s, _("%s has left the room"), name);
2432 if (!EMP_STR_EMPTY (message)) {
2433 /* Note to translators: this string is appended to
2434 * notifications like "foo has left the room", with the message
2435 * given by the user living the room. If this poses a problem,
2436 * please let us know. :-)
2438 g_string_append_printf (s, _(" (%s)"), message);
2441 return g_string_free (s, FALSE);
2445 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2446 EmpathyContact *contact,
2447 EmpathyContact *actor,
2453 EmpathyChatPriv *priv = GET_PRIV (chat);
2454 const gchar *name = empathy_contact_get_alias (contact);
2457 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2459 if (priv->block_events_timeout_id != 0)
2463 str = g_strdup_printf (_("%s has joined the room"),
2466 str = build_part_message (reason, name, actor, message);
2469 empathy_chat_view_append_event (chat->view, str);
2474 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2475 EmpathyContact *old_contact,
2476 EmpathyContact *new_contact,
2481 EmpathyChatPriv *priv = GET_PRIV (chat);
2483 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2485 if (priv->block_events_timeout_id == 0) {
2488 str = g_strdup_printf (_("%s is now known as %s"),
2489 empathy_contact_get_alias (old_contact),
2490 empathy_contact_get_alias (new_contact));
2491 empathy_chat_view_append_event (chat->view, str);
2498 chat_reset_size_request (gpointer widget)
2500 gtk_widget_set_size_request (widget, -1, -1);
2506 chat_update_contacts_visibility (EmpathyChat *chat,
2509 EmpathyChatPriv *priv = GET_PRIV (chat);
2510 GtkAllocation allocation;
2512 if (!priv->scrolled_window_contacts) {
2516 if (priv->remote_contact != NULL) {
2520 if (show && priv->contact_list_view == NULL) {
2521 EmpathyContactListStore *store;
2524 /* We are adding the contact list to the chat, we don't want the
2525 * chat view to become too small. If the chat view is already
2526 * smaller than 250 make sure that size won't change. If the
2527 * chat view is bigger the contact list will take some space on
2528 * it but we make sure the chat view don't become smaller than
2529 * 250. Relax the size request once the resize is done */
2530 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2531 min_width = MIN (allocation.width, 250);
2532 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2533 g_idle_add (chat_reset_size_request, priv->vbox_left);
2535 if (priv->contacts_width > 0) {
2536 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2537 priv->contacts_width);
2540 store = empathy_contact_list_store_new (
2541 EMPATHY_CONTACT_LIST (priv->tp_chat));
2542 empathy_contact_list_store_set_show_groups (
2543 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2545 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2546 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2547 EMPATHY_CONTACT_FEATURE_CHAT |
2548 EMPATHY_CONTACT_FEATURE_CALL |
2549 EMPATHY_CONTACT_FEATURE_LOG |
2550 EMPATHY_CONTACT_FEATURE_INFO));
2551 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2552 priv->contact_list_view);
2553 gtk_widget_show (priv->contact_list_view);
2554 gtk_widget_show (priv->scrolled_window_contacts);
2555 g_object_unref (store);
2557 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2558 gtk_widget_hide (priv->scrolled_window_contacts);
2559 if (priv->contact_list_view != NULL) {
2560 gtk_widget_destroy (priv->contact_list_view);
2561 priv->contact_list_view = NULL;
2567 empathy_chat_set_show_contacts (EmpathyChat *chat,
2570 EmpathyChatPriv *priv = GET_PRIV (chat);
2572 priv->show_contacts = show;
2574 chat_update_contacts_visibility (chat, show);
2576 g_object_notify (G_OBJECT (chat), "show-contacts");
2580 chat_remote_contact_changed_cb (EmpathyChat *chat)
2582 EmpathyChatPriv *priv = GET_PRIV (chat);
2584 if (priv->remote_contact != NULL) {
2585 g_object_unref (priv->remote_contact);
2586 priv->remote_contact = NULL;
2591 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2592 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2593 if (priv->remote_contact != NULL) {
2594 g_object_ref (priv->remote_contact);
2595 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2597 else if (priv->tp_chat != NULL) {
2598 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2601 chat_update_contacts_visibility (chat, priv->show_contacts);
2603 g_object_notify (G_OBJECT (chat), "remote-contact");
2604 g_object_notify (G_OBJECT (chat), "id");
2608 chat_destroy_cb (EmpathyTpChat *tp_chat,
2611 EmpathyChatPriv *priv;
2613 priv = GET_PRIV (chat);
2615 if (!priv->tp_chat) {
2619 chat_composing_remove_timeout (chat);
2620 g_object_unref (priv->tp_chat);
2621 priv->tp_chat = NULL;
2622 g_object_notify (G_OBJECT (chat), "tp-chat");
2624 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2625 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2627 chat_update_contacts_visibility (chat, FALSE);
2631 update_misspelled_words (gpointer data)
2633 EmpathyChat *chat = EMPATHY_CHAT (data);
2634 EmpathyChatPriv *priv = GET_PRIV (chat);
2635 GtkTextBuffer *buffer;
2639 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2641 gtk_text_buffer_get_end_iter (buffer, &iter);
2642 length = gtk_text_iter_get_offset (&iter);
2643 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2644 NULL, length, chat);
2646 priv->update_misspelled_words_id = 0;
2652 conf_spell_checking_cb (GSettings *gsettings_chat,
2656 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2657 EmpathyChatPriv *priv = GET_PRIV (chat);
2658 gboolean spell_checker;
2659 GtkTextBuffer *buffer;
2661 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2664 spell_checker = g_settings_get_boolean (gsettings_chat,
2665 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2667 if (!empathy_spell_supported ()) {
2668 spell_checker = FALSE;
2671 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2673 if (spell_checker == priv->spell_checking_enabled) {
2674 if (spell_checker) {
2675 /* Possibly changed dictionaries,
2676 * update misspelled words. Need to do so in idle
2677 * so the spell checker is updated. */
2678 priv->update_misspelled_words_id =
2679 g_idle_add (update_misspelled_words, chat);
2685 if (spell_checker) {
2688 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2689 buffer, "notify::cursor-position",
2690 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2692 priv->insert_text_id = tp_g_signal_connect_object (
2693 buffer, "insert-text",
2694 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2695 chat, G_CONNECT_AFTER);
2696 priv->delete_range_id = tp_g_signal_connect_object (
2697 buffer, "delete-range",
2698 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2699 chat, G_CONNECT_AFTER);
2701 gtk_text_buffer_create_tag (buffer, "misspelled",
2702 "underline", PANGO_UNDERLINE_ERROR,
2705 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2706 gtk_text_buffer_get_insert (buffer));
2707 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2710 /* Mark misspelled words in the existing buffer.
2711 * Need to do so in idle so the spell checker is updated. */
2712 priv->update_misspelled_words_id =
2713 g_idle_add (update_misspelled_words, chat);
2715 GtkTextTagTable *table;
2718 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2719 priv->notify_cursor_position_id = 0;
2720 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2721 priv->insert_text_id = 0;
2722 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2723 priv->delete_range_id = 0;
2725 table = gtk_text_buffer_get_tag_table (buffer);
2726 tag = gtk_text_tag_table_lookup (table, "misspelled");
2727 gtk_text_tag_table_remove (table, tag);
2729 gtk_text_buffer_delete_mark_by_name (buffer,
2730 "previous-cursor-position");
2733 priv->spell_checking_enabled = spell_checker;
2737 save_paned_pos_timeout (gpointer data)
2739 EmpathyChat *self = data;
2742 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2744 g_settings_set_int (self->priv->gsettings_ui,
2745 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2752 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2756 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2758 if (chat->priv->save_paned_pos_id != 0)
2759 g_source_remove (chat->priv->save_paned_pos_id);
2761 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2762 save_paned_pos_timeout, chat);
2768 chat_create_ui (EmpathyChat *chat)
2770 EmpathyChatPriv *priv = GET_PRIV (chat);
2774 GtkTextBuffer *buffer;
2776 EmpathyThemeManager *theme_mgr;
2778 filename = empathy_file_lookup ("empathy-chat.ui",
2780 gui = empathy_builder_get_file (filename,
2781 "chat_widget", &priv->widget,
2782 "hpaned", &priv->hpaned,
2783 "vbox_left", &priv->vbox_left,
2784 "scrolled_window_chat", &priv->scrolled_window_chat,
2785 "scrolled_window_input", &priv->scrolled_window_input,
2786 "hbox_topic", &priv->hbox_topic,
2787 "expander_topic", &priv->expander_topic,
2788 "label_topic", &priv->label_topic,
2789 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2790 "info_bar_vbox", &priv->info_bar_vbox,
2793 empathy_builder_connect (gui, chat,
2794 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2795 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2800 /* Add message view. */
2801 theme_mgr = empathy_theme_manager_dup_singleton ();
2802 chat->view = empathy_theme_manager_create_view (theme_mgr);
2803 g_object_unref (theme_mgr);
2804 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2805 and other types, even though it's non-editable and doesn't accept any
2806 drags. This steals drag motion for anything inside the scrollbars,
2807 making drag destinations on chat windows far less useful.
2809 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2810 g_signal_connect (chat->view, "focus_in_event",
2811 G_CALLBACK (chat_text_view_focus_in_event_cb),
2813 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2814 GTK_WIDGET (chat->view));
2815 gtk_widget_show (GTK_WIDGET (chat->view));
2817 /* Add input GtkTextView */
2818 chat->input_text_view = empathy_input_text_view_new ();
2819 g_signal_connect (chat->input_text_view, "notify::has-focus",
2820 G_CALLBACK (chat_input_has_focus_notify_cb),
2822 g_signal_connect (chat->input_text_view, "key-press-event",
2823 G_CALLBACK (chat_input_key_press_event_cb),
2825 g_signal_connect (chat->input_text_view, "realize",
2826 G_CALLBACK (chat_input_realize_cb),
2828 g_signal_connect (chat->input_text_view, "populate-popup",
2829 G_CALLBACK (chat_input_populate_popup_cb),
2831 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2832 tp_g_signal_connect_object (buffer, "changed",
2833 G_CALLBACK (chat_input_text_buffer_changed_cb),
2835 tp_g_signal_connect_object (priv->gsettings_chat,
2836 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2837 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2838 conf_spell_checking_cb (priv->gsettings_chat,
2839 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2840 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2841 chat->input_text_view);
2842 gtk_widget_show (chat->input_text_view);
2844 /* Add the (invisible) search bar */
2845 priv->search_bar = empathy_search_bar_new (chat->view);
2846 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2849 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2851 /* Initialy hide the topic, will be shown if not empty */
2852 gtk_widget_hide (priv->hbox_topic);
2854 g_signal_connect (priv->hpaned, "notify::position",
2855 G_CALLBACK (chat_hpaned_pos_changed_cb),
2858 /* Load the paned position */
2859 paned_pos = g_settings_get_int (priv->gsettings_ui,
2860 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2862 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2864 /* Set widget focus order */
2865 list = g_list_append (NULL, priv->search_bar);
2866 list = g_list_append (list, priv->scrolled_window_input);
2867 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2870 list = g_list_append (NULL, priv->vbox_left);
2871 list = g_list_append (list, priv->scrolled_window_contacts);
2872 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2875 list = g_list_append (NULL, priv->hpaned);
2876 list = g_list_append (list, priv->hbox_topic);
2877 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2880 /* Add the main widget in the chat widget */
2881 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2882 g_object_unref (gui);
2886 chat_size_allocate (GtkWidget *widget,
2887 GtkAllocation *allocation)
2889 GtkBin *bin = GTK_BIN (widget);
2890 GtkAllocation child_allocation;
2893 gtk_widget_set_allocation (widget, allocation);
2895 child = gtk_bin_get_child (bin);
2897 if (child && gtk_widget_get_visible (child))
2899 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2900 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2901 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2902 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2904 gtk_widget_size_allocate (child, &child_allocation);
2909 chat_finalize (GObject *object)
2912 EmpathyChatPriv *priv;
2914 chat = EMPATHY_CHAT (object);
2915 priv = GET_PRIV (chat);
2917 DEBUG ("Finalized: %p", object);
2919 if (priv->update_misspelled_words_id != 0)
2920 g_source_remove (priv->update_misspelled_words_id);
2922 if (priv->save_paned_pos_id != 0)
2923 g_source_remove (priv->save_paned_pos_id);
2925 g_object_unref (priv->gsettings_chat);
2926 g_object_unref (priv->gsettings_ui);
2928 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2929 g_list_free (priv->input_history);
2931 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2932 g_list_free (priv->compositors);
2934 chat_composing_remove_timeout (chat);
2936 g_object_unref (priv->account_manager);
2937 g_object_unref (priv->log_manager);
2939 if (priv->tp_chat) {
2940 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2941 chat_destroy_cb, chat);
2942 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2943 chat_message_received_cb, chat);
2944 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2945 chat_message_acknowledged_cb, chat);
2946 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2947 chat_send_error_cb, chat);
2948 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2949 chat_state_changed_cb, chat);
2950 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2951 chat_property_changed_cb, chat);
2952 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2953 chat_members_changed_cb, chat);
2954 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2955 chat_remote_contact_changed_cb, chat);
2956 empathy_tp_chat_leave (priv->tp_chat, "");
2957 g_object_unref (priv->tp_chat);
2959 if (priv->account) {
2960 g_object_unref (priv->account);
2962 if (priv->remote_contact) {
2963 g_object_unref (priv->remote_contact);
2966 if (priv->block_events_timeout_id) {
2967 g_source_remove (priv->block_events_timeout_id);
2971 g_free (priv->name);
2972 g_free (priv->subject);
2973 g_completion_free (priv->completion);
2975 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2979 chat_constructed (GObject *object)
2981 EmpathyChat *chat = EMPATHY_CHAT (object);
2982 EmpathyChatPriv *priv = GET_PRIV (chat);
2984 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2985 /* First display logs from the logger and then display pending messages */
2986 chat_add_logs (chat);
2989 /* Just display pending messages for rooms */
2990 priv->can_show_pending = TRUE;
2991 show_pending_messages (chat);
2996 empathy_chat_class_init (EmpathyChatClass *klass)
2998 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2999 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3001 object_class->finalize = chat_finalize;
3002 object_class->get_property = chat_get_property;
3003 object_class->set_property = chat_set_property;
3004 object_class->constructed = chat_constructed;
3006 widget_class->size_allocate = chat_size_allocate;
3008 g_object_class_install_property (object_class,
3010 g_param_spec_object ("tp-chat",
3012 "The tp chat object",
3013 EMPATHY_TYPE_TP_CHAT,
3016 G_PARAM_STATIC_STRINGS));
3017 g_object_class_install_property (object_class,
3019 g_param_spec_object ("account",
3020 "Account of the chat",
3021 "The account of the chat",
3024 G_PARAM_STATIC_STRINGS));
3025 g_object_class_install_property (object_class,
3027 g_param_spec_string ("id",
3029 "The id of the chat",
3032 G_PARAM_STATIC_STRINGS));
3033 g_object_class_install_property (object_class,
3035 g_param_spec_string ("name",
3037 "The name of the chat",
3040 G_PARAM_STATIC_STRINGS));
3041 g_object_class_install_property (object_class,
3043 g_param_spec_string ("subject",
3045 "The subject or topic of the chat",
3048 G_PARAM_STATIC_STRINGS));
3049 g_object_class_install_property (object_class,
3050 PROP_REMOTE_CONTACT,
3051 g_param_spec_object ("remote-contact",
3052 "The remote contact",
3053 "The remote contact is any",
3054 EMPATHY_TYPE_CONTACT,
3056 G_PARAM_STATIC_STRINGS));
3057 g_object_class_install_property (object_class,
3059 g_param_spec_boolean ("show-contacts",
3060 "Contacts' visibility",
3061 "The visibility of the contacts' list",
3064 G_PARAM_STATIC_STRINGS));
3066 g_object_class_install_property (object_class,
3068 g_param_spec_boolean ("sms-channel",
3070 "TRUE if this channel is for sending SMSes",
3072 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3074 g_object_class_install_property (object_class,
3075 PROP_N_MESSAGES_SENDING,
3076 g_param_spec_uint ("n-messages-sending",
3077 "Num Messages Sending",
3078 "The number of messages being sent",
3080 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3082 g_object_class_install_property (object_class,
3083 PROP_NB_UNREAD_MESSAGES,
3084 g_param_spec_uint ("nb-unread-messages",
3085 "Num Unread Messages",
3086 "The number of unread messages",
3088 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3090 signals[COMPOSING] =
3091 g_signal_new ("composing",
3092 G_OBJECT_CLASS_TYPE (object_class),
3096 g_cclosure_marshal_VOID__BOOLEAN,
3100 signals[NEW_MESSAGE] =
3101 g_signal_new ("new-message",
3102 G_OBJECT_CLASS_TYPE (object_class),
3106 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3108 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3110 signals[PART_COMMAND_ENTERED] =
3111 g_signal_new ("part-command-entered",
3112 G_OBJECT_CLASS_TYPE (object_class),
3116 g_cclosure_marshal_VOID__POINTER,
3120 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3124 chat_block_events_timeout_cb (gpointer data)
3126 EmpathyChatPriv *priv = GET_PRIV (data);
3128 priv->block_events_timeout_id = 0;
3134 account_manager_prepared_cb (GObject *source_object,
3135 GAsyncResult *result,
3138 GList *accounts, *l;
3139 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3140 EmpathyChat *chat = user_data;
3141 GError *error = NULL;
3143 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3144 DEBUG ("Failed to prepare the account manager: %s", error->message);
3145 g_error_free (error);
3149 accounts = tp_account_manager_get_valid_accounts (account_manager);
3151 for (l = accounts; l != NULL; l = l->next) {
3152 TpAccount *account = l->data;
3153 tp_g_signal_connect_object (account, "status-changed",
3154 G_CALLBACK (chat_new_connection_cb),
3158 g_list_free (accounts);
3162 empathy_chat_init (EmpathyChat *chat)
3164 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3165 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3168 priv->log_manager = tpl_log_manager_dup_singleton ();
3169 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3170 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3172 priv->contacts_width = -1;
3173 priv->input_history = NULL;
3174 priv->input_history_current = NULL;
3175 priv->account_manager = tp_account_manager_dup ();
3177 tp_account_manager_prepare_async (priv->account_manager, NULL,
3178 account_manager_prepared_cb, chat);
3180 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3181 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3183 /* Block events for some time to avoid having "has come online" or
3184 * "joined" messages. */
3185 priv->block_events_timeout_id =
3186 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3188 /* Add nick name completion */
3189 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3190 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3192 chat_create_ui (chat);
3196 empathy_chat_new (EmpathyTpChat *tp_chat)
3198 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3202 empathy_chat_get_tp_chat (EmpathyChat *chat)
3204 EmpathyChatPriv *priv = GET_PRIV (chat);
3206 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3208 return priv->tp_chat;
3214 GtkWidget *info_bar;
3224 passwd_remember_button_cb (GtkButton *button,
3227 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3231 passwd_not_now_button_cb (GtkButton *button,
3234 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3238 remember_password_infobar_response_cb (GtkWidget *info_bar,
3242 EmpathyChatPriv *priv = GET_PRIV (data->self);
3244 if (response_id == GTK_RESPONSE_OK) {
3245 DEBUG ("Saving room password");
3246 empathy_keyring_set_room_password_async (priv->account,
3247 empathy_tp_chat_get_id (priv->tp_chat),
3252 gtk_widget_destroy (info_bar);
3253 g_free (data->password);
3254 g_slice_free (PasswordData, data);
3258 chat_prompt_to_save_password (EmpathyChat *self,
3261 GtkWidget *content_area;
3268 /* save the password in case it needs to be saved */
3269 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3271 /* Remove all previous widgets */
3272 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3273 gtk_container_forall (GTK_CONTAINER (content_area),
3274 (GtkCallback) gtk_widget_destroy, NULL);
3275 data->button = NULL;
3278 data->spinner = NULL;
3280 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3281 GTK_MESSAGE_QUESTION);
3283 hbox = gtk_hbox_new (FALSE, 5);
3284 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3287 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3288 GTK_ICON_SIZE_DIALOG);
3289 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3292 label = gtk_label_new (_("Would you like to store this password?"));
3293 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3295 /* Add 'Remember' button */
3296 alig = gtk_alignment_new (0, 0.5, 1, 0);
3298 button = gtk_button_new_with_label (_("Remember"));
3299 gtk_container_add (GTK_CONTAINER (alig), button);
3300 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3302 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3305 /* Add 'Not now' button */
3306 alig = gtk_alignment_new (0, 0.5, 1, 0);
3308 button = gtk_button_new_with_label (_("Not now"));
3309 gtk_container_add (GTK_CONTAINER (alig), button);
3310 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3312 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3316 g_signal_handler_disconnect (data->info_bar, data->response_id);
3317 g_signal_connect (data->info_bar, "response",
3318 G_CALLBACK (remember_password_infobar_response_cb), data);
3320 gtk_widget_show_all (data->info_bar);
3324 provide_password_cb (GObject *tp_chat,
3328 PasswordData *data = user_data;
3329 EmpathyChat *self = data->self;
3330 EmpathyChatPriv *priv = GET_PRIV (self);
3331 GError *error = NULL;
3333 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3335 DEBUG ("error: %s", error->message);
3336 /* FIXME: what should we do if that's another error? Close the channel?
3337 * Display the raw D-Bus error to the user isn't very useful */
3338 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3340 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3341 gtk_widget_set_sensitive (data->entry, TRUE);
3342 gtk_widget_grab_focus (data->entry);
3345 gtk_info_bar_set_message_type (
3346 GTK_INFO_BAR (data->info_bar),
3350 gtk_widget_set_sensitive (data->button, TRUE);
3351 gtk_button_set_label (GTK_BUTTON (data->button),
3355 gtk_label_set_text (GTK_LABEL (data->label),
3356 _("Wrong password; please try again:"));
3359 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3360 gtk_widget_hide (data->spinner);
3362 g_error_free (error);
3366 if (empathy_keyring_is_available ()) {
3367 /* ask whether they want to save the password */
3368 chat_prompt_to_save_password (self, data);
3370 /* Get rid of the password info bar finally */
3371 gtk_widget_destroy (data->info_bar);
3372 g_slice_free (PasswordData, data);
3376 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3377 gtk_widget_grab_focus (self->input_text_view);
3381 password_infobar_response_cb (GtkWidget *info_bar,
3385 EmpathyChatPriv *priv = GET_PRIV (data->self);
3386 const gchar *password;
3388 if (response_id != GTK_RESPONSE_OK) {
3389 gtk_widget_destroy (info_bar);
3390 g_slice_free (PasswordData, data);
3394 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3396 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3397 provide_password_cb, data);
3399 gtk_widget_set_sensitive (data->button, FALSE);
3400 gtk_widget_set_sensitive (data->entry, FALSE);
3402 gtk_spinner_start (GTK_SPINNER (data->spinner));
3403 gtk_widget_show (data->spinner);
3407 password_entry_activate_cb (GtkWidget *entry,
3410 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3414 passwd_join_button_cb (GtkButton *button,
3417 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3421 clear_icon_released_cb (GtkEntry *entry,
3422 GtkEntryIconPosition icon_pos,
3426 gtk_entry_set_text (entry, "");
3430 password_entry_changed_cb (GtkEditable *entry,
3435 str = gtk_entry_get_text (GTK_ENTRY (entry));
3437 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3438 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3442 chat_invalidated_cb (TpProxy *proxy,
3446 gpointer password_infobar)
3448 /* Destroy the password infobar whenever a channel is invalidated
3449 * so we don't have multiple infobars when the MUC is rejoined */
3450 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3454 display_password_info_bar (EmpathyChat *self)
3456 EmpathyChatPriv *priv = GET_PRIV (self);
3457 GtkWidget *info_bar;
3458 GtkWidget *content_area;
3468 data = g_slice_new0 (PasswordData);
3470 info_bar = gtk_info_bar_new ();
3471 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3472 GTK_MESSAGE_QUESTION);
3474 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3476 hbox = gtk_hbox_new (FALSE, 5);
3477 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3480 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3481 GTK_ICON_SIZE_DIALOG);
3482 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3485 label = gtk_label_new (_("This room is protected by a password:"));
3486 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3488 /* Add password entry */
3489 entry = gtk_entry_new ();
3490 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3491 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3493 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3494 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3495 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3496 GTK_ENTRY_ICON_SECONDARY, FALSE);
3498 g_signal_connect (entry, "icon-release",
3499 G_CALLBACK (clear_icon_released_cb), data);
3500 g_signal_connect (entry, "changed",
3501 G_CALLBACK (password_entry_changed_cb), data);
3503 g_signal_connect (entry, "activate",
3504 G_CALLBACK (password_entry_activate_cb), data);
3506 /* Focus the password entry once it's realized */
3507 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3509 /* Add 'Join' button */
3510 alig = gtk_alignment_new (0, 0.5, 1, 0);
3512 button = gtk_button_new_with_label (_("Join"));
3513 gtk_container_add (GTK_CONTAINER (alig), button);
3514 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3516 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3520 spinner = gtk_spinner_new ();
3521 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3523 /* Save some data for messing around with later */
3525 data->info_bar = info_bar;
3526 data->button = button;
3527 data->label = label;
3528 data->entry = entry;
3529 data->spinner = spinner;
3531 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3533 gtk_widget_show_all (hbox);
3535 tp_g_signal_connect_object (priv->tp_chat,
3536 "invalidated", G_CALLBACK (chat_invalidated_cb),
3539 data->response_id = g_signal_connect (info_bar, "response",
3540 G_CALLBACK (password_infobar_response_cb), data);
3542 gtk_widget_show_all (info_bar);
3543 /* ... but hide the spinner */
3544 gtk_widget_hide (spinner);
3546 /* prevent the user from typing anything */
3547 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3551 provide_saved_password_cb (GObject *tp_chat,
3555 EmpathyChat *self = user_data;
3556 EmpathyChatPriv *priv = GET_PRIV (self);
3557 GError *error = NULL;
3559 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3561 DEBUG ("error: %s", error->message);
3562 /* FIXME: what should we do if that's another error? Close the channel?
3563 * Display the raw D-Bus error to the user isn't very useful */
3564 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3565 display_password_info_bar (self);
3566 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3568 g_error_free (error);
3573 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3574 gtk_widget_grab_focus (self->input_text_view);
3578 chat_room_got_password_cb (GObject *source,
3579 GAsyncResult *result,
3582 EmpathyChat *self = user_data;
3583 EmpathyChatPriv *priv = GET_PRIV (self);
3584 const gchar *password;
3585 GError *error = NULL;
3587 password = empathy_keyring_get_room_password_finish (priv->account,
3590 if (error != NULL) {
3591 DEBUG ("Couldn't get room password: %s\n", error->message);
3592 g_clear_error (&error);
3594 display_password_info_bar (self);
3595 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3599 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3600 provide_saved_password_cb, self);
3604 chat_password_needed_changed_cb (EmpathyChat *self)
3606 EmpathyChatPriv *priv = GET_PRIV (self);
3608 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3609 empathy_keyring_get_room_password_async (priv->account,
3610 empathy_tp_chat_get_id (priv->tp_chat),
3611 chat_room_got_password_cb, self);
3616 chat_sms_channel_changed_cb (EmpathyChat *self)
3618 EmpathyChatPriv *priv = GET_PRIV (self);
3620 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3621 g_object_notify (G_OBJECT (self), "sms-channel");
3625 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3627 g_object_notify (G_OBJECT (self), "n-messages-sending");
3631 empathy_chat_set_tp_chat (EmpathyChat *chat,
3632 EmpathyTpChat *tp_chat)
3634 EmpathyChatPriv *priv = GET_PRIV (chat);
3635 GPtrArray *properties;
3637 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3638 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3640 if (priv->tp_chat) {
3644 if (priv->account) {
3645 g_object_unref (priv->account);
3648 priv->tp_chat = g_object_ref (tp_chat);
3649 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3651 g_signal_connect (tp_chat, "destroy",
3652 G_CALLBACK (chat_destroy_cb),
3654 g_signal_connect (tp_chat, "message-received-empathy",
3655 G_CALLBACK (chat_message_received_cb),
3657 g_signal_connect (tp_chat, "message_acknowledged",
3658 G_CALLBACK (chat_message_acknowledged_cb),
3660 g_signal_connect (tp_chat, "send-error",
3661 G_CALLBACK (chat_send_error_cb),
3663 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3664 G_CALLBACK (chat_state_changed_cb),
3666 g_signal_connect (tp_chat, "property-changed",
3667 G_CALLBACK (chat_property_changed_cb),
3669 g_signal_connect (tp_chat, "members-changed",
3670 G_CALLBACK (chat_members_changed_cb),
3672 g_signal_connect (tp_chat, "member-renamed",
3673 G_CALLBACK (chat_member_renamed_cb),
3675 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3676 G_CALLBACK (chat_remote_contact_changed_cb),
3678 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3679 G_CALLBACK (chat_password_needed_changed_cb),
3681 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3682 G_CALLBACK (chat_sms_channel_changed_cb),
3684 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3685 G_CALLBACK (chat_n_messages_sending_changed_cb),
3688 /* Get initial value of properties */
3689 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3690 if (properties != NULL) {
3693 for (i = 0; i < properties->len; i++) {
3694 EmpathyTpChatProperty *property;
3696 property = g_ptr_array_index (properties, i);
3697 if (property->value == NULL)
3700 chat_property_changed_cb (priv->tp_chat,
3707 chat_sms_channel_changed_cb (chat);
3708 chat_remote_contact_changed_cb (chat);
3710 if (chat->input_text_view) {
3711 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3712 if (priv->block_events_timeout_id == 0) {
3713 empathy_chat_view_append_event (chat->view, _("Connected"));
3717 g_object_notify (G_OBJECT (chat), "tp-chat");
3718 g_object_notify (G_OBJECT (chat), "id");
3719 g_object_notify (G_OBJECT (chat), "account");
3721 /* This is a noop when tp-chat is set at object construction time and causes
3722 * the pending messages to be show when it's set on the object after it has
3724 show_pending_messages (chat);
3726 /* check if a password is needed */
3727 chat_password_needed_changed_cb (chat);
3731 empathy_chat_get_account (EmpathyChat *chat)
3733 EmpathyChatPriv *priv = GET_PRIV (chat);
3735 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3737 return priv->account;
3741 empathy_chat_get_id (EmpathyChat *chat)
3743 EmpathyChatPriv *priv = GET_PRIV (chat);
3745 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3751 empathy_chat_dup_name (EmpathyChat *chat)
3753 EmpathyChatPriv *priv = GET_PRIV (chat);
3756 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3760 if (!ret && priv->remote_contact) {
3761 ret = empathy_contact_get_alias (priv->remote_contact);
3768 ret = _("Conversation");
3770 if (priv->sms_channel)
3771 /* Translators: this string is a something like
3772 * "Escher Cat (SMS)" */
3773 return g_strdup_printf (_("%s (SMS)"), ret);
3775 return g_strdup (ret);
3779 empathy_chat_get_subject (EmpathyChat *chat)
3781 EmpathyChatPriv *priv = GET_PRIV (chat);
3783 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3785 return priv->subject;
3789 empathy_chat_get_remote_contact (EmpathyChat *chat)
3791 EmpathyChatPriv *priv = GET_PRIV (chat);
3793 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3795 return priv->remote_contact;
3799 empathy_chat_get_contact_menu (EmpathyChat *chat)
3801 EmpathyChatPriv *priv = GET_PRIV (chat);
3802 GtkWidget *menu = NULL;
3804 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3806 if (priv->remote_contact) {
3807 menu = empathy_contact_menu_new (priv->remote_contact,
3808 EMPATHY_CONTACT_FEATURE_CALL |
3809 EMPATHY_CONTACT_FEATURE_LOG |
3810 EMPATHY_CONTACT_FEATURE_INFO |
3811 EMPATHY_CONTACT_FEATURE_BLOCK);
3818 empathy_chat_clear (EmpathyChat *chat)
3820 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3822 empathy_chat_view_clear (chat->view);
3826 empathy_chat_scroll_down (EmpathyChat *chat)
3828 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3830 empathy_chat_view_scroll_down (chat->view);
3834 empathy_chat_cut (EmpathyChat *chat)
3836 GtkTextBuffer *buffer;
3838 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3840 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3841 if (gtk_text_buffer_get_has_selection (buffer)) {
3842 GtkClipboard *clipboard;
3844 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3846 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3851 empathy_chat_copy (EmpathyChat *chat)
3853 GtkTextBuffer *buffer;
3855 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3857 if (empathy_chat_view_get_has_selection (chat->view)) {
3858 empathy_chat_view_copy_clipboard (chat->view);
3862 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3863 if (gtk_text_buffer_get_has_selection (buffer)) {
3864 GtkClipboard *clipboard;
3866 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3868 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3873 EmpathyChatPriv *priv = GET_PRIV (chat);
3875 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3882 GtkClipboard *clipboard;
3884 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3885 start = g_utf8_offset_to_pointer (topic, start_offset);
3886 end = g_utf8_offset_to_pointer (topic, end_offset);
3887 selection = g_strndup (start, end - start);
3889 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3890 gtk_clipboard_set_text (clipboard, selection, -1);
3898 empathy_chat_paste (EmpathyChat *chat)
3900 GtkTextBuffer *buffer;
3901 GtkClipboard *clipboard;
3902 EmpathyChatPriv *priv;
3904 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3906 priv = GET_PRIV (chat);
3908 if (gtk_widget_get_visible (priv->search_bar)) {
3909 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3913 if (priv->tp_chat == NULL ||
3914 !gtk_widget_is_sensitive (chat->input_text_view))
3917 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3918 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3920 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3924 empathy_chat_find (EmpathyChat *chat)
3926 EmpathyChatPriv *priv;
3928 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3930 priv = GET_PRIV (chat);
3932 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3936 empathy_chat_correct_word (EmpathyChat *chat,
3939 const gchar *new_word)
3941 GtkTextBuffer *buffer;
3943 g_return_if_fail (chat != NULL);
3944 g_return_if_fail (new_word != NULL);
3946 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3948 gtk_text_buffer_delete (buffer, start, end);
3949 gtk_text_buffer_insert (buffer, start,
3955 empathy_chat_is_room (EmpathyChat *chat)
3957 EmpathyChatPriv *priv = GET_PRIV (chat);
3959 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3961 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3965 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3967 EmpathyChatPriv *priv = GET_PRIV (self);
3969 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3971 return priv->unread_messages;
3974 /* called when the messages have been read by user */
3976 empathy_chat_messages_read (EmpathyChat *self)
3978 EmpathyChatPriv *priv = GET_PRIV (self);
3980 g_return_if_fail (EMPATHY_IS_CHAT (self));
3982 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3983 * definition. If we are still retrieving the backlogs, do not ACK */
3984 if (priv->retrieving_backlogs)
3987 if (priv->tp_chat != NULL) {
3988 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3992 /* Return TRUE if on of the contacts in this chat is composing */
3994 empathy_chat_is_composing (EmpathyChat *chat)
3996 return chat->priv->compositors != NULL;
4000 empathy_chat_is_sms_channel (EmpathyChat *self)
4002 EmpathyChatPriv *priv = GET_PRIV (self);
4004 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4006 return priv->sms_channel;
4010 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4012 EmpathyChatPriv *priv;
4014 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4016 priv = GET_PRIV (self);
4018 if (priv->tp_chat == NULL) {
4023 g_object_get (priv->tp_chat,
4024 "n-messages-sending", &n_messages,
4032 empathy_chat_dup_text (EmpathyChat *self)
4034 GtkTextBuffer *buffer;
4035 GtkTextIter start, end;
4037 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4039 gtk_text_buffer_get_bounds (buffer, &start, &end);
4040 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4044 empathy_chat_set_text (EmpathyChat *self,
4047 GtkTextBuffer *buffer;
4049 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4051 gtk_text_buffer_set_text (buffer, text, -1);