1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #undef G_DISABLE_DEPRECATED /* for GCompletion */
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n-lib.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
41 #include <telepathy-logger/text-event.h>
42 #include <libempathy/empathy-contact-list.h>
43 #include <libempathy/empathy-gsettings.h>
44 #include <libempathy/empathy-keyring.h>
45 #include <libempathy/empathy-utils.h>
46 #include <libempathy/empathy-request-util.h>
47 #include <libempathy/empathy-chatroom-manager.h>
49 #include "empathy-chat.h"
50 #include "empathy-spell.h"
51 #include "empathy-contact-dialogs.h"
52 #include "empathy-individual-store-channel.h"
53 #include "empathy-individual-view.h"
54 #include "empathy-input-text-view.h"
55 #include "empathy-search-bar.h"
56 #include "empathy-theme-manager.h"
57 #include "empathy-theme-adium.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;
108 /* Source func ID for chat_contacts_visible_timeout_cb () */
109 guint contacts_visible_id;
113 GtkWidget *vbox_left;
114 GtkWidget *scrolled_window_chat;
115 GtkWidget *scrolled_window_input;
116 GtkWidget *scrolled_window_contacts;
117 GtkWidget *hbox_topic;
118 GtkWidget *expander_topic;
119 GtkWidget *label_topic;
120 GtkWidget *contact_list_view;
121 GtkWidget *info_bar_vbox;
122 GtkWidget *search_bar;
124 guint unread_messages;
125 guint unread_messages_when_offline;
126 /* TRUE if the pending messages can be displayed. This is to avoid to show
127 * pending messages *before* messages from logs. (#603980) */
128 gboolean can_show_pending;
130 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
131 * be differently handled since it introduces another race condition, which
132 * is really hard to occur, but still possible.
134 * With the current workaround (which has the race above), we need to be
135 * sure to ACK any pending messages only when the retrieval of backlogs is
136 * finished, that's why using retrieving_backlogs flag.
137 * empathy_chat_messages_read () will check this variable and not ACK
138 * anything when TRUE. It will be set TRUE at chat_constructed () and set
139 * back to FALSE when the backlog has been retrieved and the pending
140 * messages actually showed to the user.
142 * Race condition introduced with this workaround:
143 * Scenario: a message is pending, the user is notified and selects the tab.
144 * the tab with a pending message is focused before the messages are properly
145 * shown (since the preparation of the window is slower AND async WRT the
146 * tab showing), which means the user won't see any new messages (rare but
147 * possible), if he/she will change tab focus before the messages are
148 * properly shown, the tab will be set as 'seen' and the user won't be
149 * notified again about the already notified pending messages when the
150 * messages in tab will be properly shown */
151 gboolean retrieving_backlogs;
152 gboolean sms_channel;
154 /* we need to know whether populate-popup happened in response to
155 * the keyboard or the mouse. We can't ask GTK for the most recent
156 * event, because it will be a notify event. Instead we track it here */
157 GdkEventType most_recent_event_type;
161 gchar *text; /* Original message that was specified
162 * upon entry creation. */
163 gchar *modified_text; /* Message that was modified by user.
164 * When no modifications were made, it is NULL */
170 PART_COMMAND_ENTERED,
184 PROP_N_MESSAGES_SENDING,
185 PROP_NB_UNREAD_MESSAGES,
188 static guint signals[LAST_SIGNAL] = { 0 };
190 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
192 static gboolean update_misspelled_words (gpointer data);
195 chat_get_property (GObject *object,
200 EmpathyChat *chat = EMPATHY_CHAT (object);
201 EmpathyChatPriv *priv = GET_PRIV (object);
205 g_value_set_object (value, priv->tp_chat);
208 g_value_set_object (value, priv->account);
211 g_value_take_string (value, empathy_chat_dup_name (chat));
214 g_value_set_string (value, priv->id);
217 g_value_set_string (value, priv->subject);
219 case PROP_REMOTE_CONTACT:
220 g_value_set_object (value, priv->remote_contact);
222 case PROP_SHOW_CONTACTS:
223 g_value_set_boolean (value, priv->show_contacts);
225 case PROP_SMS_CHANNEL:
226 g_value_set_boolean (value, priv->sms_channel);
228 case PROP_N_MESSAGES_SENDING:
229 g_value_set_uint (value,
230 empathy_chat_get_n_messages_sending (chat));
232 case PROP_NB_UNREAD_MESSAGES:
233 g_value_set_uint (value,
234 empathy_chat_get_nb_unread_messages (chat));
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243 chat_set_property (GObject *object,
248 EmpathyChat *chat = EMPATHY_CHAT (object);
252 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
254 case PROP_SHOW_CONTACTS:
255 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
264 account_reconnected (EmpathyChat *chat,
267 EmpathyChatPriv *priv = GET_PRIV (chat);
269 DEBUG ("Account reconnected, request a new Text channel");
271 /* FIXME: Ideally we should ask to handle ourself the channel so we can
272 * report the error if any but this is blocked by
273 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
274 switch (priv->handle_type) {
275 case TP_HANDLE_TYPE_CONTACT:
276 if (priv->sms_channel)
277 empathy_sms_contact_id (
279 TP_USER_ACTION_TIME_NOT_USER_ACTION,
282 empathy_chat_with_contact_id (
284 TP_USER_ACTION_TIME_NOT_USER_ACTION,
287 case TP_HANDLE_TYPE_ROOM:
288 empathy_join_muc (account, priv->id,
289 TP_USER_ACTION_TIME_NOT_USER_ACTION);
291 case TP_HANDLE_TYPE_NONE:
292 case TP_HANDLE_TYPE_LIST:
293 case TP_HANDLE_TYPE_GROUP:
295 g_assert_not_reached ();
299 g_object_unref (chat);
303 chat_new_connection_cb (TpAccount *account,
307 gchar *dbus_error_name,
311 EmpathyChatPriv *priv = GET_PRIV (chat);
313 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
316 if (priv->tp_chat != NULL || account != priv->account ||
317 priv->handle_type == TP_HANDLE_TYPE_NONE ||
318 EMP_STR_EMPTY (priv->id))
323 account_reconnected (chat, account);
327 chat_composing_remove_timeout (EmpathyChat *chat)
329 EmpathyChatPriv *priv;
331 priv = GET_PRIV (chat);
333 if (priv->composing_stop_timeout_id) {
334 g_source_remove (priv->composing_stop_timeout_id);
335 priv->composing_stop_timeout_id = 0;
340 set_chate_state_cb (GObject *source,
341 GAsyncResult *result,
344 GError *error = NULL;
346 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
348 DEBUG ("Failed to set chat state: %s", error->message);
349 g_error_free (error);
354 set_chat_state (EmpathyChat *self,
355 TpChannelChatState state)
357 EmpathyChatPriv *priv = GET_PRIV (self);
359 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
360 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
363 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
364 set_chate_state_cb, self);
368 chat_composing_stop_timeout_cb (EmpathyChat *chat)
370 EmpathyChatPriv *priv;
372 priv = GET_PRIV (chat);
374 priv->composing_stop_timeout_id = 0;
375 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
381 chat_composing_start (EmpathyChat *chat)
383 EmpathyChatPriv *priv;
385 priv = GET_PRIV (chat);
387 if (priv->composing_stop_timeout_id) {
388 /* Just restart the timeout */
389 chat_composing_remove_timeout (chat);
391 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
394 priv->composing_stop_timeout_id = g_timeout_add_seconds (
395 COMPOSING_STOP_TIMEOUT,
396 (GSourceFunc) chat_composing_stop_timeout_cb,
401 chat_composing_stop (EmpathyChat *chat)
403 chat_composing_remove_timeout (chat);
404 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
408 chat_input_history_entry_cmp (InputHistoryEntry *entry,
411 if (!tp_strdiff (entry->text, text)) {
412 if (entry->modified_text != NULL) {
413 /* Modified entry and single string cannot be equal. */
421 static InputHistoryEntry *
422 chat_input_history_entry_new_with_text (const gchar *text)
424 InputHistoryEntry *entry;
425 entry = g_slice_new0 (InputHistoryEntry);
426 entry->text = g_strdup (text);
432 chat_input_history_entry_free (InputHistoryEntry *entry)
434 g_free (entry->text);
435 g_free (entry->modified_text);
436 g_slice_free (InputHistoryEntry, entry);
440 chat_input_history_entry_revert (InputHistoryEntry *entry)
442 g_free (entry->modified_text);
443 entry->modified_text = NULL;
447 chat_input_history_entry_update_text (InputHistoryEntry *entry,
452 if (!tp_strdiff (text, entry->text)) {
453 g_free (entry->modified_text);
454 entry->modified_text = NULL;
458 old = entry->modified_text;
459 entry->modified_text = g_strdup (text);
464 chat_input_history_entry_get_text (InputHistoryEntry *entry)
470 if (entry->modified_text != NULL) {
471 return entry->modified_text;
477 chat_input_history_remove_item (GList *list,
480 list = g_list_remove_link (list, item);
481 chat_input_history_entry_free (item->data);
482 g_list_free_1 (item);
487 chat_input_history_revert (EmpathyChat *chat)
489 EmpathyChatPriv *priv;
493 InputHistoryEntry *entry;
495 priv = GET_PRIV (chat);
496 list = priv->input_history;
499 DEBUG ("No input history");
503 /* Delete temporary entry */
504 if (priv->input_history_current != NULL) {
506 list = chat_input_history_remove_item (list, item1);
507 if (priv->input_history_current == item1) {
508 /* Removed temporary entry was current entry */
509 priv->input_history = list;
510 priv->input_history_current = NULL;
515 /* There is no entry to revert */
519 /* Restore the current history entry to original value */
520 item1 = priv->input_history_current;
522 chat_input_history_entry_revert (entry);
524 /* Remove restored entry if there is other occurance before this entry */
525 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
526 (GCompareFunc) chat_input_history_entry_cmp);
527 if (item2 != item1) {
528 list = chat_input_history_remove_item (list, item1);
531 /* Remove other occurance of the restored entry */
532 item2 = g_list_find_custom (item1->next,
533 chat_input_history_entry_get_text (entry),
534 (GCompareFunc) chat_input_history_entry_cmp);
536 list = chat_input_history_remove_item (list, item2);
540 priv->input_history_current = NULL;
541 priv->input_history = list;
545 chat_input_history_add (EmpathyChat *chat,
549 EmpathyChatPriv *priv;
552 InputHistoryEntry *entry;
554 priv = GET_PRIV (chat);
556 list = priv->input_history;
558 /* Remove any other occurances of this entry, if not temporary */
560 while ((item = g_list_find_custom (list, str,
561 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
562 list = chat_input_history_remove_item (list, item);
565 /* Trim the list to the last 10 items */
566 while (g_list_length (list) > 10) {
567 item = g_list_last (list);
569 list = chat_input_history_remove_item (list, item);
577 entry = chat_input_history_entry_new_with_text (str);
578 list = g_list_prepend (list, entry);
580 /* Set the list and the current item pointer */
581 priv->input_history = list;
583 priv->input_history_current = list;
586 priv->input_history_current = NULL;
591 chat_input_history_get_next (EmpathyChat *chat)
593 EmpathyChatPriv *priv;
597 priv = GET_PRIV (chat);
599 if (priv->input_history == NULL) {
600 DEBUG ("No input history, next entry is NULL");
603 g_assert (priv->input_history_current != NULL);
605 if ((item = g_list_next (priv->input_history_current)) == NULL)
607 item = priv->input_history_current;
610 msg = chat_input_history_entry_get_text (item->data);
612 DEBUG ("Returning next entry: '%s'", msg);
614 priv->input_history_current = item;
620 chat_input_history_get_prev (EmpathyChat *chat)
622 EmpathyChatPriv *priv;
626 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
628 priv = GET_PRIV (chat);
630 if (priv->input_history == NULL) {
631 DEBUG ("No input history, previous entry is NULL");
635 if (priv->input_history_current == NULL)
639 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
641 item = priv->input_history_current;
644 msg = chat_input_history_entry_get_text (item->data);
646 DEBUG ("Returning previous entry: '%s'", msg);
648 priv->input_history_current = item;
654 chat_input_history_update (EmpathyChat *chat,
655 GtkTextBuffer *buffer)
657 EmpathyChatPriv *priv;
658 GtkTextIter start, end;
660 InputHistoryEntry *entry;
662 priv = GET_PRIV (chat);
664 gtk_text_buffer_get_bounds (buffer, &start, &end);
665 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
667 if (priv->input_history_current == NULL) {
668 /* Add the current text temporarily to the history */
669 chat_input_history_add (chat, text, TRUE);
674 /* Save the changes in the history */
675 entry = priv->input_history_current->data;
676 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
677 chat_input_history_entry_update_text (entry, text);
686 } ChatCommandMsgData;
689 chat_command_msg_cb (GObject *source,
690 GAsyncResult *result,
693 ChatCommandMsgData *data = user_data;
694 GError *error = NULL;
697 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
698 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
700 if (channel == NULL) {
701 DEBUG ("Failed to get channel: %s", error->message);
702 g_error_free (error);
704 empathy_chat_view_append_event (data->chat->view,
705 _("Failed to open private chat"));
709 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
710 TpTextChannel *text = (TpTextChannel *) channel;
713 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
716 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
718 g_object_unref (msg);
721 g_object_unref (channel);
724 g_free (data->message);
725 g_slice_free (ChatCommandMsgData, data);
729 nick_command_supported (EmpathyChat *chat)
731 EmpathyChatPriv * priv = GET_PRIV (chat);
732 TpConnection *connection;
734 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
735 return tp_proxy_has_interface_by_id (connection,
736 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
740 part_command_supported (EmpathyChat *chat)
742 EmpathyChatPriv * priv = GET_PRIV (chat);
744 return tp_proxy_has_interface_by_id (priv->tp_chat,
745 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
749 chat_command_clear (EmpathyChat *chat,
752 empathy_chat_view_clear (chat->view);
756 chat_command_topic (EmpathyChat *chat,
759 EmpathyChatPriv *priv = GET_PRIV (chat);
761 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
762 empathy_chat_view_append_event (chat->view,
763 _("Topic not supported on this conversation"));
767 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
768 empathy_chat_view_append_event (chat->view,
769 _("You are not allowed to change the topic"));
773 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
777 empathy_chat_join_muc (EmpathyChat *chat,
780 EmpathyChatPriv *priv = GET_PRIV (chat);
782 empathy_join_muc (priv->account, room,
783 empathy_get_current_action_time ());
787 chat_command_join (EmpathyChat *chat,
792 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
794 /* FIXME: Ideally we should ask to handle ourself the channel so we can
795 * report the error if any but this is blocked by
796 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
797 while (rooms[i] != NULL) {
798 /* ignore empty strings */
799 if (!EMP_STR_EMPTY (rooms[i])) {
800 empathy_chat_join_muc (chat, rooms[i]);
808 chat_command_part (EmpathyChat *chat,
811 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
815 chat_command_msg_internal (EmpathyChat *chat,
816 const gchar *contact_id,
817 const gchar *message)
819 EmpathyChatPriv *priv = GET_PRIV (chat);
820 ChatCommandMsgData *data;
821 TpAccountChannelRequest *req;
824 request = tp_asv_new (
825 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
826 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
827 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
830 req = tp_account_channel_request_new (priv->account, request,
831 empathy_get_current_action_time ());
833 /* FIXME: We should probably search in members alias. But this
834 * is enough for IRC */
835 data = g_slice_new (ChatCommandMsgData);
837 data->message = g_strdup (message);
839 tp_account_channel_request_ensure_and_observe_channel_async (req,
840 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
842 g_object_unref (req);
843 g_hash_table_unref (request);
847 chat_command_query (EmpathyChat *chat,
850 /* If <message> part is not defined,
851 * strv[2] will be the terminal NULL */
852 chat_command_msg_internal (chat, strv[1], strv[2]);
856 chat_command_msg (EmpathyChat *chat,
859 chat_command_msg_internal (chat, strv[1], strv[2]);
863 callback_for_request_rename (TpProxy *proxy,
866 GObject *weak_object)
869 DEBUG ("Call to RequestRename method failed: %s",error->message);
874 chat_command_nick (EmpathyChat *chat,
877 EmpathyChatPriv *priv = GET_PRIV (chat);
880 proxy = TP_PROXY (tp_account_get_connection (priv->account));
882 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
883 strv[1], callback_for_request_rename, NULL, NULL, NULL);
887 chat_command_me (EmpathyChat *chat,
890 EmpathyChatPriv *priv = GET_PRIV (chat);
892 TpTextChannel *channel;
894 channel = (TpTextChannel *) (priv->tp_chat);
896 if (!tp_text_channel_supports_message_type (channel,
897 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
898 /* Action message are not supported, 'simulate' the action */
899 EmpathyContact *self_contact;
902 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
903 /* The TpChat can't be ready if it doesn't have the self contact */
904 g_assert (self_contact != NULL);
906 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
908 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
913 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
917 empathy_tp_chat_send (priv->tp_chat, message);
918 g_object_unref (message);
922 chat_command_say (EmpathyChat *chat,
925 EmpathyChatPriv *priv = GET_PRIV (chat);
928 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
930 empathy_tp_chat_send (priv->tp_chat, message);
931 g_object_unref (message);
935 whois_got_contact_cb (TpConnection *connection,
937 TpContact * const *contacts,
938 const gchar * const *requested_ids,
939 GHashTable *failed_id_errors,
942 GObject *weak_object)
944 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
946 g_return_if_fail (n_contacts <= 1);
948 if (n_contacts == 0) {
950 gpointer key = NULL, value = NULL;
954 /* tp-glib guarantees that the contacts you requested will be
955 * in failed_id_errors regardless of whether the individual
956 * contact was invalid or the whole operation failed.
958 g_hash_table_iter_init (&iter, failed_id_errors);
959 g_hash_table_iter_next (&iter, &key, &value);
963 DEBUG ("Error getting TpContact for '%s': %s %u %s",
964 id, g_quark_to_string (id_error->domain),
965 id_error->code, id_error->message);
968 /* The specific ID failed. */
969 gchar *event = g_strdup_printf (
970 _("“%s” is not a valid contact ID"), id);
971 empathy_chat_view_append_event (chat->view, event);
974 /* Otherwise we're disconnected or something; so the window
975 * will already say ‘Disconnected’, so let's not show anything.
978 EmpathyContact *empathy_contact;
981 g_return_if_fail (contacts[0] != NULL);
982 empathy_contact = empathy_contact_dup_from_tp_contact (
985 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
986 /* If we're alive and this command is running, we'd better be
988 g_return_if_fail (window != NULL);
989 g_return_if_fail (gtk_widget_is_toplevel (window));
990 empathy_contact_information_dialog_show (empathy_contact,
991 GTK_WINDOW (window));
992 g_object_unref (empathy_contact);
997 chat_command_whois (EmpathyChat *chat,
1000 EmpathyChatPriv *priv = GET_PRIV (chat);
1003 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1004 tp_connection_get_contacts_by_id (conn,
1005 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1006 * entered by the user (including spaces, if any). */
1007 1, (const gchar * const *) strv + 1,
1009 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1013 chat_command_whale (EmpathyChat *chat,
1016 EmpathyChatPriv *priv = GET_PRIV (chat);
1019 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1021 "•_______________•");
1022 empathy_tp_chat_send (priv->tp_chat, message);
1023 g_object_unref (message);
1027 chat_command_babywhale (EmpathyChat *chat,
1030 EmpathyChatPriv *priv = GET_PRIV (chat);
1033 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1036 empathy_tp_chat_send (priv->tp_chat, message);
1037 g_object_unref (message);
1041 chat_command_inspector (EmpathyChat *chat,
1044 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1045 empathy_theme_adium_show_inspector (
1046 EMPATHY_THEME_ADIUM (chat->view));
1050 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1052 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1055 const gchar *prefix;
1058 ChatCommandFunc func;
1059 gboolean (*is_supported)(EmpathyChat *chat);
1063 static ChatCommandItem commands[] = {
1064 {"clear", 1, 1, chat_command_clear, NULL,
1065 N_("/clear: clear all messages from the current conversation")},
1067 {"topic", 2, 2, chat_command_topic, NULL,
1068 N_("/topic <topic>: set the topic of the current conversation")},
1070 {"join", 2, 2, chat_command_join, NULL,
1071 N_("/join <chat room ID>: join a new chat room")},
1073 {"j", 2, 2, chat_command_join, NULL,
1074 N_("/j <chat room ID>: join a new chat room")},
1076 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1077 {"part", 1, 3, chat_command_part, part_command_supported,
1078 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1079 "by default the current one")},
1081 {"query", 2, 3, chat_command_query, NULL,
1082 N_("/query <contact ID> [<message>]: open a private chat")},
1084 {"msg", 3, 3, chat_command_msg, NULL,
1085 N_("/msg <contact ID> <message>: open a private chat")},
1087 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1088 N_("/nick <nickname>: change your nickname on the current server")},
1090 {"me", 2, 2, chat_command_me, NULL,
1091 N_("/me <message>: send an ACTION message to the current conversation")},
1093 {"say", 2, 2, chat_command_say, NULL,
1094 N_("/say <message>: send <message> to the current conversation. "
1095 "This is used to send a message starting with a '/'. For example: "
1096 "\"/say /join is used to join a new chat room\"")},
1098 {"whois", 2, 2, chat_command_whois, NULL,
1099 N_("/whois <contact ID>: display information about a contact")},
1101 {"help", 1, 2, chat_command_help, NULL,
1102 N_("/help [<command>]: show all supported commands. "
1103 "If <command> is defined, show its usage.")},
1105 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1107 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1108 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1112 chat_command_show_help (EmpathyChat *chat,
1113 ChatCommandItem *item)
1117 if (item->help == NULL) {
1121 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1122 empathy_chat_view_append_event (chat->view, str);
1127 chat_command_help (EmpathyChat *chat,
1132 /* If <command> part is not defined,
1133 * strv[1] will be the terminal NULL */
1134 if (strv[1] == NULL) {
1135 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1136 if (commands[i].is_supported != NULL) {
1137 if (!commands[i].is_supported (chat)) {
1141 if (commands[i].help == NULL) {
1144 empathy_chat_view_append_event (chat->view,
1145 _(commands[i].help));
1150 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1151 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1152 if (commands[i].is_supported != NULL) {
1153 if (!commands[i].is_supported (chat)) {
1157 if (commands[i].help == NULL) {
1160 chat_command_show_help (chat, &commands[i]);
1165 empathy_chat_view_append_event (chat->view,
1166 _("Unknown command"));
1170 chat_command_parse (const gchar *text, guint max_parts)
1175 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1177 array = g_ptr_array_sized_new (max_parts + 1);
1178 while (max_parts > 1) {
1181 /* Skip white spaces */
1182 while (g_ascii_isspace (*text)) {
1186 /* Search the end of this part, until first space. */
1187 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1193 item = g_strndup (text, end - text);
1194 g_ptr_array_add (array, item);
1195 DEBUG ("\tITEM: \"%s\"", item);
1201 /* Append last part if not empty */
1202 item = g_strstrip (g_strdup (text));
1203 if (!EMP_STR_EMPTY (item)) {
1204 g_ptr_array_add (array, item);
1205 DEBUG ("\tITEM: \"%s\"", item);
1210 /* Make the array NULL-terminated */
1211 g_ptr_array_add (array, NULL);
1213 return (GStrv) g_ptr_array_free (array, FALSE);
1217 has_prefix_case (const gchar *s,
1218 const gchar *prefix)
1220 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1224 chat_send (EmpathyChat *chat,
1227 EmpathyChatPriv *priv;
1231 if (EMP_STR_EMPTY (msg)) {
1235 priv = GET_PRIV (chat);
1237 chat_input_history_add (chat, msg, FALSE);
1239 if (msg[0] == '/') {
1240 gboolean second_slash = FALSE;
1241 const gchar *iter = msg + 1;
1243 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1248 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1251 c = *(msg + 1 + strlen (commands[i].prefix));
1252 if (c != '\0' && !g_ascii_isspace (c)) {
1255 if (commands[i].is_supported != NULL) {
1256 if (!commands[i].is_supported (chat)) {
1261 /* We can't use g_strsplit here because it does
1262 * not deal correctly if we have more than one space
1264 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1266 strv_len = g_strv_length (strv);
1267 if (strv_len < commands[i].min_parts ||
1268 strv_len > commands[i].max_parts) {
1269 chat_command_show_help (chat, &commands[i]);
1274 commands[i].func (chat, strv);
1279 /* Also allow messages with two slashes before the
1280 * first space, so it is possible to send a /unix/path.
1281 * This heuristic is kind of crap. */
1282 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1284 second_slash = TRUE;
1290 if (!second_slash) {
1291 empathy_chat_view_append_event (chat->view,
1292 _("Unknown command; see /help for the available"
1298 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1300 empathy_tp_chat_send (priv->tp_chat, message);
1301 g_object_unref (message);
1305 chat_input_text_view_send (EmpathyChat *chat)
1307 GtkTextBuffer *buffer;
1308 GtkTextIter start, end;
1311 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1313 gtk_text_buffer_get_bounds (buffer, &start, &end);
1314 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1316 /* clear the input field */
1317 gtk_text_buffer_set_text (buffer, "", -1);
1318 /* delete input history modifications */
1319 chat_input_history_revert (chat);
1321 chat_send (chat, msg);
1326 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1327 EmpathyContact *contact,
1328 TpChannelChatState state,
1331 EmpathyChatPriv *priv;
1333 gboolean was_composing;
1335 priv = GET_PRIV (chat);
1337 if (empathy_contact_is_user (contact)) {
1338 /* We don't care about our own chat state */
1342 was_composing = (priv->compositors != NULL);
1344 /* Find the contact in the list. After that l is the list elem or NULL */
1345 for (l = priv->compositors; l; l = l->next) {
1346 if (contact == l->data) {
1352 case TP_CHANNEL_CHAT_STATE_GONE:
1353 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1354 case TP_CHANNEL_CHAT_STATE_PAUSED:
1355 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1356 /* Contact is not composing */
1358 priv->compositors = g_list_remove_link (priv->compositors, l);
1359 g_object_unref (l->data);
1363 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1364 /* Contact is composing */
1366 priv->compositors = g_list_prepend (priv->compositors,
1367 g_object_ref (contact));
1371 g_assert_not_reached ();
1374 DEBUG ("Was composing: %s now composing: %s",
1375 was_composing ? "yes" : "no",
1376 priv->compositors ? "yes" : "no");
1378 if ((was_composing && !priv->compositors) ||
1379 (!was_composing && priv->compositors)) {
1380 /* Composing state changed */
1381 g_signal_emit (chat, signals[COMPOSING], 0,
1382 priv->compositors != NULL);
1387 get_highlight_regex_for (const gchar *name)
1390 gchar *name_esc, *pattern;
1391 GError *error = NULL;
1393 name_esc = g_regex_escape_string (name, -1);
1394 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1395 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1398 if (regex == NULL) {
1399 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1402 g_error_free (error);
1412 chat_should_highlight (EmpathyChat *chat,
1413 EmpathyMessage *message)
1415 EmpathyContact *contact;
1416 const gchar *msg, *to;
1417 gboolean ret_val = FALSE;
1418 TpChannelTextMessageFlags flags;
1421 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1423 if (!empathy_chat_is_room (chat)) {
1427 if (!empathy_message_is_incoming (message)) {
1431 msg = empathy_message_get_body (message);
1436 contact = empathy_tp_chat_get_self_contact (chat->priv->tp_chat);
1441 to = empathy_contact_get_alias (contact);
1446 flags = empathy_message_get_flags (message);
1447 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1448 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1449 * have already been received by the user before (and so are in the logs) */
1453 regex = get_highlight_regex_for (to);
1454 if (regex != NULL) {
1455 ret_val = g_regex_match (regex, msg, 0, NULL);
1456 g_regex_unref (regex);
1463 chat_message_received (EmpathyChat *chat,
1464 EmpathyMessage *message,
1467 EmpathyChatPriv *priv = GET_PRIV (chat);
1468 EmpathyContact *sender;
1470 sender = empathy_message_get_sender (message);
1472 if (empathy_message_is_edit (message)) {
1473 DEBUG ("Editing message '%s' to '%s'",
1474 empathy_message_get_supersedes (message),
1475 empathy_message_get_body (message));
1477 empathy_chat_view_edit_message (chat->view, message);
1479 gboolean should_highlight = chat_should_highlight (chat, message);
1480 DEBUG ("Appending new message '%s' from %s (%d)",
1481 empathy_message_get_token (message),
1482 empathy_contact_get_alias (sender),
1483 empathy_contact_get_handle (sender));
1485 empathy_chat_view_append_message (chat->view, message, should_highlight);
1487 if (empathy_message_is_incoming (message)) {
1488 priv->unread_messages++;
1489 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1492 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1496 /* We received a message so the contact is no longer
1498 chat_state_changed_cb (priv->tp_chat, sender,
1499 TP_CHANNEL_CHAT_STATE_ACTIVE,
1504 chat_message_received_cb (EmpathyTpChat *tp_chat,
1505 EmpathyMessage *message,
1508 chat_message_received (chat, message, FALSE);
1512 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1513 EmpathyMessage *message,
1516 EmpathyChatPriv *priv = GET_PRIV (chat);
1518 empathy_chat_view_message_acknowledged (chat->view,
1521 if (!empathy_message_is_edit (message)) {
1522 priv->unread_messages--;
1523 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1528 append_balance_error (EmpathyChat *chat,
1529 const gchar *message_body)
1531 EmpathyChatPriv *priv = GET_PRIV (chat);
1532 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1533 const gchar *uri = tp_connection_get_balance_uri (conn);
1534 const gchar *error = _("insufficient balance to send message");
1535 gchar *str, *str_markup = NULL;
1537 if (message_body != NULL) {
1538 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1540 str = g_strdup_printf (_("Error sending message: %s"), error);
1543 if (!tp_str_empty (uri)) {
1544 /* translators: error used when user doesn't have enough credit on his
1545 * account to send the message. */
1546 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1547 " <a href='%s'>Top up</a>."), uri);
1549 if (message_body != NULL) {
1550 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1552 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1553 escaped_body, markup_error);
1555 g_free (escaped_body);
1557 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1560 g_free (markup_error);
1563 if (str_markup != NULL)
1564 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1566 empathy_chat_view_append_event (chat->view, str);
1569 g_free (str_markup);
1573 chat_send_error_cb (EmpathyTpChat *tp_chat,
1574 const gchar *message_body,
1575 TpChannelTextSendError error_code,
1576 const gchar *dbus_error,
1579 const gchar *error = NULL;
1582 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1583 append_balance_error (chat, message_body);
1585 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1586 error = _("not capable");
1589 if (error == NULL) {
1590 /* if we didn't find a dbus-error, try the old error */
1591 switch (error_code) {
1592 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1593 error = _("offline");
1595 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1596 error = _("invalid contact");
1598 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1599 error = _("permission denied");
1601 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1602 error = _("too long message");
1604 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1605 error = _("not implemented");
1607 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1609 error = _("unknown");
1614 if (message_body != NULL) {
1615 str = g_strdup_printf (_("Error sending message '%s': %s"),
1616 message_body, error);
1619 str = g_strdup_printf (_("Error sending message: %s"), error);
1622 empathy_chat_view_append_event (chat->view, str);
1627 chat_topic_label_size_allocate_cb (GtkLabel *label,
1628 GtkAllocation *allocation,
1631 EmpathyChatPriv *priv = GET_PRIV (chat);
1633 if (!gtk_label_get_line_wrap (label)) {
1634 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1635 gtk_widget_show (priv->expander_topic);
1637 gtk_widget_hide (priv->expander_topic);
1644 chat_topic_expander_activate_cb (GtkExpander *expander,
1645 GParamSpec *param_spec,
1648 EmpathyChatPriv *priv = GET_PRIV (chat);
1650 if (gtk_expander_get_expanded (expander)) {
1651 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1652 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1654 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1655 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1660 chat_subject_changed_cb (EmpathyChat *chat)
1662 EmpathyChatPriv *priv = GET_PRIV (chat);
1664 g_free (priv->subject);
1665 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1666 g_object_notify (G_OBJECT (chat), "subject");
1668 if (EMP_STR_EMPTY (priv->subject)) {
1669 gtk_widget_hide (priv->hbox_topic);
1671 gchar *markup_topic;
1674 markup_topic = empathy_add_link_markup (priv->subject);
1675 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1676 _("Topic:"), markup_topic);
1678 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1679 g_free (markup_text);
1680 g_free (markup_topic);
1682 gtk_widget_show (priv->hbox_topic);
1684 if (priv->block_events_timeout_id == 0) {
1687 if (!EMP_STR_EMPTY (priv->subject)) {
1688 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1690 str = g_strdup (_("No topic defined"));
1692 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1698 chat_title_changed_cb (EmpathyChat *chat)
1700 EmpathyChatPriv *priv = GET_PRIV (chat);
1702 g_free (priv->name);
1703 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1704 g_object_notify (G_OBJECT (chat), "name");
1708 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1712 GtkTextIter word_start = *iter;
1713 GtkTextIter word_end = *iter;
1716 if (gtk_text_iter_inside_word (&word_end) &&
1717 !gtk_text_iter_ends_word (&word_end)) {
1718 gtk_text_iter_forward_word_end (&word_end);
1723 if (gtk_text_iter_get_char (&tmp) == '\'') {
1724 gtk_text_iter_forward_char (&tmp);
1726 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1727 gtk_text_iter_forward_word_end (&word_end);
1732 if (gtk_text_iter_inside_word (&word_start) ||
1733 gtk_text_iter_ends_word (&word_start)) {
1734 if (!gtk_text_iter_starts_word (&word_start) ||
1735 gtk_text_iter_equal (&word_start, &word_end)) {
1736 gtk_text_iter_backward_word_start (&word_start);
1740 gtk_text_iter_backward_char (&tmp);
1742 if (gtk_text_iter_get_char (&tmp) == '\'') {
1743 gtk_text_iter_backward_char (&tmp);
1745 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1746 gtk_text_iter_backward_word_start (&word_start);
1751 *start = word_start;
1757 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1758 GtkTextIter *location,
1763 GtkTextIter iter, pos;
1765 /* Remove all misspelled tags in the inserted text.
1766 * This happens when text is inserted within a misspelled word. */
1767 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1768 gtk_text_iter_get_offset (location) - len);
1769 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1772 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1775 GtkTextIter start, end;
1778 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1781 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1783 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1784 gtk_text_iter_equal (&pos, &end) ||
1785 empathy_spell_check (str)) {
1786 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1788 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1793 } while (gtk_text_iter_forward_word_end (&iter) &&
1794 gtk_text_iter_compare (&iter, location) <= 0);
1798 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1803 GtkTextIter word_start, word_end;
1805 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1806 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1807 &word_start, &word_end);
1812 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1815 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1816 chat_composing_stop (chat);
1818 chat_composing_start (chat);
1823 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1828 GtkTextIter prev_pos;
1829 GtkTextIter word_start;
1830 GtkTextIter word_end;
1834 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1836 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1837 gtk_text_buffer_get_insert (buffer));
1838 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1840 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1843 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1844 !gtk_text_iter_equal (&pos, &word_end)) {
1845 str = gtk_text_buffer_get_text (buffer,
1846 &word_start, &word_end, FALSE);
1848 if (!empathy_spell_check (str)) {
1849 gtk_text_buffer_apply_tag_by_name (buffer,
1850 "misspelled", &word_start, &word_end);
1852 gtk_text_buffer_remove_tag_by_name (buffer,
1853 "misspelled", &word_start, &word_end);
1860 gtk_text_buffer_move_mark (buffer, mark, &pos);
1864 empathy_isspace_cb (gunichar c,
1867 return g_unichar_isspace (c);
1871 chat_input_key_press_event_cb (GtkWidget *widget,
1875 EmpathyChatPriv *priv;
1878 GtkWidget *text_view_sw;
1880 priv = GET_PRIV (chat);
1882 priv->most_recent_event_type = event->type;
1884 /* Catch ctrl+up/down so we can traverse messages we sent */
1885 if ((event->state & GDK_CONTROL_MASK) &&
1886 (event->keyval == GDK_KEY_Up ||
1887 event->keyval == GDK_KEY_Down)) {
1888 GtkTextBuffer *buffer;
1891 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1892 chat_input_history_update (chat, buffer);
1894 if (event->keyval == GDK_KEY_Up) {
1895 str = chat_input_history_get_next (chat);
1897 str = chat_input_history_get_prev (chat);
1900 g_signal_handlers_block_by_func (buffer,
1901 chat_input_text_buffer_changed_cb,
1903 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1904 g_signal_handlers_unblock_by_func (buffer,
1905 chat_input_text_buffer_changed_cb,
1911 /* Catch enter but not ctrl/shift-enter */
1912 if (IS_ENTER (event->keyval) &&
1913 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1916 /* This is to make sure that kinput2 gets the enter. And if
1917 * it's handled there we shouldn't send on it. This is because
1918 * kinput2 uses Enter to commit letters. See:
1919 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1922 view = GTK_TEXT_VIEW (chat->input_text_view);
1923 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1924 gtk_text_view_reset_im_context (view);
1928 chat_input_text_view_send (chat);
1932 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1934 if (IS_ENTER (event->keyval) &&
1935 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1936 /* Newline for shift/control-enter. */
1939 if (!(event->state & GDK_CONTROL_MASK) &&
1940 event->keyval == GDK_KEY_Page_Up) {
1941 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1942 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1945 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1946 event->keyval == GDK_KEY_Page_Down) {
1947 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1948 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1949 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1950 gtk_adjustment_set_value (adj, val);
1953 if (event->keyval == GDK_KEY_Escape) {
1954 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1956 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1957 event->keyval == GDK_KEY_Tab) {
1958 GtkTextBuffer *buffer;
1959 GtkTextIter start, current;
1960 gchar *nick, *completed;
1961 GList *list, *completed_list;
1962 gboolean is_start_of_buffer;
1964 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1965 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1967 /* Get the start of the nick to complete. */
1968 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1969 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1970 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1972 is_start_of_buffer = gtk_text_iter_is_start (&start);
1974 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1975 g_completion_add_items (priv->completion, list);
1977 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1978 completed_list = g_completion_complete (priv->completion,
1987 GString *message = NULL;
1990 gtk_text_buffer_delete (buffer, &start, ¤t);
1992 len = g_list_length (completed_list);
1995 /* If we only have one hit, use that text
1996 * instead of the text in completed since the
1997 * completed text will use the typed string
1998 * which might be cased all wrong.
2001 text = empathy_contact_get_alias (completed_list->data);
2005 /* Print all hits to the scrollback view, so the
2006 * user knows what possibilities he has.
2009 message = g_string_new ("");
2010 for (l = completed_list; l != NULL; l = l->next) {
2011 g_string_append (message, empathy_contact_get_alias (l->data));
2012 g_string_append (message, " - ");
2014 empathy_chat_view_append_event (chat->view, message->str);
2015 g_string_free (message, TRUE);
2018 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2020 if (len == 1 && is_start_of_buffer) {
2021 gchar *complete_char;
2023 complete_char = g_settings_get_string (
2024 priv->gsettings_chat,
2025 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2027 if (complete_char != NULL) {
2028 gtk_text_buffer_insert_at_cursor (buffer,
2030 strlen (complete_char));
2031 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2032 g_free (complete_char);
2039 g_completion_clear_items (priv->completion);
2041 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2051 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2055 gtk_widget_grab_focus (chat->input_text_view);
2061 chat_input_realize_cb (GtkWidget *widget,
2064 DEBUG ("Setting focus to the input text view");
2065 if (gtk_widget_is_sensitive (widget)) {
2066 gtk_widget_grab_focus (widget);
2071 chat_input_has_focus_notify_cb (GtkWidget *widget,
2075 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2079 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2080 EmpathySmiley *smiley,
2083 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2084 GtkTextBuffer *buffer;
2087 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2089 gtk_text_buffer_get_end_iter (buffer, &iter);
2090 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2092 gtk_text_buffer_get_end_iter (buffer, &iter);
2093 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2104 static EmpathyChatSpell *
2105 chat_spell_new (EmpathyChat *chat,
2110 EmpathyChatSpell *chat_spell;
2112 chat_spell = g_slice_new0 (EmpathyChatSpell);
2114 chat_spell->chat = g_object_ref (chat);
2115 chat_spell->word = g_strdup (word);
2116 chat_spell->start = start;
2117 chat_spell->end = end;
2123 chat_spell_free (EmpathyChatSpell *chat_spell)
2125 g_object_unref (chat_spell->chat);
2126 g_free (chat_spell->word);
2127 g_slice_free (EmpathyChatSpell, chat_spell);
2131 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2132 EmpathyChatSpell *chat_spell)
2134 empathy_chat_correct_word (chat_spell->chat,
2135 &(chat_spell->start),
2137 gtk_menu_item_get_label (menu_item));
2142 chat_spelling_build_suggestions_menu (const gchar *code,
2143 EmpathyChatSpell *chat_spell)
2145 GList *suggestions, *l;
2146 GtkWidget *menu, *menu_item;
2148 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2149 if (suggestions == NULL)
2152 menu = gtk_menu_new ();
2153 for (l = suggestions; l; l = l->next) {
2154 menu_item = gtk_menu_item_new_with_label (l->data);
2155 g_signal_connect (G_OBJECT (menu_item), "activate",
2156 G_CALLBACK (chat_spelling_menu_activate_cb),
2158 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2160 empathy_spell_free_suggestions (suggestions);
2162 gtk_widget_show_all (menu);
2168 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2170 GtkWidget *menu, *submenu, *item;
2173 codes = empathy_spell_get_enabled_language_codes ();
2174 g_assert (codes != NULL);
2176 if (g_list_length (codes) > 1) {
2177 menu = gtk_menu_new ();
2179 for (l = codes; l; l = l->next) {
2180 const gchar *code, *name;
2183 name = empathy_spell_get_language_name (code);
2187 item = gtk_image_menu_item_new_with_label (name);
2189 submenu = chat_spelling_build_suggestions_menu (
2191 if (submenu == NULL)
2192 gtk_widget_set_sensitive (item, FALSE);
2194 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2196 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2199 menu = chat_spelling_build_suggestions_menu (codes->data,
2202 menu = gtk_menu_new ();
2203 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2204 gtk_widget_set_sensitive (item, FALSE);
2205 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2208 g_list_free (codes);
2210 gtk_widget_show_all (menu);
2221 static EmpathyChatWord *
2222 chat_word_new (EmpathyChat *chat,
2226 EmpathyChatWord *chat_word;
2228 chat_word = g_slice_new0 (EmpathyChatWord);
2230 chat_word->chat = g_object_ref (chat);
2231 chat_word->word = g_strdup (word);
2232 chat_word->code = g_strdup (code);
2238 chat_word_free (EmpathyChatWord *chat_word)
2240 g_object_unref (chat_word->chat);
2241 g_free (chat_word->word);
2242 g_free (chat_word->code);
2243 g_slice_free (EmpathyChatWord, chat_word);
2247 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2248 EmpathyChatWord *chat_word)
2250 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2252 empathy_spell_add_to_dictionary (chat_word->code,
2254 priv->update_misspelled_words_id =
2255 g_idle_add (update_misspelled_words, chat_word->chat);
2259 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2261 GtkWidget *menu, *item, *lang_item, *image;
2264 const gchar *code, *name;
2265 EmpathyChatWord *chat_word;
2267 codes = empathy_spell_get_enabled_language_codes ();
2268 g_assert (codes != NULL);
2269 if (g_list_length (codes) > 1) {
2270 /* translators: %s is the selected word */
2271 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2273 item = gtk_image_menu_item_new_with_mnemonic (label);
2275 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2276 GTK_ICON_SIZE_MENU);
2277 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2280 menu = gtk_menu_new ();
2282 for (l = codes; l; l = l->next) {
2284 name = empathy_spell_get_language_name (code);
2288 lang_item = gtk_image_menu_item_new_with_label (name);
2290 chat_word= chat_word_new (chat_spell->chat,
2291 chat_spell->word, code);
2292 g_object_set_data_full (G_OBJECT (lang_item),
2293 "chat-word", chat_word,
2294 (GDestroyNotify) chat_word_free);
2296 g_signal_connect (G_OBJECT (lang_item), "activate",
2297 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2299 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2301 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2304 name = empathy_spell_get_language_name (code);
2305 g_assert (name != NULL);
2306 /* translators: first %s is the selected word,
2307 * second %s is the language name of the target dictionary */
2308 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2309 chat_spell->word, name);
2310 item = gtk_image_menu_item_new_with_mnemonic (label);
2312 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2313 GTK_ICON_SIZE_MENU);
2314 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2316 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2318 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2319 (GDestroyNotify) chat_word_free);
2321 g_signal_connect (G_OBJECT (item), "activate",
2322 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2325 g_list_free (codes);
2327 gtk_widget_show_all (item);
2333 chat_text_send_cb (GtkMenuItem *menuitem,
2336 chat_input_text_view_send (chat);
2340 chat_input_button_press_event_cb (GtkTextView *view,
2341 GdkEventButton *event,
2344 EmpathyChatPriv *priv = GET_PRIV (chat);
2346 priv->most_recent_event_type = event->type;
2352 chat_input_populate_popup_cb (GtkTextView *view,
2356 EmpathyChatPriv *priv = GET_PRIV (chat);
2357 GtkTextBuffer *buffer;
2358 GtkTextTagTable *table;
2361 GtkTextIter iter, start, end;
2364 EmpathyChatSpell *chat_spell;
2365 GtkWidget *spell_menu;
2366 GtkWidget *spell_item;
2367 EmpathySmileyManager *smiley_manager;
2368 GtkWidget *smiley_menu;
2371 buffer = gtk_text_view_get_buffer (view);
2373 /* Add the emoticon menu. */
2374 item = gtk_separator_menu_item_new ();
2375 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2376 gtk_widget_show (item);
2378 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2379 image = gtk_image_new_from_icon_name ("face-smile",
2380 GTK_ICON_SIZE_MENU);
2381 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2382 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2383 gtk_widget_show (item);
2385 smiley_manager = empathy_smiley_manager_dup_singleton ();
2386 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2387 chat_insert_smiley_activate_cb,
2389 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2390 g_object_unref (smiley_manager);
2392 /* Add the Send menu item. */
2393 gtk_text_buffer_get_bounds (buffer, &start, &end);
2394 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2395 if (!EMP_STR_EMPTY (str)) {
2396 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2397 g_signal_connect (G_OBJECT (item), "activate",
2398 G_CALLBACK (chat_text_send_cb), chat);
2399 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2400 gtk_widget_show (item);
2404 /* Add the spell check menu item. */
2405 table = gtk_text_buffer_get_tag_table (buffer);
2406 tag = gtk_text_tag_table_lookup (table, "misspelled");
2408 switch (priv->most_recent_event_type) {
2409 case GDK_BUTTON_PRESS:
2410 /* get the location from the pointer */
2411 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2412 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2413 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2415 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2416 GTK_TEXT_WINDOW_WIDGET,
2419 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2424 g_warn_if_reached ();
2425 /* assume the KEY_PRESS case */
2428 /* get the location from the cursor */
2429 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2430 gtk_text_buffer_get_insert (buffer));
2436 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2437 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2439 str = gtk_text_buffer_get_text (buffer,
2440 &start, &end, FALSE);
2442 if (!EMP_STR_EMPTY (str)) {
2443 chat_spell = chat_spell_new (chat, str, start, end);
2444 g_object_set_data_full (G_OBJECT (menu),
2445 "chat-spell", chat_spell,
2446 (GDestroyNotify) chat_spell_free);
2448 item = gtk_separator_menu_item_new ();
2449 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2450 gtk_widget_show (item);
2452 /* Spelling suggestions */
2453 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2454 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2455 GTK_ICON_SIZE_MENU);
2456 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2458 spell_menu = chat_spelling_build_menu (chat_spell);
2459 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2462 spell_item = gtk_separator_menu_item_new ();
2463 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2464 gtk_widget_show (spell_item);
2466 /* Add to dictionary */
2467 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2469 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2470 gtk_widget_show (spell_item);
2472 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2473 gtk_widget_show (item);
2479 chat_log_filter (TplEvent *event,
2482 EmpathyChat *chat = user_data;
2483 EmpathyMessage *message;
2484 EmpathyChatPriv *priv = GET_PRIV (chat);
2485 const GList *pending;
2487 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2488 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2490 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2491 message = empathy_message_from_tpl_log_event (event);
2493 for (; pending; pending = g_list_next (pending)) {
2494 if (empathy_message_equal (message, pending->data)) {
2495 g_object_unref (message);
2500 g_object_unref (message);
2506 show_pending_messages (EmpathyChat *chat) {
2507 EmpathyChatPriv *priv = GET_PRIV (chat);
2508 const GList *messages, *l;
2510 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2512 if (chat->view == NULL || priv->tp_chat == NULL)
2515 if (!priv->can_show_pending)
2518 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2520 for (l = messages; l != NULL ; l = g_list_next (l)) {
2521 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2522 chat_message_received (chat, message, TRUE);
2528 got_filtered_messages_cb (GObject *manager,
2529 GAsyncResult *result,
2534 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2535 EmpathyChatPriv *priv = GET_PRIV (chat);
2536 GError *error = NULL;
2538 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2539 result, &messages, &error)) {
2540 DEBUG ("%s. Aborting.", error->message);
2541 empathy_chat_view_append_event (chat->view,
2542 _("Failed to retrieve recent logs"));
2543 g_error_free (error);
2547 for (l = messages; l; l = g_list_next (l)) {
2548 EmpathyMessage *message;
2550 g_assert (TPL_IS_EVENT (l->data));
2552 message = empathy_message_from_tpl_log_event (l->data);
2553 g_object_unref (l->data);
2555 if (empathy_message_is_edit (message)) {
2556 /* this is an edited message, create a synthetic event
2557 * using the supersedes token and
2558 * original-message-sent timestamp, that we can then
2560 EmpathyMessage *syn_msg = g_object_new (
2561 EMPATHY_TYPE_MESSAGE,
2563 "token", empathy_message_get_supersedes (message),
2564 "type", empathy_message_get_tptype (message),
2565 "timestamp", empathy_message_get_original_timestamp (message),
2566 "incoming", empathy_message_is_incoming (message),
2568 "receiver", empathy_message_get_receiver (message),
2569 "sender", empathy_message_get_sender (message),
2572 empathy_chat_view_append_message (chat->view, syn_msg,
2573 chat_should_highlight (chat, syn_msg));
2574 empathy_chat_view_edit_message (chat->view, message);
2576 g_object_unref (syn_msg);
2578 /* append the latest message */
2579 empathy_chat_view_append_message (chat->view, message,
2580 chat_should_highlight (chat, message));
2583 g_object_unref (message);
2585 g_list_free (messages);
2588 /* in case of TPL error, skip backlog and show pending messages */
2589 priv->can_show_pending = TRUE;
2590 show_pending_messages (chat);
2592 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2593 * about it in EmpathyChatPriv definition */
2594 priv->retrieving_backlogs = FALSE;
2595 empathy_chat_messages_read (chat);
2597 /* Turn back on scrolling */
2598 empathy_chat_view_scroll (chat->view, TRUE);
2602 chat_add_logs (EmpathyChat *chat)
2604 EmpathyChatPriv *priv = GET_PRIV (chat);
2611 /* Turn off scrolling temporarily */
2612 empathy_chat_view_scroll (chat->view, FALSE);
2614 /* Add messages from last conversation */
2615 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2616 target = tpl_entity_new_from_room_id (priv->id);
2618 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2620 priv->retrieving_backlogs = TRUE;
2621 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2624 TPL_EVENT_MASK_TEXT,
2628 got_filtered_messages_cb,
2631 g_object_unref (target);
2635 chat_contacts_completion_func (const gchar *s1,
2639 gchar *tmp, *nick1, *nick2;
2646 return s1 ? -1 : +1;
2649 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2650 nick1 = g_utf8_casefold (tmp, -1);
2653 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2654 nick2 = g_utf8_casefold (tmp, -1);
2657 ret = strncmp (nick1, nick2, n);
2666 build_part_message (guint reason,
2668 EmpathyContact *actor,
2669 const gchar *message)
2671 GString *s = g_string_new ("");
2672 const gchar *actor_name = NULL;
2674 if (actor != NULL) {
2675 actor_name = empathy_contact_get_alias (actor);
2678 /* Having an actor only really makes sense for a few actions... */
2680 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2681 g_string_append_printf (s, _("%s has disconnected"), name);
2683 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2684 if (actor_name != NULL) {
2685 /* translators: reverse the order of these arguments
2686 * if the kicked should come before the kicker in your locale.
2688 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2691 g_string_append_printf (s, _("%s was kicked"), name);
2694 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2695 if (actor_name != NULL) {
2696 /* translators: reverse the order of these arguments
2697 * if the banned should come before the banner in your locale.
2699 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2702 g_string_append_printf (s, _("%s was banned"), name);
2706 g_string_append_printf (s, _("%s has left the room"), name);
2709 if (!EMP_STR_EMPTY (message)) {
2710 /* Note to translators: this string is appended to
2711 * notifications like "foo has left the room", with the message
2712 * given by the user living the room. If this poses a problem,
2713 * please let us know. :-)
2715 g_string_append_printf (s, _(" (%s)"), message);
2718 return g_string_free (s, FALSE);
2722 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2723 EmpathyContact *contact,
2724 EmpathyContact *actor,
2730 EmpathyChatPriv *priv = GET_PRIV (chat);
2731 const gchar *name = empathy_contact_get_alias (contact);
2734 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2736 if (priv->block_events_timeout_id != 0)
2740 str = g_strdup_printf (_("%s has joined the room"),
2743 str = build_part_message (reason, name, actor, message);
2746 empathy_chat_view_append_event (chat->view, str);
2751 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2752 EmpathyContact *old_contact,
2753 EmpathyContact *new_contact,
2758 EmpathyChatPriv *priv = GET_PRIV (chat);
2760 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2762 if (priv->block_events_timeout_id == 0) {
2765 str = g_strdup_printf (_("%s is now known as %s"),
2766 empathy_contact_get_alias (old_contact),
2767 empathy_contact_get_alias (new_contact));
2768 empathy_chat_view_append_event (chat->view, str);
2775 chat_contacts_visible_timeout_cb (gpointer chat)
2777 EmpathyChatPriv *priv = GET_PRIV (chat);
2779 /* Relax the size request */
2780 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2782 /* Set the position of the slider. This must be done here because
2783 * GtkPaned need to know its size allocation and it will be settled only
2784 * after the gtk_window_resize () tough effect. */
2785 if (priv->contacts_width > 0) {
2786 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2787 priv->contacts_width);
2790 priv->contacts_visible_id = 0;
2796 chat_update_contacts_visibility (EmpathyChat *chat,
2799 EmpathyChatPriv *priv = GET_PRIV (chat);
2801 if (!priv->scrolled_window_contacts) {
2805 if (priv->remote_contact != NULL) {
2809 if (show && priv->contact_list_view == NULL) {
2810 EmpathyIndividualStore *store;
2812 GtkAllocation allocation;
2814 /* We are adding the contact list to the chat, we don't want the
2815 * chat view to become too small. If the chat view is already
2816 * smaller than 250 make sure that size won't change. If the
2817 * chat view is bigger the contact list will take some space on
2818 * it but we make sure the chat view don't become smaller than
2819 * 250. Relax the size request once the resize is done */
2820 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2821 min_width = MIN (allocation.width, 250);
2822 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2824 /* There is no way to know when the window resize will happen
2825 * since it is WM's decision. Let's hope it won't be longer. */
2826 if (priv->contacts_visible_id != 0)
2827 g_source_remove (priv->contacts_visible_id);
2828 priv->contacts_visible_id = g_timeout_add (500,
2829 chat_contacts_visible_timeout_cb, chat);
2831 store = EMPATHY_INDIVIDUAL_STORE (
2832 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2834 empathy_individual_store_set_show_groups (store, FALSE);
2836 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2837 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2838 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2839 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2840 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2841 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2842 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2844 empathy_individual_view_set_show_offline (
2845 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2846 empathy_individual_view_set_show_uninteresting (
2847 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2849 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2850 priv->contact_list_view);
2852 gtk_widget_show (priv->contact_list_view);
2853 gtk_widget_show (priv->scrolled_window_contacts);
2854 g_object_unref (store);
2856 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2857 gtk_widget_hide (priv->scrolled_window_contacts);
2858 if (priv->contact_list_view != NULL) {
2859 gtk_widget_destroy (priv->contact_list_view);
2860 priv->contact_list_view = NULL;
2866 empathy_chat_set_show_contacts (EmpathyChat *chat,
2869 EmpathyChatPriv *priv = GET_PRIV (chat);
2871 priv->show_contacts = show;
2873 chat_update_contacts_visibility (chat, show);
2875 g_object_notify (G_OBJECT (chat), "show-contacts");
2879 chat_remote_contact_changed_cb (EmpathyChat *chat)
2881 EmpathyChatPriv *priv = GET_PRIV (chat);
2883 if (priv->remote_contact != NULL) {
2884 g_object_unref (priv->remote_contact);
2885 priv->remote_contact = NULL;
2890 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2891 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2892 if (priv->remote_contact != NULL) {
2893 g_object_ref (priv->remote_contact);
2894 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2896 else if (priv->tp_chat != NULL) {
2897 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2900 chat_update_contacts_visibility (chat, priv->show_contacts);
2902 g_object_notify (G_OBJECT (chat), "remote-contact");
2903 g_object_notify (G_OBJECT (chat), "id");
2907 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2913 EmpathyChatPriv *priv;
2915 priv = GET_PRIV (chat);
2917 if (!priv->tp_chat) {
2921 chat_composing_remove_timeout (chat);
2922 g_object_unref (priv->tp_chat);
2923 priv->tp_chat = NULL;
2924 g_object_notify (G_OBJECT (chat), "tp-chat");
2926 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2927 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2929 chat_update_contacts_visibility (chat, FALSE);
2931 priv->unread_messages_when_offline = priv->unread_messages;
2935 update_misspelled_words (gpointer data)
2937 EmpathyChat *chat = EMPATHY_CHAT (data);
2938 EmpathyChatPriv *priv = GET_PRIV (chat);
2939 GtkTextBuffer *buffer;
2943 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2945 gtk_text_buffer_get_end_iter (buffer, &iter);
2946 length = gtk_text_iter_get_offset (&iter);
2947 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2948 NULL, length, chat);
2950 priv->update_misspelled_words_id = 0;
2956 conf_spell_checking_cb (GSettings *gsettings_chat,
2960 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2961 EmpathyChatPriv *priv = GET_PRIV (chat);
2962 gboolean spell_checker;
2963 GtkTextBuffer *buffer;
2965 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2968 spell_checker = g_settings_get_boolean (gsettings_chat,
2969 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2971 if (!empathy_spell_supported ()) {
2972 spell_checker = FALSE;
2975 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2977 if (spell_checker == priv->spell_checking_enabled) {
2978 if (spell_checker) {
2979 /* Possibly changed dictionaries,
2980 * update misspelled words. Need to do so in idle
2981 * so the spell checker is updated. */
2982 priv->update_misspelled_words_id =
2983 g_idle_add (update_misspelled_words, chat);
2989 if (spell_checker) {
2992 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2993 buffer, "notify::cursor-position",
2994 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2996 priv->insert_text_id = tp_g_signal_connect_object (
2997 buffer, "insert-text",
2998 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2999 chat, G_CONNECT_AFTER);
3000 priv->delete_range_id = tp_g_signal_connect_object (
3001 buffer, "delete-range",
3002 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3003 chat, G_CONNECT_AFTER);
3005 gtk_text_buffer_create_tag (buffer, "misspelled",
3006 "underline", PANGO_UNDERLINE_ERROR,
3009 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3010 gtk_text_buffer_get_insert (buffer));
3011 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3014 /* Mark misspelled words in the existing buffer.
3015 * Need to do so in idle so the spell checker is updated. */
3016 priv->update_misspelled_words_id =
3017 g_idle_add (update_misspelled_words, chat);
3019 GtkTextTagTable *table;
3022 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3023 priv->notify_cursor_position_id = 0;
3024 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3025 priv->insert_text_id = 0;
3026 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3027 priv->delete_range_id = 0;
3029 table = gtk_text_buffer_get_tag_table (buffer);
3030 tag = gtk_text_tag_table_lookup (table, "misspelled");
3031 gtk_text_tag_table_remove (table, tag);
3033 gtk_text_buffer_delete_mark_by_name (buffer,
3034 "previous-cursor-position");
3037 priv->spell_checking_enabled = spell_checker;
3041 save_paned_pos_timeout (gpointer data)
3043 EmpathyChat *self = data;
3046 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3048 g_settings_set_int (self->priv->gsettings_ui,
3049 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3056 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3060 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3062 if (chat->priv->save_paned_pos_id != 0)
3063 g_source_remove (chat->priv->save_paned_pos_id);
3065 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3066 save_paned_pos_timeout, chat);
3072 chat_create_ui (EmpathyChat *chat)
3074 EmpathyChatPriv *priv = GET_PRIV (chat);
3078 GtkTextBuffer *buffer;
3079 EmpathyThemeManager *theme_mgr;
3081 filename = empathy_file_lookup ("empathy-chat.ui",
3083 gui = empathy_builder_get_file (filename,
3084 "chat_widget", &priv->widget,
3085 "hpaned", &priv->hpaned,
3086 "vbox_left", &priv->vbox_left,
3087 "scrolled_window_chat", &priv->scrolled_window_chat,
3088 "scrolled_window_input", &priv->scrolled_window_input,
3089 "hbox_topic", &priv->hbox_topic,
3090 "expander_topic", &priv->expander_topic,
3091 "label_topic", &priv->label_topic,
3092 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3093 "info_bar_vbox", &priv->info_bar_vbox,
3096 empathy_builder_connect (gui, chat,
3097 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3098 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3103 /* Add message view. */
3104 theme_mgr = empathy_theme_manager_dup_singleton ();
3105 chat->view = empathy_theme_manager_create_view (theme_mgr);
3106 g_object_unref (theme_mgr);
3107 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3108 and other types, even though it's non-editable and doesn't accept any
3109 drags. This steals drag motion for anything inside the scrollbars,
3110 making drag destinations on chat windows far less useful.
3112 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3113 g_signal_connect (chat->view, "focus_in_event",
3114 G_CALLBACK (chat_text_view_focus_in_event_cb),
3116 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3117 GTK_WIDGET (chat->view));
3118 gtk_widget_show (GTK_WIDGET (chat->view));
3120 /* Add input GtkTextView */
3121 chat->input_text_view = empathy_input_text_view_new ();
3122 g_signal_connect (chat->input_text_view, "notify::has-focus",
3123 G_CALLBACK (chat_input_has_focus_notify_cb),
3125 g_signal_connect (chat->input_text_view, "key-press-event",
3126 G_CALLBACK (chat_input_key_press_event_cb),
3128 g_signal_connect (chat->input_text_view, "realize",
3129 G_CALLBACK (chat_input_realize_cb),
3131 g_signal_connect (chat->input_text_view, "button-press-event",
3132 G_CALLBACK (chat_input_button_press_event_cb),
3134 g_signal_connect (chat->input_text_view, "populate-popup",
3135 G_CALLBACK (chat_input_populate_popup_cb),
3137 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3138 tp_g_signal_connect_object (buffer, "changed",
3139 G_CALLBACK (chat_input_text_buffer_changed_cb),
3141 tp_g_signal_connect_object (priv->gsettings_chat,
3142 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3143 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3144 conf_spell_checking_cb (priv->gsettings_chat,
3145 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3146 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3147 chat->input_text_view);
3148 gtk_widget_show (chat->input_text_view);
3150 /* Add the (invisible) search bar */
3151 priv->search_bar = empathy_search_bar_new (chat->view);
3152 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3155 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3157 /* Initialy hide the topic, will be shown if not empty */
3158 gtk_widget_hide (priv->hbox_topic);
3160 g_signal_connect (priv->hpaned, "notify::position",
3161 G_CALLBACK (chat_hpaned_pos_changed_cb),
3164 /* Set widget focus order */
3165 list = g_list_append (NULL, priv->search_bar);
3166 list = g_list_append (list, priv->scrolled_window_input);
3167 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3170 list = g_list_append (NULL, priv->vbox_left);
3171 list = g_list_append (list, priv->scrolled_window_contacts);
3172 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3175 list = g_list_append (NULL, priv->hpaned);
3176 list = g_list_append (list, priv->hbox_topic);
3177 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3180 /* Add the main widget in the chat widget */
3181 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3182 g_object_unref (gui);
3186 chat_finalize (GObject *object)
3189 EmpathyChatPriv *priv;
3191 chat = EMPATHY_CHAT (object);
3192 priv = GET_PRIV (chat);
3194 DEBUG ("Finalized: %p", object);
3196 if (priv->update_misspelled_words_id != 0)
3197 g_source_remove (priv->update_misspelled_words_id);
3199 if (priv->save_paned_pos_id != 0)
3200 g_source_remove (priv->save_paned_pos_id);
3202 if (priv->contacts_visible_id != 0)
3203 g_source_remove (priv->contacts_visible_id);
3205 g_object_unref (priv->gsettings_chat);
3206 g_object_unref (priv->gsettings_ui);
3208 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3209 g_list_free (priv->input_history);
3211 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3212 g_list_free (priv->compositors);
3214 chat_composing_remove_timeout (chat);
3216 g_object_unref (priv->account_manager);
3217 g_object_unref (priv->log_manager);
3219 if (priv->tp_chat) {
3220 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3221 chat_invalidated_cb, chat);
3222 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3223 chat_message_received_cb, chat);
3224 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3225 chat_message_acknowledged_cb, chat);
3226 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3227 chat_send_error_cb, chat);
3228 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3229 chat_state_changed_cb, chat);
3230 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3231 chat_members_changed_cb, chat);
3232 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3233 chat_remote_contact_changed_cb, chat);
3234 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3235 chat_title_changed_cb, chat);
3236 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3237 chat_subject_changed_cb, chat);
3238 empathy_tp_chat_leave (priv->tp_chat, "");
3239 g_object_unref (priv->tp_chat);
3241 if (priv->account) {
3242 g_object_unref (priv->account);
3244 if (priv->remote_contact) {
3245 g_object_unref (priv->remote_contact);
3248 if (priv->block_events_timeout_id) {
3249 g_source_remove (priv->block_events_timeout_id);
3253 g_free (priv->name);
3254 g_free (priv->subject);
3255 g_completion_free (priv->completion);
3257 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3261 chat_constructed (GObject *object)
3263 EmpathyChat *chat = EMPATHY_CHAT (object);
3264 EmpathyChatPriv *priv = GET_PRIV (chat);
3266 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3267 /* First display logs from the logger and then display pending messages */
3268 chat_add_logs (chat);
3271 /* Just display pending messages for rooms */
3272 priv->can_show_pending = TRUE;
3273 show_pending_messages (chat);
3278 empathy_chat_class_init (EmpathyChatClass *klass)
3280 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3282 object_class->finalize = chat_finalize;
3283 object_class->get_property = chat_get_property;
3284 object_class->set_property = chat_set_property;
3285 object_class->constructed = chat_constructed;
3287 g_object_class_install_property (object_class,
3289 g_param_spec_object ("tp-chat",
3291 "The tp chat object",
3292 EMPATHY_TYPE_TP_CHAT,
3295 G_PARAM_STATIC_STRINGS));
3296 g_object_class_install_property (object_class,
3298 g_param_spec_object ("account",
3299 "Account of the chat",
3300 "The account of the chat",
3303 G_PARAM_STATIC_STRINGS));
3304 g_object_class_install_property (object_class,
3306 g_param_spec_string ("id",
3308 "The id of the chat",
3311 G_PARAM_STATIC_STRINGS));
3312 g_object_class_install_property (object_class,
3314 g_param_spec_string ("name",
3316 "The name of the chat",
3319 G_PARAM_STATIC_STRINGS));
3320 g_object_class_install_property (object_class,
3322 g_param_spec_string ("subject",
3324 "The subject or topic of the chat",
3327 G_PARAM_STATIC_STRINGS));
3328 g_object_class_install_property (object_class,
3329 PROP_REMOTE_CONTACT,
3330 g_param_spec_object ("remote-contact",
3331 "The remote contact",
3332 "The remote contact is any",
3333 EMPATHY_TYPE_CONTACT,
3335 G_PARAM_STATIC_STRINGS));
3336 g_object_class_install_property (object_class,
3338 g_param_spec_boolean ("show-contacts",
3339 "Contacts' visibility",
3340 "The visibility of the contacts' list",
3343 G_PARAM_STATIC_STRINGS));
3345 g_object_class_install_property (object_class,
3347 g_param_spec_boolean ("sms-channel",
3349 "TRUE if this channel is for sending SMSes",
3351 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3353 g_object_class_install_property (object_class,
3354 PROP_N_MESSAGES_SENDING,
3355 g_param_spec_uint ("n-messages-sending",
3356 "Num Messages Sending",
3357 "The number of messages being sent",
3359 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3361 g_object_class_install_property (object_class,
3362 PROP_NB_UNREAD_MESSAGES,
3363 g_param_spec_uint ("nb-unread-messages",
3364 "Num Unread Messages",
3365 "The number of unread messages",
3367 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3369 signals[COMPOSING] =
3370 g_signal_new ("composing",
3371 G_OBJECT_CLASS_TYPE (object_class),
3375 g_cclosure_marshal_generic,
3380 * EmpathyChat::new-message:
3381 * @self: the #EmpathyChat
3382 * @message: the new message
3383 * @pending: whether the message was in the pending queue when @self
3385 * @should_highlight: %TRUE if the message mentions the local user
3387 signals[NEW_MESSAGE] =
3388 g_signal_new ("new-message",
3389 G_OBJECT_CLASS_TYPE (object_class),
3393 g_cclosure_marshal_generic,
3395 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3397 signals[PART_COMMAND_ENTERED] =
3398 g_signal_new ("part-command-entered",
3399 G_OBJECT_CLASS_TYPE (object_class),
3403 g_cclosure_marshal_generic,
3407 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3411 chat_block_events_timeout_cb (gpointer data)
3413 EmpathyChatPriv *priv = GET_PRIV (data);
3415 priv->block_events_timeout_id = 0;
3421 account_manager_prepared_cb (GObject *source_object,
3422 GAsyncResult *result,
3425 GList *accounts, *l;
3426 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3427 EmpathyChat *chat = user_data;
3428 GError *error = NULL;
3430 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3431 DEBUG ("Failed to prepare the account manager: %s", error->message);
3432 g_error_free (error);
3436 accounts = tp_account_manager_get_valid_accounts (account_manager);
3438 for (l = accounts; l != NULL; l = l->next) {
3439 TpAccount *account = l->data;
3440 tp_g_signal_connect_object (account, "status-changed",
3441 G_CALLBACK (chat_new_connection_cb),
3445 g_list_free (accounts);
3449 empathy_chat_init (EmpathyChat *chat)
3451 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3452 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3455 priv->log_manager = tpl_log_manager_dup_singleton ();
3456 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3457 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3459 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3460 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3461 priv->input_history = NULL;
3462 priv->input_history_current = NULL;
3463 priv->account_manager = tp_account_manager_dup ();
3465 tp_proxy_prepare_async (priv->account_manager, NULL,
3466 account_manager_prepared_cb, chat);
3468 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3469 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3471 /* Block events for some time to avoid having "has come online" or
3472 * "joined" messages. */
3473 priv->block_events_timeout_id =
3474 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3476 /* Add nick name completion */
3477 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3478 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3480 chat_create_ui (chat);
3484 empathy_chat_new (EmpathyTpChat *tp_chat)
3486 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3490 empathy_chat_get_tp_chat (EmpathyChat *chat)
3492 EmpathyChatPriv *priv = GET_PRIV (chat);
3494 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3496 return priv->tp_chat;
3502 GtkWidget *info_bar;
3512 passwd_remember_button_cb (GtkButton *button,
3515 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3519 passwd_not_now_button_cb (GtkButton *button,
3522 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3526 remember_password_infobar_response_cb (GtkWidget *info_bar,
3530 EmpathyChatPriv *priv = GET_PRIV (data->self);
3532 if (response_id == GTK_RESPONSE_OK) {
3533 DEBUG ("Saving room password");
3534 empathy_keyring_set_room_password_async (priv->account,
3535 empathy_tp_chat_get_id (priv->tp_chat),
3540 gtk_widget_destroy (info_bar);
3541 g_free (data->password);
3542 g_slice_free (PasswordData, data);
3546 chat_prompt_to_save_password (EmpathyChat *self,
3549 GtkWidget *content_area;
3556 /* save the password in case it needs to be saved */
3557 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3559 /* Remove all previous widgets */
3560 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3561 gtk_container_forall (GTK_CONTAINER (content_area),
3562 (GtkCallback) gtk_widget_destroy, NULL);
3563 data->button = NULL;
3566 data->spinner = NULL;
3568 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3569 GTK_MESSAGE_QUESTION);
3571 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3572 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3575 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3576 GTK_ICON_SIZE_DIALOG);
3577 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3580 label = gtk_label_new (_("Would you like to store this password?"));
3581 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3583 /* Add 'Remember' button */
3584 alig = gtk_alignment_new (0, 0.5, 1, 0);
3586 button = gtk_button_new_with_label (_("Remember"));
3587 gtk_container_add (GTK_CONTAINER (alig), button);
3588 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3590 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3593 /* Add 'Not now' button */
3594 alig = gtk_alignment_new (0, 0.5, 1, 0);
3596 button = gtk_button_new_with_label (_("Not now"));
3597 gtk_container_add (GTK_CONTAINER (alig), button);
3598 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3600 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3604 g_signal_handler_disconnect (data->info_bar, data->response_id);
3605 g_signal_connect (data->info_bar, "response",
3606 G_CALLBACK (remember_password_infobar_response_cb), data);
3608 gtk_widget_show_all (data->info_bar);
3612 provide_password_cb (GObject *tp_chat,
3616 PasswordData *data = user_data;
3617 EmpathyChat *self = data->self;
3618 EmpathyChatPriv *priv = GET_PRIV (self);
3619 GError *error = NULL;
3621 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3623 DEBUG ("error: %s", error->message);
3624 /* FIXME: what should we do if that's another error? Close the channel?
3625 * Display the raw D-Bus error to the user isn't very useful */
3626 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3628 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3629 gtk_widget_set_sensitive (data->entry, TRUE);
3630 gtk_widget_grab_focus (data->entry);
3633 gtk_info_bar_set_message_type (
3634 GTK_INFO_BAR (data->info_bar),
3638 gtk_widget_set_sensitive (data->button, TRUE);
3639 gtk_button_set_label (GTK_BUTTON (data->button),
3643 gtk_label_set_text (GTK_LABEL (data->label),
3644 _("Wrong password; please try again:"));
3647 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3648 gtk_widget_hide (data->spinner);
3650 g_error_free (error);
3654 if (empathy_keyring_is_available ()) {
3655 /* ask whether they want to save the password */
3656 chat_prompt_to_save_password (self, data);
3658 /* Get rid of the password info bar finally */
3659 gtk_widget_destroy (data->info_bar);
3660 g_slice_free (PasswordData, data);
3664 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3665 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3666 gtk_widget_grab_focus (self->input_text_view);
3670 password_infobar_response_cb (GtkWidget *info_bar,
3674 EmpathyChatPriv *priv = GET_PRIV (data->self);
3675 const gchar *password;
3677 if (response_id != GTK_RESPONSE_OK) {
3678 gtk_widget_destroy (info_bar);
3679 g_slice_free (PasswordData, data);
3683 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3685 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3686 provide_password_cb, data);
3688 gtk_widget_set_sensitive (data->button, FALSE);
3689 gtk_widget_set_sensitive (data->entry, FALSE);
3691 gtk_spinner_start (GTK_SPINNER (data->spinner));
3692 gtk_widget_show (data->spinner);
3696 password_entry_activate_cb (GtkWidget *entry,
3699 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3703 passwd_join_button_cb (GtkButton *button,
3706 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3710 clear_icon_released_cb (GtkEntry *entry,
3711 GtkEntryIconPosition icon_pos,
3715 gtk_entry_set_text (entry, "");
3719 password_entry_changed_cb (GtkEditable *entry,
3724 str = gtk_entry_get_text (GTK_ENTRY (entry));
3726 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3727 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3731 infobar_chat_invalidated_cb (TpProxy *proxy,
3735 gpointer password_infobar)
3737 /* Destroy the password infobar whenever a channel is invalidated
3738 * so we don't have multiple infobars when the MUC is rejoined */
3739 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3743 display_password_info_bar (EmpathyChat *self)
3745 EmpathyChatPriv *priv = GET_PRIV (self);
3746 GtkWidget *info_bar;
3747 GtkWidget *content_area;
3757 data = g_slice_new0 (PasswordData);
3759 info_bar = gtk_info_bar_new ();
3760 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3761 GTK_MESSAGE_QUESTION);
3763 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3765 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3766 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3769 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3770 GTK_ICON_SIZE_DIALOG);
3771 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3774 label = gtk_label_new (_("This room is protected by a password:"));
3775 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3777 /* Add password entry */
3778 entry = gtk_entry_new ();
3779 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3780 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3782 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3783 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3784 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3785 GTK_ENTRY_ICON_SECONDARY, FALSE);
3787 g_signal_connect (entry, "icon-release",
3788 G_CALLBACK (clear_icon_released_cb), data);
3789 g_signal_connect (entry, "changed",
3790 G_CALLBACK (password_entry_changed_cb), data);
3792 g_signal_connect (entry, "activate",
3793 G_CALLBACK (password_entry_activate_cb), data);
3795 /* Focus the password entry once it's realized */
3796 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3798 /* Add 'Join' button */
3799 alig = gtk_alignment_new (0, 0.5, 1, 0);
3801 button = gtk_button_new_with_label (_("Join"));
3802 gtk_container_add (GTK_CONTAINER (alig), button);
3803 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3805 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3809 spinner = gtk_spinner_new ();
3810 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3812 /* Save some data for messing around with later */
3814 data->info_bar = info_bar;
3815 data->button = button;
3816 data->label = label;
3817 data->entry = entry;
3818 data->spinner = spinner;
3820 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3822 gtk_widget_show_all (hbox);
3824 tp_g_signal_connect_object (priv->tp_chat,
3825 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3828 data->response_id = g_signal_connect (info_bar, "response",
3829 G_CALLBACK (password_infobar_response_cb), data);
3831 gtk_widget_show_all (info_bar);
3832 /* ... but hide the spinner */
3833 gtk_widget_hide (spinner);
3835 /* prevent the user from typing anything */
3836 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3840 provide_saved_password_cb (GObject *tp_chat,
3844 EmpathyChat *self = user_data;
3845 EmpathyChatPriv *priv = GET_PRIV (self);
3846 GError *error = NULL;
3848 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3850 DEBUG ("error: %s", error->message);
3851 /* FIXME: what should we do if that's another error? Close the channel?
3852 * Display the raw D-Bus error to the user isn't very useful */
3853 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3854 display_password_info_bar (self);
3855 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3857 g_error_free (error);
3862 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3863 gtk_widget_grab_focus (self->input_text_view);
3867 chat_room_got_password_cb (GObject *source,
3868 GAsyncResult *result,
3871 EmpathyChat *self = user_data;
3872 EmpathyChatPriv *priv = GET_PRIV (self);
3873 const gchar *password;
3874 GError *error = NULL;
3876 password = empathy_keyring_get_room_password_finish (priv->account,
3879 if (error != NULL) {
3880 DEBUG ("Couldn't get room password: %s\n", error->message);
3881 g_clear_error (&error);
3883 display_password_info_bar (self);
3884 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3888 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3889 provide_saved_password_cb, self);
3893 chat_password_needed_changed_cb (EmpathyChat *self)
3895 EmpathyChatPriv *priv = GET_PRIV (self);
3897 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3898 empathy_keyring_get_room_password_async (priv->account,
3899 empathy_tp_chat_get_id (priv->tp_chat),
3900 chat_room_got_password_cb, self);
3905 chat_sms_channel_changed_cb (EmpathyChat *self)
3907 EmpathyChatPriv *priv = GET_PRIV (self);
3909 priv->sms_channel = tp_text_channel_is_sms_channel (
3910 (TpTextChannel *) priv->tp_chat);
3911 g_object_notify (G_OBJECT (self), "sms-channel");
3915 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3917 g_object_notify (G_OBJECT (self), "n-messages-sending");
3921 empathy_chat_set_tp_chat (EmpathyChat *chat,
3922 EmpathyTpChat *tp_chat)
3924 EmpathyChatPriv *priv = GET_PRIV (chat);
3926 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3927 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3929 if (priv->tp_chat) {
3933 if (priv->account) {
3934 g_object_unref (priv->account);
3937 priv->tp_chat = g_object_ref (tp_chat);
3938 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3940 g_signal_connect (tp_chat, "invalidated",
3941 G_CALLBACK (chat_invalidated_cb),
3943 g_signal_connect (tp_chat, "message-received-empathy",
3944 G_CALLBACK (chat_message_received_cb),
3946 g_signal_connect (tp_chat, "message_acknowledged",
3947 G_CALLBACK (chat_message_acknowledged_cb),
3949 g_signal_connect (tp_chat, "send-error",
3950 G_CALLBACK (chat_send_error_cb),
3952 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3953 G_CALLBACK (chat_state_changed_cb),
3955 g_signal_connect (tp_chat, "members-changed",
3956 G_CALLBACK (chat_members_changed_cb),
3958 g_signal_connect (tp_chat, "member-renamed",
3959 G_CALLBACK (chat_member_renamed_cb),
3961 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3962 G_CALLBACK (chat_remote_contact_changed_cb),
3964 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3965 G_CALLBACK (chat_password_needed_changed_cb),
3967 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
3968 G_CALLBACK (chat_sms_channel_changed_cb),
3970 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3971 G_CALLBACK (chat_n_messages_sending_changed_cb),
3973 g_signal_connect_swapped (tp_chat, "notify::title",
3974 G_CALLBACK (chat_title_changed_cb),
3976 g_signal_connect_swapped (tp_chat, "notify::subject",
3977 G_CALLBACK (chat_subject_changed_cb),
3980 /* Get initial value of properties */
3981 chat_sms_channel_changed_cb (chat);
3982 chat_remote_contact_changed_cb (chat);
3983 chat_title_changed_cb (chat);
3984 chat_subject_changed_cb (chat);
3986 if (chat->input_text_view) {
3987 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3988 if (priv->block_events_timeout_id == 0) {
3989 empathy_chat_view_append_event (chat->view, _("Connected"));
3993 g_object_notify (G_OBJECT (chat), "tp-chat");
3994 g_object_notify (G_OBJECT (chat), "id");
3995 g_object_notify (G_OBJECT (chat), "account");
3997 /* This is a noop when tp-chat is set at object construction time and causes
3998 * the pending messages to be show when it's set on the object after it has
4000 show_pending_messages (chat);
4002 /* check if a password is needed */
4003 chat_password_needed_changed_cb (chat);
4007 empathy_chat_get_account (EmpathyChat *chat)
4009 EmpathyChatPriv *priv = GET_PRIV (chat);
4011 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4013 return priv->account;
4017 empathy_chat_get_id (EmpathyChat *chat)
4019 EmpathyChatPriv *priv = GET_PRIV (chat);
4021 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4027 empathy_chat_dup_name (EmpathyChat *chat)
4029 EmpathyChatPriv *priv = GET_PRIV (chat);
4032 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4036 if (!ret && priv->remote_contact) {
4037 ret = empathy_contact_get_alias (priv->remote_contact);
4044 ret = _("Conversation");
4046 if (priv->sms_channel)
4047 /* Translators: this string is a something like
4048 * "Escher Cat (SMS)" */
4049 return g_strdup_printf (_("%s (SMS)"), ret);
4051 return g_strdup (ret);
4055 empathy_chat_get_subject (EmpathyChat *chat)
4057 EmpathyChatPriv *priv = GET_PRIV (chat);
4059 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4061 return priv->subject;
4065 empathy_chat_get_remote_contact (EmpathyChat *chat)
4067 EmpathyChatPriv *priv = GET_PRIV (chat);
4069 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4071 return priv->remote_contact;
4075 empathy_chat_get_contact_menu (EmpathyChat *chat)
4077 EmpathyChatPriv *priv = GET_PRIV (chat);
4078 GtkWidget *menu = NULL;
4079 FolksIndividual *individual;
4082 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4084 if (priv->remote_contact == NULL)
4087 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4088 if (contact == NULL)
4091 individual = empathy_create_individual_from_tp_contact (contact);
4092 if (individual == NULL)
4095 menu = empathy_individual_menu_new (individual,
4096 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4097 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4098 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4099 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4101 g_object_unref (individual);
4107 empathy_chat_clear (EmpathyChat *chat)
4109 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4111 empathy_chat_view_clear (chat->view);
4115 empathy_chat_scroll_down (EmpathyChat *chat)
4117 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4119 empathy_chat_view_scroll_down (chat->view);
4123 empathy_chat_cut (EmpathyChat *chat)
4125 GtkTextBuffer *buffer;
4127 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4129 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4130 if (gtk_text_buffer_get_has_selection (buffer)) {
4131 GtkClipboard *clipboard;
4133 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4135 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4140 empathy_chat_copy (EmpathyChat *chat)
4142 GtkTextBuffer *buffer;
4144 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4146 if (empathy_chat_view_get_has_selection (chat->view)) {
4147 empathy_chat_view_copy_clipboard (chat->view);
4151 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4152 if (gtk_text_buffer_get_has_selection (buffer)) {
4153 GtkClipboard *clipboard;
4155 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4157 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4162 EmpathyChatPriv *priv = GET_PRIV (chat);
4164 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4171 GtkClipboard *clipboard;
4173 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4174 start = g_utf8_offset_to_pointer (topic, start_offset);
4175 end = g_utf8_offset_to_pointer (topic, end_offset);
4176 selection = g_strndup (start, end - start);
4178 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4179 gtk_clipboard_set_text (clipboard, selection, -1);
4187 empathy_chat_paste (EmpathyChat *chat)
4189 GtkTextBuffer *buffer;
4190 GtkClipboard *clipboard;
4191 EmpathyChatPriv *priv;
4193 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4195 priv = GET_PRIV (chat);
4197 if (gtk_widget_get_visible (priv->search_bar)) {
4198 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4202 if (priv->tp_chat == NULL ||
4203 !gtk_widget_is_sensitive (chat->input_text_view))
4206 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4207 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4209 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4213 empathy_chat_find (EmpathyChat *chat)
4215 EmpathyChatPriv *priv;
4217 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4219 priv = GET_PRIV (chat);
4221 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4225 empathy_chat_correct_word (EmpathyChat *chat,
4228 const gchar *new_word)
4230 GtkTextBuffer *buffer;
4232 g_return_if_fail (chat != NULL);
4233 g_return_if_fail (new_word != NULL);
4235 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4237 gtk_text_buffer_delete (buffer, start, end);
4238 gtk_text_buffer_insert (buffer, start,
4244 empathy_chat_is_room (EmpathyChat *chat)
4246 EmpathyChatPriv *priv = GET_PRIV (chat);
4248 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4250 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4254 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4256 EmpathyChatPriv *priv = GET_PRIV (self);
4258 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4260 return priv->unread_messages;
4263 /* called when the messages have been read by user */
4265 empathy_chat_messages_read (EmpathyChat *self)
4267 EmpathyChatPriv *priv = GET_PRIV (self);
4269 g_return_if_fail (EMPATHY_IS_CHAT (self));
4271 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4272 * definition. If we are still retrieving the backlogs, do not ACK */
4273 if (priv->retrieving_backlogs)
4276 if (priv->tp_chat != NULL) {
4277 tp_text_channel_ack_all_pending_messages_async (
4278 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4281 if (priv->unread_messages_when_offline > 0) {
4282 /* We can't ack those as the connection has gone away so just consider
4284 priv->unread_messages -= priv->unread_messages_when_offline;
4285 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4286 priv->unread_messages_when_offline = 0;
4290 /* Return TRUE if on of the contacts in this chat is composing */
4292 empathy_chat_is_composing (EmpathyChat *chat)
4294 return chat->priv->compositors != NULL;
4298 empathy_chat_is_sms_channel (EmpathyChat *self)
4300 EmpathyChatPriv *priv = GET_PRIV (self);
4302 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4304 return priv->sms_channel;
4308 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4310 EmpathyChatPriv *priv;
4312 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4314 priv = GET_PRIV (self);
4316 if (priv->tp_chat == NULL) {
4321 g_object_get (priv->tp_chat,
4322 "n-messages-sending", &n_messages,
4330 empathy_chat_dup_text (EmpathyChat *self)
4332 GtkTextBuffer *buffer;
4333 GtkTextIter start, end;
4335 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4337 gtk_text_buffer_get_bounds (buffer, &start, &end);
4338 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4342 empathy_chat_set_text (EmpathyChat *self,
4345 GtkTextBuffer *buffer;
4347 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4349 gtk_text_buffer_set_text (buffer, text, -1);