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>
30 #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
35 #include <gdk/gdkkeysyms.h>
36 #include <glib/gi18n-lib.h>
39 #include <telepathy-glib/account-manager.h>
40 #include <telepathy-glib/util.h>
41 #include <telepathy-logger/telepathy-logger.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;
164 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
165 * Cleared by empathy_chat_messages_read (). */
166 gboolean highlighted;
170 gchar *text; /* Original message that was specified
171 * upon entry creation. */
172 gchar *modified_text; /* Message that was modified by user.
173 * When no modifications were made, it is NULL */
179 PART_COMMAND_ENTERED,
193 PROP_N_MESSAGES_SENDING,
194 PROP_NB_UNREAD_MESSAGES,
197 static guint signals[LAST_SIGNAL] = { 0 };
199 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
201 static gboolean update_misspelled_words (gpointer data);
204 chat_get_property (GObject *object,
209 EmpathyChat *chat = EMPATHY_CHAT (object);
210 EmpathyChatPriv *priv = GET_PRIV (object);
214 g_value_set_object (value, priv->tp_chat);
217 g_value_set_object (value, priv->account);
220 g_value_take_string (value, empathy_chat_dup_name (chat));
223 g_value_set_string (value, priv->id);
226 g_value_set_string (value, priv->subject);
228 case PROP_REMOTE_CONTACT:
229 g_value_set_object (value, priv->remote_contact);
231 case PROP_SHOW_CONTACTS:
232 g_value_set_boolean (value, priv->show_contacts);
234 case PROP_SMS_CHANNEL:
235 g_value_set_boolean (value, priv->sms_channel);
237 case PROP_N_MESSAGES_SENDING:
238 g_value_set_uint (value,
239 empathy_chat_get_n_messages_sending (chat));
241 case PROP_NB_UNREAD_MESSAGES:
242 g_value_set_uint (value,
243 empathy_chat_get_nb_unread_messages (chat));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
252 chat_set_property (GObject *object,
257 EmpathyChat *chat = EMPATHY_CHAT (object);
261 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
263 case PROP_SHOW_CONTACTS:
264 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
273 account_reconnected (EmpathyChat *chat,
276 EmpathyChatPriv *priv = GET_PRIV (chat);
278 DEBUG ("Account reconnected, request a new Text channel");
280 /* FIXME: Ideally we should ask to handle ourself the channel so we can
281 * report the error if any but this is blocked by
282 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
283 switch (priv->handle_type) {
284 case TP_HANDLE_TYPE_CONTACT:
285 if (priv->sms_channel)
286 empathy_sms_contact_id (
288 TP_USER_ACTION_TIME_NOT_USER_ACTION,
291 empathy_chat_with_contact_id (
293 TP_USER_ACTION_TIME_NOT_USER_ACTION,
296 case TP_HANDLE_TYPE_ROOM:
297 empathy_join_muc (account, priv->id,
298 TP_USER_ACTION_TIME_NOT_USER_ACTION);
300 case TP_HANDLE_TYPE_NONE:
301 case TP_HANDLE_TYPE_LIST:
302 case TP_HANDLE_TYPE_GROUP:
304 g_assert_not_reached ();
308 g_object_unref (chat);
312 chat_new_connection_cb (TpAccount *account,
316 gchar *dbus_error_name,
320 EmpathyChatPriv *priv = GET_PRIV (chat);
322 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
325 if (priv->tp_chat != NULL || account != priv->account ||
326 priv->handle_type == TP_HANDLE_TYPE_NONE ||
327 EMP_STR_EMPTY (priv->id))
332 account_reconnected (chat, account);
336 chat_composing_remove_timeout (EmpathyChat *chat)
338 EmpathyChatPriv *priv;
340 priv = GET_PRIV (chat);
342 if (priv->composing_stop_timeout_id) {
343 g_source_remove (priv->composing_stop_timeout_id);
344 priv->composing_stop_timeout_id = 0;
349 set_chate_state_cb (GObject *source,
350 GAsyncResult *result,
353 GError *error = NULL;
355 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
357 DEBUG ("Failed to set chat state: %s", error->message);
358 g_error_free (error);
363 set_chat_state (EmpathyChat *self,
364 TpChannelChatState state)
366 EmpathyChatPriv *priv = GET_PRIV (self);
368 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
369 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
372 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
373 set_chate_state_cb, self);
377 chat_composing_stop_timeout_cb (EmpathyChat *chat)
379 EmpathyChatPriv *priv;
380 gboolean send_chat_states;
382 priv = GET_PRIV (chat);
384 priv->composing_stop_timeout_id = 0;
385 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
386 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
387 if (!send_chat_states) {
388 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
390 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
397 chat_composing_start (EmpathyChat *chat)
399 EmpathyChatPriv *priv;
400 gboolean send_chat_states;
402 priv = GET_PRIV (chat);
404 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
405 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
406 if (!send_chat_states) {
410 if (priv->composing_stop_timeout_id) {
411 /* Just restart the timeout */
412 chat_composing_remove_timeout (chat);
414 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
417 priv->composing_stop_timeout_id = g_timeout_add_seconds (
418 COMPOSING_STOP_TIMEOUT,
419 (GSourceFunc) chat_composing_stop_timeout_cb,
424 chat_composing_stop (EmpathyChat *chat)
426 chat_composing_remove_timeout (chat);
427 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
431 chat_input_history_entry_cmp (InputHistoryEntry *entry,
434 if (!tp_strdiff (entry->text, text)) {
435 if (entry->modified_text != NULL) {
436 /* Modified entry and single string cannot be equal. */
444 static InputHistoryEntry *
445 chat_input_history_entry_new_with_text (const gchar *text)
447 InputHistoryEntry *entry;
448 entry = g_slice_new0 (InputHistoryEntry);
449 entry->text = g_strdup (text);
455 chat_input_history_entry_free (InputHistoryEntry *entry)
457 g_free (entry->text);
458 g_free (entry->modified_text);
459 g_slice_free (InputHistoryEntry, entry);
463 chat_input_history_entry_revert (InputHistoryEntry *entry)
465 g_free (entry->modified_text);
466 entry->modified_text = NULL;
470 chat_input_history_entry_update_text (InputHistoryEntry *entry,
475 if (!tp_strdiff (text, entry->text)) {
476 g_free (entry->modified_text);
477 entry->modified_text = NULL;
481 old = entry->modified_text;
482 entry->modified_text = g_strdup (text);
487 chat_input_history_entry_get_text (InputHistoryEntry *entry)
493 if (entry->modified_text != NULL) {
494 return entry->modified_text;
500 chat_input_history_remove_item (GList *list,
503 list = g_list_remove_link (list, item);
504 chat_input_history_entry_free (item->data);
505 g_list_free_1 (item);
510 chat_input_history_revert (EmpathyChat *chat)
512 EmpathyChatPriv *priv;
516 InputHistoryEntry *entry;
518 priv = GET_PRIV (chat);
519 list = priv->input_history;
522 DEBUG ("No input history");
526 /* Delete temporary entry */
527 if (priv->input_history_current != NULL) {
529 list = chat_input_history_remove_item (list, item1);
530 if (priv->input_history_current == item1) {
531 /* Removed temporary entry was current entry */
532 priv->input_history = list;
533 priv->input_history_current = NULL;
538 /* There is no entry to revert */
542 /* Restore the current history entry to original value */
543 item1 = priv->input_history_current;
545 chat_input_history_entry_revert (entry);
547 /* Remove restored entry if there is other occurance before this entry */
548 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
549 (GCompareFunc) chat_input_history_entry_cmp);
550 if (item2 != item1) {
551 list = chat_input_history_remove_item (list, item1);
554 /* Remove other occurance of the restored entry */
555 item2 = g_list_find_custom (item1->next,
556 chat_input_history_entry_get_text (entry),
557 (GCompareFunc) chat_input_history_entry_cmp);
559 list = chat_input_history_remove_item (list, item2);
563 priv->input_history_current = NULL;
564 priv->input_history = list;
568 chat_input_history_add (EmpathyChat *chat,
572 EmpathyChatPriv *priv;
575 InputHistoryEntry *entry;
577 priv = GET_PRIV (chat);
579 list = priv->input_history;
581 /* Remove any other occurances of this entry, if not temporary */
583 while ((item = g_list_find_custom (list, str,
584 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
585 list = chat_input_history_remove_item (list, item);
588 /* Trim the list to the last 10 items */
589 while (g_list_length (list) > 10) {
590 item = g_list_last (list);
592 list = chat_input_history_remove_item (list, item);
600 entry = chat_input_history_entry_new_with_text (str);
601 list = g_list_prepend (list, entry);
603 /* Set the list and the current item pointer */
604 priv->input_history = list;
606 priv->input_history_current = list;
609 priv->input_history_current = NULL;
614 chat_input_history_get_next (EmpathyChat *chat)
616 EmpathyChatPriv *priv;
620 priv = GET_PRIV (chat);
622 if (priv->input_history == NULL) {
623 DEBUG ("No input history, next entry is NULL");
626 g_assert (priv->input_history_current != NULL);
628 if ((item = g_list_next (priv->input_history_current)) == NULL)
630 item = priv->input_history_current;
633 msg = chat_input_history_entry_get_text (item->data);
635 DEBUG ("Returning next entry: '%s'", msg);
637 priv->input_history_current = item;
643 chat_input_history_get_prev (EmpathyChat *chat)
645 EmpathyChatPriv *priv;
649 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
651 priv = GET_PRIV (chat);
653 if (priv->input_history == NULL) {
654 DEBUG ("No input history, previous entry is NULL");
658 if (priv->input_history_current == NULL)
662 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
664 item = priv->input_history_current;
667 msg = chat_input_history_entry_get_text (item->data);
669 DEBUG ("Returning previous entry: '%s'", msg);
671 priv->input_history_current = item;
677 chat_input_history_update (EmpathyChat *chat,
678 GtkTextBuffer *buffer)
680 EmpathyChatPriv *priv;
681 GtkTextIter start, end;
683 InputHistoryEntry *entry;
685 priv = GET_PRIV (chat);
687 gtk_text_buffer_get_bounds (buffer, &start, &end);
688 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
690 if (priv->input_history_current == NULL) {
691 /* Add the current text temporarily to the history */
692 chat_input_history_add (chat, text, TRUE);
697 /* Save the changes in the history */
698 entry = priv->input_history_current->data;
699 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
700 chat_input_history_entry_update_text (entry, text);
709 } ChatCommandMsgData;
712 chat_command_msg_cb (GObject *source,
713 GAsyncResult *result,
716 ChatCommandMsgData *data = user_data;
717 GError *error = NULL;
720 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
721 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
723 if (channel == NULL) {
724 DEBUG ("Failed to get channel: %s", error->message);
725 g_error_free (error);
727 empathy_chat_view_append_event (data->chat->view,
728 _("Failed to open private chat"));
732 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
733 TpTextChannel *text = (TpTextChannel *) channel;
736 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
739 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
741 g_object_unref (msg);
744 g_object_unref (channel);
747 g_free (data->message);
748 g_slice_free (ChatCommandMsgData, data);
752 nick_command_supported (EmpathyChat *chat)
754 EmpathyChatPriv * priv = GET_PRIV (chat);
755 TpConnection *connection;
757 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
758 return tp_proxy_has_interface_by_id (connection,
759 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
763 part_command_supported (EmpathyChat *chat)
765 EmpathyChatPriv * priv = GET_PRIV (chat);
767 return tp_proxy_has_interface_by_id (priv->tp_chat,
768 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
772 chat_command_clear (EmpathyChat *chat,
775 empathy_chat_view_clear (chat->view);
779 chat_command_topic (EmpathyChat *chat,
782 EmpathyChatPriv *priv = GET_PRIV (chat);
784 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
785 empathy_chat_view_append_event (chat->view,
786 _("Topic not supported on this conversation"));
790 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
791 empathy_chat_view_append_event (chat->view,
792 _("You are not allowed to change the topic"));
796 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
800 empathy_chat_join_muc (EmpathyChat *chat,
803 EmpathyChatPriv *priv = GET_PRIV (chat);
805 empathy_join_muc (priv->account, room,
806 empathy_get_current_action_time ());
810 chat_command_join (EmpathyChat *chat,
815 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
817 /* FIXME: Ideally we should ask to handle ourself the channel so we can
818 * report the error if any but this is blocked by
819 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
820 while (rooms[i] != NULL) {
821 /* ignore empty strings */
822 if (!EMP_STR_EMPTY (rooms[i])) {
823 empathy_chat_join_muc (chat, rooms[i]);
831 chat_command_part (EmpathyChat *chat,
834 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
838 chat_command_msg_internal (EmpathyChat *chat,
839 const gchar *contact_id,
840 const gchar *message)
842 EmpathyChatPriv *priv = GET_PRIV (chat);
843 ChatCommandMsgData *data;
844 TpAccountChannelRequest *req;
847 request = tp_asv_new (
848 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
849 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
850 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
853 req = tp_account_channel_request_new (priv->account, request,
854 empathy_get_current_action_time ());
856 /* FIXME: We should probably search in members alias. But this
857 * is enough for IRC */
858 data = g_slice_new (ChatCommandMsgData);
860 data->message = g_strdup (message);
862 tp_account_channel_request_ensure_and_observe_channel_async (req,
863 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
865 g_object_unref (req);
866 g_hash_table_unref (request);
870 chat_command_query (EmpathyChat *chat,
873 /* If <message> part is not defined,
874 * strv[2] will be the terminal NULL */
875 chat_command_msg_internal (chat, strv[1], strv[2]);
879 chat_command_msg (EmpathyChat *chat,
882 chat_command_msg_internal (chat, strv[1], strv[2]);
886 callback_for_request_rename (TpProxy *proxy,
889 GObject *weak_object)
892 DEBUG ("Call to RequestRename method failed: %s",error->message);
897 chat_command_nick (EmpathyChat *chat,
900 EmpathyChatPriv *priv = GET_PRIV (chat);
903 proxy = TP_PROXY (tp_account_get_connection (priv->account));
905 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
906 strv[1], callback_for_request_rename, NULL, NULL, NULL);
910 chat_command_me (EmpathyChat *chat,
913 EmpathyChatPriv *priv = GET_PRIV (chat);
915 TpTextChannel *channel;
917 channel = (TpTextChannel *) (priv->tp_chat);
919 if (!tp_text_channel_supports_message_type (channel,
920 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
921 /* Action message are not supported, 'simulate' the action */
924 /* The TpChat can't be ready if it doesn't have the self contact */
925 g_assert (priv->self_contact != NULL);
927 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
929 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
934 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
938 empathy_tp_chat_send (priv->tp_chat, message);
939 g_object_unref (message);
943 chat_command_say (EmpathyChat *chat,
946 EmpathyChatPriv *priv = GET_PRIV (chat);
949 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
951 empathy_tp_chat_send (priv->tp_chat, message);
952 g_object_unref (message);
956 whois_got_contact_cb (TpConnection *connection,
958 TpContact * const *contacts,
959 const gchar * const *requested_ids,
960 GHashTable *failed_id_errors,
963 GObject *weak_object)
965 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
967 g_return_if_fail (n_contacts <= 1);
969 if (n_contacts == 0) {
971 gpointer key = NULL, value = NULL;
975 /* tp-glib guarantees that the contacts you requested will be
976 * in failed_id_errors regardless of whether the individual
977 * contact was invalid or the whole operation failed.
979 g_hash_table_iter_init (&iter, failed_id_errors);
980 g_hash_table_iter_next (&iter, &key, &value);
984 DEBUG ("Error getting TpContact for '%s': %s %u %s",
985 id, g_quark_to_string (id_error->domain),
986 id_error->code, id_error->message);
989 /* The specific ID failed. */
990 gchar *event = g_strdup_printf (
991 _("“%s” is not a valid contact ID"), id);
992 empathy_chat_view_append_event (chat->view, event);
995 /* Otherwise we're disconnected or something; so the window
996 * will already say ‘Disconnected’, so let's not show anything.
999 EmpathyContact *empathy_contact;
1002 g_return_if_fail (contacts[0] != NULL);
1003 empathy_contact = empathy_contact_dup_from_tp_contact (
1006 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
1007 /* If we're alive and this command is running, we'd better be
1009 g_return_if_fail (window != NULL);
1010 g_return_if_fail (gtk_widget_is_toplevel (window));
1011 empathy_contact_information_dialog_show (empathy_contact,
1012 GTK_WINDOW (window));
1013 g_object_unref (empathy_contact);
1018 chat_command_whois (EmpathyChat *chat,
1021 EmpathyChatPriv *priv = GET_PRIV (chat);
1024 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1025 tp_connection_get_contacts_by_id (conn,
1026 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1027 * entered by the user (including spaces, if any). */
1028 1, (const gchar * const *) strv + 1,
1030 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1034 chat_command_whale (EmpathyChat *chat,
1037 EmpathyChatPriv *priv = GET_PRIV (chat);
1040 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1042 "•_______________•");
1043 empathy_tp_chat_send (priv->tp_chat, message);
1044 g_object_unref (message);
1048 chat_command_babywhale (EmpathyChat *chat,
1051 EmpathyChatPriv *priv = GET_PRIV (chat);
1054 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1057 empathy_tp_chat_send (priv->tp_chat, message);
1058 g_object_unref (message);
1062 chat_command_inspector (EmpathyChat *chat,
1065 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1066 empathy_theme_adium_show_inspector (
1067 EMPATHY_THEME_ADIUM (chat->view));
1071 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1073 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1076 const gchar *prefix;
1079 ChatCommandFunc func;
1080 gboolean (*is_supported)(EmpathyChat *chat);
1084 static ChatCommandItem commands[] = {
1085 {"clear", 1, 1, chat_command_clear, NULL,
1086 N_("/clear: clear all messages from the current conversation")},
1088 {"topic", 2, 2, chat_command_topic, NULL,
1089 N_("/topic <topic>: set the topic of the current conversation")},
1091 {"join", 2, 2, chat_command_join, NULL,
1092 N_("/join <chat room ID>: join a new chat room")},
1094 {"j", 2, 2, chat_command_join, NULL,
1095 N_("/j <chat room ID>: join a new chat room")},
1097 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1098 {"part", 1, 3, chat_command_part, part_command_supported,
1099 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1100 "by default the current one")},
1102 {"query", 2, 3, chat_command_query, NULL,
1103 N_("/query <contact ID> [<message>]: open a private chat")},
1105 {"msg", 3, 3, chat_command_msg, NULL,
1106 N_("/msg <contact ID> <message>: open a private chat")},
1108 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1109 N_("/nick <nickname>: change your nickname on the current server")},
1111 {"me", 2, 2, chat_command_me, NULL,
1112 N_("/me <message>: send an ACTION message to the current conversation")},
1114 {"say", 2, 2, chat_command_say, NULL,
1115 N_("/say <message>: send <message> to the current conversation. "
1116 "This is used to send a message starting with a '/'. For example: "
1117 "\"/say /join is used to join a new chat room\"")},
1119 {"whois", 2, 2, chat_command_whois, NULL,
1120 N_("/whois <contact ID>: display information about a contact")},
1122 {"help", 1, 2, chat_command_help, NULL,
1123 N_("/help [<command>]: show all supported commands. "
1124 "If <command> is defined, show its usage.")},
1126 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1128 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1129 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1133 chat_command_show_help (EmpathyChat *chat,
1134 ChatCommandItem *item)
1138 if (item->help == NULL) {
1142 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1143 empathy_chat_view_append_event (chat->view, str);
1148 chat_command_help (EmpathyChat *chat,
1153 /* If <command> part is not defined,
1154 * strv[1] will be the terminal NULL */
1155 if (strv[1] == NULL) {
1156 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1157 if (commands[i].is_supported != NULL) {
1158 if (!commands[i].is_supported (chat)) {
1162 if (commands[i].help == NULL) {
1165 empathy_chat_view_append_event (chat->view,
1166 _(commands[i].help));
1171 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1172 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1173 if (commands[i].is_supported != NULL) {
1174 if (!commands[i].is_supported (chat)) {
1178 if (commands[i].help == NULL) {
1181 chat_command_show_help (chat, &commands[i]);
1186 empathy_chat_view_append_event (chat->view,
1187 _("Unknown command"));
1191 chat_command_parse (const gchar *text, guint max_parts)
1196 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1198 array = g_ptr_array_sized_new (max_parts + 1);
1199 while (max_parts > 1) {
1202 /* Skip white spaces */
1203 while (g_ascii_isspace (*text)) {
1207 /* Search the end of this part, until first space. */
1208 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1214 item = g_strndup (text, end - text);
1215 g_ptr_array_add (array, item);
1216 DEBUG ("\tITEM: \"%s\"", item);
1222 /* Append last part if not empty */
1223 item = g_strstrip (g_strdup (text));
1224 if (!EMP_STR_EMPTY (item)) {
1225 g_ptr_array_add (array, item);
1226 DEBUG ("\tITEM: \"%s\"", item);
1231 /* Make the array NULL-terminated */
1232 g_ptr_array_add (array, NULL);
1234 return (GStrv) g_ptr_array_free (array, FALSE);
1238 has_prefix_case (const gchar *s,
1239 const gchar *prefix)
1241 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1245 chat_send (EmpathyChat *chat,
1248 EmpathyChatPriv *priv;
1252 if (EMP_STR_EMPTY (msg)) {
1256 priv = GET_PRIV (chat);
1258 chat_input_history_add (chat, msg, FALSE);
1260 if (msg[0] == '/') {
1261 gboolean second_slash = FALSE;
1262 const gchar *iter = msg + 1;
1264 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1269 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1272 c = *(msg + 1 + strlen (commands[i].prefix));
1273 if (c != '\0' && !g_ascii_isspace (c)) {
1276 if (commands[i].is_supported != NULL) {
1277 if (!commands[i].is_supported (chat)) {
1282 /* We can't use g_strsplit here because it does
1283 * not deal correctly if we have more than one space
1285 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1287 strv_len = g_strv_length (strv);
1288 if (strv_len < commands[i].min_parts ||
1289 strv_len > commands[i].max_parts) {
1290 chat_command_show_help (chat, &commands[i]);
1295 commands[i].func (chat, strv);
1300 /* Also allow messages with two slashes before the
1301 * first space, so it is possible to send a /unix/path.
1302 * This heuristic is kind of crap. */
1303 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1305 second_slash = TRUE;
1311 if (!second_slash) {
1312 empathy_chat_view_append_event (chat->view,
1313 _("Unknown command; see /help for the available"
1319 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1321 empathy_tp_chat_send (priv->tp_chat, message);
1322 g_object_unref (message);
1326 chat_input_text_view_send (EmpathyChat *chat)
1328 GtkTextBuffer *buffer;
1329 GtkTextIter start, end;
1332 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1334 gtk_text_buffer_get_bounds (buffer, &start, &end);
1335 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1337 /* clear the input field */
1338 gtk_text_buffer_set_text (buffer, "", -1);
1339 /* delete input history modifications */
1340 chat_input_history_revert (chat);
1342 chat_send (chat, msg);
1347 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1348 EmpathyContact *contact,
1349 TpChannelChatState state,
1352 EmpathyChatPriv *priv;
1354 gboolean was_composing;
1356 priv = GET_PRIV (chat);
1358 if (empathy_contact_is_user (contact)) {
1359 /* We don't care about our own chat state */
1363 was_composing = (priv->compositors != NULL);
1365 /* Find the contact in the list. After that l is the list elem or NULL */
1366 for (l = priv->compositors; l; l = l->next) {
1367 if (contact == l->data) {
1373 case TP_CHANNEL_CHAT_STATE_GONE:
1374 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1375 case TP_CHANNEL_CHAT_STATE_PAUSED:
1376 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1377 /* Contact is not composing */
1379 priv->compositors = g_list_remove_link (priv->compositors, l);
1380 g_object_unref (l->data);
1384 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1385 /* Contact is composing */
1387 priv->compositors = g_list_prepend (priv->compositors,
1388 g_object_ref (contact));
1392 g_assert_not_reached ();
1395 DEBUG ("Was composing: %s now composing: %s",
1396 was_composing ? "yes" : "no",
1397 priv->compositors ? "yes" : "no");
1399 if ((was_composing && !priv->compositors) ||
1400 (!was_composing && priv->compositors)) {
1401 /* Composing state changed */
1402 g_signal_emit (chat, signals[COMPOSING], 0,
1403 priv->compositors != NULL);
1408 get_highlight_regex_for (const gchar *name)
1411 gchar *name_esc, *pattern;
1412 GError *error = NULL;
1414 name_esc = g_regex_escape_string (name, -1);
1415 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1416 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1419 if (regex == NULL) {
1420 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1423 g_error_free (error);
1432 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1433 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1436 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1438 EmpathyChatPriv *priv = GET_PRIV (chat);
1440 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1442 if (priv->self_contact != NULL) {
1443 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1445 g_return_if_fail (alias != NULL);
1446 priv->highlight_regex = get_highlight_regex_for (alias);
1451 chat_should_highlight (EmpathyChat *chat,
1452 EmpathyMessage *message)
1454 EmpathyChatPriv *priv = GET_PRIV (chat);
1456 TpChannelTextMessageFlags flags;
1458 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1460 if (!empathy_chat_is_room (chat)) {
1464 if (!empathy_message_is_incoming (message)) {
1468 msg = empathy_message_get_body (message);
1473 flags = empathy_message_get_flags (message);
1474 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1475 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1476 * have already been received by the user before (and so are in the logs) */
1480 if (priv->highlight_regex == NULL) {
1484 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1488 chat_message_received (EmpathyChat *chat,
1489 EmpathyMessage *message,
1492 EmpathyChatPriv *priv = GET_PRIV (chat);
1493 EmpathyContact *sender;
1495 sender = empathy_message_get_sender (message);
1497 if (empathy_message_is_edit (message)) {
1498 DEBUG ("Editing message '%s' to '%s'",
1499 empathy_message_get_supersedes (message),
1500 empathy_message_get_body (message));
1502 empathy_chat_view_edit_message (chat->view, message);
1504 gboolean should_highlight = chat_should_highlight (chat, message);
1506 if (should_highlight) {
1507 priv->highlighted = TRUE;
1510 DEBUG ("Appending new message '%s' from %s (%d)",
1511 empathy_message_get_token (message),
1512 empathy_contact_get_alias (sender),
1513 empathy_contact_get_handle (sender));
1515 empathy_chat_view_append_message (chat->view, message, should_highlight);
1517 if (empathy_message_is_incoming (message)) {
1518 priv->unread_messages++;
1519 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1522 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1526 /* We received a message so the contact is no longer
1528 chat_state_changed_cb (priv->tp_chat, sender,
1529 TP_CHANNEL_CHAT_STATE_ACTIVE,
1534 chat_message_received_cb (EmpathyTpChat *tp_chat,
1535 EmpathyMessage *message,
1538 chat_message_received (chat, message, FALSE);
1542 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1543 EmpathyMessage *message,
1546 EmpathyChatPriv *priv = GET_PRIV (chat);
1548 empathy_chat_view_message_acknowledged (chat->view,
1551 if (!empathy_message_is_edit (message)) {
1552 priv->unread_messages--;
1553 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1558 append_balance_error (EmpathyChat *chat,
1559 const gchar *message_body)
1561 EmpathyChatPriv *priv = GET_PRIV (chat);
1562 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1563 const gchar *uri = tp_connection_get_balance_uri (conn);
1564 const gchar *error = _("insufficient balance to send message");
1565 gchar *str, *str_markup = NULL;
1567 if (message_body != NULL) {
1568 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1570 str = g_strdup_printf (_("Error sending message: %s"), error);
1573 if (!tp_str_empty (uri)) {
1574 /* translators: error used when user doesn't have enough credit on his
1575 * account to send the message. */
1576 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1577 " <a href='%s'>Top up</a>."), uri);
1579 if (message_body != NULL) {
1580 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1582 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1583 escaped_body, markup_error);
1585 g_free (escaped_body);
1587 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1590 g_free (markup_error);
1593 if (str_markup != NULL)
1594 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1596 empathy_chat_view_append_event (chat->view, str);
1599 g_free (str_markup);
1603 chat_send_error_cb (EmpathyTpChat *tp_chat,
1604 const gchar *message_body,
1605 TpChannelTextSendError error_code,
1606 const gchar *dbus_error,
1609 const gchar *error = NULL;
1612 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1613 append_balance_error (chat, message_body);
1615 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1616 error = _("not capable");
1619 if (error == NULL) {
1620 /* if we didn't find a dbus-error, try the old error */
1621 switch (error_code) {
1622 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1623 error = _("offline");
1625 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1626 error = _("invalid contact");
1628 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1629 error = _("permission denied");
1631 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1632 error = _("too long message");
1634 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1635 error = _("not implemented");
1637 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1639 error = _("unknown");
1644 if (message_body != NULL) {
1645 str = g_strdup_printf (_("Error sending message '%s': %s"),
1646 message_body, error);
1649 str = g_strdup_printf (_("Error sending message: %s"), error);
1652 empathy_chat_view_append_event (chat->view, str);
1657 chat_topic_label_size_allocate_cb (GtkLabel *label,
1658 GtkAllocation *allocation,
1661 EmpathyChatPriv *priv = GET_PRIV (chat);
1663 if (!gtk_label_get_line_wrap (label)) {
1664 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1665 gtk_widget_show (priv->expander_topic);
1667 gtk_widget_hide (priv->expander_topic);
1674 chat_topic_expander_activate_cb (GtkExpander *expander,
1675 GParamSpec *param_spec,
1678 EmpathyChatPriv *priv = GET_PRIV (chat);
1680 if (gtk_expander_get_expanded (expander)) {
1681 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1682 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1684 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1685 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1690 chat_subject_changed_cb (EmpathyChat *chat)
1692 EmpathyChatPriv *priv = GET_PRIV (chat);
1694 g_free (priv->subject);
1695 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1696 g_object_notify (G_OBJECT (chat), "subject");
1698 if (EMP_STR_EMPTY (priv->subject)) {
1699 gtk_widget_hide (priv->hbox_topic);
1701 gchar *markup_topic;
1704 markup_topic = empathy_add_link_markup (priv->subject);
1705 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1706 _("Topic:"), markup_topic);
1708 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1709 g_free (markup_text);
1710 g_free (markup_topic);
1712 gtk_widget_show (priv->hbox_topic);
1714 if (priv->block_events_timeout_id == 0) {
1717 if (!EMP_STR_EMPTY (priv->subject)) {
1718 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1720 if (tp_str_empty (actor)) {
1721 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1723 str = g_strdup_printf (_("Topic set by %s to: %s"),
1724 actor, priv->subject);
1726 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1727 /* No need to display this 'event' is no topic can be defined anyway */
1728 str = g_strdup (_("No topic defined"));
1732 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1739 chat_title_changed_cb (EmpathyChat *chat)
1741 EmpathyChatPriv *priv = GET_PRIV (chat);
1743 g_free (priv->name);
1744 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1745 g_object_notify (G_OBJECT (chat), "name");
1749 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1753 GtkTextIter word_start = *iter;
1754 GtkTextIter word_end = *iter;
1757 if (gtk_text_iter_inside_word (&word_end) &&
1758 !gtk_text_iter_ends_word (&word_end)) {
1759 gtk_text_iter_forward_word_end (&word_end);
1764 if (gtk_text_iter_get_char (&tmp) == '\'') {
1765 gtk_text_iter_forward_char (&tmp);
1767 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1768 gtk_text_iter_forward_word_end (&word_end);
1773 if (gtk_text_iter_inside_word (&word_start) ||
1774 gtk_text_iter_ends_word (&word_start)) {
1775 if (!gtk_text_iter_starts_word (&word_start) ||
1776 gtk_text_iter_equal (&word_start, &word_end)) {
1777 gtk_text_iter_backward_word_start (&word_start);
1781 gtk_text_iter_backward_char (&tmp);
1783 if (gtk_text_iter_get_char (&tmp) == '\'') {
1784 gtk_text_iter_backward_char (&tmp);
1786 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1787 gtk_text_iter_backward_word_start (&word_start);
1792 *start = word_start;
1798 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1799 GtkTextIter *location,
1804 GtkTextIter iter, pos;
1806 /* Remove all misspelled tags in the inserted text.
1807 * This happens when text is inserted within a misspelled word. */
1808 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1809 gtk_text_iter_get_offset (location) - len);
1810 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1813 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1816 GtkTextIter start, end;
1819 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1822 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1824 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1825 gtk_text_iter_equal (&pos, &end) ||
1826 empathy_spell_check (str)) {
1827 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1829 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1834 } while (gtk_text_iter_forward_word_end (&iter) &&
1835 gtk_text_iter_compare (&iter, location) <= 0);
1839 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1844 GtkTextIter word_start, word_end;
1846 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1847 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1848 &word_start, &word_end);
1853 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1856 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1857 chat_composing_stop (chat);
1859 chat_composing_start (chat);
1864 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1869 GtkTextIter prev_pos;
1870 GtkTextIter word_start;
1871 GtkTextIter word_end;
1875 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1877 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1878 gtk_text_buffer_get_insert (buffer));
1879 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1881 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1884 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1885 !gtk_text_iter_equal (&pos, &word_end)) {
1886 str = gtk_text_buffer_get_text (buffer,
1887 &word_start, &word_end, FALSE);
1889 if (!empathy_spell_check (str)) {
1890 gtk_text_buffer_apply_tag_by_name (buffer,
1891 "misspelled", &word_start, &word_end);
1893 gtk_text_buffer_remove_tag_by_name (buffer,
1894 "misspelled", &word_start, &word_end);
1901 gtk_text_buffer_move_mark (buffer, mark, &pos);
1905 empathy_isspace_cb (gunichar c,
1908 return g_unichar_isspace (c);
1912 chat_input_key_press_event_cb (GtkWidget *widget,
1916 EmpathyChatPriv *priv;
1919 GtkWidget *text_view_sw;
1921 priv = GET_PRIV (chat);
1923 priv->most_recent_event_type = event->type;
1925 /* Catch ctrl+up/down so we can traverse messages we sent */
1926 if ((event->state & GDK_CONTROL_MASK) &&
1927 (event->keyval == GDK_KEY_Up ||
1928 event->keyval == GDK_KEY_Down)) {
1929 GtkTextBuffer *buffer;
1932 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1933 chat_input_history_update (chat, buffer);
1935 if (event->keyval == GDK_KEY_Up) {
1936 str = chat_input_history_get_next (chat);
1938 str = chat_input_history_get_prev (chat);
1941 g_signal_handlers_block_by_func (buffer,
1942 chat_input_text_buffer_changed_cb,
1944 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1945 g_signal_handlers_unblock_by_func (buffer,
1946 chat_input_text_buffer_changed_cb,
1952 /* Catch enter but not ctrl/shift-enter */
1953 if (IS_ENTER (event->keyval) &&
1954 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1957 /* This is to make sure that kinput2 gets the enter. And if
1958 * it's handled there we shouldn't send on it. This is because
1959 * kinput2 uses Enter to commit letters. See:
1960 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1963 view = GTK_TEXT_VIEW (chat->input_text_view);
1964 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1965 gtk_text_view_reset_im_context (view);
1969 chat_input_text_view_send (chat);
1973 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1975 if (IS_ENTER (event->keyval) &&
1976 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1977 /* Newline for shift/control-enter. */
1980 if (!(event->state & GDK_CONTROL_MASK) &&
1981 event->keyval == GDK_KEY_Page_Up) {
1982 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1983 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1986 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1987 event->keyval == GDK_KEY_Page_Down) {
1988 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1989 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1990 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1991 gtk_adjustment_set_value (adj, val);
1994 if (event->keyval == GDK_KEY_Escape) {
1995 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1997 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1998 event->keyval == GDK_KEY_Tab) {
1999 GtkTextBuffer *buffer;
2000 GtkTextIter start, current;
2001 gchar *nick, *completed;
2002 GList *list, *completed_list;
2003 gboolean is_start_of_buffer;
2005 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
2006 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
2008 /* Get the start of the nick to complete. */
2009 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
2010 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
2011 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
2013 is_start_of_buffer = gtk_text_iter_is_start (&start);
2015 list = empathy_tp_chat_get_members (priv->tp_chat);
2016 g_completion_add_items (priv->completion, list);
2018 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
2019 completed_list = g_completion_complete (priv->completion,
2028 GString *message = NULL;
2031 gtk_text_buffer_delete (buffer, &start, ¤t);
2033 len = g_list_length (completed_list);
2036 /* If we only have one hit, use that text
2037 * instead of the text in completed since the
2038 * completed text will use the typed string
2039 * which might be cased all wrong.
2042 text = empathy_contact_get_alias (completed_list->data);
2046 /* Print all hits to the scrollback view, so the
2047 * user knows what possibilities he has.
2050 message = g_string_new ("");
2051 for (l = completed_list; l != NULL; l = l->next) {
2052 g_string_append (message, empathy_contact_get_alias (l->data));
2053 g_string_append (message, " - ");
2055 empathy_chat_view_append_event (chat->view, message->str);
2056 g_string_free (message, TRUE);
2059 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2061 if (len == 1 && is_start_of_buffer) {
2062 gchar *complete_char;
2064 complete_char = g_settings_get_string (
2065 priv->gsettings_chat,
2066 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2068 if (complete_char != NULL) {
2069 gtk_text_buffer_insert_at_cursor (buffer,
2071 strlen (complete_char));
2072 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2073 g_free (complete_char);
2080 g_completion_clear_items (priv->completion);
2082 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2092 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2096 gtk_widget_grab_focus (chat->input_text_view);
2102 chat_input_realize_cb (GtkWidget *widget,
2105 DEBUG ("Setting focus to the input text view");
2106 if (gtk_widget_is_sensitive (widget)) {
2107 gtk_widget_grab_focus (widget);
2112 chat_input_has_focus_notify_cb (GtkWidget *widget,
2116 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2120 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2121 EmpathySmiley *smiley,
2124 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2125 GtkTextBuffer *buffer;
2128 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2130 gtk_text_buffer_get_end_iter (buffer, &iter);
2131 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2133 gtk_text_buffer_get_end_iter (buffer, &iter);
2134 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2145 static EmpathyChatSpell *
2146 chat_spell_new (EmpathyChat *chat,
2151 EmpathyChatSpell *chat_spell;
2153 chat_spell = g_slice_new0 (EmpathyChatSpell);
2155 chat_spell->chat = g_object_ref (chat);
2156 chat_spell->word = g_strdup (word);
2157 chat_spell->start = start;
2158 chat_spell->end = end;
2164 chat_spell_free (EmpathyChatSpell *chat_spell)
2166 g_object_unref (chat_spell->chat);
2167 g_free (chat_spell->word);
2168 g_slice_free (EmpathyChatSpell, chat_spell);
2172 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2173 EmpathyChatSpell *chat_spell)
2175 empathy_chat_correct_word (chat_spell->chat,
2176 &(chat_spell->start),
2178 gtk_menu_item_get_label (menu_item));
2183 chat_spelling_build_suggestions_menu (const gchar *code,
2184 EmpathyChatSpell *chat_spell)
2186 GList *suggestions, *l;
2187 GtkWidget *menu, *menu_item;
2189 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2190 if (suggestions == NULL)
2193 menu = gtk_menu_new ();
2194 for (l = suggestions; l; l = l->next) {
2195 menu_item = gtk_menu_item_new_with_label (l->data);
2196 g_signal_connect (G_OBJECT (menu_item), "activate",
2197 G_CALLBACK (chat_spelling_menu_activate_cb),
2199 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2201 empathy_spell_free_suggestions (suggestions);
2203 gtk_widget_show_all (menu);
2209 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2211 GtkWidget *menu, *submenu, *item;
2214 codes = empathy_spell_get_enabled_language_codes ();
2215 g_assert (codes != NULL);
2217 if (g_list_length (codes) > 1) {
2218 menu = gtk_menu_new ();
2220 for (l = codes; l; l = l->next) {
2221 const gchar *code, *name;
2224 name = empathy_spell_get_language_name (code);
2228 item = gtk_image_menu_item_new_with_label (name);
2230 submenu = chat_spelling_build_suggestions_menu (
2232 if (submenu == NULL)
2233 gtk_widget_set_sensitive (item, FALSE);
2235 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2237 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2240 menu = chat_spelling_build_suggestions_menu (codes->data,
2243 menu = gtk_menu_new ();
2244 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2245 gtk_widget_set_sensitive (item, FALSE);
2246 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2249 g_list_free (codes);
2251 gtk_widget_show_all (menu);
2262 static EmpathyChatWord *
2263 chat_word_new (EmpathyChat *chat,
2267 EmpathyChatWord *chat_word;
2269 chat_word = g_slice_new0 (EmpathyChatWord);
2271 chat_word->chat = g_object_ref (chat);
2272 chat_word->word = g_strdup (word);
2273 chat_word->code = g_strdup (code);
2279 chat_word_free (EmpathyChatWord *chat_word)
2281 g_object_unref (chat_word->chat);
2282 g_free (chat_word->word);
2283 g_free (chat_word->code);
2284 g_slice_free (EmpathyChatWord, chat_word);
2288 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2289 EmpathyChatWord *chat_word)
2291 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2293 empathy_spell_add_to_dictionary (chat_word->code,
2295 priv->update_misspelled_words_id =
2296 g_idle_add (update_misspelled_words, chat_word->chat);
2300 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2302 GtkWidget *menu, *item, *lang_item, *image;
2305 const gchar *code, *name;
2306 EmpathyChatWord *chat_word;
2308 codes = empathy_spell_get_enabled_language_codes ();
2309 g_assert (codes != NULL);
2310 if (g_list_length (codes) > 1) {
2311 /* translators: %s is the selected word */
2312 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2314 item = gtk_image_menu_item_new_with_mnemonic (label);
2316 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2317 GTK_ICON_SIZE_MENU);
2318 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2321 menu = gtk_menu_new ();
2323 for (l = codes; l; l = l->next) {
2325 name = empathy_spell_get_language_name (code);
2329 lang_item = gtk_image_menu_item_new_with_label (name);
2331 chat_word= chat_word_new (chat_spell->chat,
2332 chat_spell->word, code);
2333 g_object_set_data_full (G_OBJECT (lang_item),
2334 "chat-word", chat_word,
2335 (GDestroyNotify) chat_word_free);
2337 g_signal_connect (G_OBJECT (lang_item), "activate",
2338 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2340 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2342 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2345 name = empathy_spell_get_language_name (code);
2346 g_assert (name != NULL);
2347 /* translators: first %s is the selected word,
2348 * second %s is the language name of the target dictionary */
2349 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2350 chat_spell->word, name);
2351 item = gtk_image_menu_item_new_with_mnemonic (label);
2353 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2354 GTK_ICON_SIZE_MENU);
2355 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2357 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2359 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2360 (GDestroyNotify) chat_word_free);
2362 g_signal_connect (G_OBJECT (item), "activate",
2363 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2366 g_list_free (codes);
2368 gtk_widget_show_all (item);
2374 chat_text_send_cb (GtkMenuItem *menuitem,
2377 chat_input_text_view_send (chat);
2381 chat_input_button_press_event_cb (GtkTextView *view,
2382 GdkEventButton *event,
2385 EmpathyChatPriv *priv = GET_PRIV (chat);
2387 priv->most_recent_event_type = event->type;
2393 chat_input_populate_popup_cb (GtkTextView *view,
2397 EmpathyChatPriv *priv = GET_PRIV (chat);
2398 GtkTextBuffer *buffer;
2399 GtkTextTagTable *table;
2402 GtkTextIter iter, start, end;
2405 EmpathyChatSpell *chat_spell;
2406 GtkWidget *spell_menu;
2407 GtkWidget *spell_item;
2408 EmpathySmileyManager *smiley_manager;
2409 GtkWidget *smiley_menu;
2412 buffer = gtk_text_view_get_buffer (view);
2414 /* Add the emoticon menu. */
2415 item = gtk_separator_menu_item_new ();
2416 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2417 gtk_widget_show (item);
2419 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2420 image = gtk_image_new_from_icon_name ("face-smile",
2421 GTK_ICON_SIZE_MENU);
2422 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2423 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2424 gtk_widget_show (item);
2426 smiley_manager = empathy_smiley_manager_dup_singleton ();
2427 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2428 chat_insert_smiley_activate_cb,
2430 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2431 g_object_unref (smiley_manager);
2433 /* Add the Send menu item. */
2434 gtk_text_buffer_get_bounds (buffer, &start, &end);
2435 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2436 if (!EMP_STR_EMPTY (str)) {
2437 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2438 g_signal_connect (G_OBJECT (item), "activate",
2439 G_CALLBACK (chat_text_send_cb), chat);
2440 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2441 gtk_widget_show (item);
2445 /* Add the spell check menu item. */
2446 table = gtk_text_buffer_get_tag_table (buffer);
2447 tag = gtk_text_tag_table_lookup (table, "misspelled");
2449 switch (priv->most_recent_event_type) {
2450 case GDK_BUTTON_PRESS:
2451 /* get the location from the pointer */
2452 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2453 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2454 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2456 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2457 GTK_TEXT_WINDOW_WIDGET,
2460 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2465 g_warn_if_reached ();
2466 /* assume the KEY_PRESS case */
2469 /* get the location from the cursor */
2470 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2471 gtk_text_buffer_get_insert (buffer));
2477 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2478 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2480 str = gtk_text_buffer_get_text (buffer,
2481 &start, &end, FALSE);
2483 if (!EMP_STR_EMPTY (str)) {
2484 chat_spell = chat_spell_new (chat, str, start, end);
2485 g_object_set_data_full (G_OBJECT (menu),
2486 "chat-spell", chat_spell,
2487 (GDestroyNotify) chat_spell_free);
2489 item = gtk_separator_menu_item_new ();
2490 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2491 gtk_widget_show (item);
2493 /* Spelling suggestions */
2494 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2495 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2496 GTK_ICON_SIZE_MENU);
2497 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2499 spell_menu = chat_spelling_build_menu (chat_spell);
2500 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2503 spell_item = gtk_separator_menu_item_new ();
2504 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2505 gtk_widget_show (spell_item);
2507 /* Add to dictionary */
2508 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2510 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2511 gtk_widget_show (spell_item);
2513 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2514 gtk_widget_show (item);
2520 chat_log_filter (TplEvent *event,
2523 EmpathyChat *chat = user_data;
2524 EmpathyMessage *message;
2525 EmpathyChatPriv *priv = GET_PRIV (chat);
2526 const GList *pending;
2528 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2529 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2531 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2532 message = empathy_message_from_tpl_log_event (event);
2534 for (; pending; pending = g_list_next (pending)) {
2535 if (empathy_message_equal (message, pending->data)) {
2536 g_object_unref (message);
2541 g_object_unref (message);
2547 show_pending_messages (EmpathyChat *chat) {
2548 EmpathyChatPriv *priv = GET_PRIV (chat);
2549 const GList *messages, *l;
2551 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2553 if (chat->view == NULL || priv->tp_chat == NULL)
2556 if (!priv->can_show_pending)
2559 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2561 for (l = messages; l != NULL ; l = g_list_next (l)) {
2562 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2563 chat_message_received (chat, message, TRUE);
2569 got_filtered_messages_cb (GObject *manager,
2570 GAsyncResult *result,
2575 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2576 EmpathyChatPriv *priv = GET_PRIV (chat);
2577 GError *error = NULL;
2579 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2580 result, &messages, &error)) {
2581 DEBUG ("%s. Aborting.", error->message);
2582 empathy_chat_view_append_event (chat->view,
2583 _("Failed to retrieve recent logs"));
2584 g_error_free (error);
2588 for (l = messages; l; l = g_list_next (l)) {
2589 EmpathyMessage *message;
2591 g_assert (TPL_IS_EVENT (l->data));
2593 message = empathy_message_from_tpl_log_event (l->data);
2594 g_object_unref (l->data);
2596 if (empathy_message_is_edit (message)) {
2597 /* this is an edited message, create a synthetic event
2598 * using the supersedes token and
2599 * original-message-sent timestamp, that we can then
2601 EmpathyMessage *syn_msg = g_object_new (
2602 EMPATHY_TYPE_MESSAGE,
2604 "token", empathy_message_get_supersedes (message),
2605 "type", empathy_message_get_tptype (message),
2606 "timestamp", empathy_message_get_original_timestamp (message),
2607 "incoming", empathy_message_is_incoming (message),
2609 "receiver", empathy_message_get_receiver (message),
2610 "sender", empathy_message_get_sender (message),
2613 empathy_chat_view_append_message (chat->view, syn_msg,
2614 chat_should_highlight (chat, syn_msg));
2615 empathy_chat_view_edit_message (chat->view, message);
2617 g_object_unref (syn_msg);
2619 /* append the latest message */
2620 empathy_chat_view_append_message (chat->view, message,
2621 chat_should_highlight (chat, message));
2624 g_object_unref (message);
2626 g_list_free (messages);
2629 /* in case of TPL error, skip backlog and show pending messages */
2630 priv->can_show_pending = TRUE;
2631 show_pending_messages (chat);
2633 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2634 * about it in EmpathyChatPriv definition */
2635 priv->retrieving_backlogs = FALSE;
2636 empathy_chat_messages_read (chat);
2638 /* Turn back on scrolling */
2639 empathy_chat_view_scroll (chat->view, TRUE);
2643 chat_add_logs (EmpathyChat *chat)
2645 EmpathyChatPriv *priv = GET_PRIV (chat);
2652 /* Turn off scrolling temporarily */
2653 empathy_chat_view_scroll (chat->view, FALSE);
2655 /* Add messages from last conversation */
2656 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2657 target = tpl_entity_new_from_room_id (priv->id);
2659 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2661 priv->retrieving_backlogs = TRUE;
2662 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2665 TPL_EVENT_MASK_TEXT,
2669 got_filtered_messages_cb,
2672 g_object_unref (target);
2676 chat_contacts_completion_func (const gchar *s1,
2680 gchar *tmp, *nick1, *nick2;
2687 return s1 ? -1 : +1;
2690 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2691 nick1 = g_utf8_casefold (tmp, -1);
2694 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2695 nick2 = g_utf8_casefold (tmp, -1);
2698 ret = strncmp (nick1, nick2, n);
2707 build_part_message (guint reason,
2709 EmpathyContact *actor,
2710 const gchar *message)
2712 GString *s = g_string_new ("");
2713 const gchar *actor_name = NULL;
2715 if (actor != NULL) {
2716 actor_name = empathy_contact_get_alias (actor);
2719 /* Having an actor only really makes sense for a few actions... */
2721 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2722 g_string_append_printf (s, _("%s has disconnected"), name);
2724 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2725 if (actor_name != NULL) {
2726 /* translators: reverse the order of these arguments
2727 * if the kicked should come before the kicker in your locale.
2729 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2732 g_string_append_printf (s, _("%s was kicked"), name);
2735 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2736 if (actor_name != NULL) {
2737 /* translators: reverse the order of these arguments
2738 * if the banned should come before the banner in your locale.
2740 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2743 g_string_append_printf (s, _("%s was banned"), name);
2747 g_string_append_printf (s, _("%s has left the room"), name);
2750 if (!EMP_STR_EMPTY (message)) {
2751 /* Note to translators: this string is appended to
2752 * notifications like "foo has left the room", with the message
2753 * given by the user living the room. If this poses a problem,
2754 * please let us know. :-)
2756 g_string_append_printf (s, _(" (%s)"), message);
2759 return g_string_free (s, FALSE);
2763 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2764 EmpathyContact *contact,
2765 EmpathyContact *actor,
2771 EmpathyChatPriv *priv = GET_PRIV (chat);
2772 const gchar *name = empathy_contact_get_alias (contact);
2775 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2777 if (priv->block_events_timeout_id != 0)
2781 str = g_strdup_printf (_("%s has joined the room"),
2784 str = build_part_message (reason, name, actor, message);
2787 empathy_chat_view_append_event (chat->view, str);
2792 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2793 EmpathyContact *old_contact,
2794 EmpathyContact *new_contact,
2799 EmpathyChatPriv *priv = GET_PRIV (chat);
2801 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2803 if (priv->block_events_timeout_id == 0) {
2806 str = g_strdup_printf (_("%s is now known as %s"),
2807 empathy_contact_get_alias (old_contact),
2808 empathy_contact_get_alias (new_contact));
2809 empathy_chat_view_append_event (chat->view, str);
2816 chat_contacts_visible_timeout_cb (gpointer chat)
2818 EmpathyChatPriv *priv = GET_PRIV (chat);
2820 /* Relax the size request */
2821 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2823 /* Set the position of the slider. This must be done here because
2824 * GtkPaned need to know its size allocation and it will be settled only
2825 * after the gtk_window_resize () tough effect. */
2826 if (priv->contacts_width > 0) {
2827 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2828 priv->contacts_width);
2831 priv->contacts_visible_id = 0;
2837 chat_update_contacts_visibility (EmpathyChat *chat,
2840 EmpathyChatPriv *priv = GET_PRIV (chat);
2842 if (!priv->scrolled_window_contacts) {
2846 if (priv->remote_contact != NULL) {
2850 if (show && priv->contact_list_view == NULL) {
2851 EmpathyIndividualStore *store;
2853 GtkAllocation allocation;
2855 /* We are adding the contact list to the chat, we don't want the
2856 * chat view to become too small. If the chat view is already
2857 * smaller than 250 make sure that size won't change. If the
2858 * chat view is bigger the contact list will take some space on
2859 * it but we make sure the chat view don't become smaller than
2860 * 250. Relax the size request once the resize is done */
2861 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2862 min_width = MIN (allocation.width, 250);
2863 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2865 /* There is no way to know when the window resize will happen
2866 * since it is WM's decision. Let's hope it won't be longer. */
2867 if (priv->contacts_visible_id != 0)
2868 g_source_remove (priv->contacts_visible_id);
2869 priv->contacts_visible_id = g_timeout_add (500,
2870 chat_contacts_visible_timeout_cb, chat);
2872 store = EMPATHY_INDIVIDUAL_STORE (
2873 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2875 empathy_individual_store_set_show_groups (store, FALSE);
2877 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2878 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2879 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2880 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2881 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2882 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2883 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2885 empathy_individual_view_set_show_offline (
2886 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2887 empathy_individual_view_set_show_uninteresting (
2888 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2890 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2891 priv->contact_list_view);
2893 gtk_widget_show (priv->contact_list_view);
2894 gtk_widget_show (priv->scrolled_window_contacts);
2895 g_object_unref (store);
2897 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2898 gtk_widget_hide (priv->scrolled_window_contacts);
2899 if (priv->contact_list_view != NULL) {
2900 gtk_widget_destroy (priv->contact_list_view);
2901 priv->contact_list_view = NULL;
2907 empathy_chat_set_show_contacts (EmpathyChat *chat,
2910 EmpathyChatPriv *priv = GET_PRIV (chat);
2912 priv->show_contacts = show;
2914 chat_update_contacts_visibility (chat, show);
2916 g_object_notify (G_OBJECT (chat), "show-contacts");
2920 chat_self_contact_changed_cb (EmpathyChat *chat)
2922 EmpathyChatPriv *priv = GET_PRIV (chat);
2924 if (priv->self_contact != NULL) {
2925 g_signal_handlers_disconnect_by_func (priv->self_contact,
2926 chat_self_contact_alias_changed_cb,
2929 g_clear_object (&priv->self_contact);
2931 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2932 if (priv->self_contact != NULL) {
2933 g_object_ref (priv->self_contact);
2935 if (empathy_chat_is_room (chat)) {
2936 g_signal_connect_swapped (priv->self_contact, "notify::alias",
2937 G_CALLBACK (chat_self_contact_alias_changed_cb),
2942 chat_self_contact_alias_changed_cb (chat);
2946 chat_remote_contact_changed_cb (EmpathyChat *chat)
2948 EmpathyChatPriv *priv = GET_PRIV (chat);
2950 if (priv->remote_contact != NULL) {
2951 g_object_unref (priv->remote_contact);
2952 priv->remote_contact = NULL;
2957 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2958 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2959 if (priv->remote_contact != NULL) {
2960 g_object_ref (priv->remote_contact);
2961 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2963 else if (priv->tp_chat != NULL) {
2964 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2967 chat_update_contacts_visibility (chat, priv->show_contacts);
2969 g_object_notify (G_OBJECT (chat), "remote-contact");
2970 g_object_notify (G_OBJECT (chat), "id");
2974 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2980 EmpathyChatPriv *priv;
2982 priv = GET_PRIV (chat);
2984 if (!priv->tp_chat) {
2988 chat_composing_remove_timeout (chat);
2989 g_object_unref (priv->tp_chat);
2990 priv->tp_chat = NULL;
2991 g_object_notify (G_OBJECT (chat), "tp-chat");
2993 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2994 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2996 chat_update_contacts_visibility (chat, FALSE);
2998 priv->unread_messages_when_offline = priv->unread_messages;
3002 update_misspelled_words (gpointer data)
3004 EmpathyChat *chat = EMPATHY_CHAT (data);
3005 EmpathyChatPriv *priv = GET_PRIV (chat);
3006 GtkTextBuffer *buffer;
3010 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3012 gtk_text_buffer_get_end_iter (buffer, &iter);
3013 length = gtk_text_iter_get_offset (&iter);
3014 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3015 NULL, length, chat);
3017 priv->update_misspelled_words_id = 0;
3023 conf_spell_checking_cb (GSettings *gsettings_chat,
3027 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3028 EmpathyChatPriv *priv = GET_PRIV (chat);
3029 gboolean spell_checker;
3030 GtkTextBuffer *buffer;
3032 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3035 spell_checker = g_settings_get_boolean (gsettings_chat,
3036 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3038 if (!empathy_spell_supported ()) {
3039 spell_checker = FALSE;
3042 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3044 if (spell_checker == priv->spell_checking_enabled) {
3045 if (spell_checker) {
3046 /* Possibly changed dictionaries,
3047 * update misspelled words. Need to do so in idle
3048 * so the spell checker is updated. */
3049 priv->update_misspelled_words_id =
3050 g_idle_add (update_misspelled_words, chat);
3056 if (spell_checker) {
3059 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3060 buffer, "notify::cursor-position",
3061 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3063 priv->insert_text_id = tp_g_signal_connect_object (
3064 buffer, "insert-text",
3065 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3066 chat, G_CONNECT_AFTER);
3067 priv->delete_range_id = tp_g_signal_connect_object (
3068 buffer, "delete-range",
3069 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3070 chat, G_CONNECT_AFTER);
3072 gtk_text_buffer_create_tag (buffer, "misspelled",
3073 "underline", PANGO_UNDERLINE_ERROR,
3076 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3077 gtk_text_buffer_get_insert (buffer));
3078 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3081 /* Mark misspelled words in the existing buffer.
3082 * Need to do so in idle so the spell checker is updated. */
3083 priv->update_misspelled_words_id =
3084 g_idle_add (update_misspelled_words, chat);
3086 GtkTextTagTable *table;
3089 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3090 priv->notify_cursor_position_id = 0;
3091 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3092 priv->insert_text_id = 0;
3093 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3094 priv->delete_range_id = 0;
3096 table = gtk_text_buffer_get_tag_table (buffer);
3097 tag = gtk_text_tag_table_lookup (table, "misspelled");
3098 gtk_text_tag_table_remove (table, tag);
3100 gtk_text_buffer_delete_mark_by_name (buffer,
3101 "previous-cursor-position");
3104 priv->spell_checking_enabled = spell_checker;
3108 save_paned_pos_timeout (gpointer data)
3110 EmpathyChat *self = data;
3113 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3115 g_settings_set_int (self->priv->gsettings_ui,
3116 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3123 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3127 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3129 if (chat->priv->save_paned_pos_id != 0)
3130 g_source_remove (chat->priv->save_paned_pos_id);
3132 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3133 save_paned_pos_timeout, chat);
3139 chat_create_ui (EmpathyChat *chat)
3141 EmpathyChatPriv *priv = GET_PRIV (chat);
3145 GtkTextBuffer *buffer;
3146 EmpathyThemeManager *theme_mgr;
3148 filename = empathy_file_lookup ("empathy-chat.ui",
3150 gui = empathy_builder_get_file (filename,
3151 "chat_widget", &priv->widget,
3152 "hpaned", &priv->hpaned,
3153 "vbox_left", &priv->vbox_left,
3154 "scrolled_window_chat", &priv->scrolled_window_chat,
3155 "scrolled_window_input", &priv->scrolled_window_input,
3156 "hbox_topic", &priv->hbox_topic,
3157 "expander_topic", &priv->expander_topic,
3158 "label_topic", &priv->label_topic,
3159 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3160 "info_bar_vbox", &priv->info_bar_vbox,
3163 empathy_builder_connect (gui, chat,
3164 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3165 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3170 /* Add message view. */
3171 theme_mgr = empathy_theme_manager_dup_singleton ();
3172 chat->view = empathy_theme_manager_create_view (theme_mgr);
3173 g_object_unref (theme_mgr);
3174 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3175 and other types, even though it's non-editable and doesn't accept any
3176 drags. This steals drag motion for anything inside the scrollbars,
3177 making drag destinations on chat windows far less useful.
3179 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3180 g_signal_connect (chat->view, "focus_in_event",
3181 G_CALLBACK (chat_text_view_focus_in_event_cb),
3183 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3184 GTK_WIDGET (chat->view));
3185 gtk_widget_show (GTK_WIDGET (chat->view));
3187 /* Add input GtkTextView */
3188 chat->input_text_view = empathy_input_text_view_new ();
3189 g_signal_connect (chat->input_text_view, "notify::has-focus",
3190 G_CALLBACK (chat_input_has_focus_notify_cb),
3192 g_signal_connect (chat->input_text_view, "key-press-event",
3193 G_CALLBACK (chat_input_key_press_event_cb),
3195 g_signal_connect (chat->input_text_view, "realize",
3196 G_CALLBACK (chat_input_realize_cb),
3198 g_signal_connect (chat->input_text_view, "button-press-event",
3199 G_CALLBACK (chat_input_button_press_event_cb),
3201 g_signal_connect (chat->input_text_view, "populate-popup",
3202 G_CALLBACK (chat_input_populate_popup_cb),
3204 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3205 tp_g_signal_connect_object (buffer, "changed",
3206 G_CALLBACK (chat_input_text_buffer_changed_cb),
3208 tp_g_signal_connect_object (priv->gsettings_chat,
3209 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3210 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3211 conf_spell_checking_cb (priv->gsettings_chat,
3212 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3213 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3214 chat->input_text_view);
3215 gtk_widget_show (chat->input_text_view);
3217 /* Add the (invisible) search bar */
3218 priv->search_bar = empathy_search_bar_new (chat->view);
3219 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3222 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3224 /* Initialy hide the topic, will be shown if not empty */
3225 gtk_widget_hide (priv->hbox_topic);
3227 g_signal_connect (priv->hpaned, "notify::position",
3228 G_CALLBACK (chat_hpaned_pos_changed_cb),
3231 /* Set widget focus order */
3232 list = g_list_append (NULL, priv->search_bar);
3233 list = g_list_append (list, priv->scrolled_window_input);
3234 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3237 list = g_list_append (NULL, priv->vbox_left);
3238 list = g_list_append (list, priv->scrolled_window_contacts);
3239 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3242 list = g_list_append (NULL, priv->hpaned);
3243 list = g_list_append (list, priv->hbox_topic);
3244 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3247 /* Add the main widget in the chat widget */
3248 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3249 g_object_unref (gui);
3253 chat_finalize (GObject *object)
3256 EmpathyChatPriv *priv;
3258 chat = EMPATHY_CHAT (object);
3259 priv = GET_PRIV (chat);
3261 DEBUG ("Finalized: %p", object);
3263 if (priv->update_misspelled_words_id != 0)
3264 g_source_remove (priv->update_misspelled_words_id);
3266 if (priv->save_paned_pos_id != 0)
3267 g_source_remove (priv->save_paned_pos_id);
3269 if (priv->contacts_visible_id != 0)
3270 g_source_remove (priv->contacts_visible_id);
3272 g_object_unref (priv->gsettings_chat);
3273 g_object_unref (priv->gsettings_ui);
3275 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3276 g_list_free (priv->input_history);
3278 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3279 g_list_free (priv->compositors);
3281 chat_composing_remove_timeout (chat);
3283 g_object_unref (priv->account_manager);
3284 g_object_unref (priv->log_manager);
3286 if (priv->tp_chat) {
3287 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3288 chat_invalidated_cb, chat);
3289 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3290 chat_message_received_cb, chat);
3291 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3292 chat_message_acknowledged_cb, chat);
3293 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3294 chat_send_error_cb, chat);
3295 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3296 chat_state_changed_cb, chat);
3297 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3298 chat_members_changed_cb, chat);
3299 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3300 chat_self_contact_changed_cb, chat);
3301 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3302 chat_remote_contact_changed_cb, chat);
3303 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3304 chat_title_changed_cb, chat);
3305 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3306 chat_subject_changed_cb, chat);
3307 empathy_tp_chat_leave (priv->tp_chat, "");
3308 g_object_unref (priv->tp_chat);
3310 if (priv->account) {
3311 g_object_unref (priv->account);
3313 if (priv->self_contact) {
3314 g_signal_handlers_disconnect_by_func (priv->self_contact,
3315 chat_self_contact_alias_changed_cb,
3317 g_object_unref (priv->self_contact);
3319 if (priv->remote_contact) {
3320 g_object_unref (priv->remote_contact);
3323 if (priv->block_events_timeout_id) {
3324 g_source_remove (priv->block_events_timeout_id);
3328 g_free (priv->name);
3329 g_free (priv->subject);
3330 g_completion_free (priv->completion);
3332 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3334 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3338 chat_constructed (GObject *object)
3340 EmpathyChat *chat = EMPATHY_CHAT (object);
3341 EmpathyChatPriv *priv = GET_PRIV (chat);
3343 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3344 /* First display logs from the logger and then display pending messages */
3345 chat_add_logs (chat);
3348 /* Just display pending messages for rooms */
3349 priv->can_show_pending = TRUE;
3350 show_pending_messages (chat);
3355 empathy_chat_class_init (EmpathyChatClass *klass)
3357 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3359 object_class->finalize = chat_finalize;
3360 object_class->get_property = chat_get_property;
3361 object_class->set_property = chat_set_property;
3362 object_class->constructed = chat_constructed;
3364 g_object_class_install_property (object_class,
3366 g_param_spec_object ("tp-chat",
3368 "The tp chat object",
3369 EMPATHY_TYPE_TP_CHAT,
3372 G_PARAM_STATIC_STRINGS));
3373 g_object_class_install_property (object_class,
3375 g_param_spec_object ("account",
3376 "Account of the chat",
3377 "The account of the chat",
3380 G_PARAM_STATIC_STRINGS));
3381 g_object_class_install_property (object_class,
3383 g_param_spec_string ("id",
3385 "The id of the chat",
3388 G_PARAM_STATIC_STRINGS));
3389 g_object_class_install_property (object_class,
3391 g_param_spec_string ("name",
3393 "The name of the chat",
3396 G_PARAM_STATIC_STRINGS));
3397 g_object_class_install_property (object_class,
3399 g_param_spec_string ("subject",
3401 "The subject or topic of the chat",
3404 G_PARAM_STATIC_STRINGS));
3405 g_object_class_install_property (object_class,
3406 PROP_REMOTE_CONTACT,
3407 g_param_spec_object ("remote-contact",
3408 "The remote contact",
3409 "The remote contact is any",
3410 EMPATHY_TYPE_CONTACT,
3412 G_PARAM_STATIC_STRINGS));
3413 g_object_class_install_property (object_class,
3415 g_param_spec_boolean ("show-contacts",
3416 "Contacts' visibility",
3417 "The visibility of the contacts' list",
3420 G_PARAM_STATIC_STRINGS));
3422 g_object_class_install_property (object_class,
3424 g_param_spec_boolean ("sms-channel",
3426 "TRUE if this channel is for sending SMSes",
3428 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3430 g_object_class_install_property (object_class,
3431 PROP_N_MESSAGES_SENDING,
3432 g_param_spec_uint ("n-messages-sending",
3433 "Num Messages Sending",
3434 "The number of messages being sent",
3436 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3438 g_object_class_install_property (object_class,
3439 PROP_NB_UNREAD_MESSAGES,
3440 g_param_spec_uint ("nb-unread-messages",
3441 "Num Unread Messages",
3442 "The number of unread messages",
3444 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3446 signals[COMPOSING] =
3447 g_signal_new ("composing",
3448 G_OBJECT_CLASS_TYPE (object_class),
3452 g_cclosure_marshal_generic,
3457 * EmpathyChat::new-message:
3458 * @self: the #EmpathyChat
3459 * @message: the new message
3460 * @pending: whether the message was in the pending queue when @self
3462 * @should_highlight: %TRUE if the message mentions the local user
3464 signals[NEW_MESSAGE] =
3465 g_signal_new ("new-message",
3466 G_OBJECT_CLASS_TYPE (object_class),
3470 g_cclosure_marshal_generic,
3472 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3474 signals[PART_COMMAND_ENTERED] =
3475 g_signal_new ("part-command-entered",
3476 G_OBJECT_CLASS_TYPE (object_class),
3480 g_cclosure_marshal_generic,
3484 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3488 chat_block_events_timeout_cb (gpointer data)
3490 EmpathyChatPriv *priv = GET_PRIV (data);
3492 priv->block_events_timeout_id = 0;
3498 account_manager_prepared_cb (GObject *source_object,
3499 GAsyncResult *result,
3502 GList *accounts, *l;
3503 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3504 EmpathyChat *chat = user_data;
3505 GError *error = NULL;
3507 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3508 DEBUG ("Failed to prepare the account manager: %s", error->message);
3509 g_error_free (error);
3513 accounts = tp_account_manager_get_valid_accounts (account_manager);
3515 for (l = accounts; l != NULL; l = l->next) {
3516 TpAccount *account = l->data;
3517 tp_g_signal_connect_object (account, "status-changed",
3518 G_CALLBACK (chat_new_connection_cb),
3522 g_list_free (accounts);
3526 empathy_chat_init (EmpathyChat *chat)
3528 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3529 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3532 priv->log_manager = tpl_log_manager_dup_singleton ();
3533 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3534 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3536 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3537 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3538 priv->input_history = NULL;
3539 priv->input_history_current = NULL;
3540 priv->account_manager = tp_account_manager_dup ();
3542 tp_proxy_prepare_async (priv->account_manager, NULL,
3543 account_manager_prepared_cb, chat);
3545 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3546 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3548 /* Block events for some time to avoid having "has come online" or
3549 * "joined" messages. */
3550 priv->block_events_timeout_id =
3551 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3553 /* Add nick name completion */
3554 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3555 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3557 chat_create_ui (chat);
3561 empathy_chat_new (EmpathyTpChat *tp_chat)
3563 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3567 empathy_chat_get_tp_chat (EmpathyChat *chat)
3569 EmpathyChatPriv *priv = GET_PRIV (chat);
3571 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3573 return priv->tp_chat;
3579 GtkWidget *info_bar;
3589 passwd_remember_button_cb (GtkButton *button,
3592 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3596 passwd_not_now_button_cb (GtkButton *button,
3599 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3603 remember_password_infobar_response_cb (GtkWidget *info_bar,
3607 EmpathyChatPriv *priv = GET_PRIV (data->self);
3609 if (response_id == GTK_RESPONSE_OK) {
3610 DEBUG ("Saving room password");
3611 empathy_keyring_set_room_password_async (priv->account,
3612 empathy_tp_chat_get_id (priv->tp_chat),
3617 gtk_widget_destroy (info_bar);
3618 g_free (data->password);
3619 g_slice_free (PasswordData, data);
3623 chat_prompt_to_save_password (EmpathyChat *self,
3626 GtkWidget *content_area;
3633 /* save the password in case it needs to be saved */
3634 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3636 /* Remove all previous widgets */
3637 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3638 gtk_container_forall (GTK_CONTAINER (content_area),
3639 (GtkCallback) gtk_widget_destroy, NULL);
3640 data->button = NULL;
3643 data->spinner = NULL;
3645 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3646 GTK_MESSAGE_QUESTION);
3648 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3649 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3652 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3653 GTK_ICON_SIZE_DIALOG);
3654 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3657 label = gtk_label_new (_("Would you like to store this password?"));
3658 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3660 /* Add 'Remember' button */
3661 alig = gtk_alignment_new (0, 0.5, 1, 0);
3663 button = gtk_button_new_with_label (_("Remember"));
3664 gtk_container_add (GTK_CONTAINER (alig), button);
3665 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3667 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3670 /* Add 'Not now' button */
3671 alig = gtk_alignment_new (0, 0.5, 1, 0);
3673 button = gtk_button_new_with_label (_("Not now"));
3674 gtk_container_add (GTK_CONTAINER (alig), button);
3675 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3677 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3681 g_signal_handler_disconnect (data->info_bar, data->response_id);
3682 g_signal_connect (data->info_bar, "response",
3683 G_CALLBACK (remember_password_infobar_response_cb), data);
3685 gtk_widget_show_all (data->info_bar);
3689 provide_password_cb (GObject *tp_chat,
3693 PasswordData *data = user_data;
3694 EmpathyChat *self = data->self;
3695 EmpathyChatPriv *priv = GET_PRIV (self);
3696 GError *error = NULL;
3698 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3700 DEBUG ("error: %s", error->message);
3701 /* FIXME: what should we do if that's another error? Close the channel?
3702 * Display the raw D-Bus error to the user isn't very useful */
3703 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3705 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3706 gtk_widget_set_sensitive (data->entry, TRUE);
3707 gtk_widget_grab_focus (data->entry);
3710 gtk_info_bar_set_message_type (
3711 GTK_INFO_BAR (data->info_bar),
3715 gtk_widget_set_sensitive (data->button, TRUE);
3716 gtk_button_set_label (GTK_BUTTON (data->button),
3720 gtk_label_set_text (GTK_LABEL (data->label),
3721 _("Wrong password; please try again:"));
3724 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3725 gtk_widget_hide (data->spinner);
3727 g_error_free (error);
3731 if (empathy_keyring_is_available ()) {
3732 /* ask whether they want to save the password */
3733 chat_prompt_to_save_password (self, data);
3735 /* Get rid of the password info bar finally */
3736 gtk_widget_destroy (data->info_bar);
3737 g_slice_free (PasswordData, data);
3741 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3742 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3743 gtk_widget_grab_focus (self->input_text_view);
3747 password_infobar_response_cb (GtkWidget *info_bar,
3751 EmpathyChatPriv *priv = GET_PRIV (data->self);
3752 const gchar *password;
3754 if (response_id != GTK_RESPONSE_OK) {
3755 gtk_widget_destroy (info_bar);
3756 g_slice_free (PasswordData, data);
3760 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3762 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3763 provide_password_cb, data);
3765 gtk_widget_set_sensitive (data->button, FALSE);
3766 gtk_widget_set_sensitive (data->entry, FALSE);
3768 gtk_spinner_start (GTK_SPINNER (data->spinner));
3769 gtk_widget_show (data->spinner);
3773 password_entry_activate_cb (GtkWidget *entry,
3776 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3780 passwd_join_button_cb (GtkButton *button,
3783 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3787 clear_icon_released_cb (GtkEntry *entry,
3788 GtkEntryIconPosition icon_pos,
3792 gtk_entry_set_text (entry, "");
3796 password_entry_changed_cb (GtkEditable *entry,
3801 str = gtk_entry_get_text (GTK_ENTRY (entry));
3803 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3804 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3808 infobar_chat_invalidated_cb (TpProxy *proxy,
3812 gpointer password_infobar)
3814 /* Destroy the password infobar whenever a channel is invalidated
3815 * so we don't have multiple infobars when the MUC is rejoined */
3816 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3820 display_password_info_bar (EmpathyChat *self)
3822 EmpathyChatPriv *priv = GET_PRIV (self);
3823 GtkWidget *info_bar;
3824 GtkWidget *content_area;
3834 data = g_slice_new0 (PasswordData);
3836 info_bar = gtk_info_bar_new ();
3837 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3838 GTK_MESSAGE_QUESTION);
3840 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3842 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3843 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3846 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3847 GTK_ICON_SIZE_DIALOG);
3848 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3851 label = gtk_label_new (_("This room is protected by a password:"));
3852 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3854 /* Add password entry */
3855 entry = gtk_entry_new ();
3856 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3857 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3859 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3860 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3861 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3862 GTK_ENTRY_ICON_SECONDARY, FALSE);
3864 g_signal_connect (entry, "icon-release",
3865 G_CALLBACK (clear_icon_released_cb), data);
3866 g_signal_connect (entry, "changed",
3867 G_CALLBACK (password_entry_changed_cb), data);
3869 g_signal_connect (entry, "activate",
3870 G_CALLBACK (password_entry_activate_cb), data);
3872 /* Focus the password entry once it's realized */
3873 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3875 /* Add 'Join' button */
3876 alig = gtk_alignment_new (0, 0.5, 1, 0);
3878 button = gtk_button_new_with_label (_("Join"));
3879 gtk_container_add (GTK_CONTAINER (alig), button);
3880 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3882 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3886 spinner = gtk_spinner_new ();
3887 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3889 /* Save some data for messing around with later */
3891 data->info_bar = info_bar;
3892 data->button = button;
3893 data->label = label;
3894 data->entry = entry;
3895 data->spinner = spinner;
3897 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3899 gtk_widget_show_all (hbox);
3901 tp_g_signal_connect_object (priv->tp_chat,
3902 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3905 data->response_id = g_signal_connect (info_bar, "response",
3906 G_CALLBACK (password_infobar_response_cb), data);
3908 gtk_widget_show_all (info_bar);
3909 /* ... but hide the spinner */
3910 gtk_widget_hide (spinner);
3912 /* prevent the user from typing anything */
3913 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3917 provide_saved_password_cb (GObject *tp_chat,
3921 EmpathyChat *self = user_data;
3922 EmpathyChatPriv *priv = GET_PRIV (self);
3923 GError *error = NULL;
3925 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3927 DEBUG ("error: %s", error->message);
3928 /* FIXME: what should we do if that's another error? Close the channel?
3929 * Display the raw D-Bus error to the user isn't very useful */
3930 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3931 display_password_info_bar (self);
3932 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3934 g_error_free (error);
3939 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3940 gtk_widget_grab_focus (self->input_text_view);
3944 chat_room_got_password_cb (GObject *source,
3945 GAsyncResult *result,
3948 EmpathyChat *self = user_data;
3949 EmpathyChatPriv *priv = GET_PRIV (self);
3950 const gchar *password;
3951 GError *error = NULL;
3953 password = empathy_keyring_get_room_password_finish (priv->account,
3956 if (error != NULL) {
3957 DEBUG ("Couldn't get room password: %s\n", error->message);
3958 g_clear_error (&error);
3960 display_password_info_bar (self);
3961 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3965 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3966 provide_saved_password_cb, self);
3970 chat_password_needed_changed_cb (EmpathyChat *self)
3972 EmpathyChatPriv *priv = GET_PRIV (self);
3974 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3975 empathy_keyring_get_room_password_async (priv->account,
3976 empathy_tp_chat_get_id (priv->tp_chat),
3977 chat_room_got_password_cb, self);
3982 chat_sms_channel_changed_cb (EmpathyChat *self)
3984 EmpathyChatPriv *priv = GET_PRIV (self);
3986 priv->sms_channel = tp_text_channel_is_sms_channel (
3987 (TpTextChannel *) priv->tp_chat);
3988 g_object_notify (G_OBJECT (self), "sms-channel");
3992 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3994 g_object_notify (G_OBJECT (self), "n-messages-sending");
3998 empathy_chat_set_tp_chat (EmpathyChat *chat,
3999 EmpathyTpChat *tp_chat)
4001 EmpathyChatPriv *priv = GET_PRIV (chat);
4003 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4004 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4006 if (priv->tp_chat) {
4010 if (priv->account) {
4011 g_object_unref (priv->account);
4014 priv->tp_chat = g_object_ref (tp_chat);
4015 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4017 g_signal_connect (tp_chat, "invalidated",
4018 G_CALLBACK (chat_invalidated_cb),
4020 g_signal_connect (tp_chat, "message-received-empathy",
4021 G_CALLBACK (chat_message_received_cb),
4023 g_signal_connect (tp_chat, "message_acknowledged",
4024 G_CALLBACK (chat_message_acknowledged_cb),
4026 g_signal_connect (tp_chat, "send-error",
4027 G_CALLBACK (chat_send_error_cb),
4029 g_signal_connect (tp_chat, "chat-state-changed-empathy",
4030 G_CALLBACK (chat_state_changed_cb),
4032 g_signal_connect (tp_chat, "members-changed",
4033 G_CALLBACK (chat_members_changed_cb),
4035 g_signal_connect (tp_chat, "member-renamed",
4036 G_CALLBACK (chat_member_renamed_cb),
4038 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4039 G_CALLBACK (chat_self_contact_changed_cb),
4041 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4042 G_CALLBACK (chat_remote_contact_changed_cb),
4044 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4045 G_CALLBACK (chat_password_needed_changed_cb),
4047 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4048 G_CALLBACK (chat_sms_channel_changed_cb),
4050 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4051 G_CALLBACK (chat_n_messages_sending_changed_cb),
4053 g_signal_connect_swapped (tp_chat, "notify::title",
4054 G_CALLBACK (chat_title_changed_cb),
4056 g_signal_connect_swapped (tp_chat, "notify::subject",
4057 G_CALLBACK (chat_subject_changed_cb),
4060 /* Get initial value of properties */
4061 chat_sms_channel_changed_cb (chat);
4062 chat_self_contact_changed_cb (chat);
4063 chat_remote_contact_changed_cb (chat);
4064 chat_title_changed_cb (chat);
4065 chat_subject_changed_cb (chat);
4067 if (chat->input_text_view) {
4068 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4069 if (priv->block_events_timeout_id == 0) {
4070 empathy_chat_view_append_event (chat->view, _("Connected"));
4074 g_object_notify (G_OBJECT (chat), "tp-chat");
4075 g_object_notify (G_OBJECT (chat), "id");
4076 g_object_notify (G_OBJECT (chat), "account");
4078 /* This is a noop when tp-chat is set at object construction time and causes
4079 * the pending messages to be show when it's set on the object after it has
4081 show_pending_messages (chat);
4083 /* check if a password is needed */
4084 chat_password_needed_changed_cb (chat);
4088 empathy_chat_get_account (EmpathyChat *chat)
4090 EmpathyChatPriv *priv = GET_PRIV (chat);
4092 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4094 return priv->account;
4098 empathy_chat_get_id (EmpathyChat *chat)
4100 EmpathyChatPriv *priv = GET_PRIV (chat);
4102 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4108 empathy_chat_dup_name (EmpathyChat *chat)
4110 EmpathyChatPriv *priv = GET_PRIV (chat);
4113 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4117 if (!ret && priv->remote_contact) {
4118 ret = empathy_contact_get_alias (priv->remote_contact);
4125 ret = _("Conversation");
4127 if (priv->sms_channel)
4128 /* Translators: this string is a something like
4129 * "Escher Cat (SMS)" */
4130 return g_strdup_printf (_("%s (SMS)"), ret);
4132 return g_strdup (ret);
4136 empathy_chat_get_subject (EmpathyChat *chat)
4138 EmpathyChatPriv *priv = GET_PRIV (chat);
4140 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4142 return priv->subject;
4146 empathy_chat_get_remote_contact (EmpathyChat *chat)
4148 EmpathyChatPriv *priv = GET_PRIV (chat);
4150 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4152 return priv->remote_contact;
4156 empathy_chat_get_contact_menu (EmpathyChat *chat)
4158 EmpathyChatPriv *priv = GET_PRIV (chat);
4159 GtkWidget *menu = NULL;
4160 FolksIndividual *individual;
4163 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4165 if (priv->remote_contact == NULL)
4168 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4169 if (contact == NULL)
4172 individual = empathy_ensure_individual_from_tp_contact (contact);
4174 if (individual == NULL)
4177 menu = empathy_individual_menu_new (individual,
4178 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4179 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4180 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4181 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4183 g_object_unref (individual);
4189 empathy_chat_clear (EmpathyChat *chat)
4191 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4193 empathy_chat_view_clear (chat->view);
4197 empathy_chat_scroll_down (EmpathyChat *chat)
4199 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4201 empathy_chat_view_scroll_down (chat->view);
4205 empathy_chat_cut (EmpathyChat *chat)
4207 GtkTextBuffer *buffer;
4209 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4211 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4212 if (gtk_text_buffer_get_has_selection (buffer)) {
4213 GtkClipboard *clipboard;
4215 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4217 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4222 empathy_chat_copy (EmpathyChat *chat)
4224 GtkTextBuffer *buffer;
4226 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4228 if (empathy_chat_view_get_has_selection (chat->view)) {
4229 empathy_chat_view_copy_clipboard (chat->view);
4233 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4234 if (gtk_text_buffer_get_has_selection (buffer)) {
4235 GtkClipboard *clipboard;
4237 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4239 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4244 EmpathyChatPriv *priv = GET_PRIV (chat);
4246 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4253 GtkClipboard *clipboard;
4255 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4256 start = g_utf8_offset_to_pointer (topic, start_offset);
4257 end = g_utf8_offset_to_pointer (topic, end_offset);
4258 selection = g_strndup (start, end - start);
4260 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4261 gtk_clipboard_set_text (clipboard, selection, -1);
4269 empathy_chat_paste (EmpathyChat *chat)
4271 GtkTextBuffer *buffer;
4272 GtkClipboard *clipboard;
4273 EmpathyChatPriv *priv;
4275 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4277 priv = GET_PRIV (chat);
4279 if (gtk_widget_get_visible (priv->search_bar)) {
4280 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4284 if (priv->tp_chat == NULL ||
4285 !gtk_widget_is_sensitive (chat->input_text_view))
4288 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4289 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4291 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4295 empathy_chat_find (EmpathyChat *chat)
4297 EmpathyChatPriv *priv;
4299 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4301 priv = GET_PRIV (chat);
4303 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4307 empathy_chat_correct_word (EmpathyChat *chat,
4310 const gchar *new_word)
4312 GtkTextBuffer *buffer;
4314 g_return_if_fail (chat != NULL);
4315 g_return_if_fail (new_word != NULL);
4317 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4319 gtk_text_buffer_delete (buffer, start, end);
4320 gtk_text_buffer_insert (buffer, start,
4326 empathy_chat_is_room (EmpathyChat *chat)
4328 EmpathyChatPriv *priv = GET_PRIV (chat);
4330 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4332 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4336 empathy_chat_is_highlighted (EmpathyChat *chat)
4338 EmpathyChatPriv *priv = GET_PRIV (chat);
4340 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4342 return priv->highlighted;
4346 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4348 EmpathyChatPriv *priv = GET_PRIV (self);
4350 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4352 return priv->unread_messages;
4355 /* called when the messages have been read by user */
4357 empathy_chat_messages_read (EmpathyChat *self)
4359 EmpathyChatPriv *priv = GET_PRIV (self);
4361 g_return_if_fail (EMPATHY_IS_CHAT (self));
4363 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4364 * definition. If we are still retrieving the backlogs, do not ACK */
4365 if (priv->retrieving_backlogs)
4368 if (priv->tp_chat != NULL) {
4369 tp_text_channel_ack_all_pending_messages_async (
4370 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4373 priv->highlighted = FALSE;
4375 if (priv->unread_messages_when_offline > 0) {
4376 /* We can't ack those as the connection has gone away so just consider
4378 priv->unread_messages -= priv->unread_messages_when_offline;
4379 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4380 priv->unread_messages_when_offline = 0;
4384 /* Return TRUE if on of the contacts in this chat is composing */
4386 empathy_chat_is_composing (EmpathyChat *chat)
4388 return chat->priv->compositors != NULL;
4392 empathy_chat_is_sms_channel (EmpathyChat *self)
4394 EmpathyChatPriv *priv = GET_PRIV (self);
4396 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4398 return priv->sms_channel;
4402 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4404 EmpathyChatPriv *priv;
4406 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4408 priv = GET_PRIV (self);
4410 if (priv->tp_chat == NULL) {
4415 g_object_get (priv->tp_chat,
4416 "n-messages-sending", &n_messages,
4424 empathy_chat_dup_text (EmpathyChat *self)
4426 GtkTextBuffer *buffer;
4427 GtkTextIter start, end;
4429 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4431 gtk_text_buffer_get_bounds (buffer, &start, &end);
4432 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4436 empathy_chat_set_text (EmpathyChat *self,
4439 GtkTextBuffer *buffer;
4441 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4443 gtk_text_buffer_set_text (buffer, text, -1);