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;
160 /* A regex matching our own current nickname in the room, or %NULL if
161 * !empathy_chat_is_room(). */
162 GRegex *highlight_regex;
166 gchar *text; /* Original message that was specified
167 * upon entry creation. */
168 gchar *modified_text; /* Message that was modified by user.
169 * When no modifications were made, it is NULL */
175 PART_COMMAND_ENTERED,
189 PROP_N_MESSAGES_SENDING,
190 PROP_NB_UNREAD_MESSAGES,
193 static guint signals[LAST_SIGNAL] = { 0 };
195 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
197 static gboolean update_misspelled_words (gpointer data);
200 chat_get_property (GObject *object,
205 EmpathyChat *chat = EMPATHY_CHAT (object);
206 EmpathyChatPriv *priv = GET_PRIV (object);
210 g_value_set_object (value, priv->tp_chat);
213 g_value_set_object (value, priv->account);
216 g_value_take_string (value, empathy_chat_dup_name (chat));
219 g_value_set_string (value, priv->id);
222 g_value_set_string (value, priv->subject);
224 case PROP_REMOTE_CONTACT:
225 g_value_set_object (value, priv->remote_contact);
227 case PROP_SHOW_CONTACTS:
228 g_value_set_boolean (value, priv->show_contacts);
230 case PROP_SMS_CHANNEL:
231 g_value_set_boolean (value, priv->sms_channel);
233 case PROP_N_MESSAGES_SENDING:
234 g_value_set_uint (value,
235 empathy_chat_get_n_messages_sending (chat));
237 case PROP_NB_UNREAD_MESSAGES:
238 g_value_set_uint (value,
239 empathy_chat_get_nb_unread_messages (chat));
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
248 chat_set_property (GObject *object,
253 EmpathyChat *chat = EMPATHY_CHAT (object);
257 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
259 case PROP_SHOW_CONTACTS:
260 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
269 account_reconnected (EmpathyChat *chat,
272 EmpathyChatPriv *priv = GET_PRIV (chat);
274 DEBUG ("Account reconnected, request a new Text channel");
276 /* FIXME: Ideally we should ask to handle ourself the channel so we can
277 * report the error if any but this is blocked by
278 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
279 switch (priv->handle_type) {
280 case TP_HANDLE_TYPE_CONTACT:
281 if (priv->sms_channel)
282 empathy_sms_contact_id (
284 TP_USER_ACTION_TIME_NOT_USER_ACTION,
287 empathy_chat_with_contact_id (
289 TP_USER_ACTION_TIME_NOT_USER_ACTION,
292 case TP_HANDLE_TYPE_ROOM:
293 empathy_join_muc (account, priv->id,
294 TP_USER_ACTION_TIME_NOT_USER_ACTION);
296 case TP_HANDLE_TYPE_NONE:
297 case TP_HANDLE_TYPE_LIST:
298 case TP_HANDLE_TYPE_GROUP:
300 g_assert_not_reached ();
304 g_object_unref (chat);
308 chat_new_connection_cb (TpAccount *account,
312 gchar *dbus_error_name,
316 EmpathyChatPriv *priv = GET_PRIV (chat);
318 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
321 if (priv->tp_chat != NULL || account != priv->account ||
322 priv->handle_type == TP_HANDLE_TYPE_NONE ||
323 EMP_STR_EMPTY (priv->id))
328 account_reconnected (chat, account);
332 chat_composing_remove_timeout (EmpathyChat *chat)
334 EmpathyChatPriv *priv;
336 priv = GET_PRIV (chat);
338 if (priv->composing_stop_timeout_id) {
339 g_source_remove (priv->composing_stop_timeout_id);
340 priv->composing_stop_timeout_id = 0;
345 set_chate_state_cb (GObject *source,
346 GAsyncResult *result,
349 GError *error = NULL;
351 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
353 DEBUG ("Failed to set chat state: %s", error->message);
354 g_error_free (error);
359 set_chat_state (EmpathyChat *self,
360 TpChannelChatState state)
362 EmpathyChatPriv *priv = GET_PRIV (self);
364 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
365 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
368 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
369 set_chate_state_cb, self);
373 chat_composing_stop_timeout_cb (EmpathyChat *chat)
375 EmpathyChatPriv *priv;
377 priv = GET_PRIV (chat);
379 priv->composing_stop_timeout_id = 0;
380 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
386 chat_composing_start (EmpathyChat *chat)
388 EmpathyChatPriv *priv;
390 priv = GET_PRIV (chat);
392 if (priv->composing_stop_timeout_id) {
393 /* Just restart the timeout */
394 chat_composing_remove_timeout (chat);
396 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
399 priv->composing_stop_timeout_id = g_timeout_add_seconds (
400 COMPOSING_STOP_TIMEOUT,
401 (GSourceFunc) chat_composing_stop_timeout_cb,
406 chat_composing_stop (EmpathyChat *chat)
408 chat_composing_remove_timeout (chat);
409 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
413 chat_input_history_entry_cmp (InputHistoryEntry *entry,
416 if (!tp_strdiff (entry->text, text)) {
417 if (entry->modified_text != NULL) {
418 /* Modified entry and single string cannot be equal. */
426 static InputHistoryEntry *
427 chat_input_history_entry_new_with_text (const gchar *text)
429 InputHistoryEntry *entry;
430 entry = g_slice_new0 (InputHistoryEntry);
431 entry->text = g_strdup (text);
437 chat_input_history_entry_free (InputHistoryEntry *entry)
439 g_free (entry->text);
440 g_free (entry->modified_text);
441 g_slice_free (InputHistoryEntry, entry);
445 chat_input_history_entry_revert (InputHistoryEntry *entry)
447 g_free (entry->modified_text);
448 entry->modified_text = NULL;
452 chat_input_history_entry_update_text (InputHistoryEntry *entry,
457 if (!tp_strdiff (text, entry->text)) {
458 g_free (entry->modified_text);
459 entry->modified_text = NULL;
463 old = entry->modified_text;
464 entry->modified_text = g_strdup (text);
469 chat_input_history_entry_get_text (InputHistoryEntry *entry)
475 if (entry->modified_text != NULL) {
476 return entry->modified_text;
482 chat_input_history_remove_item (GList *list,
485 list = g_list_remove_link (list, item);
486 chat_input_history_entry_free (item->data);
487 g_list_free_1 (item);
492 chat_input_history_revert (EmpathyChat *chat)
494 EmpathyChatPriv *priv;
498 InputHistoryEntry *entry;
500 priv = GET_PRIV (chat);
501 list = priv->input_history;
504 DEBUG ("No input history");
508 /* Delete temporary entry */
509 if (priv->input_history_current != NULL) {
511 list = chat_input_history_remove_item (list, item1);
512 if (priv->input_history_current == item1) {
513 /* Removed temporary entry was current entry */
514 priv->input_history = list;
515 priv->input_history_current = NULL;
520 /* There is no entry to revert */
524 /* Restore the current history entry to original value */
525 item1 = priv->input_history_current;
527 chat_input_history_entry_revert (entry);
529 /* Remove restored entry if there is other occurance before this entry */
530 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
531 (GCompareFunc) chat_input_history_entry_cmp);
532 if (item2 != item1) {
533 list = chat_input_history_remove_item (list, item1);
536 /* Remove other occurance of the restored entry */
537 item2 = g_list_find_custom (item1->next,
538 chat_input_history_entry_get_text (entry),
539 (GCompareFunc) chat_input_history_entry_cmp);
541 list = chat_input_history_remove_item (list, item2);
545 priv->input_history_current = NULL;
546 priv->input_history = list;
550 chat_input_history_add (EmpathyChat *chat,
554 EmpathyChatPriv *priv;
557 InputHistoryEntry *entry;
559 priv = GET_PRIV (chat);
561 list = priv->input_history;
563 /* Remove any other occurances of this entry, if not temporary */
565 while ((item = g_list_find_custom (list, str,
566 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
567 list = chat_input_history_remove_item (list, item);
570 /* Trim the list to the last 10 items */
571 while (g_list_length (list) > 10) {
572 item = g_list_last (list);
574 list = chat_input_history_remove_item (list, item);
582 entry = chat_input_history_entry_new_with_text (str);
583 list = g_list_prepend (list, entry);
585 /* Set the list and the current item pointer */
586 priv->input_history = list;
588 priv->input_history_current = list;
591 priv->input_history_current = NULL;
596 chat_input_history_get_next (EmpathyChat *chat)
598 EmpathyChatPriv *priv;
602 priv = GET_PRIV (chat);
604 if (priv->input_history == NULL) {
605 DEBUG ("No input history, next entry is NULL");
608 g_assert (priv->input_history_current != NULL);
610 if ((item = g_list_next (priv->input_history_current)) == NULL)
612 item = priv->input_history_current;
615 msg = chat_input_history_entry_get_text (item->data);
617 DEBUG ("Returning next entry: '%s'", msg);
619 priv->input_history_current = item;
625 chat_input_history_get_prev (EmpathyChat *chat)
627 EmpathyChatPriv *priv;
631 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
633 priv = GET_PRIV (chat);
635 if (priv->input_history == NULL) {
636 DEBUG ("No input history, previous entry is NULL");
640 if (priv->input_history_current == NULL)
644 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
646 item = priv->input_history_current;
649 msg = chat_input_history_entry_get_text (item->data);
651 DEBUG ("Returning previous entry: '%s'", msg);
653 priv->input_history_current = item;
659 chat_input_history_update (EmpathyChat *chat,
660 GtkTextBuffer *buffer)
662 EmpathyChatPriv *priv;
663 GtkTextIter start, end;
665 InputHistoryEntry *entry;
667 priv = GET_PRIV (chat);
669 gtk_text_buffer_get_bounds (buffer, &start, &end);
670 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
672 if (priv->input_history_current == NULL) {
673 /* Add the current text temporarily to the history */
674 chat_input_history_add (chat, text, TRUE);
679 /* Save the changes in the history */
680 entry = priv->input_history_current->data;
681 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
682 chat_input_history_entry_update_text (entry, text);
691 } ChatCommandMsgData;
694 chat_command_msg_cb (GObject *source,
695 GAsyncResult *result,
698 ChatCommandMsgData *data = user_data;
699 GError *error = NULL;
702 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
703 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
705 if (channel == NULL) {
706 DEBUG ("Failed to get channel: %s", error->message);
707 g_error_free (error);
709 empathy_chat_view_append_event (data->chat->view,
710 _("Failed to open private chat"));
714 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
715 TpTextChannel *text = (TpTextChannel *) channel;
718 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
721 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
723 g_object_unref (msg);
726 g_object_unref (channel);
729 g_free (data->message);
730 g_slice_free (ChatCommandMsgData, data);
734 nick_command_supported (EmpathyChat *chat)
736 EmpathyChatPriv * priv = GET_PRIV (chat);
737 TpConnection *connection;
739 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
740 return tp_proxy_has_interface_by_id (connection,
741 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
745 part_command_supported (EmpathyChat *chat)
747 EmpathyChatPriv * priv = GET_PRIV (chat);
749 return tp_proxy_has_interface_by_id (priv->tp_chat,
750 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
754 chat_command_clear (EmpathyChat *chat,
757 empathy_chat_view_clear (chat->view);
761 chat_command_topic (EmpathyChat *chat,
764 EmpathyChatPriv *priv = GET_PRIV (chat);
766 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
767 empathy_chat_view_append_event (chat->view,
768 _("Topic not supported on this conversation"));
772 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
773 empathy_chat_view_append_event (chat->view,
774 _("You are not allowed to change the topic"));
778 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
782 empathy_chat_join_muc (EmpathyChat *chat,
785 EmpathyChatPriv *priv = GET_PRIV (chat);
787 empathy_join_muc (priv->account, room,
788 empathy_get_current_action_time ());
792 chat_command_join (EmpathyChat *chat,
797 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
799 /* FIXME: Ideally we should ask to handle ourself the channel so we can
800 * report the error if any but this is blocked by
801 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
802 while (rooms[i] != NULL) {
803 /* ignore empty strings */
804 if (!EMP_STR_EMPTY (rooms[i])) {
805 empathy_chat_join_muc (chat, rooms[i]);
813 chat_command_part (EmpathyChat *chat,
816 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
820 chat_command_msg_internal (EmpathyChat *chat,
821 const gchar *contact_id,
822 const gchar *message)
824 EmpathyChatPriv *priv = GET_PRIV (chat);
825 ChatCommandMsgData *data;
826 TpAccountChannelRequest *req;
829 request = tp_asv_new (
830 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
831 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
832 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
835 req = tp_account_channel_request_new (priv->account, request,
836 empathy_get_current_action_time ());
838 /* FIXME: We should probably search in members alias. But this
839 * is enough for IRC */
840 data = g_slice_new (ChatCommandMsgData);
842 data->message = g_strdup (message);
844 tp_account_channel_request_ensure_and_observe_channel_async (req,
845 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
847 g_object_unref (req);
848 g_hash_table_unref (request);
852 chat_command_query (EmpathyChat *chat,
855 /* If <message> part is not defined,
856 * strv[2] will be the terminal NULL */
857 chat_command_msg_internal (chat, strv[1], strv[2]);
861 chat_command_msg (EmpathyChat *chat,
864 chat_command_msg_internal (chat, strv[1], strv[2]);
868 callback_for_request_rename (TpProxy *proxy,
871 GObject *weak_object)
874 DEBUG ("Call to RequestRename method failed: %s",error->message);
879 chat_command_nick (EmpathyChat *chat,
882 EmpathyChatPriv *priv = GET_PRIV (chat);
885 proxy = TP_PROXY (tp_account_get_connection (priv->account));
887 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
888 strv[1], callback_for_request_rename, NULL, NULL, NULL);
892 chat_command_me (EmpathyChat *chat,
895 EmpathyChatPriv *priv = GET_PRIV (chat);
897 TpTextChannel *channel;
899 channel = (TpTextChannel *) (priv->tp_chat);
901 if (!tp_text_channel_supports_message_type (channel,
902 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
903 /* Action message are not supported, 'simulate' the action */
906 /* The TpChat can't be ready if it doesn't have the self contact */
907 g_assert (priv->self_contact != NULL);
909 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
911 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
916 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
920 empathy_tp_chat_send (priv->tp_chat, message);
921 g_object_unref (message);
925 chat_command_say (EmpathyChat *chat,
928 EmpathyChatPriv *priv = GET_PRIV (chat);
931 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
933 empathy_tp_chat_send (priv->tp_chat, message);
934 g_object_unref (message);
938 whois_got_contact_cb (TpConnection *connection,
940 TpContact * const *contacts,
941 const gchar * const *requested_ids,
942 GHashTable *failed_id_errors,
945 GObject *weak_object)
947 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
949 g_return_if_fail (n_contacts <= 1);
951 if (n_contacts == 0) {
953 gpointer key = NULL, value = NULL;
957 /* tp-glib guarantees that the contacts you requested will be
958 * in failed_id_errors regardless of whether the individual
959 * contact was invalid or the whole operation failed.
961 g_hash_table_iter_init (&iter, failed_id_errors);
962 g_hash_table_iter_next (&iter, &key, &value);
966 DEBUG ("Error getting TpContact for '%s': %s %u %s",
967 id, g_quark_to_string (id_error->domain),
968 id_error->code, id_error->message);
971 /* The specific ID failed. */
972 gchar *event = g_strdup_printf (
973 _("“%s” is not a valid contact ID"), id);
974 empathy_chat_view_append_event (chat->view, event);
977 /* Otherwise we're disconnected or something; so the window
978 * will already say ‘Disconnected’, so let's not show anything.
981 EmpathyContact *empathy_contact;
984 g_return_if_fail (contacts[0] != NULL);
985 empathy_contact = empathy_contact_dup_from_tp_contact (
988 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
989 /* If we're alive and this command is running, we'd better be
991 g_return_if_fail (window != NULL);
992 g_return_if_fail (gtk_widget_is_toplevel (window));
993 empathy_contact_information_dialog_show (empathy_contact,
994 GTK_WINDOW (window));
995 g_object_unref (empathy_contact);
1000 chat_command_whois (EmpathyChat *chat,
1003 EmpathyChatPriv *priv = GET_PRIV (chat);
1006 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1007 tp_connection_get_contacts_by_id (conn,
1008 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1009 * entered by the user (including spaces, if any). */
1010 1, (const gchar * const *) strv + 1,
1012 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1016 chat_command_whale (EmpathyChat *chat,
1019 EmpathyChatPriv *priv = GET_PRIV (chat);
1022 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1024 "•_______________•");
1025 empathy_tp_chat_send (priv->tp_chat, message);
1026 g_object_unref (message);
1030 chat_command_babywhale (EmpathyChat *chat,
1033 EmpathyChatPriv *priv = GET_PRIV (chat);
1036 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1039 empathy_tp_chat_send (priv->tp_chat, message);
1040 g_object_unref (message);
1044 chat_command_inspector (EmpathyChat *chat,
1047 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1048 empathy_theme_adium_show_inspector (
1049 EMPATHY_THEME_ADIUM (chat->view));
1053 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1055 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1058 const gchar *prefix;
1061 ChatCommandFunc func;
1062 gboolean (*is_supported)(EmpathyChat *chat);
1066 static ChatCommandItem commands[] = {
1067 {"clear", 1, 1, chat_command_clear, NULL,
1068 N_("/clear: clear all messages from the current conversation")},
1070 {"topic", 2, 2, chat_command_topic, NULL,
1071 N_("/topic <topic>: set the topic of the current conversation")},
1073 {"join", 2, 2, chat_command_join, NULL,
1074 N_("/join <chat room ID>: join a new chat room")},
1076 {"j", 2, 2, chat_command_join, NULL,
1077 N_("/j <chat room ID>: join a new chat room")},
1079 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1080 {"part", 1, 3, chat_command_part, part_command_supported,
1081 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1082 "by default the current one")},
1084 {"query", 2, 3, chat_command_query, NULL,
1085 N_("/query <contact ID> [<message>]: open a private chat")},
1087 {"msg", 3, 3, chat_command_msg, NULL,
1088 N_("/msg <contact ID> <message>: open a private chat")},
1090 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1091 N_("/nick <nickname>: change your nickname on the current server")},
1093 {"me", 2, 2, chat_command_me, NULL,
1094 N_("/me <message>: send an ACTION message to the current conversation")},
1096 {"say", 2, 2, chat_command_say, NULL,
1097 N_("/say <message>: send <message> to the current conversation. "
1098 "This is used to send a message starting with a '/'. For example: "
1099 "\"/say /join is used to join a new chat room\"")},
1101 {"whois", 2, 2, chat_command_whois, NULL,
1102 N_("/whois <contact ID>: display information about a contact")},
1104 {"help", 1, 2, chat_command_help, NULL,
1105 N_("/help [<command>]: show all supported commands. "
1106 "If <command> is defined, show its usage.")},
1108 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1110 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1111 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1115 chat_command_show_help (EmpathyChat *chat,
1116 ChatCommandItem *item)
1120 if (item->help == NULL) {
1124 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1125 empathy_chat_view_append_event (chat->view, str);
1130 chat_command_help (EmpathyChat *chat,
1135 /* If <command> part is not defined,
1136 * strv[1] will be the terminal NULL */
1137 if (strv[1] == NULL) {
1138 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1139 if (commands[i].is_supported != NULL) {
1140 if (!commands[i].is_supported (chat)) {
1144 if (commands[i].help == NULL) {
1147 empathy_chat_view_append_event (chat->view,
1148 _(commands[i].help));
1153 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1154 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1155 if (commands[i].is_supported != NULL) {
1156 if (!commands[i].is_supported (chat)) {
1160 if (commands[i].help == NULL) {
1163 chat_command_show_help (chat, &commands[i]);
1168 empathy_chat_view_append_event (chat->view,
1169 _("Unknown command"));
1173 chat_command_parse (const gchar *text, guint max_parts)
1178 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1180 array = g_ptr_array_sized_new (max_parts + 1);
1181 while (max_parts > 1) {
1184 /* Skip white spaces */
1185 while (g_ascii_isspace (*text)) {
1189 /* Search the end of this part, until first space. */
1190 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1196 item = g_strndup (text, end - text);
1197 g_ptr_array_add (array, item);
1198 DEBUG ("\tITEM: \"%s\"", item);
1204 /* Append last part if not empty */
1205 item = g_strstrip (g_strdup (text));
1206 if (!EMP_STR_EMPTY (item)) {
1207 g_ptr_array_add (array, item);
1208 DEBUG ("\tITEM: \"%s\"", item);
1213 /* Make the array NULL-terminated */
1214 g_ptr_array_add (array, NULL);
1216 return (GStrv) g_ptr_array_free (array, FALSE);
1220 has_prefix_case (const gchar *s,
1221 const gchar *prefix)
1223 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1227 chat_send (EmpathyChat *chat,
1230 EmpathyChatPriv *priv;
1234 if (EMP_STR_EMPTY (msg)) {
1238 priv = GET_PRIV (chat);
1240 chat_input_history_add (chat, msg, FALSE);
1242 if (msg[0] == '/') {
1243 gboolean second_slash = FALSE;
1244 const gchar *iter = msg + 1;
1246 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1251 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1254 c = *(msg + 1 + strlen (commands[i].prefix));
1255 if (c != '\0' && !g_ascii_isspace (c)) {
1258 if (commands[i].is_supported != NULL) {
1259 if (!commands[i].is_supported (chat)) {
1264 /* We can't use g_strsplit here because it does
1265 * not deal correctly if we have more than one space
1267 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1269 strv_len = g_strv_length (strv);
1270 if (strv_len < commands[i].min_parts ||
1271 strv_len > commands[i].max_parts) {
1272 chat_command_show_help (chat, &commands[i]);
1277 commands[i].func (chat, strv);
1282 /* Also allow messages with two slashes before the
1283 * first space, so it is possible to send a /unix/path.
1284 * This heuristic is kind of crap. */
1285 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1287 second_slash = TRUE;
1293 if (!second_slash) {
1294 empathy_chat_view_append_event (chat->view,
1295 _("Unknown command; see /help for the available"
1301 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1303 empathy_tp_chat_send (priv->tp_chat, message);
1304 g_object_unref (message);
1308 chat_input_text_view_send (EmpathyChat *chat)
1310 GtkTextBuffer *buffer;
1311 GtkTextIter start, end;
1314 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1316 gtk_text_buffer_get_bounds (buffer, &start, &end);
1317 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1319 /* clear the input field */
1320 gtk_text_buffer_set_text (buffer, "", -1);
1321 /* delete input history modifications */
1322 chat_input_history_revert (chat);
1324 chat_send (chat, msg);
1329 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1330 EmpathyContact *contact,
1331 TpChannelChatState state,
1334 EmpathyChatPriv *priv;
1336 gboolean was_composing;
1338 priv = GET_PRIV (chat);
1340 if (empathy_contact_is_user (contact)) {
1341 /* We don't care about our own chat state */
1345 was_composing = (priv->compositors != NULL);
1347 /* Find the contact in the list. After that l is the list elem or NULL */
1348 for (l = priv->compositors; l; l = l->next) {
1349 if (contact == l->data) {
1355 case TP_CHANNEL_CHAT_STATE_GONE:
1356 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1357 case TP_CHANNEL_CHAT_STATE_PAUSED:
1358 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1359 /* Contact is not composing */
1361 priv->compositors = g_list_remove_link (priv->compositors, l);
1362 g_object_unref (l->data);
1366 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1367 /* Contact is composing */
1369 priv->compositors = g_list_prepend (priv->compositors,
1370 g_object_ref (contact));
1374 g_assert_not_reached ();
1377 DEBUG ("Was composing: %s now composing: %s",
1378 was_composing ? "yes" : "no",
1379 priv->compositors ? "yes" : "no");
1381 if ((was_composing && !priv->compositors) ||
1382 (!was_composing && priv->compositors)) {
1383 /* Composing state changed */
1384 g_signal_emit (chat, signals[COMPOSING], 0,
1385 priv->compositors != NULL);
1390 get_highlight_regex_for (const gchar *name)
1393 gchar *name_esc, *pattern;
1394 GError *error = NULL;
1396 name_esc = g_regex_escape_string (name, -1);
1397 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1398 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1401 if (regex == NULL) {
1402 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1405 g_error_free (error);
1414 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1415 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1418 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1420 EmpathyChatPriv *priv = GET_PRIV (chat);
1422 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1424 if (priv->self_contact != NULL) {
1425 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1427 g_return_if_fail (alias != NULL);
1428 priv->highlight_regex = get_highlight_regex_for (alias);
1433 chat_should_highlight (EmpathyChat *chat,
1434 EmpathyMessage *message)
1436 EmpathyChatPriv *priv = GET_PRIV (chat);
1438 TpChannelTextMessageFlags flags;
1440 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1442 if (!empathy_chat_is_room (chat)) {
1446 if (!empathy_message_is_incoming (message)) {
1450 msg = empathy_message_get_body (message);
1455 flags = empathy_message_get_flags (message);
1456 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1457 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1458 * have already been received by the user before (and so are in the logs) */
1462 if (priv->highlight_regex == NULL) {
1466 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1470 chat_message_received (EmpathyChat *chat,
1471 EmpathyMessage *message,
1474 EmpathyChatPriv *priv = GET_PRIV (chat);
1475 EmpathyContact *sender;
1477 sender = empathy_message_get_sender (message);
1479 if (empathy_message_is_edit (message)) {
1480 DEBUG ("Editing message '%s' to '%s'",
1481 empathy_message_get_supersedes (message),
1482 empathy_message_get_body (message));
1484 empathy_chat_view_edit_message (chat->view, message);
1486 gboolean should_highlight = chat_should_highlight (chat, message);
1487 DEBUG ("Appending new message '%s' from %s (%d)",
1488 empathy_message_get_token (message),
1489 empathy_contact_get_alias (sender),
1490 empathy_contact_get_handle (sender));
1492 empathy_chat_view_append_message (chat->view, message, should_highlight);
1494 if (empathy_message_is_incoming (message)) {
1495 priv->unread_messages++;
1496 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1499 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1503 /* We received a message so the contact is no longer
1505 chat_state_changed_cb (priv->tp_chat, sender,
1506 TP_CHANNEL_CHAT_STATE_ACTIVE,
1511 chat_message_received_cb (EmpathyTpChat *tp_chat,
1512 EmpathyMessage *message,
1515 chat_message_received (chat, message, FALSE);
1519 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1520 EmpathyMessage *message,
1523 EmpathyChatPriv *priv = GET_PRIV (chat);
1525 empathy_chat_view_message_acknowledged (chat->view,
1528 if (!empathy_message_is_edit (message)) {
1529 priv->unread_messages--;
1530 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1535 append_balance_error (EmpathyChat *chat,
1536 const gchar *message_body)
1538 EmpathyChatPriv *priv = GET_PRIV (chat);
1539 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1540 const gchar *uri = tp_connection_get_balance_uri (conn);
1541 const gchar *error = _("insufficient balance to send message");
1542 gchar *str, *str_markup = NULL;
1544 if (message_body != NULL) {
1545 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1547 str = g_strdup_printf (_("Error sending message: %s"), error);
1550 if (!tp_str_empty (uri)) {
1551 /* translators: error used when user doesn't have enough credit on his
1552 * account to send the message. */
1553 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1554 " <a href='%s'>Top up</a>."), uri);
1556 if (message_body != NULL) {
1557 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1559 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1560 escaped_body, markup_error);
1562 g_free (escaped_body);
1564 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1567 g_free (markup_error);
1570 if (str_markup != NULL)
1571 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1573 empathy_chat_view_append_event (chat->view, str);
1576 g_free (str_markup);
1580 chat_send_error_cb (EmpathyTpChat *tp_chat,
1581 const gchar *message_body,
1582 TpChannelTextSendError error_code,
1583 const gchar *dbus_error,
1586 const gchar *error = NULL;
1589 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1590 append_balance_error (chat, message_body);
1592 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1593 error = _("not capable");
1596 if (error == NULL) {
1597 /* if we didn't find a dbus-error, try the old error */
1598 switch (error_code) {
1599 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1600 error = _("offline");
1602 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1603 error = _("invalid contact");
1605 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1606 error = _("permission denied");
1608 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1609 error = _("too long message");
1611 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1612 error = _("not implemented");
1614 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1616 error = _("unknown");
1621 if (message_body != NULL) {
1622 str = g_strdup_printf (_("Error sending message '%s': %s"),
1623 message_body, error);
1626 str = g_strdup_printf (_("Error sending message: %s"), error);
1629 empathy_chat_view_append_event (chat->view, str);
1634 chat_topic_label_size_allocate_cb (GtkLabel *label,
1635 GtkAllocation *allocation,
1638 EmpathyChatPriv *priv = GET_PRIV (chat);
1640 if (!gtk_label_get_line_wrap (label)) {
1641 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1642 gtk_widget_show (priv->expander_topic);
1644 gtk_widget_hide (priv->expander_topic);
1651 chat_topic_expander_activate_cb (GtkExpander *expander,
1652 GParamSpec *param_spec,
1655 EmpathyChatPriv *priv = GET_PRIV (chat);
1657 if (gtk_expander_get_expanded (expander)) {
1658 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1659 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1661 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1662 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1667 chat_subject_changed_cb (EmpathyChat *chat)
1669 EmpathyChatPriv *priv = GET_PRIV (chat);
1671 g_free (priv->subject);
1672 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1673 g_object_notify (G_OBJECT (chat), "subject");
1675 if (EMP_STR_EMPTY (priv->subject)) {
1676 gtk_widget_hide (priv->hbox_topic);
1678 gchar *markup_topic;
1681 markup_topic = empathy_add_link_markup (priv->subject);
1682 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1683 _("Topic:"), markup_topic);
1685 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1686 g_free (markup_text);
1687 g_free (markup_topic);
1689 gtk_widget_show (priv->hbox_topic);
1691 if (priv->block_events_timeout_id == 0) {
1694 if (!EMP_STR_EMPTY (priv->subject)) {
1695 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1696 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1697 /* No need to display this 'event' is no topic can be defined anyway */
1698 str = g_strdup (_("No topic defined"));
1702 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1709 chat_title_changed_cb (EmpathyChat *chat)
1711 EmpathyChatPriv *priv = GET_PRIV (chat);
1713 g_free (priv->name);
1714 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1715 g_object_notify (G_OBJECT (chat), "name");
1719 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1723 GtkTextIter word_start = *iter;
1724 GtkTextIter word_end = *iter;
1727 if (gtk_text_iter_inside_word (&word_end) &&
1728 !gtk_text_iter_ends_word (&word_end)) {
1729 gtk_text_iter_forward_word_end (&word_end);
1734 if (gtk_text_iter_get_char (&tmp) == '\'') {
1735 gtk_text_iter_forward_char (&tmp);
1737 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1738 gtk_text_iter_forward_word_end (&word_end);
1743 if (gtk_text_iter_inside_word (&word_start) ||
1744 gtk_text_iter_ends_word (&word_start)) {
1745 if (!gtk_text_iter_starts_word (&word_start) ||
1746 gtk_text_iter_equal (&word_start, &word_end)) {
1747 gtk_text_iter_backward_word_start (&word_start);
1751 gtk_text_iter_backward_char (&tmp);
1753 if (gtk_text_iter_get_char (&tmp) == '\'') {
1754 gtk_text_iter_backward_char (&tmp);
1756 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1757 gtk_text_iter_backward_word_start (&word_start);
1762 *start = word_start;
1768 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1769 GtkTextIter *location,
1774 GtkTextIter iter, pos;
1776 /* Remove all misspelled tags in the inserted text.
1777 * This happens when text is inserted within a misspelled word. */
1778 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1779 gtk_text_iter_get_offset (location) - len);
1780 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1783 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1786 GtkTextIter start, end;
1789 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1792 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1794 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1795 gtk_text_iter_equal (&pos, &end) ||
1796 empathy_spell_check (str)) {
1797 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1799 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1804 } while (gtk_text_iter_forward_word_end (&iter) &&
1805 gtk_text_iter_compare (&iter, location) <= 0);
1809 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1814 GtkTextIter word_start, word_end;
1816 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1817 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1818 &word_start, &word_end);
1823 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1826 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1827 chat_composing_stop (chat);
1829 chat_composing_start (chat);
1834 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1839 GtkTextIter prev_pos;
1840 GtkTextIter word_start;
1841 GtkTextIter word_end;
1845 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1847 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1848 gtk_text_buffer_get_insert (buffer));
1849 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1851 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1854 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1855 !gtk_text_iter_equal (&pos, &word_end)) {
1856 str = gtk_text_buffer_get_text (buffer,
1857 &word_start, &word_end, FALSE);
1859 if (!empathy_spell_check (str)) {
1860 gtk_text_buffer_apply_tag_by_name (buffer,
1861 "misspelled", &word_start, &word_end);
1863 gtk_text_buffer_remove_tag_by_name (buffer,
1864 "misspelled", &word_start, &word_end);
1871 gtk_text_buffer_move_mark (buffer, mark, &pos);
1875 empathy_isspace_cb (gunichar c,
1878 return g_unichar_isspace (c);
1882 chat_input_key_press_event_cb (GtkWidget *widget,
1886 EmpathyChatPriv *priv;
1889 GtkWidget *text_view_sw;
1891 priv = GET_PRIV (chat);
1893 priv->most_recent_event_type = event->type;
1895 /* Catch ctrl+up/down so we can traverse messages we sent */
1896 if ((event->state & GDK_CONTROL_MASK) &&
1897 (event->keyval == GDK_KEY_Up ||
1898 event->keyval == GDK_KEY_Down)) {
1899 GtkTextBuffer *buffer;
1902 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1903 chat_input_history_update (chat, buffer);
1905 if (event->keyval == GDK_KEY_Up) {
1906 str = chat_input_history_get_next (chat);
1908 str = chat_input_history_get_prev (chat);
1911 g_signal_handlers_block_by_func (buffer,
1912 chat_input_text_buffer_changed_cb,
1914 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1915 g_signal_handlers_unblock_by_func (buffer,
1916 chat_input_text_buffer_changed_cb,
1922 /* Catch enter but not ctrl/shift-enter */
1923 if (IS_ENTER (event->keyval) &&
1924 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1927 /* This is to make sure that kinput2 gets the enter. And if
1928 * it's handled there we shouldn't send on it. This is because
1929 * kinput2 uses Enter to commit letters. See:
1930 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1933 view = GTK_TEXT_VIEW (chat->input_text_view);
1934 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1935 gtk_text_view_reset_im_context (view);
1939 chat_input_text_view_send (chat);
1943 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1945 if (IS_ENTER (event->keyval) &&
1946 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1947 /* Newline for shift/control-enter. */
1950 if (!(event->state & GDK_CONTROL_MASK) &&
1951 event->keyval == GDK_KEY_Page_Up) {
1952 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1953 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1956 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1957 event->keyval == GDK_KEY_Page_Down) {
1958 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1959 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1960 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1961 gtk_adjustment_set_value (adj, val);
1964 if (event->keyval == GDK_KEY_Escape) {
1965 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1967 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1968 event->keyval == GDK_KEY_Tab) {
1969 GtkTextBuffer *buffer;
1970 GtkTextIter start, current;
1971 gchar *nick, *completed;
1972 GList *list, *completed_list;
1973 gboolean is_start_of_buffer;
1975 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1976 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1978 /* Get the start of the nick to complete. */
1979 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1980 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1981 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1983 is_start_of_buffer = gtk_text_iter_is_start (&start);
1985 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1986 g_completion_add_items (priv->completion, list);
1988 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1989 completed_list = g_completion_complete (priv->completion,
1998 GString *message = NULL;
2001 gtk_text_buffer_delete (buffer, &start, ¤t);
2003 len = g_list_length (completed_list);
2006 /* If we only have one hit, use that text
2007 * instead of the text in completed since the
2008 * completed text will use the typed string
2009 * which might be cased all wrong.
2012 text = empathy_contact_get_alias (completed_list->data);
2016 /* Print all hits to the scrollback view, so the
2017 * user knows what possibilities he has.
2020 message = g_string_new ("");
2021 for (l = completed_list; l != NULL; l = l->next) {
2022 g_string_append (message, empathy_contact_get_alias (l->data));
2023 g_string_append (message, " - ");
2025 empathy_chat_view_append_event (chat->view, message->str);
2026 g_string_free (message, TRUE);
2029 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2031 if (len == 1 && is_start_of_buffer) {
2032 gchar *complete_char;
2034 complete_char = g_settings_get_string (
2035 priv->gsettings_chat,
2036 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2038 if (complete_char != NULL) {
2039 gtk_text_buffer_insert_at_cursor (buffer,
2041 strlen (complete_char));
2042 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2043 g_free (complete_char);
2050 g_completion_clear_items (priv->completion);
2052 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2062 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2066 gtk_widget_grab_focus (chat->input_text_view);
2072 chat_input_realize_cb (GtkWidget *widget,
2075 DEBUG ("Setting focus to the input text view");
2076 if (gtk_widget_is_sensitive (widget)) {
2077 gtk_widget_grab_focus (widget);
2082 chat_input_has_focus_notify_cb (GtkWidget *widget,
2086 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2090 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2091 EmpathySmiley *smiley,
2094 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2095 GtkTextBuffer *buffer;
2098 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2100 gtk_text_buffer_get_end_iter (buffer, &iter);
2101 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2103 gtk_text_buffer_get_end_iter (buffer, &iter);
2104 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2115 static EmpathyChatSpell *
2116 chat_spell_new (EmpathyChat *chat,
2121 EmpathyChatSpell *chat_spell;
2123 chat_spell = g_slice_new0 (EmpathyChatSpell);
2125 chat_spell->chat = g_object_ref (chat);
2126 chat_spell->word = g_strdup (word);
2127 chat_spell->start = start;
2128 chat_spell->end = end;
2134 chat_spell_free (EmpathyChatSpell *chat_spell)
2136 g_object_unref (chat_spell->chat);
2137 g_free (chat_spell->word);
2138 g_slice_free (EmpathyChatSpell, chat_spell);
2142 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2143 EmpathyChatSpell *chat_spell)
2145 empathy_chat_correct_word (chat_spell->chat,
2146 &(chat_spell->start),
2148 gtk_menu_item_get_label (menu_item));
2153 chat_spelling_build_suggestions_menu (const gchar *code,
2154 EmpathyChatSpell *chat_spell)
2156 GList *suggestions, *l;
2157 GtkWidget *menu, *menu_item;
2159 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2160 if (suggestions == NULL)
2163 menu = gtk_menu_new ();
2164 for (l = suggestions; l; l = l->next) {
2165 menu_item = gtk_menu_item_new_with_label (l->data);
2166 g_signal_connect (G_OBJECT (menu_item), "activate",
2167 G_CALLBACK (chat_spelling_menu_activate_cb),
2169 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2171 empathy_spell_free_suggestions (suggestions);
2173 gtk_widget_show_all (menu);
2179 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2181 GtkWidget *menu, *submenu, *item;
2184 codes = empathy_spell_get_enabled_language_codes ();
2185 g_assert (codes != NULL);
2187 if (g_list_length (codes) > 1) {
2188 menu = gtk_menu_new ();
2190 for (l = codes; l; l = l->next) {
2191 const gchar *code, *name;
2194 name = empathy_spell_get_language_name (code);
2198 item = gtk_image_menu_item_new_with_label (name);
2200 submenu = chat_spelling_build_suggestions_menu (
2202 if (submenu == NULL)
2203 gtk_widget_set_sensitive (item, FALSE);
2205 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2207 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2210 menu = chat_spelling_build_suggestions_menu (codes->data,
2213 menu = gtk_menu_new ();
2214 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2215 gtk_widget_set_sensitive (item, FALSE);
2216 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2219 g_list_free (codes);
2221 gtk_widget_show_all (menu);
2232 static EmpathyChatWord *
2233 chat_word_new (EmpathyChat *chat,
2237 EmpathyChatWord *chat_word;
2239 chat_word = g_slice_new0 (EmpathyChatWord);
2241 chat_word->chat = g_object_ref (chat);
2242 chat_word->word = g_strdup (word);
2243 chat_word->code = g_strdup (code);
2249 chat_word_free (EmpathyChatWord *chat_word)
2251 g_object_unref (chat_word->chat);
2252 g_free (chat_word->word);
2253 g_free (chat_word->code);
2254 g_slice_free (EmpathyChatWord, chat_word);
2258 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2259 EmpathyChatWord *chat_word)
2261 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2263 empathy_spell_add_to_dictionary (chat_word->code,
2265 priv->update_misspelled_words_id =
2266 g_idle_add (update_misspelled_words, chat_word->chat);
2270 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2272 GtkWidget *menu, *item, *lang_item, *image;
2275 const gchar *code, *name;
2276 EmpathyChatWord *chat_word;
2278 codes = empathy_spell_get_enabled_language_codes ();
2279 g_assert (codes != NULL);
2280 if (g_list_length (codes) > 1) {
2281 /* translators: %s is the selected word */
2282 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2284 item = gtk_image_menu_item_new_with_mnemonic (label);
2286 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2287 GTK_ICON_SIZE_MENU);
2288 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2291 menu = gtk_menu_new ();
2293 for (l = codes; l; l = l->next) {
2295 name = empathy_spell_get_language_name (code);
2299 lang_item = gtk_image_menu_item_new_with_label (name);
2301 chat_word= chat_word_new (chat_spell->chat,
2302 chat_spell->word, code);
2303 g_object_set_data_full (G_OBJECT (lang_item),
2304 "chat-word", chat_word,
2305 (GDestroyNotify) chat_word_free);
2307 g_signal_connect (G_OBJECT (lang_item), "activate",
2308 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2310 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2312 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2315 name = empathy_spell_get_language_name (code);
2316 g_assert (name != NULL);
2317 /* translators: first %s is the selected word,
2318 * second %s is the language name of the target dictionary */
2319 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2320 chat_spell->word, name);
2321 item = gtk_image_menu_item_new_with_mnemonic (label);
2323 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2324 GTK_ICON_SIZE_MENU);
2325 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2327 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2329 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2330 (GDestroyNotify) chat_word_free);
2332 g_signal_connect (G_OBJECT (item), "activate",
2333 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2336 g_list_free (codes);
2338 gtk_widget_show_all (item);
2344 chat_text_send_cb (GtkMenuItem *menuitem,
2347 chat_input_text_view_send (chat);
2351 chat_input_button_press_event_cb (GtkTextView *view,
2352 GdkEventButton *event,
2355 EmpathyChatPriv *priv = GET_PRIV (chat);
2357 priv->most_recent_event_type = event->type;
2363 chat_input_populate_popup_cb (GtkTextView *view,
2367 EmpathyChatPriv *priv = GET_PRIV (chat);
2368 GtkTextBuffer *buffer;
2369 GtkTextTagTable *table;
2372 GtkTextIter iter, start, end;
2375 EmpathyChatSpell *chat_spell;
2376 GtkWidget *spell_menu;
2377 GtkWidget *spell_item;
2378 EmpathySmileyManager *smiley_manager;
2379 GtkWidget *smiley_menu;
2382 buffer = gtk_text_view_get_buffer (view);
2384 /* Add the emoticon menu. */
2385 item = gtk_separator_menu_item_new ();
2386 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2387 gtk_widget_show (item);
2389 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2390 image = gtk_image_new_from_icon_name ("face-smile",
2391 GTK_ICON_SIZE_MENU);
2392 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2393 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2394 gtk_widget_show (item);
2396 smiley_manager = empathy_smiley_manager_dup_singleton ();
2397 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2398 chat_insert_smiley_activate_cb,
2400 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2401 g_object_unref (smiley_manager);
2403 /* Add the Send menu item. */
2404 gtk_text_buffer_get_bounds (buffer, &start, &end);
2405 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2406 if (!EMP_STR_EMPTY (str)) {
2407 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2408 g_signal_connect (G_OBJECT (item), "activate",
2409 G_CALLBACK (chat_text_send_cb), chat);
2410 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2411 gtk_widget_show (item);
2415 /* Add the spell check menu item. */
2416 table = gtk_text_buffer_get_tag_table (buffer);
2417 tag = gtk_text_tag_table_lookup (table, "misspelled");
2419 switch (priv->most_recent_event_type) {
2420 case GDK_BUTTON_PRESS:
2421 /* get the location from the pointer */
2422 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2423 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2424 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2426 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2427 GTK_TEXT_WINDOW_WIDGET,
2430 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2435 g_warn_if_reached ();
2436 /* assume the KEY_PRESS case */
2439 /* get the location from the cursor */
2440 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2441 gtk_text_buffer_get_insert (buffer));
2447 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2448 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2450 str = gtk_text_buffer_get_text (buffer,
2451 &start, &end, FALSE);
2453 if (!EMP_STR_EMPTY (str)) {
2454 chat_spell = chat_spell_new (chat, str, start, end);
2455 g_object_set_data_full (G_OBJECT (menu),
2456 "chat-spell", chat_spell,
2457 (GDestroyNotify) chat_spell_free);
2459 item = gtk_separator_menu_item_new ();
2460 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2461 gtk_widget_show (item);
2463 /* Spelling suggestions */
2464 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2465 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2466 GTK_ICON_SIZE_MENU);
2467 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2469 spell_menu = chat_spelling_build_menu (chat_spell);
2470 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2473 spell_item = gtk_separator_menu_item_new ();
2474 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2475 gtk_widget_show (spell_item);
2477 /* Add to dictionary */
2478 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2480 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2481 gtk_widget_show (spell_item);
2483 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2484 gtk_widget_show (item);
2490 chat_log_filter (TplEvent *event,
2493 EmpathyChat *chat = user_data;
2494 EmpathyMessage *message;
2495 EmpathyChatPriv *priv = GET_PRIV (chat);
2496 const GList *pending;
2498 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2499 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2501 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2502 message = empathy_message_from_tpl_log_event (event);
2504 for (; pending; pending = g_list_next (pending)) {
2505 if (empathy_message_equal (message, pending->data)) {
2506 g_object_unref (message);
2511 g_object_unref (message);
2517 show_pending_messages (EmpathyChat *chat) {
2518 EmpathyChatPriv *priv = GET_PRIV (chat);
2519 const GList *messages, *l;
2521 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2523 if (chat->view == NULL || priv->tp_chat == NULL)
2526 if (!priv->can_show_pending)
2529 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2531 for (l = messages; l != NULL ; l = g_list_next (l)) {
2532 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2533 chat_message_received (chat, message, TRUE);
2539 got_filtered_messages_cb (GObject *manager,
2540 GAsyncResult *result,
2545 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2546 EmpathyChatPriv *priv = GET_PRIV (chat);
2547 GError *error = NULL;
2549 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2550 result, &messages, &error)) {
2551 DEBUG ("%s. Aborting.", error->message);
2552 empathy_chat_view_append_event (chat->view,
2553 _("Failed to retrieve recent logs"));
2554 g_error_free (error);
2558 for (l = messages; l; l = g_list_next (l)) {
2559 EmpathyMessage *message;
2561 g_assert (TPL_IS_EVENT (l->data));
2563 message = empathy_message_from_tpl_log_event (l->data);
2564 g_object_unref (l->data);
2566 if (empathy_message_is_edit (message)) {
2567 /* this is an edited message, create a synthetic event
2568 * using the supersedes token and
2569 * original-message-sent timestamp, that we can then
2571 EmpathyMessage *syn_msg = g_object_new (
2572 EMPATHY_TYPE_MESSAGE,
2574 "token", empathy_message_get_supersedes (message),
2575 "type", empathy_message_get_tptype (message),
2576 "timestamp", empathy_message_get_original_timestamp (message),
2577 "incoming", empathy_message_is_incoming (message),
2579 "receiver", empathy_message_get_receiver (message),
2580 "sender", empathy_message_get_sender (message),
2583 empathy_chat_view_append_message (chat->view, syn_msg,
2584 chat_should_highlight (chat, syn_msg));
2585 empathy_chat_view_edit_message (chat->view, message);
2587 g_object_unref (syn_msg);
2589 /* append the latest message */
2590 empathy_chat_view_append_message (chat->view, message,
2591 chat_should_highlight (chat, message));
2594 g_object_unref (message);
2596 g_list_free (messages);
2599 /* in case of TPL error, skip backlog and show pending messages */
2600 priv->can_show_pending = TRUE;
2601 show_pending_messages (chat);
2603 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2604 * about it in EmpathyChatPriv definition */
2605 priv->retrieving_backlogs = FALSE;
2606 empathy_chat_messages_read (chat);
2608 /* Turn back on scrolling */
2609 empathy_chat_view_scroll (chat->view, TRUE);
2613 chat_add_logs (EmpathyChat *chat)
2615 EmpathyChatPriv *priv = GET_PRIV (chat);
2622 /* Turn off scrolling temporarily */
2623 empathy_chat_view_scroll (chat->view, FALSE);
2625 /* Add messages from last conversation */
2626 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2627 target = tpl_entity_new_from_room_id (priv->id);
2629 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2631 priv->retrieving_backlogs = TRUE;
2632 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2635 TPL_EVENT_MASK_TEXT,
2639 got_filtered_messages_cb,
2642 g_object_unref (target);
2646 chat_contacts_completion_func (const gchar *s1,
2650 gchar *tmp, *nick1, *nick2;
2657 return s1 ? -1 : +1;
2660 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2661 nick1 = g_utf8_casefold (tmp, -1);
2664 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2665 nick2 = g_utf8_casefold (tmp, -1);
2668 ret = strncmp (nick1, nick2, n);
2677 build_part_message (guint reason,
2679 EmpathyContact *actor,
2680 const gchar *message)
2682 GString *s = g_string_new ("");
2683 const gchar *actor_name = NULL;
2685 if (actor != NULL) {
2686 actor_name = empathy_contact_get_alias (actor);
2689 /* Having an actor only really makes sense for a few actions... */
2691 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2692 g_string_append_printf (s, _("%s has disconnected"), name);
2694 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2695 if (actor_name != NULL) {
2696 /* translators: reverse the order of these arguments
2697 * if the kicked should come before the kicker in your locale.
2699 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2702 g_string_append_printf (s, _("%s was kicked"), name);
2705 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2706 if (actor_name != NULL) {
2707 /* translators: reverse the order of these arguments
2708 * if the banned should come before the banner in your locale.
2710 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2713 g_string_append_printf (s, _("%s was banned"), name);
2717 g_string_append_printf (s, _("%s has left the room"), name);
2720 if (!EMP_STR_EMPTY (message)) {
2721 /* Note to translators: this string is appended to
2722 * notifications like "foo has left the room", with the message
2723 * given by the user living the room. If this poses a problem,
2724 * please let us know. :-)
2726 g_string_append_printf (s, _(" (%s)"), message);
2729 return g_string_free (s, FALSE);
2733 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2734 EmpathyContact *contact,
2735 EmpathyContact *actor,
2741 EmpathyChatPriv *priv = GET_PRIV (chat);
2742 const gchar *name = empathy_contact_get_alias (contact);
2745 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2747 if (priv->block_events_timeout_id != 0)
2751 str = g_strdup_printf (_("%s has joined the room"),
2754 str = build_part_message (reason, name, actor, message);
2757 empathy_chat_view_append_event (chat->view, str);
2762 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2763 EmpathyContact *old_contact,
2764 EmpathyContact *new_contact,
2769 EmpathyChatPriv *priv = GET_PRIV (chat);
2771 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2773 if (priv->block_events_timeout_id == 0) {
2776 str = g_strdup_printf (_("%s is now known as %s"),
2777 empathy_contact_get_alias (old_contact),
2778 empathy_contact_get_alias (new_contact));
2779 empathy_chat_view_append_event (chat->view, str);
2786 chat_contacts_visible_timeout_cb (gpointer chat)
2788 EmpathyChatPriv *priv = GET_PRIV (chat);
2790 /* Relax the size request */
2791 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2793 /* Set the position of the slider. This must be done here because
2794 * GtkPaned need to know its size allocation and it will be settled only
2795 * after the gtk_window_resize () tough effect. */
2796 if (priv->contacts_width > 0) {
2797 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2798 priv->contacts_width);
2801 priv->contacts_visible_id = 0;
2807 chat_update_contacts_visibility (EmpathyChat *chat,
2810 EmpathyChatPriv *priv = GET_PRIV (chat);
2812 if (!priv->scrolled_window_contacts) {
2816 if (priv->remote_contact != NULL) {
2820 if (show && priv->contact_list_view == NULL) {
2821 EmpathyIndividualStore *store;
2823 GtkAllocation allocation;
2825 /* We are adding the contact list to the chat, we don't want the
2826 * chat view to become too small. If the chat view is already
2827 * smaller than 250 make sure that size won't change. If the
2828 * chat view is bigger the contact list will take some space on
2829 * it but we make sure the chat view don't become smaller than
2830 * 250. Relax the size request once the resize is done */
2831 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2832 min_width = MIN (allocation.width, 250);
2833 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2835 /* There is no way to know when the window resize will happen
2836 * since it is WM's decision. Let's hope it won't be longer. */
2837 if (priv->contacts_visible_id != 0)
2838 g_source_remove (priv->contacts_visible_id);
2839 priv->contacts_visible_id = g_timeout_add (500,
2840 chat_contacts_visible_timeout_cb, chat);
2842 store = EMPATHY_INDIVIDUAL_STORE (
2843 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2845 empathy_individual_store_set_show_groups (store, FALSE);
2847 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2848 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2849 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2850 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2851 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2852 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2853 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2855 empathy_individual_view_set_show_offline (
2856 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2857 empathy_individual_view_set_show_uninteresting (
2858 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2860 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2861 priv->contact_list_view);
2863 gtk_widget_show (priv->contact_list_view);
2864 gtk_widget_show (priv->scrolled_window_contacts);
2865 g_object_unref (store);
2867 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2868 gtk_widget_hide (priv->scrolled_window_contacts);
2869 if (priv->contact_list_view != NULL) {
2870 gtk_widget_destroy (priv->contact_list_view);
2871 priv->contact_list_view = NULL;
2877 empathy_chat_set_show_contacts (EmpathyChat *chat,
2880 EmpathyChatPriv *priv = GET_PRIV (chat);
2882 priv->show_contacts = show;
2884 chat_update_contacts_visibility (chat, show);
2886 g_object_notify (G_OBJECT (chat), "show-contacts");
2890 chat_self_contact_changed_cb (EmpathyChat *chat)
2892 EmpathyChatPriv *priv = GET_PRIV (chat);
2894 if (priv->self_contact != NULL) {
2895 g_signal_handlers_disconnect_by_func (priv->self_contact,
2896 chat_self_contact_alias_changed_cb,
2899 g_clear_object (&priv->self_contact);
2901 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2902 if (priv->self_contact != NULL) {
2903 g_object_ref (priv->self_contact);
2905 if (empathy_chat_is_room (chat)) {
2906 g_signal_connect_swapped (priv->self_contact, "notify::alias",
2907 G_CALLBACK (chat_self_contact_alias_changed_cb),
2912 chat_self_contact_alias_changed_cb (chat);
2916 chat_remote_contact_changed_cb (EmpathyChat *chat)
2918 EmpathyChatPriv *priv = GET_PRIV (chat);
2920 if (priv->remote_contact != NULL) {
2921 g_object_unref (priv->remote_contact);
2922 priv->remote_contact = NULL;
2927 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2928 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2929 if (priv->remote_contact != NULL) {
2930 g_object_ref (priv->remote_contact);
2931 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2933 else if (priv->tp_chat != NULL) {
2934 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2937 chat_update_contacts_visibility (chat, priv->show_contacts);
2939 g_object_notify (G_OBJECT (chat), "remote-contact");
2940 g_object_notify (G_OBJECT (chat), "id");
2944 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2950 EmpathyChatPriv *priv;
2952 priv = GET_PRIV (chat);
2954 if (!priv->tp_chat) {
2958 chat_composing_remove_timeout (chat);
2959 g_object_unref (priv->tp_chat);
2960 priv->tp_chat = NULL;
2961 g_object_notify (G_OBJECT (chat), "tp-chat");
2963 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2964 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2966 chat_update_contacts_visibility (chat, FALSE);
2968 priv->unread_messages_when_offline = priv->unread_messages;
2972 update_misspelled_words (gpointer data)
2974 EmpathyChat *chat = EMPATHY_CHAT (data);
2975 EmpathyChatPriv *priv = GET_PRIV (chat);
2976 GtkTextBuffer *buffer;
2980 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2982 gtk_text_buffer_get_end_iter (buffer, &iter);
2983 length = gtk_text_iter_get_offset (&iter);
2984 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2985 NULL, length, chat);
2987 priv->update_misspelled_words_id = 0;
2993 conf_spell_checking_cb (GSettings *gsettings_chat,
2997 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2998 EmpathyChatPriv *priv = GET_PRIV (chat);
2999 gboolean spell_checker;
3000 GtkTextBuffer *buffer;
3002 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3005 spell_checker = g_settings_get_boolean (gsettings_chat,
3006 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3008 if (!empathy_spell_supported ()) {
3009 spell_checker = FALSE;
3012 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3014 if (spell_checker == priv->spell_checking_enabled) {
3015 if (spell_checker) {
3016 /* Possibly changed dictionaries,
3017 * update misspelled words. Need to do so in idle
3018 * so the spell checker is updated. */
3019 priv->update_misspelled_words_id =
3020 g_idle_add (update_misspelled_words, chat);
3026 if (spell_checker) {
3029 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3030 buffer, "notify::cursor-position",
3031 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3033 priv->insert_text_id = tp_g_signal_connect_object (
3034 buffer, "insert-text",
3035 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3036 chat, G_CONNECT_AFTER);
3037 priv->delete_range_id = tp_g_signal_connect_object (
3038 buffer, "delete-range",
3039 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3040 chat, G_CONNECT_AFTER);
3042 gtk_text_buffer_create_tag (buffer, "misspelled",
3043 "underline", PANGO_UNDERLINE_ERROR,
3046 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3047 gtk_text_buffer_get_insert (buffer));
3048 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3051 /* Mark misspelled words in the existing buffer.
3052 * Need to do so in idle so the spell checker is updated. */
3053 priv->update_misspelled_words_id =
3054 g_idle_add (update_misspelled_words, chat);
3056 GtkTextTagTable *table;
3059 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3060 priv->notify_cursor_position_id = 0;
3061 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3062 priv->insert_text_id = 0;
3063 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3064 priv->delete_range_id = 0;
3066 table = gtk_text_buffer_get_tag_table (buffer);
3067 tag = gtk_text_tag_table_lookup (table, "misspelled");
3068 gtk_text_tag_table_remove (table, tag);
3070 gtk_text_buffer_delete_mark_by_name (buffer,
3071 "previous-cursor-position");
3074 priv->spell_checking_enabled = spell_checker;
3078 save_paned_pos_timeout (gpointer data)
3080 EmpathyChat *self = data;
3083 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3085 g_settings_set_int (self->priv->gsettings_ui,
3086 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3093 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3097 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3099 if (chat->priv->save_paned_pos_id != 0)
3100 g_source_remove (chat->priv->save_paned_pos_id);
3102 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3103 save_paned_pos_timeout, chat);
3109 chat_create_ui (EmpathyChat *chat)
3111 EmpathyChatPriv *priv = GET_PRIV (chat);
3115 GtkTextBuffer *buffer;
3116 EmpathyThemeManager *theme_mgr;
3118 filename = empathy_file_lookup ("empathy-chat.ui",
3120 gui = empathy_builder_get_file (filename,
3121 "chat_widget", &priv->widget,
3122 "hpaned", &priv->hpaned,
3123 "vbox_left", &priv->vbox_left,
3124 "scrolled_window_chat", &priv->scrolled_window_chat,
3125 "scrolled_window_input", &priv->scrolled_window_input,
3126 "hbox_topic", &priv->hbox_topic,
3127 "expander_topic", &priv->expander_topic,
3128 "label_topic", &priv->label_topic,
3129 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3130 "info_bar_vbox", &priv->info_bar_vbox,
3133 empathy_builder_connect (gui, chat,
3134 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3135 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3140 /* Add message view. */
3141 theme_mgr = empathy_theme_manager_dup_singleton ();
3142 chat->view = empathy_theme_manager_create_view (theme_mgr);
3143 g_object_unref (theme_mgr);
3144 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3145 and other types, even though it's non-editable and doesn't accept any
3146 drags. This steals drag motion for anything inside the scrollbars,
3147 making drag destinations on chat windows far less useful.
3149 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3150 g_signal_connect (chat->view, "focus_in_event",
3151 G_CALLBACK (chat_text_view_focus_in_event_cb),
3153 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3154 GTK_WIDGET (chat->view));
3155 gtk_widget_show (GTK_WIDGET (chat->view));
3157 /* Add input GtkTextView */
3158 chat->input_text_view = empathy_input_text_view_new ();
3159 g_signal_connect (chat->input_text_view, "notify::has-focus",
3160 G_CALLBACK (chat_input_has_focus_notify_cb),
3162 g_signal_connect (chat->input_text_view, "key-press-event",
3163 G_CALLBACK (chat_input_key_press_event_cb),
3165 g_signal_connect (chat->input_text_view, "realize",
3166 G_CALLBACK (chat_input_realize_cb),
3168 g_signal_connect (chat->input_text_view, "button-press-event",
3169 G_CALLBACK (chat_input_button_press_event_cb),
3171 g_signal_connect (chat->input_text_view, "populate-popup",
3172 G_CALLBACK (chat_input_populate_popup_cb),
3174 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3175 tp_g_signal_connect_object (buffer, "changed",
3176 G_CALLBACK (chat_input_text_buffer_changed_cb),
3178 tp_g_signal_connect_object (priv->gsettings_chat,
3179 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3180 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3181 conf_spell_checking_cb (priv->gsettings_chat,
3182 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3183 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3184 chat->input_text_view);
3185 gtk_widget_show (chat->input_text_view);
3187 /* Add the (invisible) search bar */
3188 priv->search_bar = empathy_search_bar_new (chat->view);
3189 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3192 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3194 /* Initialy hide the topic, will be shown if not empty */
3195 gtk_widget_hide (priv->hbox_topic);
3197 g_signal_connect (priv->hpaned, "notify::position",
3198 G_CALLBACK (chat_hpaned_pos_changed_cb),
3201 /* Set widget focus order */
3202 list = g_list_append (NULL, priv->search_bar);
3203 list = g_list_append (list, priv->scrolled_window_input);
3204 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3207 list = g_list_append (NULL, priv->vbox_left);
3208 list = g_list_append (list, priv->scrolled_window_contacts);
3209 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3212 list = g_list_append (NULL, priv->hpaned);
3213 list = g_list_append (list, priv->hbox_topic);
3214 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3217 /* Add the main widget in the chat widget */
3218 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3219 g_object_unref (gui);
3223 chat_finalize (GObject *object)
3226 EmpathyChatPriv *priv;
3228 chat = EMPATHY_CHAT (object);
3229 priv = GET_PRIV (chat);
3231 DEBUG ("Finalized: %p", object);
3233 if (priv->update_misspelled_words_id != 0)
3234 g_source_remove (priv->update_misspelled_words_id);
3236 if (priv->save_paned_pos_id != 0)
3237 g_source_remove (priv->save_paned_pos_id);
3239 if (priv->contacts_visible_id != 0)
3240 g_source_remove (priv->contacts_visible_id);
3242 g_object_unref (priv->gsettings_chat);
3243 g_object_unref (priv->gsettings_ui);
3245 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3246 g_list_free (priv->input_history);
3248 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3249 g_list_free (priv->compositors);
3251 chat_composing_remove_timeout (chat);
3253 g_object_unref (priv->account_manager);
3254 g_object_unref (priv->log_manager);
3256 if (priv->tp_chat) {
3257 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3258 chat_invalidated_cb, chat);
3259 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3260 chat_message_received_cb, chat);
3261 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3262 chat_message_acknowledged_cb, chat);
3263 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3264 chat_send_error_cb, chat);
3265 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3266 chat_state_changed_cb, chat);
3267 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3268 chat_members_changed_cb, chat);
3269 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3270 chat_self_contact_changed_cb, chat);
3271 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3272 chat_remote_contact_changed_cb, chat);
3273 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3274 chat_title_changed_cb, chat);
3275 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3276 chat_subject_changed_cb, chat);
3277 empathy_tp_chat_leave (priv->tp_chat, "");
3278 g_object_unref (priv->tp_chat);
3280 if (priv->account) {
3281 g_object_unref (priv->account);
3283 if (priv->self_contact) {
3284 g_signal_handlers_disconnect_by_func (priv->self_contact,
3285 chat_self_contact_alias_changed_cb,
3287 g_object_unref (priv->self_contact);
3289 if (priv->remote_contact) {
3290 g_object_unref (priv->remote_contact);
3293 if (priv->block_events_timeout_id) {
3294 g_source_remove (priv->block_events_timeout_id);
3298 g_free (priv->name);
3299 g_free (priv->subject);
3300 g_completion_free (priv->completion);
3302 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3304 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3308 chat_constructed (GObject *object)
3310 EmpathyChat *chat = EMPATHY_CHAT (object);
3311 EmpathyChatPriv *priv = GET_PRIV (chat);
3313 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3314 /* First display logs from the logger and then display pending messages */
3315 chat_add_logs (chat);
3318 /* Just display pending messages for rooms */
3319 priv->can_show_pending = TRUE;
3320 show_pending_messages (chat);
3325 empathy_chat_class_init (EmpathyChatClass *klass)
3327 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3329 object_class->finalize = chat_finalize;
3330 object_class->get_property = chat_get_property;
3331 object_class->set_property = chat_set_property;
3332 object_class->constructed = chat_constructed;
3334 g_object_class_install_property (object_class,
3336 g_param_spec_object ("tp-chat",
3338 "The tp chat object",
3339 EMPATHY_TYPE_TP_CHAT,
3342 G_PARAM_STATIC_STRINGS));
3343 g_object_class_install_property (object_class,
3345 g_param_spec_object ("account",
3346 "Account of the chat",
3347 "The account of the chat",
3350 G_PARAM_STATIC_STRINGS));
3351 g_object_class_install_property (object_class,
3353 g_param_spec_string ("id",
3355 "The id of the chat",
3358 G_PARAM_STATIC_STRINGS));
3359 g_object_class_install_property (object_class,
3361 g_param_spec_string ("name",
3363 "The name of the chat",
3366 G_PARAM_STATIC_STRINGS));
3367 g_object_class_install_property (object_class,
3369 g_param_spec_string ("subject",
3371 "The subject or topic of the chat",
3374 G_PARAM_STATIC_STRINGS));
3375 g_object_class_install_property (object_class,
3376 PROP_REMOTE_CONTACT,
3377 g_param_spec_object ("remote-contact",
3378 "The remote contact",
3379 "The remote contact is any",
3380 EMPATHY_TYPE_CONTACT,
3382 G_PARAM_STATIC_STRINGS));
3383 g_object_class_install_property (object_class,
3385 g_param_spec_boolean ("show-contacts",
3386 "Contacts' visibility",
3387 "The visibility of the contacts' list",
3390 G_PARAM_STATIC_STRINGS));
3392 g_object_class_install_property (object_class,
3394 g_param_spec_boolean ("sms-channel",
3396 "TRUE if this channel is for sending SMSes",
3398 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3400 g_object_class_install_property (object_class,
3401 PROP_N_MESSAGES_SENDING,
3402 g_param_spec_uint ("n-messages-sending",
3403 "Num Messages Sending",
3404 "The number of messages being sent",
3406 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3408 g_object_class_install_property (object_class,
3409 PROP_NB_UNREAD_MESSAGES,
3410 g_param_spec_uint ("nb-unread-messages",
3411 "Num Unread Messages",
3412 "The number of unread messages",
3414 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3416 signals[COMPOSING] =
3417 g_signal_new ("composing",
3418 G_OBJECT_CLASS_TYPE (object_class),
3422 g_cclosure_marshal_generic,
3427 * EmpathyChat::new-message:
3428 * @self: the #EmpathyChat
3429 * @message: the new message
3430 * @pending: whether the message was in the pending queue when @self
3432 * @should_highlight: %TRUE if the message mentions the local user
3434 signals[NEW_MESSAGE] =
3435 g_signal_new ("new-message",
3436 G_OBJECT_CLASS_TYPE (object_class),
3440 g_cclosure_marshal_generic,
3442 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3444 signals[PART_COMMAND_ENTERED] =
3445 g_signal_new ("part-command-entered",
3446 G_OBJECT_CLASS_TYPE (object_class),
3450 g_cclosure_marshal_generic,
3454 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3458 chat_block_events_timeout_cb (gpointer data)
3460 EmpathyChatPriv *priv = GET_PRIV (data);
3462 priv->block_events_timeout_id = 0;
3468 account_manager_prepared_cb (GObject *source_object,
3469 GAsyncResult *result,
3472 GList *accounts, *l;
3473 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3474 EmpathyChat *chat = user_data;
3475 GError *error = NULL;
3477 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3478 DEBUG ("Failed to prepare the account manager: %s", error->message);
3479 g_error_free (error);
3483 accounts = tp_account_manager_get_valid_accounts (account_manager);
3485 for (l = accounts; l != NULL; l = l->next) {
3486 TpAccount *account = l->data;
3487 tp_g_signal_connect_object (account, "status-changed",
3488 G_CALLBACK (chat_new_connection_cb),
3492 g_list_free (accounts);
3496 empathy_chat_init (EmpathyChat *chat)
3498 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3499 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3502 priv->log_manager = tpl_log_manager_dup_singleton ();
3503 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3504 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3506 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3507 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3508 priv->input_history = NULL;
3509 priv->input_history_current = NULL;
3510 priv->account_manager = tp_account_manager_dup ();
3512 tp_proxy_prepare_async (priv->account_manager, NULL,
3513 account_manager_prepared_cb, chat);
3515 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3516 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3518 /* Block events for some time to avoid having "has come online" or
3519 * "joined" messages. */
3520 priv->block_events_timeout_id =
3521 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3523 /* Add nick name completion */
3524 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3525 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3527 chat_create_ui (chat);
3531 empathy_chat_new (EmpathyTpChat *tp_chat)
3533 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3537 empathy_chat_get_tp_chat (EmpathyChat *chat)
3539 EmpathyChatPriv *priv = GET_PRIV (chat);
3541 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3543 return priv->tp_chat;
3549 GtkWidget *info_bar;
3559 passwd_remember_button_cb (GtkButton *button,
3562 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3566 passwd_not_now_button_cb (GtkButton *button,
3569 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3573 remember_password_infobar_response_cb (GtkWidget *info_bar,
3577 EmpathyChatPriv *priv = GET_PRIV (data->self);
3579 if (response_id == GTK_RESPONSE_OK) {
3580 DEBUG ("Saving room password");
3581 empathy_keyring_set_room_password_async (priv->account,
3582 empathy_tp_chat_get_id (priv->tp_chat),
3587 gtk_widget_destroy (info_bar);
3588 g_free (data->password);
3589 g_slice_free (PasswordData, data);
3593 chat_prompt_to_save_password (EmpathyChat *self,
3596 GtkWidget *content_area;
3603 /* save the password in case it needs to be saved */
3604 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3606 /* Remove all previous widgets */
3607 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3608 gtk_container_forall (GTK_CONTAINER (content_area),
3609 (GtkCallback) gtk_widget_destroy, NULL);
3610 data->button = NULL;
3613 data->spinner = NULL;
3615 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3616 GTK_MESSAGE_QUESTION);
3618 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3619 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3622 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3623 GTK_ICON_SIZE_DIALOG);
3624 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3627 label = gtk_label_new (_("Would you like to store this password?"));
3628 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3630 /* Add 'Remember' button */
3631 alig = gtk_alignment_new (0, 0.5, 1, 0);
3633 button = gtk_button_new_with_label (_("Remember"));
3634 gtk_container_add (GTK_CONTAINER (alig), button);
3635 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3637 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3640 /* Add 'Not now' button */
3641 alig = gtk_alignment_new (0, 0.5, 1, 0);
3643 button = gtk_button_new_with_label (_("Not now"));
3644 gtk_container_add (GTK_CONTAINER (alig), button);
3645 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3647 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3651 g_signal_handler_disconnect (data->info_bar, data->response_id);
3652 g_signal_connect (data->info_bar, "response",
3653 G_CALLBACK (remember_password_infobar_response_cb), data);
3655 gtk_widget_show_all (data->info_bar);
3659 provide_password_cb (GObject *tp_chat,
3663 PasswordData *data = user_data;
3664 EmpathyChat *self = data->self;
3665 EmpathyChatPriv *priv = GET_PRIV (self);
3666 GError *error = NULL;
3668 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3670 DEBUG ("error: %s", error->message);
3671 /* FIXME: what should we do if that's another error? Close the channel?
3672 * Display the raw D-Bus error to the user isn't very useful */
3673 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3675 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3676 gtk_widget_set_sensitive (data->entry, TRUE);
3677 gtk_widget_grab_focus (data->entry);
3680 gtk_info_bar_set_message_type (
3681 GTK_INFO_BAR (data->info_bar),
3685 gtk_widget_set_sensitive (data->button, TRUE);
3686 gtk_button_set_label (GTK_BUTTON (data->button),
3690 gtk_label_set_text (GTK_LABEL (data->label),
3691 _("Wrong password; please try again:"));
3694 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3695 gtk_widget_hide (data->spinner);
3697 g_error_free (error);
3701 if (empathy_keyring_is_available ()) {
3702 /* ask whether they want to save the password */
3703 chat_prompt_to_save_password (self, data);
3705 /* Get rid of the password info bar finally */
3706 gtk_widget_destroy (data->info_bar);
3707 g_slice_free (PasswordData, data);
3711 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3712 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3713 gtk_widget_grab_focus (self->input_text_view);
3717 password_infobar_response_cb (GtkWidget *info_bar,
3721 EmpathyChatPriv *priv = GET_PRIV (data->self);
3722 const gchar *password;
3724 if (response_id != GTK_RESPONSE_OK) {
3725 gtk_widget_destroy (info_bar);
3726 g_slice_free (PasswordData, data);
3730 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3732 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3733 provide_password_cb, data);
3735 gtk_widget_set_sensitive (data->button, FALSE);
3736 gtk_widget_set_sensitive (data->entry, FALSE);
3738 gtk_spinner_start (GTK_SPINNER (data->spinner));
3739 gtk_widget_show (data->spinner);
3743 password_entry_activate_cb (GtkWidget *entry,
3746 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3750 passwd_join_button_cb (GtkButton *button,
3753 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3757 clear_icon_released_cb (GtkEntry *entry,
3758 GtkEntryIconPosition icon_pos,
3762 gtk_entry_set_text (entry, "");
3766 password_entry_changed_cb (GtkEditable *entry,
3771 str = gtk_entry_get_text (GTK_ENTRY (entry));
3773 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3774 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3778 infobar_chat_invalidated_cb (TpProxy *proxy,
3782 gpointer password_infobar)
3784 /* Destroy the password infobar whenever a channel is invalidated
3785 * so we don't have multiple infobars when the MUC is rejoined */
3786 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3790 display_password_info_bar (EmpathyChat *self)
3792 EmpathyChatPriv *priv = GET_PRIV (self);
3793 GtkWidget *info_bar;
3794 GtkWidget *content_area;
3804 data = g_slice_new0 (PasswordData);
3806 info_bar = gtk_info_bar_new ();
3807 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3808 GTK_MESSAGE_QUESTION);
3810 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3812 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3813 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3816 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3817 GTK_ICON_SIZE_DIALOG);
3818 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3821 label = gtk_label_new (_("This room is protected by a password:"));
3822 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3824 /* Add password entry */
3825 entry = gtk_entry_new ();
3826 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3827 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3829 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3830 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3831 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3832 GTK_ENTRY_ICON_SECONDARY, FALSE);
3834 g_signal_connect (entry, "icon-release",
3835 G_CALLBACK (clear_icon_released_cb), data);
3836 g_signal_connect (entry, "changed",
3837 G_CALLBACK (password_entry_changed_cb), data);
3839 g_signal_connect (entry, "activate",
3840 G_CALLBACK (password_entry_activate_cb), data);
3842 /* Focus the password entry once it's realized */
3843 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3845 /* Add 'Join' button */
3846 alig = gtk_alignment_new (0, 0.5, 1, 0);
3848 button = gtk_button_new_with_label (_("Join"));
3849 gtk_container_add (GTK_CONTAINER (alig), button);
3850 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3852 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3856 spinner = gtk_spinner_new ();
3857 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3859 /* Save some data for messing around with later */
3861 data->info_bar = info_bar;
3862 data->button = button;
3863 data->label = label;
3864 data->entry = entry;
3865 data->spinner = spinner;
3867 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3869 gtk_widget_show_all (hbox);
3871 tp_g_signal_connect_object (priv->tp_chat,
3872 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3875 data->response_id = g_signal_connect (info_bar, "response",
3876 G_CALLBACK (password_infobar_response_cb), data);
3878 gtk_widget_show_all (info_bar);
3879 /* ... but hide the spinner */
3880 gtk_widget_hide (spinner);
3882 /* prevent the user from typing anything */
3883 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3887 provide_saved_password_cb (GObject *tp_chat,
3891 EmpathyChat *self = user_data;
3892 EmpathyChatPriv *priv = GET_PRIV (self);
3893 GError *error = NULL;
3895 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3897 DEBUG ("error: %s", error->message);
3898 /* FIXME: what should we do if that's another error? Close the channel?
3899 * Display the raw D-Bus error to the user isn't very useful */
3900 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3901 display_password_info_bar (self);
3902 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3904 g_error_free (error);
3909 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3910 gtk_widget_grab_focus (self->input_text_view);
3914 chat_room_got_password_cb (GObject *source,
3915 GAsyncResult *result,
3918 EmpathyChat *self = user_data;
3919 EmpathyChatPriv *priv = GET_PRIV (self);
3920 const gchar *password;
3921 GError *error = NULL;
3923 password = empathy_keyring_get_room_password_finish (priv->account,
3926 if (error != NULL) {
3927 DEBUG ("Couldn't get room password: %s\n", error->message);
3928 g_clear_error (&error);
3930 display_password_info_bar (self);
3931 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3935 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3936 provide_saved_password_cb, self);
3940 chat_password_needed_changed_cb (EmpathyChat *self)
3942 EmpathyChatPriv *priv = GET_PRIV (self);
3944 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3945 empathy_keyring_get_room_password_async (priv->account,
3946 empathy_tp_chat_get_id (priv->tp_chat),
3947 chat_room_got_password_cb, self);
3952 chat_sms_channel_changed_cb (EmpathyChat *self)
3954 EmpathyChatPriv *priv = GET_PRIV (self);
3956 priv->sms_channel = tp_text_channel_is_sms_channel (
3957 (TpTextChannel *) priv->tp_chat);
3958 g_object_notify (G_OBJECT (self), "sms-channel");
3962 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3964 g_object_notify (G_OBJECT (self), "n-messages-sending");
3968 empathy_chat_set_tp_chat (EmpathyChat *chat,
3969 EmpathyTpChat *tp_chat)
3971 EmpathyChatPriv *priv = GET_PRIV (chat);
3973 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3974 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3976 if (priv->tp_chat) {
3980 if (priv->account) {
3981 g_object_unref (priv->account);
3984 priv->tp_chat = g_object_ref (tp_chat);
3985 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3987 g_signal_connect (tp_chat, "invalidated",
3988 G_CALLBACK (chat_invalidated_cb),
3990 g_signal_connect (tp_chat, "message-received-empathy",
3991 G_CALLBACK (chat_message_received_cb),
3993 g_signal_connect (tp_chat, "message_acknowledged",
3994 G_CALLBACK (chat_message_acknowledged_cb),
3996 g_signal_connect (tp_chat, "send-error",
3997 G_CALLBACK (chat_send_error_cb),
3999 g_signal_connect (tp_chat, "chat-state-changed-empathy",
4000 G_CALLBACK (chat_state_changed_cb),
4002 g_signal_connect (tp_chat, "members-changed",
4003 G_CALLBACK (chat_members_changed_cb),
4005 g_signal_connect (tp_chat, "member-renamed",
4006 G_CALLBACK (chat_member_renamed_cb),
4008 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4009 G_CALLBACK (chat_self_contact_changed_cb),
4011 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4012 G_CALLBACK (chat_remote_contact_changed_cb),
4014 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4015 G_CALLBACK (chat_password_needed_changed_cb),
4017 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4018 G_CALLBACK (chat_sms_channel_changed_cb),
4020 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4021 G_CALLBACK (chat_n_messages_sending_changed_cb),
4023 g_signal_connect_swapped (tp_chat, "notify::title",
4024 G_CALLBACK (chat_title_changed_cb),
4026 g_signal_connect_swapped (tp_chat, "notify::subject",
4027 G_CALLBACK (chat_subject_changed_cb),
4030 /* Get initial value of properties */
4031 chat_sms_channel_changed_cb (chat);
4032 chat_self_contact_changed_cb (chat);
4033 chat_remote_contact_changed_cb (chat);
4034 chat_title_changed_cb (chat);
4035 chat_subject_changed_cb (chat);
4037 if (chat->input_text_view) {
4038 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4039 if (priv->block_events_timeout_id == 0) {
4040 empathy_chat_view_append_event (chat->view, _("Connected"));
4044 g_object_notify (G_OBJECT (chat), "tp-chat");
4045 g_object_notify (G_OBJECT (chat), "id");
4046 g_object_notify (G_OBJECT (chat), "account");
4048 /* This is a noop when tp-chat is set at object construction time and causes
4049 * the pending messages to be show when it's set on the object after it has
4051 show_pending_messages (chat);
4053 /* check if a password is needed */
4054 chat_password_needed_changed_cb (chat);
4058 empathy_chat_get_account (EmpathyChat *chat)
4060 EmpathyChatPriv *priv = GET_PRIV (chat);
4062 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4064 return priv->account;
4068 empathy_chat_get_id (EmpathyChat *chat)
4070 EmpathyChatPriv *priv = GET_PRIV (chat);
4072 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4078 empathy_chat_dup_name (EmpathyChat *chat)
4080 EmpathyChatPriv *priv = GET_PRIV (chat);
4083 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4087 if (!ret && priv->remote_contact) {
4088 ret = empathy_contact_get_alias (priv->remote_contact);
4095 ret = _("Conversation");
4097 if (priv->sms_channel)
4098 /* Translators: this string is a something like
4099 * "Escher Cat (SMS)" */
4100 return g_strdup_printf (_("%s (SMS)"), ret);
4102 return g_strdup (ret);
4106 empathy_chat_get_subject (EmpathyChat *chat)
4108 EmpathyChatPriv *priv = GET_PRIV (chat);
4110 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4112 return priv->subject;
4116 empathy_chat_get_remote_contact (EmpathyChat *chat)
4118 EmpathyChatPriv *priv = GET_PRIV (chat);
4120 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4122 return priv->remote_contact;
4126 empathy_chat_get_contact_menu (EmpathyChat *chat)
4128 EmpathyChatPriv *priv = GET_PRIV (chat);
4129 GtkWidget *menu = NULL;
4130 FolksIndividual *individual;
4133 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4135 if (priv->remote_contact == NULL)
4138 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4139 if (contact == NULL)
4142 individual = empathy_create_individual_from_tp_contact (contact);
4143 if (individual == NULL)
4146 menu = empathy_individual_menu_new (individual,
4147 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4148 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4149 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4150 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4152 g_object_unref (individual);
4158 empathy_chat_clear (EmpathyChat *chat)
4160 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4162 empathy_chat_view_clear (chat->view);
4166 empathy_chat_scroll_down (EmpathyChat *chat)
4168 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4170 empathy_chat_view_scroll_down (chat->view);
4174 empathy_chat_cut (EmpathyChat *chat)
4176 GtkTextBuffer *buffer;
4178 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4180 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4181 if (gtk_text_buffer_get_has_selection (buffer)) {
4182 GtkClipboard *clipboard;
4184 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4186 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4191 empathy_chat_copy (EmpathyChat *chat)
4193 GtkTextBuffer *buffer;
4195 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4197 if (empathy_chat_view_get_has_selection (chat->view)) {
4198 empathy_chat_view_copy_clipboard (chat->view);
4202 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4203 if (gtk_text_buffer_get_has_selection (buffer)) {
4204 GtkClipboard *clipboard;
4206 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4208 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4213 EmpathyChatPriv *priv = GET_PRIV (chat);
4215 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4222 GtkClipboard *clipboard;
4224 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4225 start = g_utf8_offset_to_pointer (topic, start_offset);
4226 end = g_utf8_offset_to_pointer (topic, end_offset);
4227 selection = g_strndup (start, end - start);
4229 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4230 gtk_clipboard_set_text (clipboard, selection, -1);
4238 empathy_chat_paste (EmpathyChat *chat)
4240 GtkTextBuffer *buffer;
4241 GtkClipboard *clipboard;
4242 EmpathyChatPriv *priv;
4244 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4246 priv = GET_PRIV (chat);
4248 if (gtk_widget_get_visible (priv->search_bar)) {
4249 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4253 if (priv->tp_chat == NULL ||
4254 !gtk_widget_is_sensitive (chat->input_text_view))
4257 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4258 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4260 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4264 empathy_chat_find (EmpathyChat *chat)
4266 EmpathyChatPriv *priv;
4268 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4270 priv = GET_PRIV (chat);
4272 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4276 empathy_chat_correct_word (EmpathyChat *chat,
4279 const gchar *new_word)
4281 GtkTextBuffer *buffer;
4283 g_return_if_fail (chat != NULL);
4284 g_return_if_fail (new_word != NULL);
4286 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4288 gtk_text_buffer_delete (buffer, start, end);
4289 gtk_text_buffer_insert (buffer, start,
4295 empathy_chat_is_room (EmpathyChat *chat)
4297 EmpathyChatPriv *priv = GET_PRIV (chat);
4299 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4301 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4305 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4307 EmpathyChatPriv *priv = GET_PRIV (self);
4309 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4311 return priv->unread_messages;
4314 /* called when the messages have been read by user */
4316 empathy_chat_messages_read (EmpathyChat *self)
4318 EmpathyChatPriv *priv = GET_PRIV (self);
4320 g_return_if_fail (EMPATHY_IS_CHAT (self));
4322 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4323 * definition. If we are still retrieving the backlogs, do not ACK */
4324 if (priv->retrieving_backlogs)
4327 if (priv->tp_chat != NULL) {
4328 tp_text_channel_ack_all_pending_messages_async (
4329 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4332 if (priv->unread_messages_when_offline > 0) {
4333 /* We can't ack those as the connection has gone away so just consider
4335 priv->unread_messages -= priv->unread_messages_when_offline;
4336 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4337 priv->unread_messages_when_offline = 0;
4341 /* Return TRUE if on of the contacts in this chat is composing */
4343 empathy_chat_is_composing (EmpathyChat *chat)
4345 return chat->priv->compositors != NULL;
4349 empathy_chat_is_sms_channel (EmpathyChat *self)
4351 EmpathyChatPriv *priv = GET_PRIV (self);
4353 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4355 return priv->sms_channel;
4359 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4361 EmpathyChatPriv *priv;
4363 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4365 priv = GET_PRIV (self);
4367 if (priv->tp_chat == NULL) {
4372 g_object_get (priv->tp_chat,
4373 "n-messages-sending", &n_messages,
4381 empathy_chat_dup_text (EmpathyChat *self)
4383 GtkTextBuffer *buffer;
4384 GtkTextIter start, end;
4386 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4388 gtk_text_buffer_get_bounds (buffer, &start, &end);
4389 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4393 empathy_chat_set_text (EmpathyChat *self,
4396 GtkTextBuffer *buffer;
4398 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4400 gtk_text_buffer_set_text (buffer, text, -1);