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 chat_message_received (EmpathyChat *chat,
1388 EmpathyMessage *message,
1391 EmpathyChatPriv *priv = GET_PRIV (chat);
1392 EmpathyContact *sender;
1394 sender = empathy_message_get_sender (message);
1396 if (empathy_message_is_edit (message)) {
1397 DEBUG ("Editing message '%s' to '%s'",
1398 empathy_message_get_supersedes (message),
1399 empathy_message_get_body (message));
1401 empathy_chat_view_edit_message (chat->view, message);
1403 DEBUG ("Appending new message '%s' from %s (%d)",
1404 empathy_message_get_token (message),
1405 empathy_contact_get_alias (sender),
1406 empathy_contact_get_handle (sender));
1408 empathy_chat_view_append_message (chat->view, message);
1410 if (empathy_message_is_incoming (message)) {
1411 priv->unread_messages++;
1412 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1415 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1418 /* We received a message so the contact is no longer
1420 chat_state_changed_cb (priv->tp_chat, sender,
1421 TP_CHANNEL_CHAT_STATE_ACTIVE,
1426 chat_message_received_cb (EmpathyTpChat *tp_chat,
1427 EmpathyMessage *message,
1430 chat_message_received (chat, message, FALSE);
1434 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1435 EmpathyMessage *message,
1438 EmpathyChatPriv *priv = GET_PRIV (chat);
1440 empathy_chat_view_message_acknowledged (chat->view,
1443 if (!empathy_message_is_edit (message)) {
1444 priv->unread_messages--;
1445 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1450 append_balance_error (EmpathyChat *chat,
1451 const gchar *message_body)
1453 EmpathyChatPriv *priv = GET_PRIV (chat);
1454 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1455 const gchar *uri = tp_connection_get_balance_uri (conn);
1456 const gchar *error = _("insufficient balance to send message");
1457 gchar *str, *str_markup = NULL;
1459 if (message_body != NULL) {
1460 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1462 str = g_strdup_printf (_("Error sending message: %s"), error);
1465 if (!tp_str_empty (uri)) {
1466 /* translators: error used when user doesn't have enough credit on his
1467 * account to send the message. */
1468 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1469 " <a href='%s'>Top up</a>."), uri);
1471 if (message_body != NULL) {
1472 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1474 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1475 escaped_body, markup_error);
1477 g_free (escaped_body);
1479 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1482 g_free (markup_error);
1485 if (str_markup != NULL)
1486 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1488 empathy_chat_view_append_event (chat->view, str);
1491 g_free (str_markup);
1495 chat_send_error_cb (EmpathyTpChat *tp_chat,
1496 const gchar *message_body,
1497 TpChannelTextSendError error_code,
1498 const gchar *dbus_error,
1501 const gchar *error = NULL;
1504 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1505 append_balance_error (chat, message_body);
1507 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1508 error = _("not capable");
1511 if (error == NULL) {
1512 /* if we didn't find a dbus-error, try the old error */
1513 switch (error_code) {
1514 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1515 error = _("offline");
1517 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1518 error = _("invalid contact");
1520 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1521 error = _("permission denied");
1523 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1524 error = _("too long message");
1526 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1527 error = _("not implemented");
1529 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1531 error = _("unknown");
1536 if (message_body != NULL) {
1537 str = g_strdup_printf (_("Error sending message '%s': %s"),
1538 message_body, error);
1541 str = g_strdup_printf (_("Error sending message: %s"), error);
1544 empathy_chat_view_append_event (chat->view, str);
1549 chat_topic_label_size_allocate_cb (GtkLabel *label,
1550 GtkAllocation *allocation,
1553 EmpathyChatPriv *priv = GET_PRIV (chat);
1555 if (!gtk_label_get_line_wrap (label)) {
1556 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1557 gtk_widget_show (priv->expander_topic);
1559 gtk_widget_hide (priv->expander_topic);
1566 chat_topic_expander_activate_cb (GtkExpander *expander,
1567 GParamSpec *param_spec,
1570 EmpathyChatPriv *priv = GET_PRIV (chat);
1572 if (gtk_expander_get_expanded (expander)) {
1573 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1574 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1576 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1577 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1582 chat_subject_changed_cb (EmpathyChat *chat)
1584 EmpathyChatPriv *priv = GET_PRIV (chat);
1586 g_free (priv->subject);
1587 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1588 g_object_notify (G_OBJECT (chat), "subject");
1590 if (EMP_STR_EMPTY (priv->subject)) {
1591 gtk_widget_hide (priv->hbox_topic);
1593 gchar *markup_topic;
1596 markup_topic = empathy_add_link_markup (priv->subject);
1597 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1598 _("Topic:"), markup_topic);
1600 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1601 g_free (markup_text);
1602 g_free (markup_topic);
1604 gtk_widget_show (priv->hbox_topic);
1606 if (priv->block_events_timeout_id == 0) {
1609 if (!EMP_STR_EMPTY (priv->subject)) {
1610 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1611 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1612 /* No need to display this 'event' is no topic can be defined anyway */
1613 str = g_strdup (_("No topic defined"));
1617 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1624 chat_title_changed_cb (EmpathyChat *chat)
1626 EmpathyChatPriv *priv = GET_PRIV (chat);
1628 g_free (priv->name);
1629 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1630 g_object_notify (G_OBJECT (chat), "name");
1634 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1638 GtkTextIter word_start = *iter;
1639 GtkTextIter word_end = *iter;
1642 if (gtk_text_iter_inside_word (&word_end) &&
1643 !gtk_text_iter_ends_word (&word_end)) {
1644 gtk_text_iter_forward_word_end (&word_end);
1649 if (gtk_text_iter_get_char (&tmp) == '\'') {
1650 gtk_text_iter_forward_char (&tmp);
1652 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1653 gtk_text_iter_forward_word_end (&word_end);
1658 if (gtk_text_iter_inside_word (&word_start) ||
1659 gtk_text_iter_ends_word (&word_start)) {
1660 if (!gtk_text_iter_starts_word (&word_start) ||
1661 gtk_text_iter_equal (&word_start, &word_end)) {
1662 gtk_text_iter_backward_word_start (&word_start);
1666 gtk_text_iter_backward_char (&tmp);
1668 if (gtk_text_iter_get_char (&tmp) == '\'') {
1669 gtk_text_iter_backward_char (&tmp);
1671 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1672 gtk_text_iter_backward_word_start (&word_start);
1677 *start = word_start;
1683 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1684 GtkTextIter *location,
1689 GtkTextIter iter, pos;
1691 /* Remove all misspelled tags in the inserted text.
1692 * This happens when text is inserted within a misspelled word. */
1693 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1694 gtk_text_iter_get_offset (location) - len);
1695 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1698 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1701 GtkTextIter start, end;
1704 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1707 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1709 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1710 gtk_text_iter_equal (&pos, &end) ||
1711 empathy_spell_check (str)) {
1712 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1714 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1719 } while (gtk_text_iter_forward_word_end (&iter) &&
1720 gtk_text_iter_compare (&iter, location) <= 0);
1724 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1729 GtkTextIter word_start, word_end;
1731 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1732 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1733 &word_start, &word_end);
1738 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1741 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1742 chat_composing_stop (chat);
1744 chat_composing_start (chat);
1749 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1754 GtkTextIter prev_pos;
1755 GtkTextIter word_start;
1756 GtkTextIter word_end;
1760 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1762 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1763 gtk_text_buffer_get_insert (buffer));
1764 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1766 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1769 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1770 !gtk_text_iter_equal (&pos, &word_end)) {
1771 str = gtk_text_buffer_get_text (buffer,
1772 &word_start, &word_end, FALSE);
1774 if (!empathy_spell_check (str)) {
1775 gtk_text_buffer_apply_tag_by_name (buffer,
1776 "misspelled", &word_start, &word_end);
1778 gtk_text_buffer_remove_tag_by_name (buffer,
1779 "misspelled", &word_start, &word_end);
1786 gtk_text_buffer_move_mark (buffer, mark, &pos);
1790 empathy_isspace_cb (gunichar c,
1793 return g_unichar_isspace (c);
1797 chat_input_key_press_event_cb (GtkWidget *widget,
1801 EmpathyChatPriv *priv;
1804 GtkWidget *text_view_sw;
1806 priv = GET_PRIV (chat);
1808 priv->most_recent_event_type = event->type;
1810 /* Catch ctrl+up/down so we can traverse messages we sent */
1811 if ((event->state & GDK_CONTROL_MASK) &&
1812 (event->keyval == GDK_KEY_Up ||
1813 event->keyval == GDK_KEY_Down)) {
1814 GtkTextBuffer *buffer;
1817 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1818 chat_input_history_update (chat, buffer);
1820 if (event->keyval == GDK_KEY_Up) {
1821 str = chat_input_history_get_next (chat);
1823 str = chat_input_history_get_prev (chat);
1826 g_signal_handlers_block_by_func (buffer,
1827 chat_input_text_buffer_changed_cb,
1829 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1830 g_signal_handlers_unblock_by_func (buffer,
1831 chat_input_text_buffer_changed_cb,
1837 /* Catch enter but not ctrl/shift-enter */
1838 if (IS_ENTER (event->keyval) &&
1839 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1842 /* This is to make sure that kinput2 gets the enter. And if
1843 * it's handled there we shouldn't send on it. This is because
1844 * kinput2 uses Enter to commit letters. See:
1845 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1848 view = GTK_TEXT_VIEW (chat->input_text_view);
1849 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1850 gtk_text_view_reset_im_context (view);
1854 chat_input_text_view_send (chat);
1858 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1860 if (IS_ENTER (event->keyval) &&
1861 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1862 /* Newline for shift/control-enter. */
1865 if (!(event->state & GDK_CONTROL_MASK) &&
1866 event->keyval == GDK_KEY_Page_Up) {
1867 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1868 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1871 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1872 event->keyval == GDK_KEY_Page_Down) {
1873 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1874 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1875 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1876 gtk_adjustment_set_value (adj, val);
1879 if (event->keyval == GDK_KEY_Escape) {
1880 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1882 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1883 event->keyval == GDK_KEY_Tab) {
1884 GtkTextBuffer *buffer;
1885 GtkTextIter start, current;
1886 gchar *nick, *completed;
1887 GList *list, *completed_list;
1888 gboolean is_start_of_buffer;
1890 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1891 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1893 /* Get the start of the nick to complete. */
1894 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1895 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1896 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1898 is_start_of_buffer = gtk_text_iter_is_start (&start);
1900 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1901 g_completion_add_items (priv->completion, list);
1903 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1904 completed_list = g_completion_complete (priv->completion,
1913 GString *message = NULL;
1916 gtk_text_buffer_delete (buffer, &start, ¤t);
1918 len = g_list_length (completed_list);
1921 /* If we only have one hit, use that text
1922 * instead of the text in completed since the
1923 * completed text will use the typed string
1924 * which might be cased all wrong.
1927 text = empathy_contact_get_alias (completed_list->data);
1931 /* Print all hits to the scrollback view, so the
1932 * user knows what possibilities he has.
1935 message = g_string_new ("");
1936 for (l = completed_list; l != NULL; l = l->next) {
1937 g_string_append (message, empathy_contact_get_alias (l->data));
1938 g_string_append (message, " - ");
1940 empathy_chat_view_append_event (chat->view, message->str);
1941 g_string_free (message, TRUE);
1944 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1946 if (len == 1 && is_start_of_buffer) {
1947 gchar *complete_char;
1949 complete_char = g_settings_get_string (
1950 priv->gsettings_chat,
1951 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1953 if (complete_char != NULL) {
1954 gtk_text_buffer_insert_at_cursor (buffer,
1956 strlen (complete_char));
1957 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1958 g_free (complete_char);
1965 g_completion_clear_items (priv->completion);
1967 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1977 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1981 gtk_widget_grab_focus (chat->input_text_view);
1987 chat_input_realize_cb (GtkWidget *widget,
1990 DEBUG ("Setting focus to the input text view");
1991 if (gtk_widget_is_sensitive (widget)) {
1992 gtk_widget_grab_focus (widget);
1997 chat_input_has_focus_notify_cb (GtkWidget *widget,
2001 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2005 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2006 EmpathySmiley *smiley,
2009 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2010 GtkTextBuffer *buffer;
2013 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2015 gtk_text_buffer_get_end_iter (buffer, &iter);
2016 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2018 gtk_text_buffer_get_end_iter (buffer, &iter);
2019 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2030 static EmpathyChatSpell *
2031 chat_spell_new (EmpathyChat *chat,
2036 EmpathyChatSpell *chat_spell;
2038 chat_spell = g_slice_new0 (EmpathyChatSpell);
2040 chat_spell->chat = g_object_ref (chat);
2041 chat_spell->word = g_strdup (word);
2042 chat_spell->start = start;
2043 chat_spell->end = end;
2049 chat_spell_free (EmpathyChatSpell *chat_spell)
2051 g_object_unref (chat_spell->chat);
2052 g_free (chat_spell->word);
2053 g_slice_free (EmpathyChatSpell, chat_spell);
2057 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2058 EmpathyChatSpell *chat_spell)
2060 empathy_chat_correct_word (chat_spell->chat,
2061 &(chat_spell->start),
2063 gtk_menu_item_get_label (menu_item));
2068 chat_spelling_build_suggestions_menu (const gchar *code,
2069 EmpathyChatSpell *chat_spell)
2071 GList *suggestions, *l;
2072 GtkWidget *menu, *menu_item;
2074 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2075 if (suggestions == NULL)
2078 menu = gtk_menu_new ();
2079 for (l = suggestions; l; l = l->next) {
2080 menu_item = gtk_menu_item_new_with_label (l->data);
2081 g_signal_connect (G_OBJECT (menu_item), "activate",
2082 G_CALLBACK (chat_spelling_menu_activate_cb),
2084 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2086 empathy_spell_free_suggestions (suggestions);
2088 gtk_widget_show_all (menu);
2094 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2096 GtkWidget *menu, *submenu, *item;
2099 codes = empathy_spell_get_enabled_language_codes ();
2100 g_assert (codes != NULL);
2102 if (g_list_length (codes) > 1) {
2103 menu = gtk_menu_new ();
2105 for (l = codes; l; l = l->next) {
2106 const gchar *code, *name;
2109 name = empathy_spell_get_language_name (code);
2113 item = gtk_image_menu_item_new_with_label (name);
2115 submenu = chat_spelling_build_suggestions_menu (
2117 if (submenu == NULL)
2118 gtk_widget_set_sensitive (item, FALSE);
2120 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2122 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2125 menu = chat_spelling_build_suggestions_menu (codes->data,
2128 menu = gtk_menu_new ();
2129 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2130 gtk_widget_set_sensitive (item, FALSE);
2131 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2134 g_list_free (codes);
2136 gtk_widget_show_all (menu);
2147 static EmpathyChatWord *
2148 chat_word_new (EmpathyChat *chat,
2152 EmpathyChatWord *chat_word;
2154 chat_word = g_slice_new0 (EmpathyChatWord);
2156 chat_word->chat = g_object_ref (chat);
2157 chat_word->word = g_strdup (word);
2158 chat_word->code = g_strdup (code);
2164 chat_word_free (EmpathyChatWord *chat_word)
2166 g_object_unref (chat_word->chat);
2167 g_free (chat_word->word);
2168 g_free (chat_word->code);
2169 g_slice_free (EmpathyChatWord, chat_word);
2173 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2174 EmpathyChatWord *chat_word)
2176 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2178 empathy_spell_add_to_dictionary (chat_word->code,
2180 priv->update_misspelled_words_id =
2181 g_idle_add (update_misspelled_words, chat_word->chat);
2185 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2187 GtkWidget *menu, *item, *lang_item, *image;
2190 const gchar *code, *name;
2191 EmpathyChatWord *chat_word;
2193 codes = empathy_spell_get_enabled_language_codes ();
2194 g_assert (codes != NULL);
2195 if (g_list_length (codes) > 1) {
2196 /* translators: %s is the selected word */
2197 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2199 item = gtk_image_menu_item_new_with_mnemonic (label);
2201 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2202 GTK_ICON_SIZE_MENU);
2203 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2206 menu = gtk_menu_new ();
2208 for (l = codes; l; l = l->next) {
2210 name = empathy_spell_get_language_name (code);
2214 lang_item = gtk_image_menu_item_new_with_label (name);
2216 chat_word= chat_word_new (chat_spell->chat,
2217 chat_spell->word, code);
2218 g_object_set_data_full (G_OBJECT (lang_item),
2219 "chat-word", chat_word,
2220 (GDestroyNotify) chat_word_free);
2222 g_signal_connect (G_OBJECT (lang_item), "activate",
2223 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2225 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2227 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2230 name = empathy_spell_get_language_name (code);
2231 g_assert (name != NULL);
2232 /* translators: first %s is the selected word,
2233 * second %s is the language name of the target dictionary */
2234 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2235 chat_spell->word, name);
2236 item = gtk_image_menu_item_new_with_mnemonic (label);
2238 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2239 GTK_ICON_SIZE_MENU);
2240 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2242 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2244 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2245 (GDestroyNotify) chat_word_free);
2247 g_signal_connect (G_OBJECT (item), "activate",
2248 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2251 g_list_free (codes);
2253 gtk_widget_show_all (item);
2259 chat_text_send_cb (GtkMenuItem *menuitem,
2262 chat_input_text_view_send (chat);
2266 chat_input_button_press_event_cb (GtkTextView *view,
2267 GdkEventButton *event,
2270 EmpathyChatPriv *priv = GET_PRIV (chat);
2272 priv->most_recent_event_type = event->type;
2278 chat_input_populate_popup_cb (GtkTextView *view,
2282 EmpathyChatPriv *priv = GET_PRIV (chat);
2283 GtkTextBuffer *buffer;
2284 GtkTextTagTable *table;
2287 GtkTextIter iter, start, end;
2290 EmpathyChatSpell *chat_spell;
2291 GtkWidget *spell_menu;
2292 GtkWidget *spell_item;
2293 EmpathySmileyManager *smiley_manager;
2294 GtkWidget *smiley_menu;
2297 buffer = gtk_text_view_get_buffer (view);
2299 /* Add the emoticon menu. */
2300 item = gtk_separator_menu_item_new ();
2301 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2302 gtk_widget_show (item);
2304 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2305 image = gtk_image_new_from_icon_name ("face-smile",
2306 GTK_ICON_SIZE_MENU);
2307 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2308 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2309 gtk_widget_show (item);
2311 smiley_manager = empathy_smiley_manager_dup_singleton ();
2312 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2313 chat_insert_smiley_activate_cb,
2315 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2316 g_object_unref (smiley_manager);
2318 /* Add the Send menu item. */
2319 gtk_text_buffer_get_bounds (buffer, &start, &end);
2320 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2321 if (!EMP_STR_EMPTY (str)) {
2322 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2323 g_signal_connect (G_OBJECT (item), "activate",
2324 G_CALLBACK (chat_text_send_cb), chat);
2325 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2326 gtk_widget_show (item);
2330 /* Add the spell check menu item. */
2331 table = gtk_text_buffer_get_tag_table (buffer);
2332 tag = gtk_text_tag_table_lookup (table, "misspelled");
2334 switch (priv->most_recent_event_type) {
2335 case GDK_BUTTON_PRESS:
2336 /* get the location from the pointer */
2337 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2338 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2339 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2341 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2342 GTK_TEXT_WINDOW_WIDGET,
2345 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2350 g_warn_if_reached ();
2351 /* assume the KEY_PRESS case */
2354 /* get the location from the cursor */
2355 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2356 gtk_text_buffer_get_insert (buffer));
2362 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2363 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2365 str = gtk_text_buffer_get_text (buffer,
2366 &start, &end, FALSE);
2368 if (!EMP_STR_EMPTY (str)) {
2369 chat_spell = chat_spell_new (chat, str, start, end);
2370 g_object_set_data_full (G_OBJECT (menu),
2371 "chat-spell", chat_spell,
2372 (GDestroyNotify) chat_spell_free);
2374 item = gtk_separator_menu_item_new ();
2375 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2376 gtk_widget_show (item);
2378 /* Spelling suggestions */
2379 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2380 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2381 GTK_ICON_SIZE_MENU);
2382 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2384 spell_menu = chat_spelling_build_menu (chat_spell);
2385 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2388 spell_item = gtk_separator_menu_item_new ();
2389 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2390 gtk_widget_show (spell_item);
2392 /* Add to dictionary */
2393 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2395 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2396 gtk_widget_show (spell_item);
2398 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2399 gtk_widget_show (item);
2405 chat_log_filter (TplEvent *event,
2408 EmpathyChat *chat = user_data;
2409 EmpathyMessage *message;
2410 EmpathyChatPriv *priv = GET_PRIV (chat);
2411 const GList *pending;
2413 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2414 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2416 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2417 message = empathy_message_from_tpl_log_event (event);
2419 for (; pending; pending = g_list_next (pending)) {
2420 if (empathy_message_equal (message, pending->data)) {
2421 g_object_unref (message);
2426 g_object_unref (message);
2432 show_pending_messages (EmpathyChat *chat) {
2433 EmpathyChatPriv *priv = GET_PRIV (chat);
2434 const GList *messages, *l;
2436 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2438 if (chat->view == NULL || priv->tp_chat == NULL)
2441 if (!priv->can_show_pending)
2444 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2446 for (l = messages; l != NULL ; l = g_list_next (l)) {
2447 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2448 chat_message_received (chat, message, TRUE);
2454 got_filtered_messages_cb (GObject *manager,
2455 GAsyncResult *result,
2460 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2461 EmpathyChatPriv *priv = GET_PRIV (chat);
2462 GError *error = NULL;
2464 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2465 result, &messages, &error)) {
2466 DEBUG ("%s. Aborting.", error->message);
2467 empathy_chat_view_append_event (chat->view,
2468 _("Failed to retrieve recent logs"));
2469 g_error_free (error);
2473 for (l = messages; l; l = g_list_next (l)) {
2474 EmpathyMessage *message;
2476 g_assert (TPL_IS_EVENT (l->data));
2478 message = empathy_message_from_tpl_log_event (l->data);
2479 g_object_unref (l->data);
2481 if (empathy_message_is_edit (message)) {
2482 /* this is an edited message, create a synthetic event
2483 * using the supersedes token and
2484 * original-message-sent timestamp, that we can then
2486 EmpathyMessage *syn_msg = g_object_new (
2487 EMPATHY_TYPE_MESSAGE,
2489 "token", empathy_message_get_supersedes (message),
2490 "type", empathy_message_get_tptype (message),
2491 "timestamp", empathy_message_get_original_timestamp (message),
2492 "incoming", empathy_message_is_incoming (message),
2494 "receiver", empathy_message_get_receiver (message),
2495 "sender", empathy_message_get_sender (message),
2498 empathy_chat_view_append_message (chat->view, syn_msg);
2499 empathy_chat_view_edit_message (chat->view, message);
2501 g_object_unref (syn_msg);
2503 /* append the latest message */
2504 empathy_chat_view_append_message (chat->view, message);
2507 g_object_unref (message);
2509 g_list_free (messages);
2512 /* in case of TPL error, skip backlog and show pending messages */
2513 priv->can_show_pending = TRUE;
2514 show_pending_messages (chat);
2516 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2517 * about it in EmpathyChatPriv definition */
2518 priv->retrieving_backlogs = FALSE;
2519 empathy_chat_messages_read (chat);
2521 /* Turn back on scrolling */
2522 empathy_chat_view_scroll (chat->view, TRUE);
2526 chat_add_logs (EmpathyChat *chat)
2528 EmpathyChatPriv *priv = GET_PRIV (chat);
2535 /* Turn off scrolling temporarily */
2536 empathy_chat_view_scroll (chat->view, FALSE);
2538 /* Add messages from last conversation */
2539 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2540 target = tpl_entity_new_from_room_id (priv->id);
2542 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2544 priv->retrieving_backlogs = TRUE;
2545 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2548 TPL_EVENT_MASK_TEXT,
2552 got_filtered_messages_cb,
2555 g_object_unref (target);
2559 chat_contacts_completion_func (const gchar *s1,
2563 gchar *tmp, *nick1, *nick2;
2570 return s1 ? -1 : +1;
2573 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2574 nick1 = g_utf8_casefold (tmp, -1);
2577 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2578 nick2 = g_utf8_casefold (tmp, -1);
2581 ret = strncmp (nick1, nick2, n);
2590 build_part_message (guint reason,
2592 EmpathyContact *actor,
2593 const gchar *message)
2595 GString *s = g_string_new ("");
2596 const gchar *actor_name = NULL;
2598 if (actor != NULL) {
2599 actor_name = empathy_contact_get_alias (actor);
2602 /* Having an actor only really makes sense for a few actions... */
2604 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2605 g_string_append_printf (s, _("%s has disconnected"), name);
2607 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2608 if (actor_name != NULL) {
2609 /* translators: reverse the order of these arguments
2610 * if the kicked should come before the kicker in your locale.
2612 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2615 g_string_append_printf (s, _("%s was kicked"), name);
2618 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2619 if (actor_name != NULL) {
2620 /* translators: reverse the order of these arguments
2621 * if the banned should come before the banner in your locale.
2623 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2626 g_string_append_printf (s, _("%s was banned"), name);
2630 g_string_append_printf (s, _("%s has left the room"), name);
2633 if (!EMP_STR_EMPTY (message)) {
2634 /* Note to translators: this string is appended to
2635 * notifications like "foo has left the room", with the message
2636 * given by the user living the room. If this poses a problem,
2637 * please let us know. :-)
2639 g_string_append_printf (s, _(" (%s)"), message);
2642 return g_string_free (s, FALSE);
2646 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2647 EmpathyContact *contact,
2648 EmpathyContact *actor,
2654 EmpathyChatPriv *priv = GET_PRIV (chat);
2655 const gchar *name = empathy_contact_get_alias (contact);
2658 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2660 if (priv->block_events_timeout_id != 0)
2664 str = g_strdup_printf (_("%s has joined the room"),
2667 str = build_part_message (reason, name, actor, message);
2670 empathy_chat_view_append_event (chat->view, str);
2675 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2676 EmpathyContact *old_contact,
2677 EmpathyContact *new_contact,
2682 EmpathyChatPriv *priv = GET_PRIV (chat);
2684 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2686 if (priv->block_events_timeout_id == 0) {
2689 str = g_strdup_printf (_("%s is now known as %s"),
2690 empathy_contact_get_alias (old_contact),
2691 empathy_contact_get_alias (new_contact));
2692 empathy_chat_view_append_event (chat->view, str);
2699 chat_contacts_visible_timeout_cb (gpointer chat)
2701 EmpathyChatPriv *priv = GET_PRIV (chat);
2703 /* Relax the size request */
2704 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2706 /* Set the position of the slider. This must be done here because
2707 * GtkPaned need to know its size allocation and it will be settled only
2708 * after the gtk_window_resize () tough effect. */
2709 if (priv->contacts_width > 0) {
2710 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2711 priv->contacts_width);
2714 priv->contacts_visible_id = 0;
2720 chat_update_contacts_visibility (EmpathyChat *chat,
2723 EmpathyChatPriv *priv = GET_PRIV (chat);
2725 if (!priv->scrolled_window_contacts) {
2729 if (priv->remote_contact != NULL) {
2733 if (show && priv->contact_list_view == NULL) {
2734 EmpathyIndividualStore *store;
2736 GtkAllocation allocation;
2738 /* We are adding the contact list to the chat, we don't want the
2739 * chat view to become too small. If the chat view is already
2740 * smaller than 250 make sure that size won't change. If the
2741 * chat view is bigger the contact list will take some space on
2742 * it but we make sure the chat view don't become smaller than
2743 * 250. Relax the size request once the resize is done */
2744 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2745 min_width = MIN (allocation.width, 250);
2746 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2748 /* There is no way to know when the window resize will happen
2749 * since it is WM's decision. Let's hope it won't be longer. */
2750 if (priv->contacts_visible_id != 0)
2751 g_source_remove (priv->contacts_visible_id);
2752 priv->contacts_visible_id = g_timeout_add (500,
2753 chat_contacts_visible_timeout_cb, chat);
2755 store = EMPATHY_INDIVIDUAL_STORE (
2756 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2758 empathy_individual_store_set_show_groups (store, FALSE);
2760 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2761 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2762 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2763 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2764 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2765 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2766 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2768 empathy_individual_view_set_show_offline (
2769 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2770 empathy_individual_view_set_show_uninteresting (
2771 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2773 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2774 priv->contact_list_view);
2776 gtk_widget_show (priv->contact_list_view);
2777 gtk_widget_show (priv->scrolled_window_contacts);
2778 g_object_unref (store);
2780 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2781 gtk_widget_hide (priv->scrolled_window_contacts);
2782 if (priv->contact_list_view != NULL) {
2783 gtk_widget_destroy (priv->contact_list_view);
2784 priv->contact_list_view = NULL;
2790 empathy_chat_set_show_contacts (EmpathyChat *chat,
2793 EmpathyChatPriv *priv = GET_PRIV (chat);
2795 priv->show_contacts = show;
2797 chat_update_contacts_visibility (chat, show);
2799 g_object_notify (G_OBJECT (chat), "show-contacts");
2803 chat_remote_contact_changed_cb (EmpathyChat *chat)
2805 EmpathyChatPriv *priv = GET_PRIV (chat);
2807 if (priv->remote_contact != NULL) {
2808 g_object_unref (priv->remote_contact);
2809 priv->remote_contact = NULL;
2814 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2815 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2816 if (priv->remote_contact != NULL) {
2817 g_object_ref (priv->remote_contact);
2818 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2820 else if (priv->tp_chat != NULL) {
2821 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2824 chat_update_contacts_visibility (chat, priv->show_contacts);
2826 g_object_notify (G_OBJECT (chat), "remote-contact");
2827 g_object_notify (G_OBJECT (chat), "id");
2831 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2837 EmpathyChatPriv *priv;
2839 priv = GET_PRIV (chat);
2841 if (!priv->tp_chat) {
2845 chat_composing_remove_timeout (chat);
2846 g_object_unref (priv->tp_chat);
2847 priv->tp_chat = NULL;
2848 g_object_notify (G_OBJECT (chat), "tp-chat");
2850 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2851 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2853 chat_update_contacts_visibility (chat, FALSE);
2855 priv->unread_messages_when_offline = priv->unread_messages;
2859 update_misspelled_words (gpointer data)
2861 EmpathyChat *chat = EMPATHY_CHAT (data);
2862 EmpathyChatPriv *priv = GET_PRIV (chat);
2863 GtkTextBuffer *buffer;
2867 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2869 gtk_text_buffer_get_end_iter (buffer, &iter);
2870 length = gtk_text_iter_get_offset (&iter);
2871 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2872 NULL, length, chat);
2874 priv->update_misspelled_words_id = 0;
2880 conf_spell_checking_cb (GSettings *gsettings_chat,
2884 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2885 EmpathyChatPriv *priv = GET_PRIV (chat);
2886 gboolean spell_checker;
2887 GtkTextBuffer *buffer;
2889 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2892 spell_checker = g_settings_get_boolean (gsettings_chat,
2893 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2895 if (!empathy_spell_supported ()) {
2896 spell_checker = FALSE;
2899 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2901 if (spell_checker == priv->spell_checking_enabled) {
2902 if (spell_checker) {
2903 /* Possibly changed dictionaries,
2904 * update misspelled words. Need to do so in idle
2905 * so the spell checker is updated. */
2906 priv->update_misspelled_words_id =
2907 g_idle_add (update_misspelled_words, chat);
2913 if (spell_checker) {
2916 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2917 buffer, "notify::cursor-position",
2918 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2920 priv->insert_text_id = tp_g_signal_connect_object (
2921 buffer, "insert-text",
2922 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2923 chat, G_CONNECT_AFTER);
2924 priv->delete_range_id = tp_g_signal_connect_object (
2925 buffer, "delete-range",
2926 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2927 chat, G_CONNECT_AFTER);
2929 gtk_text_buffer_create_tag (buffer, "misspelled",
2930 "underline", PANGO_UNDERLINE_ERROR,
2933 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2934 gtk_text_buffer_get_insert (buffer));
2935 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2938 /* Mark misspelled words in the existing buffer.
2939 * Need to do so in idle so the spell checker is updated. */
2940 priv->update_misspelled_words_id =
2941 g_idle_add (update_misspelled_words, chat);
2943 GtkTextTagTable *table;
2946 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2947 priv->notify_cursor_position_id = 0;
2948 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2949 priv->insert_text_id = 0;
2950 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2951 priv->delete_range_id = 0;
2953 table = gtk_text_buffer_get_tag_table (buffer);
2954 tag = gtk_text_tag_table_lookup (table, "misspelled");
2955 gtk_text_tag_table_remove (table, tag);
2957 gtk_text_buffer_delete_mark_by_name (buffer,
2958 "previous-cursor-position");
2961 priv->spell_checking_enabled = spell_checker;
2965 save_paned_pos_timeout (gpointer data)
2967 EmpathyChat *self = data;
2970 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2972 g_settings_set_int (self->priv->gsettings_ui,
2973 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2980 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2984 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2986 if (chat->priv->save_paned_pos_id != 0)
2987 g_source_remove (chat->priv->save_paned_pos_id);
2989 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2990 save_paned_pos_timeout, chat);
2996 chat_create_ui (EmpathyChat *chat)
2998 EmpathyChatPriv *priv = GET_PRIV (chat);
3002 GtkTextBuffer *buffer;
3003 EmpathyThemeManager *theme_mgr;
3005 filename = empathy_file_lookup ("empathy-chat.ui",
3007 gui = empathy_builder_get_file (filename,
3008 "chat_widget", &priv->widget,
3009 "hpaned", &priv->hpaned,
3010 "vbox_left", &priv->vbox_left,
3011 "scrolled_window_chat", &priv->scrolled_window_chat,
3012 "scrolled_window_input", &priv->scrolled_window_input,
3013 "hbox_topic", &priv->hbox_topic,
3014 "expander_topic", &priv->expander_topic,
3015 "label_topic", &priv->label_topic,
3016 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3017 "info_bar_vbox", &priv->info_bar_vbox,
3020 empathy_builder_connect (gui, chat,
3021 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3022 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3027 /* Add message view. */
3028 theme_mgr = empathy_theme_manager_dup_singleton ();
3029 chat->view = empathy_theme_manager_create_view (theme_mgr);
3030 g_object_unref (theme_mgr);
3031 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3032 and other types, even though it's non-editable and doesn't accept any
3033 drags. This steals drag motion for anything inside the scrollbars,
3034 making drag destinations on chat windows far less useful.
3036 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3037 g_signal_connect (chat->view, "focus_in_event",
3038 G_CALLBACK (chat_text_view_focus_in_event_cb),
3040 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3041 GTK_WIDGET (chat->view));
3042 gtk_widget_show (GTK_WIDGET (chat->view));
3044 /* Add input GtkTextView */
3045 chat->input_text_view = empathy_input_text_view_new ();
3046 g_signal_connect (chat->input_text_view, "notify::has-focus",
3047 G_CALLBACK (chat_input_has_focus_notify_cb),
3049 g_signal_connect (chat->input_text_view, "key-press-event",
3050 G_CALLBACK (chat_input_key_press_event_cb),
3052 g_signal_connect (chat->input_text_view, "realize",
3053 G_CALLBACK (chat_input_realize_cb),
3055 g_signal_connect (chat->input_text_view, "button-press-event",
3056 G_CALLBACK (chat_input_button_press_event_cb),
3058 g_signal_connect (chat->input_text_view, "populate-popup",
3059 G_CALLBACK (chat_input_populate_popup_cb),
3061 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3062 tp_g_signal_connect_object (buffer, "changed",
3063 G_CALLBACK (chat_input_text_buffer_changed_cb),
3065 tp_g_signal_connect_object (priv->gsettings_chat,
3066 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3067 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3068 conf_spell_checking_cb (priv->gsettings_chat,
3069 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3070 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3071 chat->input_text_view);
3072 gtk_widget_show (chat->input_text_view);
3074 /* Add the (invisible) search bar */
3075 priv->search_bar = empathy_search_bar_new (chat->view);
3076 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3079 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3081 /* Initialy hide the topic, will be shown if not empty */
3082 gtk_widget_hide (priv->hbox_topic);
3084 g_signal_connect (priv->hpaned, "notify::position",
3085 G_CALLBACK (chat_hpaned_pos_changed_cb),
3088 /* Set widget focus order */
3089 list = g_list_append (NULL, priv->search_bar);
3090 list = g_list_append (list, priv->scrolled_window_input);
3091 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3094 list = g_list_append (NULL, priv->vbox_left);
3095 list = g_list_append (list, priv->scrolled_window_contacts);
3096 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3099 list = g_list_append (NULL, priv->hpaned);
3100 list = g_list_append (list, priv->hbox_topic);
3101 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3104 /* Add the main widget in the chat widget */
3105 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3106 g_object_unref (gui);
3110 chat_finalize (GObject *object)
3113 EmpathyChatPriv *priv;
3115 chat = EMPATHY_CHAT (object);
3116 priv = GET_PRIV (chat);
3118 DEBUG ("Finalized: %p", object);
3120 if (priv->update_misspelled_words_id != 0)
3121 g_source_remove (priv->update_misspelled_words_id);
3123 if (priv->save_paned_pos_id != 0)
3124 g_source_remove (priv->save_paned_pos_id);
3126 if (priv->contacts_visible_id != 0)
3127 g_source_remove (priv->contacts_visible_id);
3129 g_object_unref (priv->gsettings_chat);
3130 g_object_unref (priv->gsettings_ui);
3132 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3133 g_list_free (priv->input_history);
3135 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3136 g_list_free (priv->compositors);
3138 chat_composing_remove_timeout (chat);
3140 g_object_unref (priv->account_manager);
3141 g_object_unref (priv->log_manager);
3143 if (priv->tp_chat) {
3144 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3145 chat_invalidated_cb, chat);
3146 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3147 chat_message_received_cb, chat);
3148 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3149 chat_message_acknowledged_cb, chat);
3150 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3151 chat_send_error_cb, chat);
3152 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3153 chat_state_changed_cb, chat);
3154 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3155 chat_members_changed_cb, chat);
3156 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3157 chat_remote_contact_changed_cb, chat);
3158 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3159 chat_title_changed_cb, chat);
3160 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3161 chat_subject_changed_cb, chat);
3162 empathy_tp_chat_leave (priv->tp_chat, "");
3163 g_object_unref (priv->tp_chat);
3165 if (priv->account) {
3166 g_object_unref (priv->account);
3168 if (priv->remote_contact) {
3169 g_object_unref (priv->remote_contact);
3172 if (priv->block_events_timeout_id) {
3173 g_source_remove (priv->block_events_timeout_id);
3177 g_free (priv->name);
3178 g_free (priv->subject);
3179 g_completion_free (priv->completion);
3181 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3185 chat_constructed (GObject *object)
3187 EmpathyChat *chat = EMPATHY_CHAT (object);
3188 EmpathyChatPriv *priv = GET_PRIV (chat);
3190 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3191 /* First display logs from the logger and then display pending messages */
3192 chat_add_logs (chat);
3195 /* Just display pending messages for rooms */
3196 priv->can_show_pending = TRUE;
3197 show_pending_messages (chat);
3202 empathy_chat_class_init (EmpathyChatClass *klass)
3204 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3206 object_class->finalize = chat_finalize;
3207 object_class->get_property = chat_get_property;
3208 object_class->set_property = chat_set_property;
3209 object_class->constructed = chat_constructed;
3211 g_object_class_install_property (object_class,
3213 g_param_spec_object ("tp-chat",
3215 "The tp chat object",
3216 EMPATHY_TYPE_TP_CHAT,
3219 G_PARAM_STATIC_STRINGS));
3220 g_object_class_install_property (object_class,
3222 g_param_spec_object ("account",
3223 "Account of the chat",
3224 "The account of the chat",
3227 G_PARAM_STATIC_STRINGS));
3228 g_object_class_install_property (object_class,
3230 g_param_spec_string ("id",
3232 "The id of the chat",
3235 G_PARAM_STATIC_STRINGS));
3236 g_object_class_install_property (object_class,
3238 g_param_spec_string ("name",
3240 "The name of the chat",
3243 G_PARAM_STATIC_STRINGS));
3244 g_object_class_install_property (object_class,
3246 g_param_spec_string ("subject",
3248 "The subject or topic of the chat",
3251 G_PARAM_STATIC_STRINGS));
3252 g_object_class_install_property (object_class,
3253 PROP_REMOTE_CONTACT,
3254 g_param_spec_object ("remote-contact",
3255 "The remote contact",
3256 "The remote contact is any",
3257 EMPATHY_TYPE_CONTACT,
3259 G_PARAM_STATIC_STRINGS));
3260 g_object_class_install_property (object_class,
3262 g_param_spec_boolean ("show-contacts",
3263 "Contacts' visibility",
3264 "The visibility of the contacts' list",
3267 G_PARAM_STATIC_STRINGS));
3269 g_object_class_install_property (object_class,
3271 g_param_spec_boolean ("sms-channel",
3273 "TRUE if this channel is for sending SMSes",
3275 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3277 g_object_class_install_property (object_class,
3278 PROP_N_MESSAGES_SENDING,
3279 g_param_spec_uint ("n-messages-sending",
3280 "Num Messages Sending",
3281 "The number of messages being sent",
3283 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3285 g_object_class_install_property (object_class,
3286 PROP_NB_UNREAD_MESSAGES,
3287 g_param_spec_uint ("nb-unread-messages",
3288 "Num Unread Messages",
3289 "The number of unread messages",
3291 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3293 signals[COMPOSING] =
3294 g_signal_new ("composing",
3295 G_OBJECT_CLASS_TYPE (object_class),
3299 g_cclosure_marshal_generic,
3303 signals[NEW_MESSAGE] =
3304 g_signal_new ("new-message",
3305 G_OBJECT_CLASS_TYPE (object_class),
3309 g_cclosure_marshal_generic,
3311 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3313 signals[PART_COMMAND_ENTERED] =
3314 g_signal_new ("part-command-entered",
3315 G_OBJECT_CLASS_TYPE (object_class),
3319 g_cclosure_marshal_generic,
3323 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3327 chat_block_events_timeout_cb (gpointer data)
3329 EmpathyChatPriv *priv = GET_PRIV (data);
3331 priv->block_events_timeout_id = 0;
3337 account_manager_prepared_cb (GObject *source_object,
3338 GAsyncResult *result,
3341 GList *accounts, *l;
3342 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3343 EmpathyChat *chat = user_data;
3344 GError *error = NULL;
3346 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3347 DEBUG ("Failed to prepare the account manager: %s", error->message);
3348 g_error_free (error);
3352 accounts = tp_account_manager_get_valid_accounts (account_manager);
3354 for (l = accounts; l != NULL; l = l->next) {
3355 TpAccount *account = l->data;
3356 tp_g_signal_connect_object (account, "status-changed",
3357 G_CALLBACK (chat_new_connection_cb),
3361 g_list_free (accounts);
3365 empathy_chat_init (EmpathyChat *chat)
3367 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3368 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3371 priv->log_manager = tpl_log_manager_dup_singleton ();
3372 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3373 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3375 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3376 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3377 priv->input_history = NULL;
3378 priv->input_history_current = NULL;
3379 priv->account_manager = tp_account_manager_dup ();
3381 tp_proxy_prepare_async (priv->account_manager, NULL,
3382 account_manager_prepared_cb, chat);
3384 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3385 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3387 /* Block events for some time to avoid having "has come online" or
3388 * "joined" messages. */
3389 priv->block_events_timeout_id =
3390 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3392 /* Add nick name completion */
3393 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3394 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3396 chat_create_ui (chat);
3400 empathy_chat_new (EmpathyTpChat *tp_chat)
3402 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3406 empathy_chat_get_tp_chat (EmpathyChat *chat)
3408 EmpathyChatPriv *priv = GET_PRIV (chat);
3410 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3412 return priv->tp_chat;
3418 GtkWidget *info_bar;
3428 passwd_remember_button_cb (GtkButton *button,
3431 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3435 passwd_not_now_button_cb (GtkButton *button,
3438 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3442 remember_password_infobar_response_cb (GtkWidget *info_bar,
3446 EmpathyChatPriv *priv = GET_PRIV (data->self);
3448 if (response_id == GTK_RESPONSE_OK) {
3449 DEBUG ("Saving room password");
3450 empathy_keyring_set_room_password_async (priv->account,
3451 empathy_tp_chat_get_id (priv->tp_chat),
3456 gtk_widget_destroy (info_bar);
3457 g_free (data->password);
3458 g_slice_free (PasswordData, data);
3462 chat_prompt_to_save_password (EmpathyChat *self,
3465 GtkWidget *content_area;
3472 /* save the password in case it needs to be saved */
3473 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3475 /* Remove all previous widgets */
3476 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3477 gtk_container_forall (GTK_CONTAINER (content_area),
3478 (GtkCallback) gtk_widget_destroy, NULL);
3479 data->button = NULL;
3482 data->spinner = NULL;
3484 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3485 GTK_MESSAGE_QUESTION);
3487 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3488 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3491 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3492 GTK_ICON_SIZE_DIALOG);
3493 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3496 label = gtk_label_new (_("Would you like to store this password?"));
3497 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3499 /* Add 'Remember' button */
3500 alig = gtk_alignment_new (0, 0.5, 1, 0);
3502 button = gtk_button_new_with_label (_("Remember"));
3503 gtk_container_add (GTK_CONTAINER (alig), button);
3504 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3506 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3509 /* Add 'Not now' button */
3510 alig = gtk_alignment_new (0, 0.5, 1, 0);
3512 button = gtk_button_new_with_label (_("Not now"));
3513 gtk_container_add (GTK_CONTAINER (alig), button);
3514 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3516 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3520 g_signal_handler_disconnect (data->info_bar, data->response_id);
3521 g_signal_connect (data->info_bar, "response",
3522 G_CALLBACK (remember_password_infobar_response_cb), data);
3524 gtk_widget_show_all (data->info_bar);
3528 provide_password_cb (GObject *tp_chat,
3532 PasswordData *data = user_data;
3533 EmpathyChat *self = data->self;
3534 EmpathyChatPriv *priv = GET_PRIV (self);
3535 GError *error = NULL;
3537 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3539 DEBUG ("error: %s", error->message);
3540 /* FIXME: what should we do if that's another error? Close the channel?
3541 * Display the raw D-Bus error to the user isn't very useful */
3542 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3544 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3545 gtk_widget_set_sensitive (data->entry, TRUE);
3546 gtk_widget_grab_focus (data->entry);
3549 gtk_info_bar_set_message_type (
3550 GTK_INFO_BAR (data->info_bar),
3554 gtk_widget_set_sensitive (data->button, TRUE);
3555 gtk_button_set_label (GTK_BUTTON (data->button),
3559 gtk_label_set_text (GTK_LABEL (data->label),
3560 _("Wrong password; please try again:"));
3563 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3564 gtk_widget_hide (data->spinner);
3566 g_error_free (error);
3570 if (empathy_keyring_is_available ()) {
3571 /* ask whether they want to save the password */
3572 chat_prompt_to_save_password (self, data);
3574 /* Get rid of the password info bar finally */
3575 gtk_widget_destroy (data->info_bar);
3576 g_slice_free (PasswordData, data);
3580 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3581 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3582 gtk_widget_grab_focus (self->input_text_view);
3586 password_infobar_response_cb (GtkWidget *info_bar,
3590 EmpathyChatPriv *priv = GET_PRIV (data->self);
3591 const gchar *password;
3593 if (response_id != GTK_RESPONSE_OK) {
3594 gtk_widget_destroy (info_bar);
3595 g_slice_free (PasswordData, data);
3599 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3601 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3602 provide_password_cb, data);
3604 gtk_widget_set_sensitive (data->button, FALSE);
3605 gtk_widget_set_sensitive (data->entry, FALSE);
3607 gtk_spinner_start (GTK_SPINNER (data->spinner));
3608 gtk_widget_show (data->spinner);
3612 password_entry_activate_cb (GtkWidget *entry,
3615 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3619 passwd_join_button_cb (GtkButton *button,
3622 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3626 clear_icon_released_cb (GtkEntry *entry,
3627 GtkEntryIconPosition icon_pos,
3631 gtk_entry_set_text (entry, "");
3635 password_entry_changed_cb (GtkEditable *entry,
3640 str = gtk_entry_get_text (GTK_ENTRY (entry));
3642 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3643 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3647 infobar_chat_invalidated_cb (TpProxy *proxy,
3651 gpointer password_infobar)
3653 /* Destroy the password infobar whenever a channel is invalidated
3654 * so we don't have multiple infobars when the MUC is rejoined */
3655 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3659 display_password_info_bar (EmpathyChat *self)
3661 EmpathyChatPriv *priv = GET_PRIV (self);
3662 GtkWidget *info_bar;
3663 GtkWidget *content_area;
3673 data = g_slice_new0 (PasswordData);
3675 info_bar = gtk_info_bar_new ();
3676 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3677 GTK_MESSAGE_QUESTION);
3679 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3681 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3682 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3685 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3686 GTK_ICON_SIZE_DIALOG);
3687 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3690 label = gtk_label_new (_("This room is protected by a password:"));
3691 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3693 /* Add password entry */
3694 entry = gtk_entry_new ();
3695 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3696 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3698 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3699 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3700 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3701 GTK_ENTRY_ICON_SECONDARY, FALSE);
3703 g_signal_connect (entry, "icon-release",
3704 G_CALLBACK (clear_icon_released_cb), data);
3705 g_signal_connect (entry, "changed",
3706 G_CALLBACK (password_entry_changed_cb), data);
3708 g_signal_connect (entry, "activate",
3709 G_CALLBACK (password_entry_activate_cb), data);
3711 /* Focus the password entry once it's realized */
3712 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3714 /* Add 'Join' button */
3715 alig = gtk_alignment_new (0, 0.5, 1, 0);
3717 button = gtk_button_new_with_label (_("Join"));
3718 gtk_container_add (GTK_CONTAINER (alig), button);
3719 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3721 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3725 spinner = gtk_spinner_new ();
3726 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3728 /* Save some data for messing around with later */
3730 data->info_bar = info_bar;
3731 data->button = button;
3732 data->label = label;
3733 data->entry = entry;
3734 data->spinner = spinner;
3736 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3738 gtk_widget_show_all (hbox);
3740 tp_g_signal_connect_object (priv->tp_chat,
3741 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3744 data->response_id = g_signal_connect (info_bar, "response",
3745 G_CALLBACK (password_infobar_response_cb), data);
3747 gtk_widget_show_all (info_bar);
3748 /* ... but hide the spinner */
3749 gtk_widget_hide (spinner);
3751 /* prevent the user from typing anything */
3752 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3756 provide_saved_password_cb (GObject *tp_chat,
3760 EmpathyChat *self = user_data;
3761 EmpathyChatPriv *priv = GET_PRIV (self);
3762 GError *error = NULL;
3764 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3766 DEBUG ("error: %s", error->message);
3767 /* FIXME: what should we do if that's another error? Close the channel?
3768 * Display the raw D-Bus error to the user isn't very useful */
3769 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3770 display_password_info_bar (self);
3771 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3773 g_error_free (error);
3778 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3779 gtk_widget_grab_focus (self->input_text_view);
3783 chat_room_got_password_cb (GObject *source,
3784 GAsyncResult *result,
3787 EmpathyChat *self = user_data;
3788 EmpathyChatPriv *priv = GET_PRIV (self);
3789 const gchar *password;
3790 GError *error = NULL;
3792 password = empathy_keyring_get_room_password_finish (priv->account,
3795 if (error != NULL) {
3796 DEBUG ("Couldn't get room password: %s\n", error->message);
3797 g_clear_error (&error);
3799 display_password_info_bar (self);
3800 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3804 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3805 provide_saved_password_cb, self);
3809 chat_password_needed_changed_cb (EmpathyChat *self)
3811 EmpathyChatPriv *priv = GET_PRIV (self);
3813 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3814 empathy_keyring_get_room_password_async (priv->account,
3815 empathy_tp_chat_get_id (priv->tp_chat),
3816 chat_room_got_password_cb, self);
3821 chat_sms_channel_changed_cb (EmpathyChat *self)
3823 EmpathyChatPriv *priv = GET_PRIV (self);
3825 priv->sms_channel = tp_text_channel_is_sms_channel (
3826 (TpTextChannel *) priv->tp_chat);
3827 g_object_notify (G_OBJECT (self), "sms-channel");
3831 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3833 g_object_notify (G_OBJECT (self), "n-messages-sending");
3837 empathy_chat_set_tp_chat (EmpathyChat *chat,
3838 EmpathyTpChat *tp_chat)
3840 EmpathyChatPriv *priv = GET_PRIV (chat);
3842 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3843 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3845 if (priv->tp_chat) {
3849 if (priv->account) {
3850 g_object_unref (priv->account);
3853 priv->tp_chat = g_object_ref (tp_chat);
3854 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3856 g_signal_connect (tp_chat, "invalidated",
3857 G_CALLBACK (chat_invalidated_cb),
3859 g_signal_connect (tp_chat, "message-received-empathy",
3860 G_CALLBACK (chat_message_received_cb),
3862 g_signal_connect (tp_chat, "message_acknowledged",
3863 G_CALLBACK (chat_message_acknowledged_cb),
3865 g_signal_connect (tp_chat, "send-error",
3866 G_CALLBACK (chat_send_error_cb),
3868 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3869 G_CALLBACK (chat_state_changed_cb),
3871 g_signal_connect (tp_chat, "members-changed",
3872 G_CALLBACK (chat_members_changed_cb),
3874 g_signal_connect (tp_chat, "member-renamed",
3875 G_CALLBACK (chat_member_renamed_cb),
3877 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3878 G_CALLBACK (chat_remote_contact_changed_cb),
3880 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3881 G_CALLBACK (chat_password_needed_changed_cb),
3883 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
3884 G_CALLBACK (chat_sms_channel_changed_cb),
3886 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3887 G_CALLBACK (chat_n_messages_sending_changed_cb),
3889 g_signal_connect_swapped (tp_chat, "notify::title",
3890 G_CALLBACK (chat_title_changed_cb),
3892 g_signal_connect_swapped (tp_chat, "notify::subject",
3893 G_CALLBACK (chat_subject_changed_cb),
3896 /* Get initial value of properties */
3897 chat_sms_channel_changed_cb (chat);
3898 chat_remote_contact_changed_cb (chat);
3899 chat_title_changed_cb (chat);
3900 chat_subject_changed_cb (chat);
3902 if (chat->input_text_view) {
3903 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3904 if (priv->block_events_timeout_id == 0) {
3905 empathy_chat_view_append_event (chat->view, _("Connected"));
3909 g_object_notify (G_OBJECT (chat), "tp-chat");
3910 g_object_notify (G_OBJECT (chat), "id");
3911 g_object_notify (G_OBJECT (chat), "account");
3913 /* This is a noop when tp-chat is set at object construction time and causes
3914 * the pending messages to be show when it's set on the object after it has
3916 show_pending_messages (chat);
3918 /* check if a password is needed */
3919 chat_password_needed_changed_cb (chat);
3923 empathy_chat_get_account (EmpathyChat *chat)
3925 EmpathyChatPriv *priv = GET_PRIV (chat);
3927 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3929 return priv->account;
3933 empathy_chat_get_id (EmpathyChat *chat)
3935 EmpathyChatPriv *priv = GET_PRIV (chat);
3937 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3943 empathy_chat_dup_name (EmpathyChat *chat)
3945 EmpathyChatPriv *priv = GET_PRIV (chat);
3948 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3952 if (!ret && priv->remote_contact) {
3953 ret = empathy_contact_get_alias (priv->remote_contact);
3960 ret = _("Conversation");
3962 if (priv->sms_channel)
3963 /* Translators: this string is a something like
3964 * "Escher Cat (SMS)" */
3965 return g_strdup_printf (_("%s (SMS)"), ret);
3967 return g_strdup (ret);
3971 empathy_chat_get_subject (EmpathyChat *chat)
3973 EmpathyChatPriv *priv = GET_PRIV (chat);
3975 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3977 return priv->subject;
3981 empathy_chat_get_remote_contact (EmpathyChat *chat)
3983 EmpathyChatPriv *priv = GET_PRIV (chat);
3985 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3987 return priv->remote_contact;
3991 empathy_chat_get_contact_menu (EmpathyChat *chat)
3993 EmpathyChatPriv *priv = GET_PRIV (chat);
3994 GtkWidget *menu = NULL;
3995 FolksIndividual *individual;
3998 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4000 if (priv->remote_contact == NULL)
4003 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4004 if (contact == NULL)
4007 individual = empathy_create_individual_from_tp_contact (contact);
4008 if (individual == NULL)
4011 menu = empathy_individual_menu_new (individual,
4012 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4013 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4014 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4015 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4017 g_object_unref (individual);
4023 empathy_chat_clear (EmpathyChat *chat)
4025 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4027 empathy_chat_view_clear (chat->view);
4031 empathy_chat_scroll_down (EmpathyChat *chat)
4033 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4035 empathy_chat_view_scroll_down (chat->view);
4039 empathy_chat_cut (EmpathyChat *chat)
4041 GtkTextBuffer *buffer;
4043 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4045 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4046 if (gtk_text_buffer_get_has_selection (buffer)) {
4047 GtkClipboard *clipboard;
4049 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4051 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4056 empathy_chat_copy (EmpathyChat *chat)
4058 GtkTextBuffer *buffer;
4060 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4062 if (empathy_chat_view_get_has_selection (chat->view)) {
4063 empathy_chat_view_copy_clipboard (chat->view);
4067 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4068 if (gtk_text_buffer_get_has_selection (buffer)) {
4069 GtkClipboard *clipboard;
4071 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4073 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4078 EmpathyChatPriv *priv = GET_PRIV (chat);
4080 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4087 GtkClipboard *clipboard;
4089 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4090 start = g_utf8_offset_to_pointer (topic, start_offset);
4091 end = g_utf8_offset_to_pointer (topic, end_offset);
4092 selection = g_strndup (start, end - start);
4094 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4095 gtk_clipboard_set_text (clipboard, selection, -1);
4103 empathy_chat_paste (EmpathyChat *chat)
4105 GtkTextBuffer *buffer;
4106 GtkClipboard *clipboard;
4107 EmpathyChatPriv *priv;
4109 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4111 priv = GET_PRIV (chat);
4113 if (gtk_widget_get_visible (priv->search_bar)) {
4114 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4118 if (priv->tp_chat == NULL ||
4119 !gtk_widget_is_sensitive (chat->input_text_view))
4122 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4123 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4125 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4129 empathy_chat_find (EmpathyChat *chat)
4131 EmpathyChatPriv *priv;
4133 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4135 priv = GET_PRIV (chat);
4137 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4141 empathy_chat_correct_word (EmpathyChat *chat,
4144 const gchar *new_word)
4146 GtkTextBuffer *buffer;
4148 g_return_if_fail (chat != NULL);
4149 g_return_if_fail (new_word != NULL);
4151 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4153 gtk_text_buffer_delete (buffer, start, end);
4154 gtk_text_buffer_insert (buffer, start,
4160 empathy_chat_is_room (EmpathyChat *chat)
4162 EmpathyChatPriv *priv = GET_PRIV (chat);
4164 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4166 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4170 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4172 EmpathyChatPriv *priv = GET_PRIV (self);
4174 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4176 return priv->unread_messages;
4179 /* called when the messages have been read by user */
4181 empathy_chat_messages_read (EmpathyChat *self)
4183 EmpathyChatPriv *priv = GET_PRIV (self);
4185 g_return_if_fail (EMPATHY_IS_CHAT (self));
4187 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4188 * definition. If we are still retrieving the backlogs, do not ACK */
4189 if (priv->retrieving_backlogs)
4192 if (priv->tp_chat != NULL) {
4193 tp_text_channel_ack_all_pending_messages_async (
4194 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4197 if (priv->unread_messages_when_offline > 0) {
4198 /* We can't ack those as the connection has gone away so just consider
4200 priv->unread_messages -= priv->unread_messages_when_offline;
4201 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4202 priv->unread_messages_when_offline = 0;
4206 /* Return TRUE if on of the contacts in this chat is composing */
4208 empathy_chat_is_composing (EmpathyChat *chat)
4210 return chat->priv->compositors != NULL;
4214 empathy_chat_is_sms_channel (EmpathyChat *self)
4216 EmpathyChatPriv *priv = GET_PRIV (self);
4218 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4220 return priv->sms_channel;
4224 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4226 EmpathyChatPriv *priv;
4228 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4230 priv = GET_PRIV (self);
4232 if (priv->tp_chat == NULL) {
4237 g_object_get (priv->tp_chat,
4238 "n-messages-sending", &n_messages,
4246 empathy_chat_dup_text (EmpathyChat *self)
4248 GtkTextBuffer *buffer;
4249 GtkTextIter start, end;
4251 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4253 gtk_text_buffer_get_bounds (buffer, &start, &end);
4254 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4258 empathy_chat_set_text (EmpathyChat *self,
4261 GtkTextBuffer *buffer;
4263 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4265 gtk_text_buffer_set_text (buffer, text, -1);