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 *self_contact;
77 EmpathyContact *remote_contact;
78 gboolean show_contacts;
80 GSettings *gsettings_chat;
81 GSettings *gsettings_ui;
83 TplLogManager *log_manager;
84 TpAccountManager *account_manager;
86 GList *input_history_current;
88 GCompletion *completion;
89 guint composing_stop_timeout_id;
90 guint block_events_timeout_id;
91 TpHandleType handle_type;
93 gboolean has_input_vscroll;
95 /* TRUE if spell checking is enabled, FALSE otherwise.
96 * This is to keep track of the last state of spell checking
98 gboolean spell_checking_enabled;
100 /* These store the signal handler ids for the enclosed text entry. */
101 gulong insert_text_id;
102 gulong delete_range_id;
103 gulong notify_cursor_position_id;
105 /* Source func ID for update_misspelled_words () */
106 guint update_misspelled_words_id;
107 /* Source func ID for save_paned_pos_timeout () */
108 guint save_paned_pos_id;
109 /* Source func ID for chat_contacts_visible_timeout_cb () */
110 guint contacts_visible_id;
114 GtkWidget *vbox_left;
115 GtkWidget *scrolled_window_chat;
116 GtkWidget *scrolled_window_input;
117 GtkWidget *scrolled_window_contacts;
118 GtkWidget *hbox_topic;
119 GtkWidget *expander_topic;
120 GtkWidget *label_topic;
121 GtkWidget *contact_list_view;
122 GtkWidget *info_bar_vbox;
123 GtkWidget *search_bar;
125 guint unread_messages;
126 guint unread_messages_when_offline;
127 /* TRUE if the pending messages can be displayed. This is to avoid to show
128 * pending messages *before* messages from logs. (#603980) */
129 gboolean can_show_pending;
131 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
132 * be differently handled since it introduces another race condition, which
133 * is really hard to occur, but still possible.
135 * With the current workaround (which has the race above), we need to be
136 * sure to ACK any pending messages only when the retrieval of backlogs is
137 * finished, that's why using retrieving_backlogs flag.
138 * empathy_chat_messages_read () will check this variable and not ACK
139 * anything when TRUE. It will be set TRUE at chat_constructed () and set
140 * back to FALSE when the backlog has been retrieved and the pending
141 * messages actually showed to the user.
143 * Race condition introduced with this workaround:
144 * Scenario: a message is pending, the user is notified and selects the tab.
145 * the tab with a pending message is focused before the messages are properly
146 * shown (since the preparation of the window is slower AND async WRT the
147 * tab showing), which means the user won't see any new messages (rare but
148 * possible), if he/she will change tab focus before the messages are
149 * properly shown, the tab will be set as 'seen' and the user won't be
150 * notified again about the already notified pending messages when the
151 * messages in tab will be properly shown */
152 gboolean retrieving_backlogs;
153 gboolean sms_channel;
155 /* we need to know whether populate-popup happened in response to
156 * the keyboard or the mouse. We can't ask GTK for the most recent
157 * event, because it will be a notify event. Instead we track it here */
158 GdkEventType most_recent_event_type;
162 gchar *text; /* Original message that was specified
163 * upon entry creation. */
164 gchar *modified_text; /* Message that was modified by user.
165 * When no modifications were made, it is NULL */
171 PART_COMMAND_ENTERED,
185 PROP_N_MESSAGES_SENDING,
186 PROP_NB_UNREAD_MESSAGES,
189 static guint signals[LAST_SIGNAL] = { 0 };
191 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
193 static gboolean update_misspelled_words (gpointer data);
196 chat_get_property (GObject *object,
201 EmpathyChat *chat = EMPATHY_CHAT (object);
202 EmpathyChatPriv *priv = GET_PRIV (object);
206 g_value_set_object (value, priv->tp_chat);
209 g_value_set_object (value, priv->account);
212 g_value_take_string (value, empathy_chat_dup_name (chat));
215 g_value_set_string (value, priv->id);
218 g_value_set_string (value, priv->subject);
220 case PROP_REMOTE_CONTACT:
221 g_value_set_object (value, priv->remote_contact);
223 case PROP_SHOW_CONTACTS:
224 g_value_set_boolean (value, priv->show_contacts);
226 case PROP_SMS_CHANNEL:
227 g_value_set_boolean (value, priv->sms_channel);
229 case PROP_N_MESSAGES_SENDING:
230 g_value_set_uint (value,
231 empathy_chat_get_n_messages_sending (chat));
233 case PROP_NB_UNREAD_MESSAGES:
234 g_value_set_uint (value,
235 empathy_chat_get_nb_unread_messages (chat));
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
244 chat_set_property (GObject *object,
249 EmpathyChat *chat = EMPATHY_CHAT (object);
253 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
255 case PROP_SHOW_CONTACTS:
256 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
265 account_reconnected (EmpathyChat *chat,
268 EmpathyChatPriv *priv = GET_PRIV (chat);
270 DEBUG ("Account reconnected, request a new Text channel");
272 /* FIXME: Ideally we should ask to handle ourself the channel so we can
273 * report the error if any but this is blocked by
274 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
275 switch (priv->handle_type) {
276 case TP_HANDLE_TYPE_CONTACT:
277 if (priv->sms_channel)
278 empathy_sms_contact_id (
280 TP_USER_ACTION_TIME_NOT_USER_ACTION,
283 empathy_chat_with_contact_id (
285 TP_USER_ACTION_TIME_NOT_USER_ACTION,
288 case TP_HANDLE_TYPE_ROOM:
289 empathy_join_muc (account, priv->id,
290 TP_USER_ACTION_TIME_NOT_USER_ACTION);
292 case TP_HANDLE_TYPE_NONE:
293 case TP_HANDLE_TYPE_LIST:
294 case TP_HANDLE_TYPE_GROUP:
296 g_assert_not_reached ();
300 g_object_unref (chat);
304 chat_new_connection_cb (TpAccount *account,
308 gchar *dbus_error_name,
312 EmpathyChatPriv *priv = GET_PRIV (chat);
314 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
317 if (priv->tp_chat != NULL || account != priv->account ||
318 priv->handle_type == TP_HANDLE_TYPE_NONE ||
319 EMP_STR_EMPTY (priv->id))
324 account_reconnected (chat, account);
328 chat_composing_remove_timeout (EmpathyChat *chat)
330 EmpathyChatPriv *priv;
332 priv = GET_PRIV (chat);
334 if (priv->composing_stop_timeout_id) {
335 g_source_remove (priv->composing_stop_timeout_id);
336 priv->composing_stop_timeout_id = 0;
341 set_chate_state_cb (GObject *source,
342 GAsyncResult *result,
345 GError *error = NULL;
347 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
349 DEBUG ("Failed to set chat state: %s", error->message);
350 g_error_free (error);
355 set_chat_state (EmpathyChat *self,
356 TpChannelChatState state)
358 EmpathyChatPriv *priv = GET_PRIV (self);
360 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
361 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
364 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
365 set_chate_state_cb, self);
369 chat_composing_stop_timeout_cb (EmpathyChat *chat)
371 EmpathyChatPriv *priv;
373 priv = GET_PRIV (chat);
375 priv->composing_stop_timeout_id = 0;
376 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
382 chat_composing_start (EmpathyChat *chat)
384 EmpathyChatPriv *priv;
386 priv = GET_PRIV (chat);
388 if (priv->composing_stop_timeout_id) {
389 /* Just restart the timeout */
390 chat_composing_remove_timeout (chat);
392 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
395 priv->composing_stop_timeout_id = g_timeout_add_seconds (
396 COMPOSING_STOP_TIMEOUT,
397 (GSourceFunc) chat_composing_stop_timeout_cb,
402 chat_composing_stop (EmpathyChat *chat)
404 chat_composing_remove_timeout (chat);
405 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
409 chat_input_history_entry_cmp (InputHistoryEntry *entry,
412 if (!tp_strdiff (entry->text, text)) {
413 if (entry->modified_text != NULL) {
414 /* Modified entry and single string cannot be equal. */
422 static InputHistoryEntry *
423 chat_input_history_entry_new_with_text (const gchar *text)
425 InputHistoryEntry *entry;
426 entry = g_slice_new0 (InputHistoryEntry);
427 entry->text = g_strdup (text);
433 chat_input_history_entry_free (InputHistoryEntry *entry)
435 g_free (entry->text);
436 g_free (entry->modified_text);
437 g_slice_free (InputHistoryEntry, entry);
441 chat_input_history_entry_revert (InputHistoryEntry *entry)
443 g_free (entry->modified_text);
444 entry->modified_text = NULL;
448 chat_input_history_entry_update_text (InputHistoryEntry *entry,
453 if (!tp_strdiff (text, entry->text)) {
454 g_free (entry->modified_text);
455 entry->modified_text = NULL;
459 old = entry->modified_text;
460 entry->modified_text = g_strdup (text);
465 chat_input_history_entry_get_text (InputHistoryEntry *entry)
471 if (entry->modified_text != NULL) {
472 return entry->modified_text;
478 chat_input_history_remove_item (GList *list,
481 list = g_list_remove_link (list, item);
482 chat_input_history_entry_free (item->data);
483 g_list_free_1 (item);
488 chat_input_history_revert (EmpathyChat *chat)
490 EmpathyChatPriv *priv;
494 InputHistoryEntry *entry;
496 priv = GET_PRIV (chat);
497 list = priv->input_history;
500 DEBUG ("No input history");
504 /* Delete temporary entry */
505 if (priv->input_history_current != NULL) {
507 list = chat_input_history_remove_item (list, item1);
508 if (priv->input_history_current == item1) {
509 /* Removed temporary entry was current entry */
510 priv->input_history = list;
511 priv->input_history_current = NULL;
516 /* There is no entry to revert */
520 /* Restore the current history entry to original value */
521 item1 = priv->input_history_current;
523 chat_input_history_entry_revert (entry);
525 /* Remove restored entry if there is other occurance before this entry */
526 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
527 (GCompareFunc) chat_input_history_entry_cmp);
528 if (item2 != item1) {
529 list = chat_input_history_remove_item (list, item1);
532 /* Remove other occurance of the restored entry */
533 item2 = g_list_find_custom (item1->next,
534 chat_input_history_entry_get_text (entry),
535 (GCompareFunc) chat_input_history_entry_cmp);
537 list = chat_input_history_remove_item (list, item2);
541 priv->input_history_current = NULL;
542 priv->input_history = list;
546 chat_input_history_add (EmpathyChat *chat,
550 EmpathyChatPriv *priv;
553 InputHistoryEntry *entry;
555 priv = GET_PRIV (chat);
557 list = priv->input_history;
559 /* Remove any other occurances of this entry, if not temporary */
561 while ((item = g_list_find_custom (list, str,
562 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
563 list = chat_input_history_remove_item (list, item);
566 /* Trim the list to the last 10 items */
567 while (g_list_length (list) > 10) {
568 item = g_list_last (list);
570 list = chat_input_history_remove_item (list, item);
578 entry = chat_input_history_entry_new_with_text (str);
579 list = g_list_prepend (list, entry);
581 /* Set the list and the current item pointer */
582 priv->input_history = list;
584 priv->input_history_current = list;
587 priv->input_history_current = NULL;
592 chat_input_history_get_next (EmpathyChat *chat)
594 EmpathyChatPriv *priv;
598 priv = GET_PRIV (chat);
600 if (priv->input_history == NULL) {
601 DEBUG ("No input history, next entry is NULL");
604 g_assert (priv->input_history_current != NULL);
606 if ((item = g_list_next (priv->input_history_current)) == NULL)
608 item = priv->input_history_current;
611 msg = chat_input_history_entry_get_text (item->data);
613 DEBUG ("Returning next entry: '%s'", msg);
615 priv->input_history_current = item;
621 chat_input_history_get_prev (EmpathyChat *chat)
623 EmpathyChatPriv *priv;
627 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
629 priv = GET_PRIV (chat);
631 if (priv->input_history == NULL) {
632 DEBUG ("No input history, previous entry is NULL");
636 if (priv->input_history_current == NULL)
640 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
642 item = priv->input_history_current;
645 msg = chat_input_history_entry_get_text (item->data);
647 DEBUG ("Returning previous entry: '%s'", msg);
649 priv->input_history_current = item;
655 chat_input_history_update (EmpathyChat *chat,
656 GtkTextBuffer *buffer)
658 EmpathyChatPriv *priv;
659 GtkTextIter start, end;
661 InputHistoryEntry *entry;
663 priv = GET_PRIV (chat);
665 gtk_text_buffer_get_bounds (buffer, &start, &end);
666 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
668 if (priv->input_history_current == NULL) {
669 /* Add the current text temporarily to the history */
670 chat_input_history_add (chat, text, TRUE);
675 /* Save the changes in the history */
676 entry = priv->input_history_current->data;
677 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
678 chat_input_history_entry_update_text (entry, text);
687 } ChatCommandMsgData;
690 chat_command_msg_cb (GObject *source,
691 GAsyncResult *result,
694 ChatCommandMsgData *data = user_data;
695 GError *error = NULL;
698 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
699 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
701 if (channel == NULL) {
702 DEBUG ("Failed to get channel: %s", error->message);
703 g_error_free (error);
705 empathy_chat_view_append_event (data->chat->view,
706 _("Failed to open private chat"));
710 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
711 TpTextChannel *text = (TpTextChannel *) channel;
714 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
717 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
719 g_object_unref (msg);
722 g_object_unref (channel);
725 g_free (data->message);
726 g_slice_free (ChatCommandMsgData, data);
730 nick_command_supported (EmpathyChat *chat)
732 EmpathyChatPriv * priv = GET_PRIV (chat);
733 TpConnection *connection;
735 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
736 return tp_proxy_has_interface_by_id (connection,
737 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
741 part_command_supported (EmpathyChat *chat)
743 EmpathyChatPriv * priv = GET_PRIV (chat);
745 return tp_proxy_has_interface_by_id (priv->tp_chat,
746 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
750 chat_command_clear (EmpathyChat *chat,
753 empathy_chat_view_clear (chat->view);
757 chat_command_topic (EmpathyChat *chat,
760 EmpathyChatPriv *priv = GET_PRIV (chat);
762 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
763 empathy_chat_view_append_event (chat->view,
764 _("Topic not supported on this conversation"));
768 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
769 empathy_chat_view_append_event (chat->view,
770 _("You are not allowed to change the topic"));
774 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
778 empathy_chat_join_muc (EmpathyChat *chat,
781 EmpathyChatPriv *priv = GET_PRIV (chat);
783 empathy_join_muc (priv->account, room,
784 empathy_get_current_action_time ());
788 chat_command_join (EmpathyChat *chat,
793 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
795 /* FIXME: Ideally we should ask to handle ourself the channel so we can
796 * report the error if any but this is blocked by
797 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
798 while (rooms[i] != NULL) {
799 /* ignore empty strings */
800 if (!EMP_STR_EMPTY (rooms[i])) {
801 empathy_chat_join_muc (chat, rooms[i]);
809 chat_command_part (EmpathyChat *chat,
812 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
816 chat_command_msg_internal (EmpathyChat *chat,
817 const gchar *contact_id,
818 const gchar *message)
820 EmpathyChatPriv *priv = GET_PRIV (chat);
821 ChatCommandMsgData *data;
822 TpAccountChannelRequest *req;
825 request = tp_asv_new (
826 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
827 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
828 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
831 req = tp_account_channel_request_new (priv->account, request,
832 empathy_get_current_action_time ());
834 /* FIXME: We should probably search in members alias. But this
835 * is enough for IRC */
836 data = g_slice_new (ChatCommandMsgData);
838 data->message = g_strdup (message);
840 tp_account_channel_request_ensure_and_observe_channel_async (req,
841 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
843 g_object_unref (req);
844 g_hash_table_unref (request);
848 chat_command_query (EmpathyChat *chat,
851 /* If <message> part is not defined,
852 * strv[2] will be the terminal NULL */
853 chat_command_msg_internal (chat, strv[1], strv[2]);
857 chat_command_msg (EmpathyChat *chat,
860 chat_command_msg_internal (chat, strv[1], strv[2]);
864 callback_for_request_rename (TpProxy *proxy,
867 GObject *weak_object)
870 DEBUG ("Call to RequestRename method failed: %s",error->message);
875 chat_command_nick (EmpathyChat *chat,
878 EmpathyChatPriv *priv = GET_PRIV (chat);
881 proxy = TP_PROXY (tp_account_get_connection (priv->account));
883 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
884 strv[1], callback_for_request_rename, NULL, NULL, NULL);
888 chat_command_me (EmpathyChat *chat,
891 EmpathyChatPriv *priv = GET_PRIV (chat);
893 TpTextChannel *channel;
895 channel = (TpTextChannel *) (priv->tp_chat);
897 if (!tp_text_channel_supports_message_type (channel,
898 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
899 /* Action message are not supported, 'simulate' the action */
902 /* The TpChat can't be ready if it doesn't have the self contact */
903 g_assert (priv->self_contact != NULL);
905 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
907 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
912 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
916 empathy_tp_chat_send (priv->tp_chat, message);
917 g_object_unref (message);
921 chat_command_say (EmpathyChat *chat,
924 EmpathyChatPriv *priv = GET_PRIV (chat);
927 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
929 empathy_tp_chat_send (priv->tp_chat, message);
930 g_object_unref (message);
934 whois_got_contact_cb (TpConnection *connection,
936 TpContact * const *contacts,
937 const gchar * const *requested_ids,
938 GHashTable *failed_id_errors,
941 GObject *weak_object)
943 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
945 g_return_if_fail (n_contacts <= 1);
947 if (n_contacts == 0) {
949 gpointer key = NULL, value = NULL;
953 /* tp-glib guarantees that the contacts you requested will be
954 * in failed_id_errors regardless of whether the individual
955 * contact was invalid or the whole operation failed.
957 g_hash_table_iter_init (&iter, failed_id_errors);
958 g_hash_table_iter_next (&iter, &key, &value);
962 DEBUG ("Error getting TpContact for '%s': %s %u %s",
963 id, g_quark_to_string (id_error->domain),
964 id_error->code, id_error->message);
967 /* The specific ID failed. */
968 gchar *event = g_strdup_printf (
969 _("“%s” is not a valid contact ID"), id);
970 empathy_chat_view_append_event (chat->view, event);
973 /* Otherwise we're disconnected or something; so the window
974 * will already say ‘Disconnected’, so let's not show anything.
977 EmpathyContact *empathy_contact;
980 g_return_if_fail (contacts[0] != NULL);
981 empathy_contact = empathy_contact_dup_from_tp_contact (
984 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
985 /* If we're alive and this command is running, we'd better be
987 g_return_if_fail (window != NULL);
988 g_return_if_fail (gtk_widget_is_toplevel (window));
989 empathy_contact_information_dialog_show (empathy_contact,
990 GTK_WINDOW (window));
991 g_object_unref (empathy_contact);
996 chat_command_whois (EmpathyChat *chat,
999 EmpathyChatPriv *priv = GET_PRIV (chat);
1002 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1003 tp_connection_get_contacts_by_id (conn,
1004 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1005 * entered by the user (including spaces, if any). */
1006 1, (const gchar * const *) strv + 1,
1008 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1012 chat_command_whale (EmpathyChat *chat,
1015 EmpathyChatPriv *priv = GET_PRIV (chat);
1018 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1020 "•_______________•");
1021 empathy_tp_chat_send (priv->tp_chat, message);
1022 g_object_unref (message);
1026 chat_command_babywhale (EmpathyChat *chat,
1029 EmpathyChatPriv *priv = GET_PRIV (chat);
1032 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1035 empathy_tp_chat_send (priv->tp_chat, message);
1036 g_object_unref (message);
1040 chat_command_inspector (EmpathyChat *chat,
1043 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1044 empathy_theme_adium_show_inspector (
1045 EMPATHY_THEME_ADIUM (chat->view));
1049 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1051 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1054 const gchar *prefix;
1057 ChatCommandFunc func;
1058 gboolean (*is_supported)(EmpathyChat *chat);
1062 static ChatCommandItem commands[] = {
1063 {"clear", 1, 1, chat_command_clear, NULL,
1064 N_("/clear: clear all messages from the current conversation")},
1066 {"topic", 2, 2, chat_command_topic, NULL,
1067 N_("/topic <topic>: set the topic of the current conversation")},
1069 {"join", 2, 2, chat_command_join, NULL,
1070 N_("/join <chat room ID>: join a new chat room")},
1072 {"j", 2, 2, chat_command_join, NULL,
1073 N_("/j <chat room ID>: join a new chat room")},
1075 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1076 {"part", 1, 3, chat_command_part, part_command_supported,
1077 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1078 "by default the current one")},
1080 {"query", 2, 3, chat_command_query, NULL,
1081 N_("/query <contact ID> [<message>]: open a private chat")},
1083 {"msg", 3, 3, chat_command_msg, NULL,
1084 N_("/msg <contact ID> <message>: open a private chat")},
1086 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1087 N_("/nick <nickname>: change your nickname on the current server")},
1089 {"me", 2, 2, chat_command_me, NULL,
1090 N_("/me <message>: send an ACTION message to the current conversation")},
1092 {"say", 2, 2, chat_command_say, NULL,
1093 N_("/say <message>: send <message> to the current conversation. "
1094 "This is used to send a message starting with a '/'. For example: "
1095 "\"/say /join is used to join a new chat room\"")},
1097 {"whois", 2, 2, chat_command_whois, NULL,
1098 N_("/whois <contact ID>: display information about a contact")},
1100 {"help", 1, 2, chat_command_help, NULL,
1101 N_("/help [<command>]: show all supported commands. "
1102 "If <command> is defined, show its usage.")},
1104 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1106 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1107 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1111 chat_command_show_help (EmpathyChat *chat,
1112 ChatCommandItem *item)
1116 if (item->help == NULL) {
1120 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1121 empathy_chat_view_append_event (chat->view, str);
1126 chat_command_help (EmpathyChat *chat,
1131 /* If <command> part is not defined,
1132 * strv[1] will be the terminal NULL */
1133 if (strv[1] == NULL) {
1134 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1135 if (commands[i].is_supported != NULL) {
1136 if (!commands[i].is_supported (chat)) {
1140 if (commands[i].help == NULL) {
1143 empathy_chat_view_append_event (chat->view,
1144 _(commands[i].help));
1149 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1150 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1151 if (commands[i].is_supported != NULL) {
1152 if (!commands[i].is_supported (chat)) {
1156 if (commands[i].help == NULL) {
1159 chat_command_show_help (chat, &commands[i]);
1164 empathy_chat_view_append_event (chat->view,
1165 _("Unknown command"));
1169 chat_command_parse (const gchar *text, guint max_parts)
1174 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1176 array = g_ptr_array_sized_new (max_parts + 1);
1177 while (max_parts > 1) {
1180 /* Skip white spaces */
1181 while (g_ascii_isspace (*text)) {
1185 /* Search the end of this part, until first space. */
1186 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1192 item = g_strndup (text, end - text);
1193 g_ptr_array_add (array, item);
1194 DEBUG ("\tITEM: \"%s\"", item);
1200 /* Append last part if not empty */
1201 item = g_strstrip (g_strdup (text));
1202 if (!EMP_STR_EMPTY (item)) {
1203 g_ptr_array_add (array, item);
1204 DEBUG ("\tITEM: \"%s\"", item);
1209 /* Make the array NULL-terminated */
1210 g_ptr_array_add (array, NULL);
1212 return (GStrv) g_ptr_array_free (array, FALSE);
1216 has_prefix_case (const gchar *s,
1217 const gchar *prefix)
1219 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1223 chat_send (EmpathyChat *chat,
1226 EmpathyChatPriv *priv;
1230 if (EMP_STR_EMPTY (msg)) {
1234 priv = GET_PRIV (chat);
1236 chat_input_history_add (chat, msg, FALSE);
1238 if (msg[0] == '/') {
1239 gboolean second_slash = FALSE;
1240 const gchar *iter = msg + 1;
1242 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1247 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1250 c = *(msg + 1 + strlen (commands[i].prefix));
1251 if (c != '\0' && !g_ascii_isspace (c)) {
1254 if (commands[i].is_supported != NULL) {
1255 if (!commands[i].is_supported (chat)) {
1260 /* We can't use g_strsplit here because it does
1261 * not deal correctly if we have more than one space
1263 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1265 strv_len = g_strv_length (strv);
1266 if (strv_len < commands[i].min_parts ||
1267 strv_len > commands[i].max_parts) {
1268 chat_command_show_help (chat, &commands[i]);
1273 commands[i].func (chat, strv);
1278 /* Also allow messages with two slashes before the
1279 * first space, so it is possible to send a /unix/path.
1280 * This heuristic is kind of crap. */
1281 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1283 second_slash = TRUE;
1289 if (!second_slash) {
1290 empathy_chat_view_append_event (chat->view,
1291 _("Unknown command; see /help for the available"
1297 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1299 empathy_tp_chat_send (priv->tp_chat, message);
1300 g_object_unref (message);
1304 chat_input_text_view_send (EmpathyChat *chat)
1306 GtkTextBuffer *buffer;
1307 GtkTextIter start, end;
1310 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1312 gtk_text_buffer_get_bounds (buffer, &start, &end);
1313 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1315 /* clear the input field */
1316 gtk_text_buffer_set_text (buffer, "", -1);
1317 /* delete input history modifications */
1318 chat_input_history_revert (chat);
1320 chat_send (chat, msg);
1325 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1326 EmpathyContact *contact,
1327 TpChannelChatState state,
1330 EmpathyChatPriv *priv;
1332 gboolean was_composing;
1334 priv = GET_PRIV (chat);
1336 if (empathy_contact_is_user (contact)) {
1337 /* We don't care about our own chat state */
1341 was_composing = (priv->compositors != NULL);
1343 /* Find the contact in the list. After that l is the list elem or NULL */
1344 for (l = priv->compositors; l; l = l->next) {
1345 if (contact == l->data) {
1351 case TP_CHANNEL_CHAT_STATE_GONE:
1352 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1353 case TP_CHANNEL_CHAT_STATE_PAUSED:
1354 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1355 /* Contact is not composing */
1357 priv->compositors = g_list_remove_link (priv->compositors, l);
1358 g_object_unref (l->data);
1362 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1363 /* Contact is composing */
1365 priv->compositors = g_list_prepend (priv->compositors,
1366 g_object_ref (contact));
1370 g_assert_not_reached ();
1373 DEBUG ("Was composing: %s now composing: %s",
1374 was_composing ? "yes" : "no",
1375 priv->compositors ? "yes" : "no");
1377 if ((was_composing && !priv->compositors) ||
1378 (!was_composing && priv->compositors)) {
1379 /* Composing state changed */
1380 g_signal_emit (chat, signals[COMPOSING], 0,
1381 priv->compositors != NULL);
1386 get_highlight_regex_for (const gchar *name)
1389 gchar *name_esc, *pattern;
1390 GError *error = NULL;
1392 name_esc = g_regex_escape_string (name, -1);
1393 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1394 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1397 if (regex == NULL) {
1398 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1401 g_error_free (error);
1411 chat_should_highlight (EmpathyChat *chat,
1412 EmpathyMessage *message)
1414 const gchar *msg, *to;
1415 gboolean ret_val = FALSE;
1416 TpChannelTextMessageFlags flags;
1419 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1421 if (!empathy_chat_is_room (chat)) {
1425 if (!empathy_message_is_incoming (message)) {
1429 msg = empathy_message_get_body (message);
1434 if (!chat->priv->self_contact) {
1438 to = empathy_contact_get_alias (chat->priv->self_contact);
1443 flags = empathy_message_get_flags (message);
1444 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1445 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1446 * have already been received by the user before (and so are in the logs) */
1450 regex = get_highlight_regex_for (to);
1451 if (regex != NULL) {
1452 ret_val = g_regex_match (regex, msg, 0, NULL);
1453 g_regex_unref (regex);
1460 chat_message_received (EmpathyChat *chat,
1461 EmpathyMessage *message,
1464 EmpathyChatPriv *priv = GET_PRIV (chat);
1465 EmpathyContact *sender;
1467 sender = empathy_message_get_sender (message);
1469 if (empathy_message_is_edit (message)) {
1470 DEBUG ("Editing message '%s' to '%s'",
1471 empathy_message_get_supersedes (message),
1472 empathy_message_get_body (message));
1474 empathy_chat_view_edit_message (chat->view, message);
1476 gboolean should_highlight = chat_should_highlight (chat, message);
1477 DEBUG ("Appending new message '%s' from %s (%d)",
1478 empathy_message_get_token (message),
1479 empathy_contact_get_alias (sender),
1480 empathy_contact_get_handle (sender));
1482 empathy_chat_view_append_message (chat->view, message, should_highlight);
1484 if (empathy_message_is_incoming (message)) {
1485 priv->unread_messages++;
1486 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1489 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1493 /* We received a message so the contact is no longer
1495 chat_state_changed_cb (priv->tp_chat, sender,
1496 TP_CHANNEL_CHAT_STATE_ACTIVE,
1501 chat_message_received_cb (EmpathyTpChat *tp_chat,
1502 EmpathyMessage *message,
1505 chat_message_received (chat, message, FALSE);
1509 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1510 EmpathyMessage *message,
1513 EmpathyChatPriv *priv = GET_PRIV (chat);
1515 empathy_chat_view_message_acknowledged (chat->view,
1518 if (!empathy_message_is_edit (message)) {
1519 priv->unread_messages--;
1520 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1525 append_balance_error (EmpathyChat *chat,
1526 const gchar *message_body)
1528 EmpathyChatPriv *priv = GET_PRIV (chat);
1529 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1530 const gchar *uri = tp_connection_get_balance_uri (conn);
1531 const gchar *error = _("insufficient balance to send message");
1532 gchar *str, *str_markup = NULL;
1534 if (message_body != NULL) {
1535 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1537 str = g_strdup_printf (_("Error sending message: %s"), error);
1540 if (!tp_str_empty (uri)) {
1541 /* translators: error used when user doesn't have enough credit on his
1542 * account to send the message. */
1543 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1544 " <a href='%s'>Top up</a>."), uri);
1546 if (message_body != NULL) {
1547 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1549 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1550 escaped_body, markup_error);
1552 g_free (escaped_body);
1554 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1557 g_free (markup_error);
1560 if (str_markup != NULL)
1561 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1563 empathy_chat_view_append_event (chat->view, str);
1566 g_free (str_markup);
1570 chat_send_error_cb (EmpathyTpChat *tp_chat,
1571 const gchar *message_body,
1572 TpChannelTextSendError error_code,
1573 const gchar *dbus_error,
1576 const gchar *error = NULL;
1579 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1580 append_balance_error (chat, message_body);
1582 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1583 error = _("not capable");
1586 if (error == NULL) {
1587 /* if we didn't find a dbus-error, try the old error */
1588 switch (error_code) {
1589 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1590 error = _("offline");
1592 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1593 error = _("invalid contact");
1595 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1596 error = _("permission denied");
1598 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1599 error = _("too long message");
1601 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1602 error = _("not implemented");
1604 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1606 error = _("unknown");
1611 if (message_body != NULL) {
1612 str = g_strdup_printf (_("Error sending message '%s': %s"),
1613 message_body, error);
1616 str = g_strdup_printf (_("Error sending message: %s"), error);
1619 empathy_chat_view_append_event (chat->view, str);
1624 chat_topic_label_size_allocate_cb (GtkLabel *label,
1625 GtkAllocation *allocation,
1628 EmpathyChatPriv *priv = GET_PRIV (chat);
1630 if (!gtk_label_get_line_wrap (label)) {
1631 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1632 gtk_widget_show (priv->expander_topic);
1634 gtk_widget_hide (priv->expander_topic);
1641 chat_topic_expander_activate_cb (GtkExpander *expander,
1642 GParamSpec *param_spec,
1645 EmpathyChatPriv *priv = GET_PRIV (chat);
1647 if (gtk_expander_get_expanded (expander)) {
1648 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1649 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1651 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1652 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1657 chat_subject_changed_cb (EmpathyChat *chat)
1659 EmpathyChatPriv *priv = GET_PRIV (chat);
1661 g_free (priv->subject);
1662 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1663 g_object_notify (G_OBJECT (chat), "subject");
1665 if (EMP_STR_EMPTY (priv->subject)) {
1666 gtk_widget_hide (priv->hbox_topic);
1668 gchar *markup_topic;
1671 markup_topic = empathy_add_link_markup (priv->subject);
1672 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1673 _("Topic:"), markup_topic);
1675 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1676 g_free (markup_text);
1677 g_free (markup_topic);
1679 gtk_widget_show (priv->hbox_topic);
1681 if (priv->block_events_timeout_id == 0) {
1684 if (!EMP_STR_EMPTY (priv->subject)) {
1685 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1687 str = g_strdup (_("No topic defined"));
1689 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1695 chat_title_changed_cb (EmpathyChat *chat)
1697 EmpathyChatPriv *priv = GET_PRIV (chat);
1699 g_free (priv->name);
1700 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1701 g_object_notify (G_OBJECT (chat), "name");
1705 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1709 GtkTextIter word_start = *iter;
1710 GtkTextIter word_end = *iter;
1713 if (gtk_text_iter_inside_word (&word_end) &&
1714 !gtk_text_iter_ends_word (&word_end)) {
1715 gtk_text_iter_forward_word_end (&word_end);
1720 if (gtk_text_iter_get_char (&tmp) == '\'') {
1721 gtk_text_iter_forward_char (&tmp);
1723 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1724 gtk_text_iter_forward_word_end (&word_end);
1729 if (gtk_text_iter_inside_word (&word_start) ||
1730 gtk_text_iter_ends_word (&word_start)) {
1731 if (!gtk_text_iter_starts_word (&word_start) ||
1732 gtk_text_iter_equal (&word_start, &word_end)) {
1733 gtk_text_iter_backward_word_start (&word_start);
1737 gtk_text_iter_backward_char (&tmp);
1739 if (gtk_text_iter_get_char (&tmp) == '\'') {
1740 gtk_text_iter_backward_char (&tmp);
1742 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1743 gtk_text_iter_backward_word_start (&word_start);
1748 *start = word_start;
1754 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1755 GtkTextIter *location,
1760 GtkTextIter iter, pos;
1762 /* Remove all misspelled tags in the inserted text.
1763 * This happens when text is inserted within a misspelled word. */
1764 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1765 gtk_text_iter_get_offset (location) - len);
1766 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1769 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1772 GtkTextIter start, end;
1775 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1778 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1780 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1781 gtk_text_iter_equal (&pos, &end) ||
1782 empathy_spell_check (str)) {
1783 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1785 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1790 } while (gtk_text_iter_forward_word_end (&iter) &&
1791 gtk_text_iter_compare (&iter, location) <= 0);
1795 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1800 GtkTextIter word_start, word_end;
1802 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1803 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1804 &word_start, &word_end);
1809 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1812 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1813 chat_composing_stop (chat);
1815 chat_composing_start (chat);
1820 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1825 GtkTextIter prev_pos;
1826 GtkTextIter word_start;
1827 GtkTextIter word_end;
1831 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1833 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1834 gtk_text_buffer_get_insert (buffer));
1835 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1837 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1840 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1841 !gtk_text_iter_equal (&pos, &word_end)) {
1842 str = gtk_text_buffer_get_text (buffer,
1843 &word_start, &word_end, FALSE);
1845 if (!empathy_spell_check (str)) {
1846 gtk_text_buffer_apply_tag_by_name (buffer,
1847 "misspelled", &word_start, &word_end);
1849 gtk_text_buffer_remove_tag_by_name (buffer,
1850 "misspelled", &word_start, &word_end);
1857 gtk_text_buffer_move_mark (buffer, mark, &pos);
1861 empathy_isspace_cb (gunichar c,
1864 return g_unichar_isspace (c);
1868 chat_input_key_press_event_cb (GtkWidget *widget,
1872 EmpathyChatPriv *priv;
1875 GtkWidget *text_view_sw;
1877 priv = GET_PRIV (chat);
1879 priv->most_recent_event_type = event->type;
1881 /* Catch ctrl+up/down so we can traverse messages we sent */
1882 if ((event->state & GDK_CONTROL_MASK) &&
1883 (event->keyval == GDK_KEY_Up ||
1884 event->keyval == GDK_KEY_Down)) {
1885 GtkTextBuffer *buffer;
1888 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1889 chat_input_history_update (chat, buffer);
1891 if (event->keyval == GDK_KEY_Up) {
1892 str = chat_input_history_get_next (chat);
1894 str = chat_input_history_get_prev (chat);
1897 g_signal_handlers_block_by_func (buffer,
1898 chat_input_text_buffer_changed_cb,
1900 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1901 g_signal_handlers_unblock_by_func (buffer,
1902 chat_input_text_buffer_changed_cb,
1908 /* Catch enter but not ctrl/shift-enter */
1909 if (IS_ENTER (event->keyval) &&
1910 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1913 /* This is to make sure that kinput2 gets the enter. And if
1914 * it's handled there we shouldn't send on it. This is because
1915 * kinput2 uses Enter to commit letters. See:
1916 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1919 view = GTK_TEXT_VIEW (chat->input_text_view);
1920 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1921 gtk_text_view_reset_im_context (view);
1925 chat_input_text_view_send (chat);
1929 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1931 if (IS_ENTER (event->keyval) &&
1932 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1933 /* Newline for shift/control-enter. */
1936 if (!(event->state & GDK_CONTROL_MASK) &&
1937 event->keyval == GDK_KEY_Page_Up) {
1938 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1939 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1942 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1943 event->keyval == GDK_KEY_Page_Down) {
1944 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1945 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1946 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1947 gtk_adjustment_set_value (adj, val);
1950 if (event->keyval == GDK_KEY_Escape) {
1951 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1953 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1954 event->keyval == GDK_KEY_Tab) {
1955 GtkTextBuffer *buffer;
1956 GtkTextIter start, current;
1957 gchar *nick, *completed;
1958 GList *list, *completed_list;
1959 gboolean is_start_of_buffer;
1961 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1962 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1964 /* Get the start of the nick to complete. */
1965 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1966 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1967 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1969 is_start_of_buffer = gtk_text_iter_is_start (&start);
1971 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1972 g_completion_add_items (priv->completion, list);
1974 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1975 completed_list = g_completion_complete (priv->completion,
1984 GString *message = NULL;
1987 gtk_text_buffer_delete (buffer, &start, ¤t);
1989 len = g_list_length (completed_list);
1992 /* If we only have one hit, use that text
1993 * instead of the text in completed since the
1994 * completed text will use the typed string
1995 * which might be cased all wrong.
1998 text = empathy_contact_get_alias (completed_list->data);
2002 /* Print all hits to the scrollback view, so the
2003 * user knows what possibilities he has.
2006 message = g_string_new ("");
2007 for (l = completed_list; l != NULL; l = l->next) {
2008 g_string_append (message, empathy_contact_get_alias (l->data));
2009 g_string_append (message, " - ");
2011 empathy_chat_view_append_event (chat->view, message->str);
2012 g_string_free (message, TRUE);
2015 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2017 if (len == 1 && is_start_of_buffer) {
2018 gchar *complete_char;
2020 complete_char = g_settings_get_string (
2021 priv->gsettings_chat,
2022 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2024 if (complete_char != NULL) {
2025 gtk_text_buffer_insert_at_cursor (buffer,
2027 strlen (complete_char));
2028 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2029 g_free (complete_char);
2036 g_completion_clear_items (priv->completion);
2038 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2048 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2052 gtk_widget_grab_focus (chat->input_text_view);
2058 chat_input_realize_cb (GtkWidget *widget,
2061 DEBUG ("Setting focus to the input text view");
2062 if (gtk_widget_is_sensitive (widget)) {
2063 gtk_widget_grab_focus (widget);
2068 chat_input_has_focus_notify_cb (GtkWidget *widget,
2072 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2076 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2077 EmpathySmiley *smiley,
2080 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2081 GtkTextBuffer *buffer;
2084 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2086 gtk_text_buffer_get_end_iter (buffer, &iter);
2087 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2089 gtk_text_buffer_get_end_iter (buffer, &iter);
2090 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2101 static EmpathyChatSpell *
2102 chat_spell_new (EmpathyChat *chat,
2107 EmpathyChatSpell *chat_spell;
2109 chat_spell = g_slice_new0 (EmpathyChatSpell);
2111 chat_spell->chat = g_object_ref (chat);
2112 chat_spell->word = g_strdup (word);
2113 chat_spell->start = start;
2114 chat_spell->end = end;
2120 chat_spell_free (EmpathyChatSpell *chat_spell)
2122 g_object_unref (chat_spell->chat);
2123 g_free (chat_spell->word);
2124 g_slice_free (EmpathyChatSpell, chat_spell);
2128 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2129 EmpathyChatSpell *chat_spell)
2131 empathy_chat_correct_word (chat_spell->chat,
2132 &(chat_spell->start),
2134 gtk_menu_item_get_label (menu_item));
2139 chat_spelling_build_suggestions_menu (const gchar *code,
2140 EmpathyChatSpell *chat_spell)
2142 GList *suggestions, *l;
2143 GtkWidget *menu, *menu_item;
2145 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2146 if (suggestions == NULL)
2149 menu = gtk_menu_new ();
2150 for (l = suggestions; l; l = l->next) {
2151 menu_item = gtk_menu_item_new_with_label (l->data);
2152 g_signal_connect (G_OBJECT (menu_item), "activate",
2153 G_CALLBACK (chat_spelling_menu_activate_cb),
2155 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2157 empathy_spell_free_suggestions (suggestions);
2159 gtk_widget_show_all (menu);
2165 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2167 GtkWidget *menu, *submenu, *item;
2170 codes = empathy_spell_get_enabled_language_codes ();
2171 g_assert (codes != NULL);
2173 if (g_list_length (codes) > 1) {
2174 menu = gtk_menu_new ();
2176 for (l = codes; l; l = l->next) {
2177 const gchar *code, *name;
2180 name = empathy_spell_get_language_name (code);
2184 item = gtk_image_menu_item_new_with_label (name);
2186 submenu = chat_spelling_build_suggestions_menu (
2188 if (submenu == NULL)
2189 gtk_widget_set_sensitive (item, FALSE);
2191 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2193 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2196 menu = chat_spelling_build_suggestions_menu (codes->data,
2199 menu = gtk_menu_new ();
2200 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2201 gtk_widget_set_sensitive (item, FALSE);
2202 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2205 g_list_free (codes);
2207 gtk_widget_show_all (menu);
2218 static EmpathyChatWord *
2219 chat_word_new (EmpathyChat *chat,
2223 EmpathyChatWord *chat_word;
2225 chat_word = g_slice_new0 (EmpathyChatWord);
2227 chat_word->chat = g_object_ref (chat);
2228 chat_word->word = g_strdup (word);
2229 chat_word->code = g_strdup (code);
2235 chat_word_free (EmpathyChatWord *chat_word)
2237 g_object_unref (chat_word->chat);
2238 g_free (chat_word->word);
2239 g_free (chat_word->code);
2240 g_slice_free (EmpathyChatWord, chat_word);
2244 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2245 EmpathyChatWord *chat_word)
2247 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2249 empathy_spell_add_to_dictionary (chat_word->code,
2251 priv->update_misspelled_words_id =
2252 g_idle_add (update_misspelled_words, chat_word->chat);
2256 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2258 GtkWidget *menu, *item, *lang_item, *image;
2261 const gchar *code, *name;
2262 EmpathyChatWord *chat_word;
2264 codes = empathy_spell_get_enabled_language_codes ();
2265 g_assert (codes != NULL);
2266 if (g_list_length (codes) > 1) {
2267 /* translators: %s is the selected word */
2268 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2270 item = gtk_image_menu_item_new_with_mnemonic (label);
2272 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2273 GTK_ICON_SIZE_MENU);
2274 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2277 menu = gtk_menu_new ();
2279 for (l = codes; l; l = l->next) {
2281 name = empathy_spell_get_language_name (code);
2285 lang_item = gtk_image_menu_item_new_with_label (name);
2287 chat_word= chat_word_new (chat_spell->chat,
2288 chat_spell->word, code);
2289 g_object_set_data_full (G_OBJECT (lang_item),
2290 "chat-word", chat_word,
2291 (GDestroyNotify) chat_word_free);
2293 g_signal_connect (G_OBJECT (lang_item), "activate",
2294 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2296 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2298 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2301 name = empathy_spell_get_language_name (code);
2302 g_assert (name != NULL);
2303 /* translators: first %s is the selected word,
2304 * second %s is the language name of the target dictionary */
2305 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2306 chat_spell->word, name);
2307 item = gtk_image_menu_item_new_with_mnemonic (label);
2309 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2310 GTK_ICON_SIZE_MENU);
2311 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2313 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2315 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2316 (GDestroyNotify) chat_word_free);
2318 g_signal_connect (G_OBJECT (item), "activate",
2319 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2322 g_list_free (codes);
2324 gtk_widget_show_all (item);
2330 chat_text_send_cb (GtkMenuItem *menuitem,
2333 chat_input_text_view_send (chat);
2337 chat_input_button_press_event_cb (GtkTextView *view,
2338 GdkEventButton *event,
2341 EmpathyChatPriv *priv = GET_PRIV (chat);
2343 priv->most_recent_event_type = event->type;
2349 chat_input_populate_popup_cb (GtkTextView *view,
2353 EmpathyChatPriv *priv = GET_PRIV (chat);
2354 GtkTextBuffer *buffer;
2355 GtkTextTagTable *table;
2358 GtkTextIter iter, start, end;
2361 EmpathyChatSpell *chat_spell;
2362 GtkWidget *spell_menu;
2363 GtkWidget *spell_item;
2364 EmpathySmileyManager *smiley_manager;
2365 GtkWidget *smiley_menu;
2368 buffer = gtk_text_view_get_buffer (view);
2370 /* Add the emoticon menu. */
2371 item = gtk_separator_menu_item_new ();
2372 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2373 gtk_widget_show (item);
2375 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2376 image = gtk_image_new_from_icon_name ("face-smile",
2377 GTK_ICON_SIZE_MENU);
2378 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2379 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2380 gtk_widget_show (item);
2382 smiley_manager = empathy_smiley_manager_dup_singleton ();
2383 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2384 chat_insert_smiley_activate_cb,
2386 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2387 g_object_unref (smiley_manager);
2389 /* Add the Send menu item. */
2390 gtk_text_buffer_get_bounds (buffer, &start, &end);
2391 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2392 if (!EMP_STR_EMPTY (str)) {
2393 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2394 g_signal_connect (G_OBJECT (item), "activate",
2395 G_CALLBACK (chat_text_send_cb), chat);
2396 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2397 gtk_widget_show (item);
2401 /* Add the spell check menu item. */
2402 table = gtk_text_buffer_get_tag_table (buffer);
2403 tag = gtk_text_tag_table_lookup (table, "misspelled");
2405 switch (priv->most_recent_event_type) {
2406 case GDK_BUTTON_PRESS:
2407 /* get the location from the pointer */
2408 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2409 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2410 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2412 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2413 GTK_TEXT_WINDOW_WIDGET,
2416 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2421 g_warn_if_reached ();
2422 /* assume the KEY_PRESS case */
2425 /* get the location from the cursor */
2426 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2427 gtk_text_buffer_get_insert (buffer));
2433 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2434 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2436 str = gtk_text_buffer_get_text (buffer,
2437 &start, &end, FALSE);
2439 if (!EMP_STR_EMPTY (str)) {
2440 chat_spell = chat_spell_new (chat, str, start, end);
2441 g_object_set_data_full (G_OBJECT (menu),
2442 "chat-spell", chat_spell,
2443 (GDestroyNotify) chat_spell_free);
2445 item = gtk_separator_menu_item_new ();
2446 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2447 gtk_widget_show (item);
2449 /* Spelling suggestions */
2450 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2451 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2452 GTK_ICON_SIZE_MENU);
2453 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2455 spell_menu = chat_spelling_build_menu (chat_spell);
2456 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2459 spell_item = gtk_separator_menu_item_new ();
2460 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2461 gtk_widget_show (spell_item);
2463 /* Add to dictionary */
2464 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2466 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2467 gtk_widget_show (spell_item);
2469 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2470 gtk_widget_show (item);
2476 chat_log_filter (TplEvent *event,
2479 EmpathyChat *chat = user_data;
2480 EmpathyMessage *message;
2481 EmpathyChatPriv *priv = GET_PRIV (chat);
2482 const GList *pending;
2484 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2485 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2487 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2488 message = empathy_message_from_tpl_log_event (event);
2490 for (; pending; pending = g_list_next (pending)) {
2491 if (empathy_message_equal (message, pending->data)) {
2492 g_object_unref (message);
2497 g_object_unref (message);
2503 show_pending_messages (EmpathyChat *chat) {
2504 EmpathyChatPriv *priv = GET_PRIV (chat);
2505 const GList *messages, *l;
2507 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2509 if (chat->view == NULL || priv->tp_chat == NULL)
2512 if (!priv->can_show_pending)
2515 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2517 for (l = messages; l != NULL ; l = g_list_next (l)) {
2518 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2519 chat_message_received (chat, message, TRUE);
2525 got_filtered_messages_cb (GObject *manager,
2526 GAsyncResult *result,
2531 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2532 EmpathyChatPriv *priv = GET_PRIV (chat);
2533 GError *error = NULL;
2535 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2536 result, &messages, &error)) {
2537 DEBUG ("%s. Aborting.", error->message);
2538 empathy_chat_view_append_event (chat->view,
2539 _("Failed to retrieve recent logs"));
2540 g_error_free (error);
2544 for (l = messages; l; l = g_list_next (l)) {
2545 EmpathyMessage *message;
2547 g_assert (TPL_IS_EVENT (l->data));
2549 message = empathy_message_from_tpl_log_event (l->data);
2550 g_object_unref (l->data);
2552 if (empathy_message_is_edit (message)) {
2553 /* this is an edited message, create a synthetic event
2554 * using the supersedes token and
2555 * original-message-sent timestamp, that we can then
2557 EmpathyMessage *syn_msg = g_object_new (
2558 EMPATHY_TYPE_MESSAGE,
2560 "token", empathy_message_get_supersedes (message),
2561 "type", empathy_message_get_tptype (message),
2562 "timestamp", empathy_message_get_original_timestamp (message),
2563 "incoming", empathy_message_is_incoming (message),
2565 "receiver", empathy_message_get_receiver (message),
2566 "sender", empathy_message_get_sender (message),
2569 empathy_chat_view_append_message (chat->view, syn_msg,
2570 chat_should_highlight (chat, syn_msg));
2571 empathy_chat_view_edit_message (chat->view, message);
2573 g_object_unref (syn_msg);
2575 /* append the latest message */
2576 empathy_chat_view_append_message (chat->view, message,
2577 chat_should_highlight (chat, message));
2580 g_object_unref (message);
2582 g_list_free (messages);
2585 /* in case of TPL error, skip backlog and show pending messages */
2586 priv->can_show_pending = TRUE;
2587 show_pending_messages (chat);
2589 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2590 * about it in EmpathyChatPriv definition */
2591 priv->retrieving_backlogs = FALSE;
2592 empathy_chat_messages_read (chat);
2594 /* Turn back on scrolling */
2595 empathy_chat_view_scroll (chat->view, TRUE);
2599 chat_add_logs (EmpathyChat *chat)
2601 EmpathyChatPriv *priv = GET_PRIV (chat);
2608 /* Turn off scrolling temporarily */
2609 empathy_chat_view_scroll (chat->view, FALSE);
2611 /* Add messages from last conversation */
2612 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2613 target = tpl_entity_new_from_room_id (priv->id);
2615 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2617 priv->retrieving_backlogs = TRUE;
2618 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2621 TPL_EVENT_MASK_TEXT,
2625 got_filtered_messages_cb,
2628 g_object_unref (target);
2632 chat_contacts_completion_func (const gchar *s1,
2636 gchar *tmp, *nick1, *nick2;
2643 return s1 ? -1 : +1;
2646 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2647 nick1 = g_utf8_casefold (tmp, -1);
2650 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2651 nick2 = g_utf8_casefold (tmp, -1);
2654 ret = strncmp (nick1, nick2, n);
2663 build_part_message (guint reason,
2665 EmpathyContact *actor,
2666 const gchar *message)
2668 GString *s = g_string_new ("");
2669 const gchar *actor_name = NULL;
2671 if (actor != NULL) {
2672 actor_name = empathy_contact_get_alias (actor);
2675 /* Having an actor only really makes sense for a few actions... */
2677 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2678 g_string_append_printf (s, _("%s has disconnected"), name);
2680 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2681 if (actor_name != NULL) {
2682 /* translators: reverse the order of these arguments
2683 * if the kicked should come before the kicker in your locale.
2685 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2688 g_string_append_printf (s, _("%s was kicked"), name);
2691 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2692 if (actor_name != NULL) {
2693 /* translators: reverse the order of these arguments
2694 * if the banned should come before the banner in your locale.
2696 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2699 g_string_append_printf (s, _("%s was banned"), name);
2703 g_string_append_printf (s, _("%s has left the room"), name);
2706 if (!EMP_STR_EMPTY (message)) {
2707 /* Note to translators: this string is appended to
2708 * notifications like "foo has left the room", with the message
2709 * given by the user living the room. If this poses a problem,
2710 * please let us know. :-)
2712 g_string_append_printf (s, _(" (%s)"), message);
2715 return g_string_free (s, FALSE);
2719 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2720 EmpathyContact *contact,
2721 EmpathyContact *actor,
2727 EmpathyChatPriv *priv = GET_PRIV (chat);
2728 const gchar *name = empathy_contact_get_alias (contact);
2731 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2733 if (priv->block_events_timeout_id != 0)
2737 str = g_strdup_printf (_("%s has joined the room"),
2740 str = build_part_message (reason, name, actor, message);
2743 empathy_chat_view_append_event (chat->view, str);
2748 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2749 EmpathyContact *old_contact,
2750 EmpathyContact *new_contact,
2755 EmpathyChatPriv *priv = GET_PRIV (chat);
2757 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2759 if (priv->block_events_timeout_id == 0) {
2762 str = g_strdup_printf (_("%s is now known as %s"),
2763 empathy_contact_get_alias (old_contact),
2764 empathy_contact_get_alias (new_contact));
2765 empathy_chat_view_append_event (chat->view, str);
2772 chat_contacts_visible_timeout_cb (gpointer chat)
2774 EmpathyChatPriv *priv = GET_PRIV (chat);
2776 /* Relax the size request */
2777 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2779 /* Set the position of the slider. This must be done here because
2780 * GtkPaned need to know its size allocation and it will be settled only
2781 * after the gtk_window_resize () tough effect. */
2782 if (priv->contacts_width > 0) {
2783 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2784 priv->contacts_width);
2787 priv->contacts_visible_id = 0;
2793 chat_update_contacts_visibility (EmpathyChat *chat,
2796 EmpathyChatPriv *priv = GET_PRIV (chat);
2798 if (!priv->scrolled_window_contacts) {
2802 if (priv->remote_contact != NULL) {
2806 if (show && priv->contact_list_view == NULL) {
2807 EmpathyIndividualStore *store;
2809 GtkAllocation allocation;
2811 /* We are adding the contact list to the chat, we don't want the
2812 * chat view to become too small. If the chat view is already
2813 * smaller than 250 make sure that size won't change. If the
2814 * chat view is bigger the contact list will take some space on
2815 * it but we make sure the chat view don't become smaller than
2816 * 250. Relax the size request once the resize is done */
2817 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2818 min_width = MIN (allocation.width, 250);
2819 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2821 /* There is no way to know when the window resize will happen
2822 * since it is WM's decision. Let's hope it won't be longer. */
2823 if (priv->contacts_visible_id != 0)
2824 g_source_remove (priv->contacts_visible_id);
2825 priv->contacts_visible_id = g_timeout_add (500,
2826 chat_contacts_visible_timeout_cb, chat);
2828 store = EMPATHY_INDIVIDUAL_STORE (
2829 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2831 empathy_individual_store_set_show_groups (store, FALSE);
2833 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2834 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2835 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2836 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2837 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2838 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2839 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2841 empathy_individual_view_set_show_offline (
2842 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2843 empathy_individual_view_set_show_uninteresting (
2844 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2846 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2847 priv->contact_list_view);
2849 gtk_widget_show (priv->contact_list_view);
2850 gtk_widget_show (priv->scrolled_window_contacts);
2851 g_object_unref (store);
2853 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2854 gtk_widget_hide (priv->scrolled_window_contacts);
2855 if (priv->contact_list_view != NULL) {
2856 gtk_widget_destroy (priv->contact_list_view);
2857 priv->contact_list_view = NULL;
2863 empathy_chat_set_show_contacts (EmpathyChat *chat,
2866 EmpathyChatPriv *priv = GET_PRIV (chat);
2868 priv->show_contacts = show;
2870 chat_update_contacts_visibility (chat, show);
2872 g_object_notify (G_OBJECT (chat), "show-contacts");
2876 chat_self_contact_changed_cb (EmpathyChat *chat)
2878 EmpathyChatPriv *priv = GET_PRIV (chat);
2880 g_clear_object (&priv->self_contact);
2882 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2883 if (priv->self_contact != NULL) {
2884 g_object_ref (priv->self_contact);
2889 chat_remote_contact_changed_cb (EmpathyChat *chat)
2891 EmpathyChatPriv *priv = GET_PRIV (chat);
2893 if (priv->remote_contact != NULL) {
2894 g_object_unref (priv->remote_contact);
2895 priv->remote_contact = NULL;
2900 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2901 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2902 if (priv->remote_contact != NULL) {
2903 g_object_ref (priv->remote_contact);
2904 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2906 else if (priv->tp_chat != NULL) {
2907 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2910 chat_update_contacts_visibility (chat, priv->show_contacts);
2912 g_object_notify (G_OBJECT (chat), "remote-contact");
2913 g_object_notify (G_OBJECT (chat), "id");
2917 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2923 EmpathyChatPriv *priv;
2925 priv = GET_PRIV (chat);
2927 if (!priv->tp_chat) {
2931 chat_composing_remove_timeout (chat);
2932 g_object_unref (priv->tp_chat);
2933 priv->tp_chat = NULL;
2934 g_object_notify (G_OBJECT (chat), "tp-chat");
2936 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2937 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2939 chat_update_contacts_visibility (chat, FALSE);
2941 priv->unread_messages_when_offline = priv->unread_messages;
2945 update_misspelled_words (gpointer data)
2947 EmpathyChat *chat = EMPATHY_CHAT (data);
2948 EmpathyChatPriv *priv = GET_PRIV (chat);
2949 GtkTextBuffer *buffer;
2953 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2955 gtk_text_buffer_get_end_iter (buffer, &iter);
2956 length = gtk_text_iter_get_offset (&iter);
2957 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2958 NULL, length, chat);
2960 priv->update_misspelled_words_id = 0;
2966 conf_spell_checking_cb (GSettings *gsettings_chat,
2970 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2971 EmpathyChatPriv *priv = GET_PRIV (chat);
2972 gboolean spell_checker;
2973 GtkTextBuffer *buffer;
2975 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2978 spell_checker = g_settings_get_boolean (gsettings_chat,
2979 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2981 if (!empathy_spell_supported ()) {
2982 spell_checker = FALSE;
2985 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2987 if (spell_checker == priv->spell_checking_enabled) {
2988 if (spell_checker) {
2989 /* Possibly changed dictionaries,
2990 * update misspelled words. Need to do so in idle
2991 * so the spell checker is updated. */
2992 priv->update_misspelled_words_id =
2993 g_idle_add (update_misspelled_words, chat);
2999 if (spell_checker) {
3002 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3003 buffer, "notify::cursor-position",
3004 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3006 priv->insert_text_id = tp_g_signal_connect_object (
3007 buffer, "insert-text",
3008 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3009 chat, G_CONNECT_AFTER);
3010 priv->delete_range_id = tp_g_signal_connect_object (
3011 buffer, "delete-range",
3012 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3013 chat, G_CONNECT_AFTER);
3015 gtk_text_buffer_create_tag (buffer, "misspelled",
3016 "underline", PANGO_UNDERLINE_ERROR,
3019 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3020 gtk_text_buffer_get_insert (buffer));
3021 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3024 /* Mark misspelled words in the existing buffer.
3025 * Need to do so in idle so the spell checker is updated. */
3026 priv->update_misspelled_words_id =
3027 g_idle_add (update_misspelled_words, chat);
3029 GtkTextTagTable *table;
3032 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3033 priv->notify_cursor_position_id = 0;
3034 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3035 priv->insert_text_id = 0;
3036 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3037 priv->delete_range_id = 0;
3039 table = gtk_text_buffer_get_tag_table (buffer);
3040 tag = gtk_text_tag_table_lookup (table, "misspelled");
3041 gtk_text_tag_table_remove (table, tag);
3043 gtk_text_buffer_delete_mark_by_name (buffer,
3044 "previous-cursor-position");
3047 priv->spell_checking_enabled = spell_checker;
3051 save_paned_pos_timeout (gpointer data)
3053 EmpathyChat *self = data;
3056 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3058 g_settings_set_int (self->priv->gsettings_ui,
3059 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3066 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3070 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3072 if (chat->priv->save_paned_pos_id != 0)
3073 g_source_remove (chat->priv->save_paned_pos_id);
3075 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3076 save_paned_pos_timeout, chat);
3082 chat_create_ui (EmpathyChat *chat)
3084 EmpathyChatPriv *priv = GET_PRIV (chat);
3088 GtkTextBuffer *buffer;
3089 EmpathyThemeManager *theme_mgr;
3091 filename = empathy_file_lookup ("empathy-chat.ui",
3093 gui = empathy_builder_get_file (filename,
3094 "chat_widget", &priv->widget,
3095 "hpaned", &priv->hpaned,
3096 "vbox_left", &priv->vbox_left,
3097 "scrolled_window_chat", &priv->scrolled_window_chat,
3098 "scrolled_window_input", &priv->scrolled_window_input,
3099 "hbox_topic", &priv->hbox_topic,
3100 "expander_topic", &priv->expander_topic,
3101 "label_topic", &priv->label_topic,
3102 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3103 "info_bar_vbox", &priv->info_bar_vbox,
3106 empathy_builder_connect (gui, chat,
3107 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3108 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3113 /* Add message view. */
3114 theme_mgr = empathy_theme_manager_dup_singleton ();
3115 chat->view = empathy_theme_manager_create_view (theme_mgr);
3116 g_object_unref (theme_mgr);
3117 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3118 and other types, even though it's non-editable and doesn't accept any
3119 drags. This steals drag motion for anything inside the scrollbars,
3120 making drag destinations on chat windows far less useful.
3122 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3123 g_signal_connect (chat->view, "focus_in_event",
3124 G_CALLBACK (chat_text_view_focus_in_event_cb),
3126 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3127 GTK_WIDGET (chat->view));
3128 gtk_widget_show (GTK_WIDGET (chat->view));
3130 /* Add input GtkTextView */
3131 chat->input_text_view = empathy_input_text_view_new ();
3132 g_signal_connect (chat->input_text_view, "notify::has-focus",
3133 G_CALLBACK (chat_input_has_focus_notify_cb),
3135 g_signal_connect (chat->input_text_view, "key-press-event",
3136 G_CALLBACK (chat_input_key_press_event_cb),
3138 g_signal_connect (chat->input_text_view, "realize",
3139 G_CALLBACK (chat_input_realize_cb),
3141 g_signal_connect (chat->input_text_view, "button-press-event",
3142 G_CALLBACK (chat_input_button_press_event_cb),
3144 g_signal_connect (chat->input_text_view, "populate-popup",
3145 G_CALLBACK (chat_input_populate_popup_cb),
3147 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3148 tp_g_signal_connect_object (buffer, "changed",
3149 G_CALLBACK (chat_input_text_buffer_changed_cb),
3151 tp_g_signal_connect_object (priv->gsettings_chat,
3152 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3153 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3154 conf_spell_checking_cb (priv->gsettings_chat,
3155 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3156 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3157 chat->input_text_view);
3158 gtk_widget_show (chat->input_text_view);
3160 /* Add the (invisible) search bar */
3161 priv->search_bar = empathy_search_bar_new (chat->view);
3162 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3165 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3167 /* Initialy hide the topic, will be shown if not empty */
3168 gtk_widget_hide (priv->hbox_topic);
3170 g_signal_connect (priv->hpaned, "notify::position",
3171 G_CALLBACK (chat_hpaned_pos_changed_cb),
3174 /* Set widget focus order */
3175 list = g_list_append (NULL, priv->search_bar);
3176 list = g_list_append (list, priv->scrolled_window_input);
3177 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3180 list = g_list_append (NULL, priv->vbox_left);
3181 list = g_list_append (list, priv->scrolled_window_contacts);
3182 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3185 list = g_list_append (NULL, priv->hpaned);
3186 list = g_list_append (list, priv->hbox_topic);
3187 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3190 /* Add the main widget in the chat widget */
3191 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3192 g_object_unref (gui);
3196 chat_finalize (GObject *object)
3199 EmpathyChatPriv *priv;
3201 chat = EMPATHY_CHAT (object);
3202 priv = GET_PRIV (chat);
3204 DEBUG ("Finalized: %p", object);
3206 if (priv->update_misspelled_words_id != 0)
3207 g_source_remove (priv->update_misspelled_words_id);
3209 if (priv->save_paned_pos_id != 0)
3210 g_source_remove (priv->save_paned_pos_id);
3212 if (priv->contacts_visible_id != 0)
3213 g_source_remove (priv->contacts_visible_id);
3215 g_object_unref (priv->gsettings_chat);
3216 g_object_unref (priv->gsettings_ui);
3218 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3219 g_list_free (priv->input_history);
3221 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3222 g_list_free (priv->compositors);
3224 chat_composing_remove_timeout (chat);
3226 g_object_unref (priv->account_manager);
3227 g_object_unref (priv->log_manager);
3229 if (priv->tp_chat) {
3230 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3231 chat_invalidated_cb, chat);
3232 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3233 chat_message_received_cb, chat);
3234 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3235 chat_message_acknowledged_cb, chat);
3236 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3237 chat_send_error_cb, chat);
3238 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3239 chat_state_changed_cb, chat);
3240 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3241 chat_members_changed_cb, chat);
3242 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3243 chat_self_contact_changed_cb, chat);
3244 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3245 chat_remote_contact_changed_cb, chat);
3246 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3247 chat_title_changed_cb, chat);
3248 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3249 chat_subject_changed_cb, chat);
3250 empathy_tp_chat_leave (priv->tp_chat, "");
3251 g_object_unref (priv->tp_chat);
3253 if (priv->account) {
3254 g_object_unref (priv->account);
3256 if (priv->self_contact) {
3257 g_object_unref (priv->self_contact);
3259 if (priv->remote_contact) {
3260 g_object_unref (priv->remote_contact);
3263 if (priv->block_events_timeout_id) {
3264 g_source_remove (priv->block_events_timeout_id);
3268 g_free (priv->name);
3269 g_free (priv->subject);
3270 g_completion_free (priv->completion);
3272 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3276 chat_constructed (GObject *object)
3278 EmpathyChat *chat = EMPATHY_CHAT (object);
3279 EmpathyChatPriv *priv = GET_PRIV (chat);
3281 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3282 /* First display logs from the logger and then display pending messages */
3283 chat_add_logs (chat);
3286 /* Just display pending messages for rooms */
3287 priv->can_show_pending = TRUE;
3288 show_pending_messages (chat);
3293 empathy_chat_class_init (EmpathyChatClass *klass)
3295 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3297 object_class->finalize = chat_finalize;
3298 object_class->get_property = chat_get_property;
3299 object_class->set_property = chat_set_property;
3300 object_class->constructed = chat_constructed;
3302 g_object_class_install_property (object_class,
3304 g_param_spec_object ("tp-chat",
3306 "The tp chat object",
3307 EMPATHY_TYPE_TP_CHAT,
3310 G_PARAM_STATIC_STRINGS));
3311 g_object_class_install_property (object_class,
3313 g_param_spec_object ("account",
3314 "Account of the chat",
3315 "The account of the chat",
3318 G_PARAM_STATIC_STRINGS));
3319 g_object_class_install_property (object_class,
3321 g_param_spec_string ("id",
3323 "The id of the chat",
3326 G_PARAM_STATIC_STRINGS));
3327 g_object_class_install_property (object_class,
3329 g_param_spec_string ("name",
3331 "The name of the chat",
3334 G_PARAM_STATIC_STRINGS));
3335 g_object_class_install_property (object_class,
3337 g_param_spec_string ("subject",
3339 "The subject or topic of the chat",
3342 G_PARAM_STATIC_STRINGS));
3343 g_object_class_install_property (object_class,
3344 PROP_REMOTE_CONTACT,
3345 g_param_spec_object ("remote-contact",
3346 "The remote contact",
3347 "The remote contact is any",
3348 EMPATHY_TYPE_CONTACT,
3350 G_PARAM_STATIC_STRINGS));
3351 g_object_class_install_property (object_class,
3353 g_param_spec_boolean ("show-contacts",
3354 "Contacts' visibility",
3355 "The visibility of the contacts' list",
3358 G_PARAM_STATIC_STRINGS));
3360 g_object_class_install_property (object_class,
3362 g_param_spec_boolean ("sms-channel",
3364 "TRUE if this channel is for sending SMSes",
3366 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3368 g_object_class_install_property (object_class,
3369 PROP_N_MESSAGES_SENDING,
3370 g_param_spec_uint ("n-messages-sending",
3371 "Num Messages Sending",
3372 "The number of messages being sent",
3374 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3376 g_object_class_install_property (object_class,
3377 PROP_NB_UNREAD_MESSAGES,
3378 g_param_spec_uint ("nb-unread-messages",
3379 "Num Unread Messages",
3380 "The number of unread messages",
3382 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3384 signals[COMPOSING] =
3385 g_signal_new ("composing",
3386 G_OBJECT_CLASS_TYPE (object_class),
3390 g_cclosure_marshal_generic,
3395 * EmpathyChat::new-message:
3396 * @self: the #EmpathyChat
3397 * @message: the new message
3398 * @pending: whether the message was in the pending queue when @self
3400 * @should_highlight: %TRUE if the message mentions the local user
3402 signals[NEW_MESSAGE] =
3403 g_signal_new ("new-message",
3404 G_OBJECT_CLASS_TYPE (object_class),
3408 g_cclosure_marshal_generic,
3410 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3412 signals[PART_COMMAND_ENTERED] =
3413 g_signal_new ("part-command-entered",
3414 G_OBJECT_CLASS_TYPE (object_class),
3418 g_cclosure_marshal_generic,
3422 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3426 chat_block_events_timeout_cb (gpointer data)
3428 EmpathyChatPriv *priv = GET_PRIV (data);
3430 priv->block_events_timeout_id = 0;
3436 account_manager_prepared_cb (GObject *source_object,
3437 GAsyncResult *result,
3440 GList *accounts, *l;
3441 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3442 EmpathyChat *chat = user_data;
3443 GError *error = NULL;
3445 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3446 DEBUG ("Failed to prepare the account manager: %s", error->message);
3447 g_error_free (error);
3451 accounts = tp_account_manager_get_valid_accounts (account_manager);
3453 for (l = accounts; l != NULL; l = l->next) {
3454 TpAccount *account = l->data;
3455 tp_g_signal_connect_object (account, "status-changed",
3456 G_CALLBACK (chat_new_connection_cb),
3460 g_list_free (accounts);
3464 empathy_chat_init (EmpathyChat *chat)
3466 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3467 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3470 priv->log_manager = tpl_log_manager_dup_singleton ();
3471 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3472 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3474 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3475 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3476 priv->input_history = NULL;
3477 priv->input_history_current = NULL;
3478 priv->account_manager = tp_account_manager_dup ();
3480 tp_proxy_prepare_async (priv->account_manager, NULL,
3481 account_manager_prepared_cb, chat);
3483 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3484 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3486 /* Block events for some time to avoid having "has come online" or
3487 * "joined" messages. */
3488 priv->block_events_timeout_id =
3489 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3491 /* Add nick name completion */
3492 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3493 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3495 chat_create_ui (chat);
3499 empathy_chat_new (EmpathyTpChat *tp_chat)
3501 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3505 empathy_chat_get_tp_chat (EmpathyChat *chat)
3507 EmpathyChatPriv *priv = GET_PRIV (chat);
3509 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3511 return priv->tp_chat;
3517 GtkWidget *info_bar;
3527 passwd_remember_button_cb (GtkButton *button,
3530 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3534 passwd_not_now_button_cb (GtkButton *button,
3537 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3541 remember_password_infobar_response_cb (GtkWidget *info_bar,
3545 EmpathyChatPriv *priv = GET_PRIV (data->self);
3547 if (response_id == GTK_RESPONSE_OK) {
3548 DEBUG ("Saving room password");
3549 empathy_keyring_set_room_password_async (priv->account,
3550 empathy_tp_chat_get_id (priv->tp_chat),
3555 gtk_widget_destroy (info_bar);
3556 g_free (data->password);
3557 g_slice_free (PasswordData, data);
3561 chat_prompt_to_save_password (EmpathyChat *self,
3564 GtkWidget *content_area;
3571 /* save the password in case it needs to be saved */
3572 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3574 /* Remove all previous widgets */
3575 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3576 gtk_container_forall (GTK_CONTAINER (content_area),
3577 (GtkCallback) gtk_widget_destroy, NULL);
3578 data->button = NULL;
3581 data->spinner = NULL;
3583 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3584 GTK_MESSAGE_QUESTION);
3586 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3587 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3590 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3591 GTK_ICON_SIZE_DIALOG);
3592 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3595 label = gtk_label_new (_("Would you like to store this password?"));
3596 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3598 /* Add 'Remember' button */
3599 alig = gtk_alignment_new (0, 0.5, 1, 0);
3601 button = gtk_button_new_with_label (_("Remember"));
3602 gtk_container_add (GTK_CONTAINER (alig), button);
3603 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3605 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3608 /* Add 'Not now' button */
3609 alig = gtk_alignment_new (0, 0.5, 1, 0);
3611 button = gtk_button_new_with_label (_("Not now"));
3612 gtk_container_add (GTK_CONTAINER (alig), button);
3613 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3615 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3619 g_signal_handler_disconnect (data->info_bar, data->response_id);
3620 g_signal_connect (data->info_bar, "response",
3621 G_CALLBACK (remember_password_infobar_response_cb), data);
3623 gtk_widget_show_all (data->info_bar);
3627 provide_password_cb (GObject *tp_chat,
3631 PasswordData *data = user_data;
3632 EmpathyChat *self = data->self;
3633 EmpathyChatPriv *priv = GET_PRIV (self);
3634 GError *error = NULL;
3636 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3638 DEBUG ("error: %s", error->message);
3639 /* FIXME: what should we do if that's another error? Close the channel?
3640 * Display the raw D-Bus error to the user isn't very useful */
3641 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3643 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3644 gtk_widget_set_sensitive (data->entry, TRUE);
3645 gtk_widget_grab_focus (data->entry);
3648 gtk_info_bar_set_message_type (
3649 GTK_INFO_BAR (data->info_bar),
3653 gtk_widget_set_sensitive (data->button, TRUE);
3654 gtk_button_set_label (GTK_BUTTON (data->button),
3658 gtk_label_set_text (GTK_LABEL (data->label),
3659 _("Wrong password; please try again:"));
3662 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3663 gtk_widget_hide (data->spinner);
3665 g_error_free (error);
3669 if (empathy_keyring_is_available ()) {
3670 /* ask whether they want to save the password */
3671 chat_prompt_to_save_password (self, data);
3673 /* Get rid of the password info bar finally */
3674 gtk_widget_destroy (data->info_bar);
3675 g_slice_free (PasswordData, data);
3679 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3680 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3681 gtk_widget_grab_focus (self->input_text_view);
3685 password_infobar_response_cb (GtkWidget *info_bar,
3689 EmpathyChatPriv *priv = GET_PRIV (data->self);
3690 const gchar *password;
3692 if (response_id != GTK_RESPONSE_OK) {
3693 gtk_widget_destroy (info_bar);
3694 g_slice_free (PasswordData, data);
3698 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3700 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3701 provide_password_cb, data);
3703 gtk_widget_set_sensitive (data->button, FALSE);
3704 gtk_widget_set_sensitive (data->entry, FALSE);
3706 gtk_spinner_start (GTK_SPINNER (data->spinner));
3707 gtk_widget_show (data->spinner);
3711 password_entry_activate_cb (GtkWidget *entry,
3714 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3718 passwd_join_button_cb (GtkButton *button,
3721 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3725 clear_icon_released_cb (GtkEntry *entry,
3726 GtkEntryIconPosition icon_pos,
3730 gtk_entry_set_text (entry, "");
3734 password_entry_changed_cb (GtkEditable *entry,
3739 str = gtk_entry_get_text (GTK_ENTRY (entry));
3741 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3742 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3746 infobar_chat_invalidated_cb (TpProxy *proxy,
3750 gpointer password_infobar)
3752 /* Destroy the password infobar whenever a channel is invalidated
3753 * so we don't have multiple infobars when the MUC is rejoined */
3754 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3758 display_password_info_bar (EmpathyChat *self)
3760 EmpathyChatPriv *priv = GET_PRIV (self);
3761 GtkWidget *info_bar;
3762 GtkWidget *content_area;
3772 data = g_slice_new0 (PasswordData);
3774 info_bar = gtk_info_bar_new ();
3775 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3776 GTK_MESSAGE_QUESTION);
3778 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3780 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3781 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3784 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3785 GTK_ICON_SIZE_DIALOG);
3786 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3789 label = gtk_label_new (_("This room is protected by a password:"));
3790 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3792 /* Add password entry */
3793 entry = gtk_entry_new ();
3794 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3795 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3797 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3798 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3799 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3800 GTK_ENTRY_ICON_SECONDARY, FALSE);
3802 g_signal_connect (entry, "icon-release",
3803 G_CALLBACK (clear_icon_released_cb), data);
3804 g_signal_connect (entry, "changed",
3805 G_CALLBACK (password_entry_changed_cb), data);
3807 g_signal_connect (entry, "activate",
3808 G_CALLBACK (password_entry_activate_cb), data);
3810 /* Focus the password entry once it's realized */
3811 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3813 /* Add 'Join' button */
3814 alig = gtk_alignment_new (0, 0.5, 1, 0);
3816 button = gtk_button_new_with_label (_("Join"));
3817 gtk_container_add (GTK_CONTAINER (alig), button);
3818 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3820 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3824 spinner = gtk_spinner_new ();
3825 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3827 /* Save some data for messing around with later */
3829 data->info_bar = info_bar;
3830 data->button = button;
3831 data->label = label;
3832 data->entry = entry;
3833 data->spinner = spinner;
3835 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3837 gtk_widget_show_all (hbox);
3839 tp_g_signal_connect_object (priv->tp_chat,
3840 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3843 data->response_id = g_signal_connect (info_bar, "response",
3844 G_CALLBACK (password_infobar_response_cb), data);
3846 gtk_widget_show_all (info_bar);
3847 /* ... but hide the spinner */
3848 gtk_widget_hide (spinner);
3850 /* prevent the user from typing anything */
3851 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3855 provide_saved_password_cb (GObject *tp_chat,
3859 EmpathyChat *self = user_data;
3860 EmpathyChatPriv *priv = GET_PRIV (self);
3861 GError *error = NULL;
3863 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3865 DEBUG ("error: %s", error->message);
3866 /* FIXME: what should we do if that's another error? Close the channel?
3867 * Display the raw D-Bus error to the user isn't very useful */
3868 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3869 display_password_info_bar (self);
3870 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3872 g_error_free (error);
3877 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3878 gtk_widget_grab_focus (self->input_text_view);
3882 chat_room_got_password_cb (GObject *source,
3883 GAsyncResult *result,
3886 EmpathyChat *self = user_data;
3887 EmpathyChatPriv *priv = GET_PRIV (self);
3888 const gchar *password;
3889 GError *error = NULL;
3891 password = empathy_keyring_get_room_password_finish (priv->account,
3894 if (error != NULL) {
3895 DEBUG ("Couldn't get room password: %s\n", error->message);
3896 g_clear_error (&error);
3898 display_password_info_bar (self);
3899 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3903 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3904 provide_saved_password_cb, self);
3908 chat_password_needed_changed_cb (EmpathyChat *self)
3910 EmpathyChatPriv *priv = GET_PRIV (self);
3912 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3913 empathy_keyring_get_room_password_async (priv->account,
3914 empathy_tp_chat_get_id (priv->tp_chat),
3915 chat_room_got_password_cb, self);
3920 chat_sms_channel_changed_cb (EmpathyChat *self)
3922 EmpathyChatPriv *priv = GET_PRIV (self);
3924 priv->sms_channel = tp_text_channel_is_sms_channel (
3925 (TpTextChannel *) priv->tp_chat);
3926 g_object_notify (G_OBJECT (self), "sms-channel");
3930 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3932 g_object_notify (G_OBJECT (self), "n-messages-sending");
3936 empathy_chat_set_tp_chat (EmpathyChat *chat,
3937 EmpathyTpChat *tp_chat)
3939 EmpathyChatPriv *priv = GET_PRIV (chat);
3941 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3942 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3944 if (priv->tp_chat) {
3948 if (priv->account) {
3949 g_object_unref (priv->account);
3952 priv->tp_chat = g_object_ref (tp_chat);
3953 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3955 g_signal_connect (tp_chat, "invalidated",
3956 G_CALLBACK (chat_invalidated_cb),
3958 g_signal_connect (tp_chat, "message-received-empathy",
3959 G_CALLBACK (chat_message_received_cb),
3961 g_signal_connect (tp_chat, "message_acknowledged",
3962 G_CALLBACK (chat_message_acknowledged_cb),
3964 g_signal_connect (tp_chat, "send-error",
3965 G_CALLBACK (chat_send_error_cb),
3967 g_signal_connect (tp_chat, "chat-state-changed-empathy",
3968 G_CALLBACK (chat_state_changed_cb),
3970 g_signal_connect (tp_chat, "members-changed",
3971 G_CALLBACK (chat_members_changed_cb),
3973 g_signal_connect (tp_chat, "member-renamed",
3974 G_CALLBACK (chat_member_renamed_cb),
3976 g_signal_connect_swapped (tp_chat, "notify::self-contact",
3977 G_CALLBACK (chat_self_contact_changed_cb),
3979 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3980 G_CALLBACK (chat_remote_contact_changed_cb),
3982 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3983 G_CALLBACK (chat_password_needed_changed_cb),
3985 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
3986 G_CALLBACK (chat_sms_channel_changed_cb),
3988 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3989 G_CALLBACK (chat_n_messages_sending_changed_cb),
3991 g_signal_connect_swapped (tp_chat, "notify::title",
3992 G_CALLBACK (chat_title_changed_cb),
3994 g_signal_connect_swapped (tp_chat, "notify::subject",
3995 G_CALLBACK (chat_subject_changed_cb),
3998 /* Get initial value of properties */
3999 chat_sms_channel_changed_cb (chat);
4000 chat_self_contact_changed_cb (chat);
4001 chat_remote_contact_changed_cb (chat);
4002 chat_title_changed_cb (chat);
4003 chat_subject_changed_cb (chat);
4005 if (chat->input_text_view) {
4006 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4007 if (priv->block_events_timeout_id == 0) {
4008 empathy_chat_view_append_event (chat->view, _("Connected"));
4012 g_object_notify (G_OBJECT (chat), "tp-chat");
4013 g_object_notify (G_OBJECT (chat), "id");
4014 g_object_notify (G_OBJECT (chat), "account");
4016 /* This is a noop when tp-chat is set at object construction time and causes
4017 * the pending messages to be show when it's set on the object after it has
4019 show_pending_messages (chat);
4021 /* check if a password is needed */
4022 chat_password_needed_changed_cb (chat);
4026 empathy_chat_get_account (EmpathyChat *chat)
4028 EmpathyChatPriv *priv = GET_PRIV (chat);
4030 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4032 return priv->account;
4036 empathy_chat_get_id (EmpathyChat *chat)
4038 EmpathyChatPriv *priv = GET_PRIV (chat);
4040 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4046 empathy_chat_dup_name (EmpathyChat *chat)
4048 EmpathyChatPriv *priv = GET_PRIV (chat);
4051 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4055 if (!ret && priv->remote_contact) {
4056 ret = empathy_contact_get_alias (priv->remote_contact);
4063 ret = _("Conversation");
4065 if (priv->sms_channel)
4066 /* Translators: this string is a something like
4067 * "Escher Cat (SMS)" */
4068 return g_strdup_printf (_("%s (SMS)"), ret);
4070 return g_strdup (ret);
4074 empathy_chat_get_subject (EmpathyChat *chat)
4076 EmpathyChatPriv *priv = GET_PRIV (chat);
4078 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4080 return priv->subject;
4084 empathy_chat_get_remote_contact (EmpathyChat *chat)
4086 EmpathyChatPriv *priv = GET_PRIV (chat);
4088 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4090 return priv->remote_contact;
4094 empathy_chat_get_contact_menu (EmpathyChat *chat)
4096 EmpathyChatPriv *priv = GET_PRIV (chat);
4097 GtkWidget *menu = NULL;
4098 FolksIndividual *individual;
4101 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4103 if (priv->remote_contact == NULL)
4106 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4107 if (contact == NULL)
4110 individual = empathy_create_individual_from_tp_contact (contact);
4111 if (individual == NULL)
4114 menu = empathy_individual_menu_new (individual,
4115 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4116 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4117 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4118 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4120 g_object_unref (individual);
4126 empathy_chat_clear (EmpathyChat *chat)
4128 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4130 empathy_chat_view_clear (chat->view);
4134 empathy_chat_scroll_down (EmpathyChat *chat)
4136 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4138 empathy_chat_view_scroll_down (chat->view);
4142 empathy_chat_cut (EmpathyChat *chat)
4144 GtkTextBuffer *buffer;
4146 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4148 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4149 if (gtk_text_buffer_get_has_selection (buffer)) {
4150 GtkClipboard *clipboard;
4152 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4154 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4159 empathy_chat_copy (EmpathyChat *chat)
4161 GtkTextBuffer *buffer;
4163 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4165 if (empathy_chat_view_get_has_selection (chat->view)) {
4166 empathy_chat_view_copy_clipboard (chat->view);
4170 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4171 if (gtk_text_buffer_get_has_selection (buffer)) {
4172 GtkClipboard *clipboard;
4174 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4176 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4181 EmpathyChatPriv *priv = GET_PRIV (chat);
4183 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4190 GtkClipboard *clipboard;
4192 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4193 start = g_utf8_offset_to_pointer (topic, start_offset);
4194 end = g_utf8_offset_to_pointer (topic, end_offset);
4195 selection = g_strndup (start, end - start);
4197 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4198 gtk_clipboard_set_text (clipboard, selection, -1);
4206 empathy_chat_paste (EmpathyChat *chat)
4208 GtkTextBuffer *buffer;
4209 GtkClipboard *clipboard;
4210 EmpathyChatPriv *priv;
4212 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4214 priv = GET_PRIV (chat);
4216 if (gtk_widget_get_visible (priv->search_bar)) {
4217 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4221 if (priv->tp_chat == NULL ||
4222 !gtk_widget_is_sensitive (chat->input_text_view))
4225 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4226 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4228 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4232 empathy_chat_find (EmpathyChat *chat)
4234 EmpathyChatPriv *priv;
4236 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4238 priv = GET_PRIV (chat);
4240 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4244 empathy_chat_correct_word (EmpathyChat *chat,
4247 const gchar *new_word)
4249 GtkTextBuffer *buffer;
4251 g_return_if_fail (chat != NULL);
4252 g_return_if_fail (new_word != NULL);
4254 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4256 gtk_text_buffer_delete (buffer, start, end);
4257 gtk_text_buffer_insert (buffer, start,
4263 empathy_chat_is_room (EmpathyChat *chat)
4265 EmpathyChatPriv *priv = GET_PRIV (chat);
4267 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4269 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4273 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4275 EmpathyChatPriv *priv = GET_PRIV (self);
4277 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4279 return priv->unread_messages;
4282 /* called when the messages have been read by user */
4284 empathy_chat_messages_read (EmpathyChat *self)
4286 EmpathyChatPriv *priv = GET_PRIV (self);
4288 g_return_if_fail (EMPATHY_IS_CHAT (self));
4290 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4291 * definition. If we are still retrieving the backlogs, do not ACK */
4292 if (priv->retrieving_backlogs)
4295 if (priv->tp_chat != NULL) {
4296 tp_text_channel_ack_all_pending_messages_async (
4297 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4300 if (priv->unread_messages_when_offline > 0) {
4301 /* We can't ack those as the connection has gone away so just consider
4303 priv->unread_messages -= priv->unread_messages_when_offline;
4304 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4305 priv->unread_messages_when_offline = 0;
4309 /* Return TRUE if on of the contacts in this chat is composing */
4311 empathy_chat_is_composing (EmpathyChat *chat)
4313 return chat->priv->compositors != NULL;
4317 empathy_chat_is_sms_channel (EmpathyChat *self)
4319 EmpathyChatPriv *priv = GET_PRIV (self);
4321 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4323 return priv->sms_channel;
4327 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4329 EmpathyChatPriv *priv;
4331 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4333 priv = GET_PRIV (self);
4335 if (priv->tp_chat == NULL) {
4340 g_object_get (priv->tp_chat,
4341 "n-messages-sending", &n_messages,
4349 empathy_chat_dup_text (EmpathyChat *self)
4351 GtkTextBuffer *buffer;
4352 GtkTextIter start, end;
4354 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4356 gtk_text_buffer_get_bounds (buffer, &start, &end);
4357 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4361 empathy_chat_set_text (EmpathyChat *self,
4364 GtkTextBuffer *buffer;
4366 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4368 gtk_text_buffer_set_text (buffer, text, -1);