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.
5 * Copyright (C) 2012 Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301 USA
22 * Authors: Mikael Hallendal <micke@imendio.com>
23 * Richard Hult <richard@imendio.com>
24 * Martyn Russell <martyn@imendio.com>
25 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
26 * Xavier Claessens <xclaesse@gmail.com>
30 #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
33 #include "empathy-chat.h"
35 #include <glib/gi18n-lib.h>
36 #include <tp-account-widgets/tpaw-keyring.h>
37 #include <tp-account-widgets/tpaw-builder.h>
39 #include "empathy-client-factory.h"
40 #include "empathy-gsettings.h"
41 #include "empathy-individual-information-dialog.h"
42 #include "empathy-individual-store-channel.h"
43 #include "empathy-individual-view.h"
44 #include "empathy-input-text-view.h"
45 #include "empathy-request-util.h"
46 #include "empathy-search-bar.h"
47 #include "empathy-smiley-manager.h"
48 #include "empathy-spell.h"
49 #include "empathy-string-parser.h"
50 #include "empathy-theme-manager.h"
51 #include "empathy-ui-utils.h"
52 #include "empathy-utils.h"
53 #include "extensions.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include "empathy-debug.h"
58 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
59 #define COMPOSING_STOP_TIMEOUT 5
61 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
62 struct _EmpathyChatPriv {
63 EmpathyTpChat *tp_chat;
68 EmpathyContact *self_contact;
69 EmpathyContact *remote_contact;
70 gboolean show_contacts;
72 GSettings *gsettings_chat;
73 GSettings *gsettings_ui;
75 TplLogManager *log_manager;
76 TplLogWalker *log_walker;
77 /* Are we watching for scrolling movements? */
78 gboolean watch_scroll;
79 /* Maximum page size of the chat->view. */
81 /* The offset from the lower edge of the chat->view before it
82 * expanded to fit in the newly fetched logs. This is to
83 * restore the chat->view to the page it was on before the
84 * latest batch of logs were inserted. */
87 TpAccountManager *account_manager;
89 GList *input_history_current;
91 GCompletion *completion;
92 guint composing_stop_timeout_id;
93 guint block_events_timeout_id;
94 TpHandleType handle_type;
96 gboolean has_input_vscroll;
98 /* TRUE if spell checking is enabled, FALSE otherwise.
99 * This is to keep track of the last state of spell checking
100 * when it changes. */
101 gboolean spell_checking_enabled;
103 /* These store the signal handler ids for the enclosed text entry. */
104 gulong insert_text_id;
105 gulong delete_range_id;
106 gulong notify_cursor_position_id;
108 /* Source func ID for update_misspelled_words () */
109 guint update_misspelled_words_id;
110 /* Source func ID for save_paned_pos_timeout () */
111 guint save_paned_pos_id;
112 /* Source func ID for chat_contacts_visible_timeout_cb () */
113 guint contacts_visible_id;
117 GtkWidget *vbox_left;
118 GtkWidget *scrolled_window_chat;
119 GtkWidget *scrolled_window_input;
120 GtkWidget *scrolled_window_contacts;
121 GtkWidget *hbox_topic;
122 GtkWidget *expander_topic;
123 GtkWidget *label_topic;
124 GtkWidget *contact_list_view;
125 GtkWidget *info_bar_vbox;
126 GtkWidget *search_bar;
128 guint unread_messages;
129 guint unread_messages_when_offline;
130 /* TRUE if the pending messages can be displayed. This is to avoid to show
131 * pending messages *before* messages from logs. (#603980) */
132 gboolean can_show_pending;
134 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
135 * be differently handled since it introduces another race condition, which
136 * is really hard to occur, but still possible.
138 * With the current workaround (which has the race above), we need to be
139 * sure to ACK any pending messages only when the retrieval of backlogs is
140 * finished, that's why using retrieving_backlogs flag.
141 * empathy_chat_messages_read () will check this variable and not ACK
142 * anything when TRUE. It will be set TRUE at chat_constructed () and set
143 * back to FALSE when the backlog has been retrieved and the pending
144 * messages actually showed to the user.
146 * Race condition introduced with this workaround:
147 * Scenario: a message is pending, the user is notified and selects the tab.
148 * the tab with a pending message is focused before the messages are properly
149 * shown (since the preparation of the window is slower AND async WRT the
150 * tab showing), which means the user won't see any new messages (rare but
151 * possible), if he/she will change tab focus before the messages are
152 * properly shown, the tab will be set as 'seen' and the user won't be
153 * notified again about the already notified pending messages when the
154 * messages in tab will be properly shown */
155 gboolean retrieving_backlogs;
156 gboolean sms_channel;
158 /* we need to know whether populate-popup happened in response to
159 * the keyboard or the mouse. We can't ask GTK for the most recent
160 * event, because it will be a notify event. Instead we track it here */
161 GdkEventType most_recent_event_type;
163 /* A regex matching our own current nickname in the room, or %NULL if
164 * !empathy_chat_is_room (). */
165 GRegex *highlight_regex;
167 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
168 * Cleared by empathy_chat_messages_read (). */
169 gboolean highlighted;
173 gchar *text; /* Original message that was specified
174 * upon entry creation. */
175 gchar *modified_text; /* Message that was modified by user.
176 * When no modifications were made, it is NULL */
182 PART_COMMAND_ENTERED,
196 PROP_N_MESSAGES_SENDING,
197 PROP_NB_UNREAD_MESSAGES,
200 static guint signals[LAST_SIGNAL] = { 0 };
202 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
204 static gboolean chat_scrollable_connect (gpointer user_data);
205 static gboolean update_misspelled_words (gpointer data);
208 chat_get_property (GObject *object,
213 EmpathyChat *chat = EMPATHY_CHAT (object);
214 EmpathyChatPriv *priv = GET_PRIV (object);
218 g_value_set_object (value, priv->tp_chat);
221 g_value_set_object (value, priv->account);
224 g_value_take_string (value, empathy_chat_dup_name (chat));
227 g_value_set_string (value, priv->id);
230 g_value_set_string (value, priv->subject);
232 case PROP_REMOTE_CONTACT:
233 g_value_set_object (value, priv->remote_contact);
235 case PROP_SHOW_CONTACTS:
236 g_value_set_boolean (value, priv->show_contacts);
238 case PROP_SMS_CHANNEL:
239 g_value_set_boolean (value, priv->sms_channel);
241 case PROP_N_MESSAGES_SENDING:
242 g_value_set_uint (value,
243 empathy_chat_get_n_messages_sending (chat));
245 case PROP_NB_UNREAD_MESSAGES:
246 g_value_set_uint (value,
247 empathy_chat_get_nb_unread_messages (chat));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
256 chat_set_property (GObject *object,
261 EmpathyChat *chat = EMPATHY_CHAT (object);
265 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
267 case PROP_SHOW_CONTACTS:
268 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
277 account_reconnected (EmpathyChat *chat,
280 EmpathyChatPriv *priv = GET_PRIV (chat);
282 DEBUG ("Account reconnected, request a new Text channel");
284 /* FIXME: Ideally we should ask to handle ourself the channel so we can
285 * report the error if any but this is blocked by
286 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
287 switch (priv->handle_type) {
288 case TP_HANDLE_TYPE_CONTACT:
289 if (priv->sms_channel)
290 empathy_sms_contact_id (
292 TP_USER_ACTION_TIME_NOT_USER_ACTION,
295 empathy_chat_with_contact_id (
297 TP_USER_ACTION_TIME_NOT_USER_ACTION,
300 case TP_HANDLE_TYPE_ROOM:
301 empathy_join_muc (account, priv->id,
302 TP_USER_ACTION_TIME_NOT_USER_ACTION);
304 case TP_HANDLE_TYPE_NONE:
305 case TP_HANDLE_TYPE_LIST:
306 case TP_HANDLE_TYPE_GROUP:
308 g_assert_not_reached ();
312 g_object_unref (chat);
316 chat_new_connection_cb (TpAccount *account,
320 gchar *dbus_error_name,
324 EmpathyChatPriv *priv = GET_PRIV (chat);
326 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
329 if (priv->tp_chat != NULL || account != priv->account ||
330 priv->handle_type == TP_HANDLE_TYPE_NONE ||
331 EMP_STR_EMPTY (priv->id))
336 account_reconnected (chat, account);
340 chat_composing_remove_timeout (EmpathyChat *chat)
342 EmpathyChatPriv *priv;
344 priv = GET_PRIV (chat);
346 if (priv->composing_stop_timeout_id) {
347 g_source_remove (priv->composing_stop_timeout_id);
348 priv->composing_stop_timeout_id = 0;
353 set_chate_state_cb (GObject *source,
354 GAsyncResult *result,
357 GError *error = NULL;
359 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
361 DEBUG ("Failed to set chat state: %s", error->message);
362 g_error_free (error);
367 set_chat_state (EmpathyChat *self,
368 TpChannelChatState state)
370 EmpathyChatPriv *priv = GET_PRIV (self);
372 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
373 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
376 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
377 set_chate_state_cb, self);
381 chat_composing_stop_timeout_cb (EmpathyChat *chat)
383 EmpathyChatPriv *priv;
384 gboolean send_chat_states;
386 priv = GET_PRIV (chat);
388 priv->composing_stop_timeout_id = 0;
389 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
390 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
391 if (!send_chat_states) {
392 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
394 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
401 chat_composing_start (EmpathyChat *chat)
403 EmpathyChatPriv *priv;
404 gboolean send_chat_states;
406 priv = GET_PRIV (chat);
408 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
409 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
410 if (!send_chat_states) {
414 if (priv->composing_stop_timeout_id) {
415 /* Just restart the timeout */
416 chat_composing_remove_timeout (chat);
418 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
421 priv->composing_stop_timeout_id = g_timeout_add_seconds (
422 COMPOSING_STOP_TIMEOUT,
423 (GSourceFunc) chat_composing_stop_timeout_cb,
428 chat_composing_stop (EmpathyChat *chat)
430 chat_composing_remove_timeout (chat);
431 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
435 chat_input_history_entry_cmp (InputHistoryEntry *entry,
438 if (!tp_strdiff (entry->text, text)) {
439 if (entry->modified_text != NULL) {
440 /* Modified entry and single string cannot be equal. */
448 static InputHistoryEntry *
449 chat_input_history_entry_new_with_text (const gchar *text)
451 InputHistoryEntry *entry;
452 entry = g_slice_new0 (InputHistoryEntry);
453 entry->text = g_strdup (text);
459 chat_input_history_entry_free (InputHistoryEntry *entry)
461 g_free (entry->text);
462 g_free (entry->modified_text);
463 g_slice_free (InputHistoryEntry, entry);
467 chat_input_history_entry_revert (InputHistoryEntry *entry)
469 g_free (entry->modified_text);
470 entry->modified_text = NULL;
474 chat_input_history_entry_update_text (InputHistoryEntry *entry,
479 if (!tp_strdiff (text, entry->text)) {
480 g_free (entry->modified_text);
481 entry->modified_text = NULL;
485 old = entry->modified_text;
486 entry->modified_text = g_strdup (text);
491 chat_input_history_entry_get_text (InputHistoryEntry *entry)
497 if (entry->modified_text != NULL) {
498 return entry->modified_text;
504 chat_input_history_remove_item (GList *list,
507 list = g_list_remove_link (list, item);
508 chat_input_history_entry_free (item->data);
509 g_list_free_1 (item);
514 chat_input_history_revert (EmpathyChat *chat)
516 EmpathyChatPriv *priv;
520 InputHistoryEntry *entry;
522 priv = GET_PRIV (chat);
523 list = priv->input_history;
526 DEBUG ("No input history");
530 /* Delete temporary entry */
531 if (priv->input_history_current != NULL) {
533 list = chat_input_history_remove_item (list, item1);
534 if (priv->input_history_current == item1) {
535 /* Removed temporary entry was current entry */
536 priv->input_history = list;
537 priv->input_history_current = NULL;
542 /* There is no entry to revert */
546 /* Restore the current history entry to original value */
547 item1 = priv->input_history_current;
549 chat_input_history_entry_revert (entry);
551 /* Remove restored entry if there is other occurance before this entry */
552 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
553 (GCompareFunc) chat_input_history_entry_cmp);
554 if (item2 != item1) {
555 list = chat_input_history_remove_item (list, item1);
558 /* Remove other occurance of the restored entry */
559 item2 = g_list_find_custom (item1->next,
560 chat_input_history_entry_get_text (entry),
561 (GCompareFunc) chat_input_history_entry_cmp);
563 list = chat_input_history_remove_item (list, item2);
567 priv->input_history_current = NULL;
568 priv->input_history = list;
572 chat_input_history_add (EmpathyChat *chat,
576 EmpathyChatPriv *priv;
579 InputHistoryEntry *entry;
581 priv = GET_PRIV (chat);
583 list = priv->input_history;
585 /* Remove any other occurances of this entry, if not temporary */
587 while ((item = g_list_find_custom (list, str,
588 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
589 list = chat_input_history_remove_item (list, item);
592 /* Trim the list to the last 10 items */
593 while (g_list_length (list) > 10) {
594 item = g_list_last (list);
596 list = chat_input_history_remove_item (list, item);
604 entry = chat_input_history_entry_new_with_text (str);
605 list = g_list_prepend (list, entry);
607 /* Set the list and the current item pointer */
608 priv->input_history = list;
610 priv->input_history_current = list;
613 priv->input_history_current = NULL;
618 chat_input_history_get_next (EmpathyChat *chat)
620 EmpathyChatPriv *priv;
624 priv = GET_PRIV (chat);
626 if (priv->input_history == NULL) {
627 DEBUG ("No input history, next entry is NULL");
630 g_assert (priv->input_history_current != NULL);
632 if ((item = g_list_next (priv->input_history_current)) == NULL)
634 item = priv->input_history_current;
637 msg = chat_input_history_entry_get_text (item->data);
639 DEBUG ("Returning next entry: '%s'", msg);
641 priv->input_history_current = item;
647 chat_input_history_get_prev (EmpathyChat *chat)
649 EmpathyChatPriv *priv;
653 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
655 priv = GET_PRIV (chat);
657 if (priv->input_history == NULL) {
658 DEBUG ("No input history, previous entry is NULL");
662 if (priv->input_history_current == NULL)
666 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
668 item = priv->input_history_current;
671 msg = chat_input_history_entry_get_text (item->data);
673 DEBUG ("Returning previous entry: '%s'", msg);
675 priv->input_history_current = item;
681 chat_input_history_update (EmpathyChat *chat,
682 GtkTextBuffer *buffer)
684 EmpathyChatPriv *priv;
685 GtkTextIter start, end;
687 InputHistoryEntry *entry;
689 priv = GET_PRIV (chat);
691 gtk_text_buffer_get_bounds (buffer, &start, &end);
692 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
694 if (priv->input_history_current == NULL) {
695 /* Add the current text temporarily to the history */
696 chat_input_history_add (chat, text, TRUE);
701 /* Save the changes in the history */
702 entry = priv->input_history_current->data;
703 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
704 chat_input_history_entry_update_text (entry, text);
713 } ChatCommandMsgData;
716 chat_command_msg_cb (GObject *source,
717 GAsyncResult *result,
720 ChatCommandMsgData *data = user_data;
721 GError *error = NULL;
724 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
725 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
727 if (channel == NULL) {
728 DEBUG ("Failed to get channel: %s", error->message);
729 g_error_free (error);
731 empathy_theme_adium_append_event (data->chat->view,
732 _("Failed to open private chat"));
736 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
737 TpTextChannel *text = (TpTextChannel *) channel;
740 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
743 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
745 g_object_unref (msg);
748 g_object_unref (channel);
751 g_free (data->message);
752 g_slice_free (ChatCommandMsgData, data);
756 nick_command_supported (EmpathyChat *chat)
758 EmpathyChatPriv * priv = GET_PRIV (chat);
759 TpConnection *connection;
761 connection = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
762 return tp_proxy_has_interface_by_id (connection,
763 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
767 part_command_supported (EmpathyChat *chat)
769 EmpathyChatPriv * priv = GET_PRIV (chat);
771 return tp_proxy_has_interface_by_id (priv->tp_chat,
772 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
776 chat_command_clear (EmpathyChat *chat,
779 empathy_theme_adium_clear (chat->view);
783 chat_command_topic (EmpathyChat *chat,
786 EmpathyChatPriv *priv = GET_PRIV (chat);
788 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
789 empathy_theme_adium_append_event (chat->view,
790 _("Topic not supported on this conversation"));
794 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
795 empathy_theme_adium_append_event (chat->view,
796 _("You are not allowed to change the topic"));
800 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
804 empathy_chat_join_muc (EmpathyChat *chat,
807 EmpathyChatPriv *priv = GET_PRIV (chat);
809 empathy_join_muc (priv->account, room,
810 empathy_get_current_action_time ());
814 chat_command_join (EmpathyChat *chat,
819 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
821 /* FIXME: Ideally we should ask to handle ourself the channel so we can
822 * report the error if any but this is blocked by
823 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
824 while (rooms[i] != NULL) {
825 /* ignore empty strings */
826 if (!EMP_STR_EMPTY (rooms[i])) {
827 empathy_chat_join_muc (chat, rooms[i]);
835 chat_command_part (EmpathyChat *chat,
838 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
842 chat_command_msg_internal (EmpathyChat *chat,
843 const gchar *contact_id,
844 const gchar *message)
846 EmpathyChatPriv *priv = GET_PRIV (chat);
847 ChatCommandMsgData *data;
848 TpAccountChannelRequest *req;
851 request = tp_asv_new (
852 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
853 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
854 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
857 req = tp_account_channel_request_new (priv->account, request,
858 empathy_get_current_action_time ());
860 /* FIXME: We should probably search in members alias. But this
861 * is enough for IRC */
862 data = g_slice_new (ChatCommandMsgData);
864 data->message = g_strdup (message);
866 tp_account_channel_request_ensure_and_observe_channel_async (req,
867 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
869 g_object_unref (req);
870 g_hash_table_unref (request);
874 chat_command_query (EmpathyChat *chat,
877 /* If <message> part is not defined,
878 * strv[2] will be the terminal NULL */
879 chat_command_msg_internal (chat, strv[1], strv[2]);
883 chat_command_msg (EmpathyChat *chat,
886 chat_command_msg_internal (chat, strv[1], strv[2]);
890 callback_for_request_rename (TpProxy *proxy,
893 GObject *weak_object)
896 DEBUG ("Call to RequestRename method failed: %s",error->message);
901 chat_command_nick (EmpathyChat *chat,
904 EmpathyChatPriv *priv = GET_PRIV (chat);
907 proxy = TP_PROXY (tp_account_get_connection (priv->account));
909 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
910 strv[1], callback_for_request_rename, NULL, NULL, NULL);
914 chat_command_me (EmpathyChat *chat,
917 EmpathyChatPriv *priv = GET_PRIV (chat);
919 TpTextChannel *channel;
921 channel = (TpTextChannel *) (priv->tp_chat);
923 if (!tp_text_channel_supports_message_type (channel,
924 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
925 /* Action message are not supported, 'simulate' the action */
928 /* The TpChat can't be ready if it doesn't have the self contact */
929 g_assert (priv->self_contact != NULL);
931 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
933 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
938 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
942 empathy_tp_chat_send (priv->tp_chat, message);
943 g_object_unref (message);
947 chat_command_say (EmpathyChat *chat,
950 EmpathyChatPriv *priv = GET_PRIV (chat);
953 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
955 empathy_tp_chat_send (priv->tp_chat, message);
956 g_object_unref (message);
960 whois_got_contact_cb (GObject *source,
961 GAsyncResult *result,
964 EmpathyChat *chat = user_data;
965 EmpathyContact *contact;
966 FolksIndividual *individual;
968 contact = empathy_client_factory_dup_contact_by_id_finish (
969 EMPATHY_CLIENT_FACTORY (source), result, NULL);
971 if (contact == NULL) {
972 empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
976 individual = empathy_ensure_individual_from_tp_contact (
977 empathy_contact_get_tp_contact (contact));
978 empathy_display_individual_info (individual);
980 g_object_unref (individual);
981 g_object_unref (contact);
984 g_object_unref (chat);
988 chat_command_whois (EmpathyChat *chat,
991 EmpathyChatPriv *priv = GET_PRIV (chat);
993 EmpathyClientFactory *factory;
995 conn = tp_channel_get_connection ((TpChannel *) priv->tp_chat);
996 factory = empathy_client_factory_dup ();
998 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
999 * entered by the user (including spaces, if any). */
1000 empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
1001 whois_got_contact_cb, g_object_ref (chat));
1003 g_object_unref (factory);
1007 chat_command_whale (EmpathyChat *chat,
1010 EmpathyChatPriv *priv = GET_PRIV (chat);
1013 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1015 "•_______________•");
1016 empathy_tp_chat_send (priv->tp_chat, message);
1017 g_object_unref (message);
1021 chat_command_babywhale (EmpathyChat *chat,
1024 EmpathyChatPriv *priv = GET_PRIV (chat);
1027 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1030 empathy_tp_chat_send (priv->tp_chat, message);
1031 g_object_unref (message);
1035 chat_command_inspector (EmpathyChat *chat,
1038 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1039 empathy_theme_adium_show_inspector (
1040 EMPATHY_THEME_ADIUM (chat->view));
1044 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1046 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1049 const gchar *prefix;
1052 ChatCommandFunc func;
1053 gboolean (*is_supported)(EmpathyChat *chat);
1057 static ChatCommandItem commands[] = {
1058 {"clear", 1, 1, chat_command_clear, NULL,
1059 N_("/clear: clear all messages from the current conversation")},
1061 {"topic", 2, 2, chat_command_topic, NULL,
1062 N_("/topic <topic>: set the topic of the current conversation")},
1064 {"join", 2, 2, chat_command_join, NULL,
1065 N_("/join <chat room ID>: join a new chat room")},
1067 {"j", 2, 2, chat_command_join, NULL,
1068 N_("/j <chat room ID>: join a new chat room")},
1070 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1071 {"part", 1, 3, chat_command_part, part_command_supported,
1072 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1073 "by default the current one")},
1075 {"query", 2, 3, chat_command_query, NULL,
1076 N_("/query <contact ID> [<message>]: open a private chat")},
1078 {"msg", 3, 3, chat_command_msg, NULL,
1079 N_("/msg <contact ID> <message>: open a private chat")},
1081 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1082 N_("/nick <nickname>: change your nickname on the current server")},
1084 {"me", 2, 2, chat_command_me, NULL,
1085 N_("/me <message>: send an ACTION message to the current conversation")},
1087 {"say", 2, 2, chat_command_say, NULL,
1088 N_("/say <message>: send <message> to the current conversation. "
1089 "This is used to send a message starting with a '/'. For example: "
1090 "\"/say /join is used to join a new chat room\"")},
1092 {"whois", 2, 2, chat_command_whois, NULL,
1093 N_("/whois <contact ID>: display information about a contact")},
1095 {"help", 1, 2, chat_command_help, NULL,
1096 N_("/help [<command>]: show all supported commands. "
1097 "If <command> is defined, show its usage.")},
1099 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1101 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1102 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1106 chat_command_show_help (EmpathyChat *chat,
1107 ChatCommandItem *item)
1111 if (item->help == NULL) {
1115 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1116 empathy_theme_adium_append_event (chat->view, str);
1121 chat_command_help (EmpathyChat *chat,
1126 /* If <command> part is not defined,
1127 * strv[1] will be the terminal NULL */
1128 if (strv[1] == NULL) {
1129 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1130 if (commands[i].is_supported != NULL) {
1131 if (!commands[i].is_supported (chat)) {
1135 if (commands[i].help == NULL) {
1138 empathy_theme_adium_append_event (chat->view,
1139 _(commands[i].help));
1144 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1145 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1146 if (commands[i].is_supported != NULL) {
1147 if (!commands[i].is_supported (chat)) {
1151 if (commands[i].help == NULL) {
1154 chat_command_show_help (chat, &commands[i]);
1159 empathy_theme_adium_append_event (chat->view,
1160 _("Unknown command"));
1164 chat_command_parse (const gchar *text, guint max_parts)
1169 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1171 array = g_ptr_array_sized_new (max_parts + 1);
1172 while (max_parts > 1) {
1175 /* Skip white spaces */
1176 while (g_ascii_isspace (*text)) {
1180 /* Search the end of this part, until first space. */
1181 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1187 item = g_strndup (text, end - text);
1188 g_ptr_array_add (array, item);
1189 DEBUG ("\tITEM: \"%s\"", item);
1195 /* Append last part if not empty */
1196 item = g_strstrip (g_strdup (text));
1197 if (!EMP_STR_EMPTY (item)) {
1198 g_ptr_array_add (array, item);
1199 DEBUG ("\tITEM: \"%s\"", item);
1204 /* Make the array NULL-terminated */
1205 g_ptr_array_add (array, NULL);
1207 return (GStrv) g_ptr_array_free (array, FALSE);
1211 has_prefix_case (const gchar *s,
1212 const gchar *prefix)
1214 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1218 chat_send (EmpathyChat *chat,
1221 EmpathyChatPriv *priv;
1225 if (EMP_STR_EMPTY (msg)) {
1229 priv = GET_PRIV (chat);
1231 chat_input_history_add (chat, msg, FALSE);
1233 if (msg[0] == '/') {
1234 gboolean second_slash = FALSE;
1235 const gchar *iter = msg + 1;
1237 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1242 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1245 c = *(msg + 1 + strlen (commands[i].prefix));
1246 if (c != '\0' && !g_ascii_isspace (c)) {
1249 if (commands[i].is_supported != NULL) {
1250 if (!commands[i].is_supported (chat)) {
1255 /* We can't use g_strsplit here because it does
1256 * not deal correctly if we have more than one space
1258 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1260 strv_len = g_strv_length (strv);
1261 if (strv_len < commands[i].min_parts ||
1262 strv_len > commands[i].max_parts) {
1263 chat_command_show_help (chat, &commands[i]);
1268 commands[i].func (chat, strv);
1273 /* Also allow messages with two slashes before the
1274 * first space, so it is possible to send a /unix/path.
1275 * This heuristic is kind of crap. */
1276 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1278 second_slash = TRUE;
1284 if (!second_slash) {
1285 empathy_theme_adium_append_event (chat->view,
1286 _("Unknown command; see /help for the available"
1292 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1294 empathy_tp_chat_send (priv->tp_chat, message);
1295 g_object_unref (message);
1299 chat_input_text_view_send (EmpathyChat *chat)
1301 GtkTextBuffer *buffer;
1302 GtkTextIter start, end;
1305 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1307 gtk_text_buffer_get_bounds (buffer, &start, &end);
1308 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1310 /* clear the input field */
1311 gtk_text_buffer_set_text (buffer, "", -1);
1312 /* delete input history modifications */
1313 chat_input_history_revert (chat);
1315 chat_send (chat, msg);
1320 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1321 TpContact *tp_contact,
1322 TpChannelChatState state,
1325 EmpathyChatPriv *priv;
1327 gboolean was_composing;
1328 EmpathyContact *contact;
1330 priv = GET_PRIV (chat);
1332 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1334 if (empathy_contact_is_user (contact)) {
1335 /* We don't care about our own chat state */
1339 was_composing = (priv->compositors != NULL);
1341 /* Find the contact in the list. After that l is the list elem or NULL */
1342 for (l = priv->compositors; l; l = l->next) {
1343 if (contact == l->data) {
1349 case TP_CHANNEL_CHAT_STATE_GONE:
1350 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1351 case TP_CHANNEL_CHAT_STATE_PAUSED:
1352 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1353 /* Contact is not composing */
1355 priv->compositors = g_list_remove_link (priv->compositors, l);
1356 g_object_unref (l->data);
1360 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1361 /* Contact is composing */
1363 priv->compositors = g_list_prepend (priv->compositors,
1364 g_object_ref (contact));
1368 g_assert_not_reached ();
1371 DEBUG ("Was composing: %s now composing: %s",
1372 was_composing ? "yes" : "no",
1373 priv->compositors ? "yes" : "no");
1375 if ((was_composing && !priv->compositors) ||
1376 (!was_composing && priv->compositors)) {
1377 /* Composing state changed */
1378 g_signal_emit (chat, signals[COMPOSING], 0,
1379 priv->compositors != NULL);
1383 g_object_unref (contact);
1387 get_highlight_regex_for (const gchar *name)
1390 gchar *name_esc, *pattern;
1391 GError *error = NULL;
1393 name_esc = g_regex_escape_string (name, -1);
1394 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1395 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1398 if (regex == NULL) {
1399 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1402 g_error_free (error);
1411 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1412 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1415 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1417 EmpathyChatPriv *priv = GET_PRIV (chat);
1419 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1421 if (priv->self_contact != NULL) {
1422 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1424 g_return_if_fail (alias != NULL);
1425 priv->highlight_regex = get_highlight_regex_for (alias);
1430 chat_should_highlight (EmpathyChat *chat,
1431 EmpathyMessage *message)
1433 EmpathyChatPriv *priv = GET_PRIV (chat);
1435 TpChannelTextMessageFlags flags;
1437 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1439 if (!empathy_chat_is_room (chat)) {
1443 if (!empathy_message_is_incoming (message)) {
1447 msg = empathy_message_get_body (message);
1452 flags = empathy_message_get_flags (message);
1453 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1454 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1455 * have already been received by the user before (and so are in the logs) */
1459 if (priv->highlight_regex == NULL) {
1463 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1467 chat_message_received (EmpathyChat *chat,
1468 EmpathyMessage *message,
1471 EmpathyChatPriv *priv = GET_PRIV (chat);
1472 EmpathyContact *sender;
1474 sender = empathy_message_get_sender (message);
1476 if (empathy_message_is_edit (message)) {
1477 DEBUG ("Editing message '%s' to '%s'",
1478 empathy_message_get_supersedes (message),
1479 empathy_message_get_body (message));
1481 empathy_theme_adium_edit_message (chat->view, message);
1483 gboolean should_highlight = chat_should_highlight (chat, message);
1485 if (should_highlight) {
1486 priv->highlighted = TRUE;
1489 DEBUG ("Appending new message '%s' from %s (%d)",
1490 empathy_message_get_token (message),
1491 empathy_contact_get_alias (sender),
1492 empathy_contact_get_handle (sender));
1494 empathy_theme_adium_append_message (chat->view, message, should_highlight);
1496 if (empathy_message_is_incoming (message)) {
1497 priv->unread_messages++;
1498 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1501 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1505 /* We received a message so the contact is no longer
1507 chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
1508 TP_CHANNEL_CHAT_STATE_ACTIVE,
1513 chat_message_received_cb (EmpathyTpChat *tp_chat,
1514 EmpathyMessage *message,
1517 chat_message_received (chat, message, FALSE);
1521 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1522 EmpathyMessage *message,
1525 EmpathyChatPriv *priv = GET_PRIV (chat);
1527 empathy_theme_adium_message_acknowledged (chat->view,
1530 if (!empathy_message_is_edit (message)) {
1531 priv->unread_messages--;
1532 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1537 append_balance_error (EmpathyChat *chat,
1538 const gchar *message_body)
1540 EmpathyChatPriv *priv = GET_PRIV (chat);
1541 TpConnection *conn = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
1542 const gchar *uri = tp_connection_get_balance_uri (conn);
1543 const gchar *error = _("insufficient balance to send message");
1544 gchar *str, *str_markup = NULL;
1546 if (message_body != NULL) {
1547 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1549 str = g_strdup_printf (_("Error sending message: %s"), error);
1552 if (!tp_str_empty (uri)) {
1553 /* translators: error used when user doesn't have enough credit on his
1554 * account to send the message. */
1555 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1556 " <a href='%s'>Top up</a>."), uri);
1558 if (message_body != NULL) {
1559 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1561 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1562 escaped_body, markup_error);
1564 g_free (escaped_body);
1566 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1569 g_free (markup_error);
1572 if (str_markup != NULL)
1573 empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
1575 empathy_theme_adium_append_event (chat->view, str);
1578 g_free (str_markup);
1582 chat_send_error_cb (EmpathyTpChat *tp_chat,
1583 const gchar *message_body,
1584 TpChannelTextSendError error_code,
1585 const gchar *dbus_error,
1588 const gchar *error = NULL;
1591 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1592 append_balance_error (chat, message_body);
1594 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1595 error = _("not capable");
1598 if (error == NULL) {
1599 /* if we didn't find a dbus-error, try the old error */
1600 switch (error_code) {
1601 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1602 error = _("offline");
1604 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1605 error = _("invalid contact");
1607 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1608 error = _("permission denied");
1610 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1611 error = _("too long message");
1613 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1614 error = _("not implemented");
1616 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1618 error = _("unknown");
1623 if (message_body != NULL) {
1624 str = g_strdup_printf (_("Error sending message '%s': %s"),
1625 message_body, error);
1628 str = g_strdup_printf (_("Error sending message: %s"), error);
1631 empathy_theme_adium_append_event (chat->view, str);
1636 chat_topic_label_size_allocate_cb (GtkLabel *label,
1637 GtkAllocation *allocation,
1640 EmpathyChatPriv *priv = GET_PRIV (chat);
1642 if (!gtk_label_get_line_wrap (label)) {
1643 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1644 gtk_widget_show (priv->expander_topic);
1646 gtk_widget_hide (priv->expander_topic);
1653 chat_topic_expander_activate_cb (GtkExpander *expander,
1654 GParamSpec *param_spec,
1657 EmpathyChatPriv *priv = GET_PRIV (chat);
1659 if (gtk_expander_get_expanded (expander)) {
1660 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1661 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1663 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1664 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1669 chat_subject_changed_cb (EmpathyChat *chat)
1671 EmpathyChatPriv *priv = GET_PRIV (chat);
1673 g_free (priv->subject);
1674 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1675 g_object_notify (G_OBJECT (chat), "subject");
1677 if (EMP_STR_EMPTY (priv->subject)) {
1678 gtk_widget_hide (priv->hbox_topic);
1680 gchar *markup_topic;
1683 markup_topic = tpaw_add_link_markup (priv->subject);
1684 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1685 _("Topic:"), markup_topic);
1687 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1688 g_free (markup_text);
1689 g_free (markup_topic);
1691 gtk_widget_show (priv->hbox_topic);
1693 if (priv->block_events_timeout_id == 0) {
1696 if (!EMP_STR_EMPTY (priv->subject)) {
1697 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1699 if (tp_str_empty (actor)) {
1700 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1702 str = g_strdup_printf (_("Topic set by %s to: %s"),
1703 actor, priv->subject);
1705 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1706 /* No need to display this 'event' is no topic can be defined anyway */
1707 str = g_strdup (_("No topic defined"));
1711 empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
1718 chat_title_changed_cb (EmpathyChat *chat)
1720 EmpathyChatPriv *priv = GET_PRIV (chat);
1722 g_free (priv->name);
1723 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1724 g_object_notify (G_OBJECT (chat), "name");
1728 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1732 GtkTextIter word_start = *iter;
1733 GtkTextIter word_end = *iter;
1736 if (gtk_text_iter_inside_word (&word_end) &&
1737 !gtk_text_iter_ends_word (&word_end)) {
1738 gtk_text_iter_forward_word_end (&word_end);
1743 if (gtk_text_iter_get_char (&tmp) == '\'') {
1744 gtk_text_iter_forward_char (&tmp);
1746 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1747 gtk_text_iter_forward_word_end (&word_end);
1752 if (gtk_text_iter_inside_word (&word_start) ||
1753 gtk_text_iter_ends_word (&word_start)) {
1754 if (!gtk_text_iter_starts_word (&word_start) ||
1755 gtk_text_iter_equal (&word_start, &word_end)) {
1756 gtk_text_iter_backward_word_start (&word_start);
1760 gtk_text_iter_backward_char (&tmp);
1762 if (gtk_text_iter_get_char (&tmp) == '\'') {
1763 gtk_text_iter_backward_char (&tmp);
1765 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1766 gtk_text_iter_backward_word_start (&word_start);
1771 *start = word_start;
1777 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1778 GtkTextIter *location,
1783 GtkTextIter iter, pos;
1785 /* Remove all misspelled tags in the inserted text.
1786 * This happens when text is inserted within a misspelled word. */
1787 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1788 gtk_text_iter_get_offset (location) - len);
1789 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1792 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1795 GtkTextIter start, end;
1798 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1801 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1803 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1804 gtk_text_iter_equal (&pos, &end) ||
1805 empathy_spell_check (str)) {
1806 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1808 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1813 } while (gtk_text_iter_forward_word_end (&iter) &&
1814 gtk_text_iter_compare (&iter, location) <= 0);
1818 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1823 GtkTextIter word_start, word_end;
1825 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1826 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1827 &word_start, &word_end);
1832 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1835 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1836 chat_composing_stop (chat);
1838 chat_composing_start (chat);
1843 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1848 GtkTextIter prev_pos;
1849 GtkTextIter word_start;
1850 GtkTextIter word_end;
1854 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1856 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1857 gtk_text_buffer_get_insert (buffer));
1858 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1860 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1863 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1864 !gtk_text_iter_equal (&pos, &word_end)) {
1865 str = gtk_text_buffer_get_text (buffer,
1866 &word_start, &word_end, FALSE);
1868 if (!empathy_spell_check (str)) {
1869 gtk_text_buffer_apply_tag_by_name (buffer,
1870 "misspelled", &word_start, &word_end);
1872 gtk_text_buffer_remove_tag_by_name (buffer,
1873 "misspelled", &word_start, &word_end);
1880 gtk_text_buffer_move_mark (buffer, mark, &pos);
1884 empathy_isspace_cb (gunichar c,
1887 return g_unichar_isspace (c);
1891 chat_input_key_press_event_cb (GtkWidget *widget,
1895 EmpathyChatPriv *priv;
1898 GtkWidget *text_view_sw;
1900 priv = GET_PRIV (chat);
1902 priv->most_recent_event_type = event->type;
1904 /* Catch ctrl+up/down so we can traverse messages we sent */
1905 if ((event->state & GDK_CONTROL_MASK) &&
1906 (event->keyval == GDK_KEY_Up ||
1907 event->keyval == GDK_KEY_Down)) {
1908 GtkTextBuffer *buffer;
1911 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1912 chat_input_history_update (chat, buffer);
1914 if (event->keyval == GDK_KEY_Up) {
1915 str = chat_input_history_get_next (chat);
1917 str = chat_input_history_get_prev (chat);
1920 g_signal_handlers_block_by_func (buffer,
1921 chat_input_text_buffer_changed_cb,
1923 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1924 g_signal_handlers_unblock_by_func (buffer,
1925 chat_input_text_buffer_changed_cb,
1931 /* Catch enter but not ctrl/shift-enter */
1932 if (IS_ENTER (event->keyval) &&
1933 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1936 /* This is to make sure that kinput2 gets the enter. And if
1937 * it's handled there we shouldn't send on it. This is because
1938 * kinput2 uses Enter to commit letters. See:
1939 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1942 view = GTK_TEXT_VIEW (chat->input_text_view);
1943 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1944 gtk_text_view_reset_im_context (view);
1948 chat_input_text_view_send (chat);
1952 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1954 if (IS_ENTER (event->keyval) &&
1955 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1956 /* Newline for shift/control-enter. */
1959 if (!(event->state & GDK_CONTROL_MASK) &&
1960 event->keyval == GDK_KEY_Page_Up) {
1961 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1962 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1965 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1966 event->keyval == GDK_KEY_Page_Down) {
1967 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1968 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1969 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1970 gtk_adjustment_set_value (adj, val);
1973 if (event->keyval == GDK_KEY_Escape) {
1974 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1976 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1977 event->keyval == GDK_KEY_Tab) {
1978 GtkTextBuffer *buffer;
1979 GtkTextIter start, current;
1980 gchar *nick, *completed;
1981 GList *list, *completed_list;
1982 gboolean is_start_of_buffer;
1984 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1985 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1987 /* Get the start of the nick to complete. */
1988 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1989 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1990 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1992 is_start_of_buffer = gtk_text_iter_is_start (&start);
1994 list = empathy_tp_chat_get_members (priv->tp_chat);
1995 g_completion_add_items (priv->completion, list);
1997 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1998 completed_list = g_completion_complete (priv->completion,
2007 GString *message = NULL;
2010 gtk_text_buffer_delete (buffer, &start, ¤t);
2012 len = g_list_length (completed_list);
2015 /* If we only have one hit, use that text
2016 * instead of the text in completed since the
2017 * completed text will use the typed string
2018 * which might be cased all wrong.
2021 text = empathy_contact_get_alias (completed_list->data);
2025 /* Print all hits to the scrollback view, so the
2026 * user knows what possibilities he has.
2029 message = g_string_new ("");
2030 for (l = completed_list; l != NULL; l = l->next) {
2031 g_string_append (message, empathy_contact_get_alias (l->data));
2032 g_string_append (message, " - ");
2034 empathy_theme_adium_append_event (chat->view, message->str);
2035 g_string_free (message, TRUE);
2038 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2040 if (len == 1 && is_start_of_buffer) {
2041 gchar *complete_char;
2043 complete_char = g_settings_get_string (
2044 priv->gsettings_chat,
2045 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2047 if (complete_char != NULL) {
2048 gtk_text_buffer_insert_at_cursor (buffer,
2050 strlen (complete_char));
2051 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2052 g_free (complete_char);
2059 g_completion_clear_items (priv->completion);
2061 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2071 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2075 gtk_widget_grab_focus (chat->input_text_view);
2081 chat_input_realize_cb (GtkWidget *widget,
2084 DEBUG ("Setting focus to the input text view");
2085 if (gtk_widget_is_sensitive (widget)) {
2086 gtk_widget_grab_focus (widget);
2091 chat_input_has_focus_notify_cb (GtkWidget *widget,
2095 empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2099 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2100 EmpathySmiley *smiley,
2103 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2104 GtkTextBuffer *buffer;
2107 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2109 gtk_text_buffer_get_end_iter (buffer, &iter);
2110 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2112 gtk_text_buffer_get_end_iter (buffer, &iter);
2113 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2124 static EmpathyChatSpell *
2125 chat_spell_new (EmpathyChat *chat,
2130 EmpathyChatSpell *chat_spell;
2132 chat_spell = g_slice_new0 (EmpathyChatSpell);
2134 chat_spell->chat = g_object_ref (chat);
2135 chat_spell->word = g_strdup (word);
2136 chat_spell->start = start;
2137 chat_spell->end = end;
2143 chat_spell_free (EmpathyChatSpell *chat_spell)
2145 g_object_unref (chat_spell->chat);
2146 g_free (chat_spell->word);
2147 g_slice_free (EmpathyChatSpell, chat_spell);
2151 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2152 EmpathyChatSpell *chat_spell)
2154 empathy_chat_correct_word (chat_spell->chat,
2155 &(chat_spell->start),
2157 gtk_menu_item_get_label (menu_item));
2162 chat_spelling_build_suggestions_menu (const gchar *code,
2163 EmpathyChatSpell *chat_spell)
2165 GList *suggestions, *l;
2166 GtkWidget *menu, *menu_item;
2168 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2169 if (suggestions == NULL)
2172 menu = gtk_menu_new ();
2173 for (l = suggestions; l; l = l->next) {
2174 menu_item = gtk_menu_item_new_with_label (l->data);
2175 g_signal_connect (G_OBJECT (menu_item), "activate",
2176 G_CALLBACK (chat_spelling_menu_activate_cb),
2178 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2180 empathy_spell_free_suggestions (suggestions);
2182 gtk_widget_show_all (menu);
2188 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2190 GtkWidget *menu, *submenu, *item;
2193 codes = empathy_spell_get_enabled_language_codes ();
2194 g_assert (codes != NULL);
2196 if (g_list_length (codes) > 1) {
2197 menu = gtk_menu_new ();
2199 for (l = codes; l; l = l->next) {
2200 const gchar *code, *name;
2203 name = empathy_spell_get_language_name (code);
2207 item = gtk_image_menu_item_new_with_label (name);
2209 submenu = chat_spelling_build_suggestions_menu (
2211 if (submenu == NULL)
2212 gtk_widget_set_sensitive (item, FALSE);
2214 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2216 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2219 menu = chat_spelling_build_suggestions_menu (codes->data,
2222 menu = gtk_menu_new ();
2223 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2224 gtk_widget_set_sensitive (item, FALSE);
2225 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2228 g_list_free (codes);
2230 gtk_widget_show_all (menu);
2241 static EmpathyChatWord *
2242 chat_word_new (EmpathyChat *chat,
2246 EmpathyChatWord *chat_word;
2248 chat_word = g_slice_new0 (EmpathyChatWord);
2250 chat_word->chat = g_object_ref (chat);
2251 chat_word->word = g_strdup (word);
2252 chat_word->code = g_strdup (code);
2258 chat_word_free (EmpathyChatWord *chat_word)
2260 g_object_unref (chat_word->chat);
2261 g_free (chat_word->word);
2262 g_free (chat_word->code);
2263 g_slice_free (EmpathyChatWord, chat_word);
2267 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2268 EmpathyChatWord *chat_word)
2270 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2272 empathy_spell_add_to_dictionary (chat_word->code,
2274 priv->update_misspelled_words_id =
2275 g_idle_add (update_misspelled_words, chat_word->chat);
2279 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2281 GtkWidget *menu, *item, *lang_item, *image;
2284 const gchar *code, *name;
2285 EmpathyChatWord *chat_word;
2287 codes = empathy_spell_get_enabled_language_codes ();
2288 g_assert (codes != NULL);
2289 if (g_list_length (codes) > 1) {
2290 /* translators: %s is the selected word */
2291 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2293 item = gtk_image_menu_item_new_with_mnemonic (label);
2295 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2296 GTK_ICON_SIZE_MENU);
2297 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2300 menu = gtk_menu_new ();
2302 for (l = codes; l; l = l->next) {
2304 name = empathy_spell_get_language_name (code);
2308 lang_item = gtk_image_menu_item_new_with_label (name);
2310 chat_word= chat_word_new (chat_spell->chat,
2311 chat_spell->word, code);
2312 g_object_set_data_full (G_OBJECT (lang_item),
2313 "chat-word", chat_word,
2314 (GDestroyNotify) chat_word_free);
2316 g_signal_connect (G_OBJECT (lang_item), "activate",
2317 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2319 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2321 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2324 name = empathy_spell_get_language_name (code);
2325 g_assert (name != NULL);
2326 /* translators: first %s is the selected word,
2327 * second %s is the language name of the target dictionary */
2328 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2329 chat_spell->word, name);
2330 item = gtk_image_menu_item_new_with_mnemonic (label);
2332 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2333 GTK_ICON_SIZE_MENU);
2334 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2336 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2338 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2339 (GDestroyNotify) chat_word_free);
2341 g_signal_connect (G_OBJECT (item), "activate",
2342 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2345 g_list_free (codes);
2347 gtk_widget_show_all (item);
2353 chat_text_send_cb (GtkMenuItem *menuitem,
2356 chat_input_text_view_send (chat);
2360 chat_input_button_press_event_cb (GtkTextView *view,
2361 GdkEventButton *event,
2364 EmpathyChatPriv *priv = GET_PRIV (chat);
2366 priv->most_recent_event_type = event->type;
2372 chat_input_populate_popup_cb (GtkTextView *view,
2376 EmpathyChatPriv *priv = GET_PRIV (chat);
2377 GtkTextBuffer *buffer;
2378 GtkTextTagTable *table;
2381 GtkTextIter iter, start, end;
2384 EmpathyChatSpell *chat_spell;
2385 GtkWidget *spell_menu;
2386 GtkWidget *spell_item;
2387 EmpathySmileyManager *smiley_manager;
2388 GtkWidget *smiley_menu;
2391 buffer = gtk_text_view_get_buffer (view);
2393 /* Add the emoticon menu. */
2394 item = gtk_separator_menu_item_new ();
2395 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2396 gtk_widget_show (item);
2398 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2399 image = gtk_image_new_from_icon_name ("face-smile",
2400 GTK_ICON_SIZE_MENU);
2401 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2402 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2403 gtk_widget_show (item);
2405 smiley_manager = empathy_smiley_manager_dup_singleton ();
2406 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2407 chat_insert_smiley_activate_cb,
2409 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2410 g_object_unref (smiley_manager);
2412 /* Add the Send menu item. */
2413 gtk_text_buffer_get_bounds (buffer, &start, &end);
2414 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2415 if (!EMP_STR_EMPTY (str)) {
2416 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2417 g_signal_connect (G_OBJECT (item), "activate",
2418 G_CALLBACK (chat_text_send_cb), chat);
2419 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2420 gtk_widget_show (item);
2424 /* Add the spell check menu item. */
2425 table = gtk_text_buffer_get_tag_table (buffer);
2426 tag = gtk_text_tag_table_lookup (table, "misspelled");
2428 switch (priv->most_recent_event_type) {
2429 case GDK_BUTTON_PRESS:
2430 /* get the location from the pointer */
2431 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2432 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2433 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2435 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2436 GTK_TEXT_WINDOW_WIDGET,
2439 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2444 g_warn_if_reached ();
2445 /* assume the KEY_PRESS case */
2448 /* get the location from the cursor */
2449 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2450 gtk_text_buffer_get_insert (buffer));
2456 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2457 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2459 str = gtk_text_buffer_get_text (buffer,
2460 &start, &end, FALSE);
2462 if (!EMP_STR_EMPTY (str)) {
2463 chat_spell = chat_spell_new (chat, str, start, end);
2464 g_object_set_data_full (G_OBJECT (menu),
2465 "chat-spell", chat_spell,
2466 (GDestroyNotify) chat_spell_free);
2468 item = gtk_separator_menu_item_new ();
2469 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2470 gtk_widget_show (item);
2472 /* Spelling suggestions */
2473 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2474 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2475 GTK_ICON_SIZE_MENU);
2476 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2478 spell_menu = chat_spelling_build_menu (chat_spell);
2479 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2482 spell_item = gtk_separator_menu_item_new ();
2483 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2484 gtk_widget_show (spell_item);
2486 /* Add to dictionary */
2487 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2489 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2490 gtk_widget_show (spell_item);
2492 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2493 gtk_widget_show (item);
2499 chat_log_filter (TplEvent *event,
2502 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2503 EmpathyChatPriv *priv = GET_PRIV (chat);
2504 EmpathyMessage *message;
2505 const GList *pending;
2506 bool retval = FALSE;
2508 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2509 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2511 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2512 message = empathy_message_from_tpl_log_event (event);
2514 for (; pending; pending = g_list_next (pending)) {
2515 if (empathy_message_equal (message, pending->data))
2522 g_object_unref (message);
2528 show_pending_messages (EmpathyChat *chat) {
2529 EmpathyChatPriv *priv = GET_PRIV (chat);
2530 const GList *messages, *l;
2532 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2534 if (chat->view == NULL || priv->tp_chat == NULL)
2537 if (!priv->can_show_pending)
2540 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2542 for (l = messages; l != NULL ; l = g_list_next (l)) {
2543 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2544 chat_message_received (chat, message, TRUE);
2550 chat_scrollable_set_value (gpointer user_data)
2552 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2553 EmpathyChatPriv *priv = GET_PRIV (chat);
2554 GtkAdjustment *adjustment;
2557 adjustment = gtk_scrollable_get_vadjustment (
2558 GTK_SCROLLABLE (chat->view));
2560 /* Set the chat->view's adjustment back to the value it had
2561 * before it grew as a result of new logs being inserted.
2563 upper = (guint) gtk_adjustment_get_upper (adjustment);
2564 gtk_adjustment_set_value (adjustment, upper - priv->scroll_offset);
2566 return G_SOURCE_REMOVE;
2570 got_filtered_messages_cb (GObject *walker,
2571 GAsyncResult *result,
2576 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2577 EmpathyChatPriv *priv = GET_PRIV (chat);
2578 GError *error = NULL;
2580 if (!tpl_log_walker_get_events_finish (TPL_LOG_WALKER (walker),
2581 result, &messages, &error)) {
2582 DEBUG ("%s. Aborting.", error->message);
2583 empathy_theme_adium_append_event (chat->view,
2584 _("Failed to retrieve recent logs"));
2585 g_error_free (error);
2589 for (l = g_list_last (messages); l; l = g_list_previous (l)) {
2590 EmpathyMessage *message;
2592 g_assert (TPL_IS_EVENT (l->data));
2594 message = empathy_message_from_tpl_log_event (l->data);
2595 g_object_unref (l->data);
2597 if (empathy_message_is_edit (message)) {
2598 /* this is an edited message, create a synthetic event
2599 * using the supersedes token and
2600 * original-message-sent timestamp, that we can then
2602 EmpathyMessage *syn_msg = g_object_new (
2603 EMPATHY_TYPE_MESSAGE,
2605 "token", empathy_message_get_supersedes (message),
2606 "type", empathy_message_get_tptype (message),
2607 "timestamp", empathy_message_get_original_timestamp (message),
2608 "incoming", empathy_message_is_incoming (message),
2610 "receiver", empathy_message_get_receiver (message),
2611 "sender", empathy_message_get_sender (message),
2614 empathy_theme_adium_prepend_message (chat->view, syn_msg,
2615 chat_should_highlight (chat, syn_msg));
2616 empathy_theme_adium_edit_message (chat->view, message);
2618 g_object_unref (syn_msg);
2620 /* append the latest message */
2621 empathy_theme_adium_prepend_message (chat->view, message,
2622 chat_should_highlight (chat, message));
2625 g_object_unref (message);
2627 g_list_free (messages);
2630 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2631 * about it in EmpathyChatPriv definition */
2632 priv->retrieving_backlogs = FALSE;
2633 empathy_chat_messages_read (chat);
2635 /* Turn back on scrolling */
2636 empathy_theme_adium_scroll (chat->view, TRUE);
2638 /* We start watching the scrolling movements only after the first
2639 * batch of logs have been fetched. Otherwise, if the
2640 * chat->view's page size is too small the scrollbar might hit
2641 * the upper edge and trigger another batch of logs to be
2644 if (G_UNLIKELY (!priv->watch_scroll &&
2645 !tpl_log_walker_is_end (priv->log_walker))) {
2646 /* The pending messages need not be shown after the
2647 * first batch of logs have been displayed */
2648 priv->can_show_pending = TRUE;
2649 show_pending_messages (chat);
2651 priv->watch_scroll = TRUE;
2652 g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_connect,
2653 g_object_ref (chat), g_object_unref);
2656 GtkAdjustment *adjustment;
2660 /* The chat->view's adjustment won't change unless we
2661 * return to the main loop. Save the current offset
2662 * from the lower edge (or the upper value of the
2663 * adjustment) so that we can restore it later once the
2666 adjustment = gtk_scrollable_get_vadjustment (
2667 GTK_SCROLLABLE (chat->view));
2668 upper = (guint) gtk_adjustment_get_upper (adjustment);
2669 value = (guint) gtk_adjustment_get_value (adjustment);
2670 priv->scroll_offset = upper - value;
2672 g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_set_value,
2673 g_object_ref (chat), g_object_unref);
2676 g_object_unref (chat);
2680 chat_add_logs (EmpathyChat *chat)
2682 EmpathyChatPriv *priv = GET_PRIV (chat);
2685 return G_SOURCE_REMOVE;
2688 /* Turn off scrolling temporarily */
2689 empathy_theme_adium_scroll (chat->view, FALSE);
2691 tpl_log_walker_get_events_async (priv->log_walker, 5,
2692 got_filtered_messages_cb, g_object_ref (chat));
2694 return G_SOURCE_REMOVE;
2698 chat_schedule_logs (EmpathyChat *chat)
2700 EmpathyChatPriv *priv = GET_PRIV (chat);
2702 if (priv->retrieving_backlogs)
2705 priv->retrieving_backlogs = TRUE;
2706 g_timeout_add_full (G_PRIORITY_LOW, 500, /* ms */
2707 (GSourceFunc) chat_add_logs, g_object_ref (chat), g_object_unref);
2711 chat_view_adjustment_changed_cb (GtkAdjustment *adjustment,
2714 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2715 EmpathyChatPriv *priv = GET_PRIV (chat);
2718 if (tpl_log_walker_is_end (priv->log_walker)) {
2719 g_signal_handlers_disconnect_by_func (adjustment,
2720 chat_view_adjustment_changed_cb, user_data);
2724 page_size = (guint) gtk_adjustment_get_page_size (adjustment);
2725 if (page_size <= priv->max_page_size)
2728 /* We need to fetch more logs if the page size of the view
2729 * increases, so that there is no empty space at the top.
2731 if (G_LIKELY (priv->max_page_size != 0))
2732 chat_schedule_logs (chat);
2734 priv->max_page_size = page_size;
2738 chat_view_adjustment_value_changed_cb (GtkAdjustment *adjustment,
2741 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2742 EmpathyChatPriv *priv = GET_PRIV (chat);
2746 if (tpl_log_walker_is_end (priv->log_walker)) {
2747 g_signal_handlers_disconnect_by_func (adjustment,
2748 chat_view_adjustment_value_changed_cb, user_data);
2752 lower = (guint) gtk_adjustment_get_lower (adjustment);
2753 value = (guint) gtk_adjustment_get_value (adjustment);
2757 /* Request for more logs to be fetched if the user hit the
2758 * upper edge of the chat->view.
2760 chat_schedule_logs (chat);
2764 chat_scrollable_connect (gpointer user_data)
2766 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2767 GtkAdjustment *adjustment;
2769 adjustment = gtk_scrollable_get_vadjustment (
2770 GTK_SCROLLABLE (chat->view));
2772 g_signal_connect (adjustment, "changed",
2773 G_CALLBACK (chat_view_adjustment_changed_cb), chat);
2774 g_signal_connect (adjustment, "value-changed",
2775 G_CALLBACK (chat_view_adjustment_value_changed_cb), chat);
2777 return G_SOURCE_REMOVE;
2781 chat_contacts_completion_func (const gchar *s1,
2785 gchar *tmp, *nick1, *nick2;
2792 return s1 ? -1 : +1;
2795 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2796 nick1 = g_utf8_casefold (tmp, -1);
2799 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2800 nick2 = g_utf8_casefold (tmp, -1);
2803 ret = strncmp (nick1, nick2, n);
2812 build_part_message (guint reason,
2814 EmpathyContact *actor,
2815 const gchar *message)
2817 GString *s = g_string_new ("");
2818 const gchar *actor_name = NULL;
2820 if (actor != NULL) {
2821 actor_name = empathy_contact_get_alias (actor);
2824 /* Having an actor only really makes sense for a few actions... */
2826 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2827 g_string_append_printf (s, _("%s has disconnected"), name);
2829 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2830 if (actor_name != NULL) {
2831 /* translators: reverse the order of these arguments
2832 * if the kicked should come before the kicker in your locale.
2834 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2837 g_string_append_printf (s, _("%s was kicked"), name);
2840 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2841 if (actor_name != NULL) {
2842 /* translators: reverse the order of these arguments
2843 * if the banned should come before the banner in your locale.
2845 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2848 g_string_append_printf (s, _("%s was banned"), name);
2852 g_string_append_printf (s, _("%s has left the room"), name);
2855 if (!EMP_STR_EMPTY (message)) {
2856 /* Note to translators: this string is appended to
2857 * notifications like "foo has left the room", with the message
2858 * given by the user living the room. If this poses a problem,
2859 * please let us know. :-)
2861 g_string_append_printf (s, _(" (%s)"), message);
2864 return g_string_free (s, FALSE);
2868 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2869 EmpathyContact *contact,
2870 EmpathyContact *actor,
2876 EmpathyChatPriv *priv = GET_PRIV (chat);
2877 const gchar *name = empathy_contact_get_alias (contact);
2880 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2882 if (priv->block_events_timeout_id != 0)
2886 str = g_strdup_printf (_("%s has joined the room"),
2889 str = build_part_message (reason, name, actor, message);
2892 empathy_theme_adium_append_event (chat->view, str);
2897 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2898 EmpathyContact *old_contact,
2899 EmpathyContact *new_contact,
2904 EmpathyChatPriv *priv = GET_PRIV (chat);
2906 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2908 if (priv->block_events_timeout_id == 0) {
2911 str = g_strdup_printf (_("%s is now known as %s"),
2912 empathy_contact_get_alias (old_contact),
2913 empathy_contact_get_alias (new_contact));
2914 empathy_theme_adium_append_event (chat->view, str);
2921 chat_contacts_visible_timeout_cb (gpointer chat)
2923 EmpathyChatPriv *priv = GET_PRIV (chat);
2925 /* Relax the size request */
2926 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2928 /* Set the position of the slider. This must be done here because
2929 * GtkPaned need to know its size allocation and it will be settled only
2930 * after the gtk_window_resize () tough effect. */
2931 if (priv->contacts_width > 0) {
2932 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2933 priv->contacts_width);
2936 priv->contacts_visible_id = 0;
2942 chat_update_contacts_visibility (EmpathyChat *chat,
2945 EmpathyChatPriv *priv = GET_PRIV (chat);
2947 if (!priv->scrolled_window_contacts) {
2951 if (priv->remote_contact != NULL) {
2955 if (show && priv->contact_list_view == NULL) {
2956 EmpathyIndividualStore *store;
2958 GtkAllocation allocation;
2960 /* We are adding the contact list to the chat, we don't want the
2961 * chat view to become too small. If the chat view is already
2962 * smaller than 250 make sure that size won't change. If the
2963 * chat view is bigger the contact list will take some space on
2964 * it but we make sure the chat view don't become smaller than
2965 * 250. Relax the size request once the resize is done */
2966 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2967 min_width = MIN (allocation.width, 250);
2968 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2970 /* There is no way to know when the window resize will happen
2971 * since it is WM's decision. Let's hope it won't be longer. */
2972 if (priv->contacts_visible_id != 0)
2973 g_source_remove (priv->contacts_visible_id);
2974 priv->contacts_visible_id = g_timeout_add (500,
2975 chat_contacts_visible_timeout_cb, chat);
2977 store = EMPATHY_INDIVIDUAL_STORE (
2978 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2980 empathy_individual_store_set_show_groups (store, FALSE);
2982 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2983 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2984 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2985 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2986 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2987 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2988 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2990 empathy_individual_view_set_show_offline (
2991 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2992 empathy_individual_view_set_show_uninteresting (
2993 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2995 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2996 priv->contact_list_view);
2998 gtk_widget_show (priv->contact_list_view);
2999 gtk_widget_show (priv->scrolled_window_contacts);
3000 g_object_unref (store);
3002 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
3003 gtk_widget_hide (priv->scrolled_window_contacts);
3004 if (priv->contact_list_view != NULL) {
3005 gtk_widget_destroy (priv->contact_list_view);
3006 priv->contact_list_view = NULL;
3012 empathy_chat_set_show_contacts (EmpathyChat *chat,
3015 EmpathyChatPriv *priv = GET_PRIV (chat);
3017 priv->show_contacts = show;
3019 chat_update_contacts_visibility (chat, show);
3021 g_object_notify (G_OBJECT (chat), "show-contacts");
3025 chat_self_contact_changed_cb (EmpathyChat *chat)
3027 EmpathyChatPriv *priv = GET_PRIV (chat);
3029 if (priv->self_contact != NULL) {
3030 g_signal_handlers_disconnect_by_func (priv->self_contact,
3031 chat_self_contact_alias_changed_cb,
3034 g_clear_object (&priv->self_contact);
3036 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
3037 if (priv->self_contact != NULL) {
3038 g_object_ref (priv->self_contact);
3040 if (empathy_chat_is_room (chat)) {
3041 g_signal_connect_swapped (priv->self_contact, "notify::alias",
3042 G_CALLBACK (chat_self_contact_alias_changed_cb),
3047 chat_self_contact_alias_changed_cb (chat);
3051 chat_remote_contact_changed_cb (EmpathyChat *chat)
3053 EmpathyChatPriv *priv = GET_PRIV (chat);
3055 if (priv->remote_contact != NULL) {
3056 g_object_unref (priv->remote_contact);
3057 priv->remote_contact = NULL;
3062 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
3063 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
3064 if (priv->remote_contact != NULL) {
3065 g_object_ref (priv->remote_contact);
3066 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
3068 else if (priv->tp_chat != NULL) {
3069 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
3072 chat_update_contacts_visibility (chat, priv->show_contacts);
3074 g_object_notify (G_OBJECT (chat), "remote-contact");
3075 g_object_notify (G_OBJECT (chat), "id");
3079 chat_invalidated_cb (EmpathyTpChat *tp_chat,
3085 EmpathyChatPriv *priv;
3087 priv = GET_PRIV (chat);
3089 if (!priv->tp_chat) {
3093 chat_composing_remove_timeout (chat);
3094 g_object_unref (priv->tp_chat);
3095 priv->tp_chat = NULL;
3096 g_object_notify (G_OBJECT (chat), "tp-chat");
3098 empathy_theme_adium_append_event (chat->view, _("Disconnected"));
3099 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
3101 chat_update_contacts_visibility (chat, FALSE);
3103 priv->unread_messages_when_offline = priv->unread_messages;
3107 update_misspelled_words (gpointer data)
3109 EmpathyChat *chat = EMPATHY_CHAT (data);
3110 EmpathyChatPriv *priv = GET_PRIV (chat);
3111 GtkTextBuffer *buffer;
3115 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3117 gtk_text_buffer_get_end_iter (buffer, &iter);
3118 length = gtk_text_iter_get_offset (&iter);
3119 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3120 NULL, length, chat);
3122 priv->update_misspelled_words_id = 0;
3128 conf_spell_checking_cb (GSettings *gsettings_chat,
3132 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3133 EmpathyChatPriv *priv = GET_PRIV (chat);
3134 gboolean spell_checker;
3135 GtkTextBuffer *buffer;
3137 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3140 spell_checker = g_settings_get_boolean (gsettings_chat,
3141 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3143 if (!empathy_spell_supported ()) {
3144 spell_checker = FALSE;
3147 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3149 if (spell_checker == priv->spell_checking_enabled) {
3150 if (spell_checker) {
3151 /* Possibly changed dictionaries,
3152 * update misspelled words. Need to do so in idle
3153 * so the spell checker is updated. */
3154 priv->update_misspelled_words_id =
3155 g_idle_add (update_misspelled_words, chat);
3161 if (spell_checker) {
3164 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3165 buffer, "notify::cursor-position",
3166 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3168 priv->insert_text_id = tp_g_signal_connect_object (
3169 buffer, "insert-text",
3170 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3171 chat, G_CONNECT_AFTER);
3172 priv->delete_range_id = tp_g_signal_connect_object (
3173 buffer, "delete-range",
3174 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3175 chat, G_CONNECT_AFTER);
3177 gtk_text_buffer_create_tag (buffer, "misspelled",
3178 "underline", PANGO_UNDERLINE_ERROR,
3181 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3182 gtk_text_buffer_get_insert (buffer));
3183 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3186 /* Mark misspelled words in the existing buffer.
3187 * Need to do so in idle so the spell checker is updated. */
3188 priv->update_misspelled_words_id =
3189 g_idle_add (update_misspelled_words, chat);
3191 GtkTextTagTable *table;
3194 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3195 priv->notify_cursor_position_id = 0;
3196 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3197 priv->insert_text_id = 0;
3198 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3199 priv->delete_range_id = 0;
3201 table = gtk_text_buffer_get_tag_table (buffer);
3202 tag = gtk_text_tag_table_lookup (table, "misspelled");
3203 gtk_text_tag_table_remove (table, tag);
3205 gtk_text_buffer_delete_mark_by_name (buffer,
3206 "previous-cursor-position");
3209 priv->spell_checking_enabled = spell_checker;
3213 save_paned_pos_timeout (gpointer data)
3215 EmpathyChat *self = data;
3218 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3220 g_settings_set_int (self->priv->gsettings_ui,
3221 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3228 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3232 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3234 if (chat->priv->save_paned_pos_id != 0)
3235 g_source_remove (chat->priv->save_paned_pos_id);
3237 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3238 save_paned_pos_timeout, chat);
3244 chat_create_ui (EmpathyChat *chat)
3246 EmpathyChatPriv *priv = GET_PRIV (chat);
3250 GtkTextBuffer *buffer;
3251 EmpathyThemeManager *theme_mgr;
3253 filename = empathy_file_lookup ("empathy-chat.ui",
3255 gui = tpaw_builder_get_file (filename,
3256 "chat_widget", &priv->widget,
3257 "hpaned", &priv->hpaned,
3258 "vbox_left", &priv->vbox_left,
3259 "scrolled_window_chat", &priv->scrolled_window_chat,
3260 "scrolled_window_input", &priv->scrolled_window_input,
3261 "hbox_topic", &priv->hbox_topic,
3262 "expander_topic", &priv->expander_topic,
3263 "label_topic", &priv->label_topic,
3264 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3265 "info_bar_vbox", &priv->info_bar_vbox,
3268 tpaw_builder_connect (gui, chat,
3269 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3270 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3275 /* Add message view. */
3276 theme_mgr = empathy_theme_manager_dup_singleton ();
3277 chat->view = empathy_theme_manager_create_view (theme_mgr);
3278 g_object_unref (theme_mgr);
3279 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3280 and other types, even though it's non-editable and doesn't accept any
3281 drags. This steals drag motion for anything inside the scrollbars,
3282 making drag destinations on chat windows far less useful.
3284 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3285 g_signal_connect (chat->view, "focus_in_event",
3286 G_CALLBACK (chat_text_view_focus_in_event_cb),
3288 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3289 GTK_WIDGET (chat->view));
3290 gtk_widget_show (GTK_WIDGET (chat->view));
3292 /* Add input GtkTextView */
3293 chat->input_text_view = empathy_input_text_view_new ();
3294 g_signal_connect (chat->input_text_view, "notify::has-focus",
3295 G_CALLBACK (chat_input_has_focus_notify_cb),
3297 g_signal_connect (chat->input_text_view, "key-press-event",
3298 G_CALLBACK (chat_input_key_press_event_cb),
3300 g_signal_connect (chat->input_text_view, "realize",
3301 G_CALLBACK (chat_input_realize_cb),
3303 g_signal_connect (chat->input_text_view, "button-press-event",
3304 G_CALLBACK (chat_input_button_press_event_cb),
3306 g_signal_connect (chat->input_text_view, "populate-popup",
3307 G_CALLBACK (chat_input_populate_popup_cb),
3309 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3310 tp_g_signal_connect_object (buffer, "changed",
3311 G_CALLBACK (chat_input_text_buffer_changed_cb),
3313 tp_g_signal_connect_object (priv->gsettings_chat,
3314 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3315 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3316 conf_spell_checking_cb (priv->gsettings_chat,
3317 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3318 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3319 chat->input_text_view);
3320 gtk_widget_show (chat->input_text_view);
3322 /* Add the (invisible) search bar */
3323 priv->search_bar = empathy_search_bar_new (chat->view);
3324 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3327 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3329 /* Initialy hide the topic, will be shown if not empty */
3330 gtk_widget_hide (priv->hbox_topic);
3332 g_signal_connect (priv->hpaned, "notify::position",
3333 G_CALLBACK (chat_hpaned_pos_changed_cb),
3336 /* Set widget focus order */
3337 list = g_list_append (NULL, priv->search_bar);
3338 list = g_list_append (list, priv->scrolled_window_input);
3339 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3342 list = g_list_append (NULL, priv->vbox_left);
3343 list = g_list_append (list, priv->scrolled_window_contacts);
3344 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3347 list = g_list_append (NULL, priv->hpaned);
3348 list = g_list_append (list, priv->hbox_topic);
3349 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3352 /* Add the main widget in the chat widget */
3353 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3354 g_object_unref (gui);
3358 chat_finalize (GObject *object)
3361 EmpathyChatPriv *priv;
3363 chat = EMPATHY_CHAT (object);
3364 priv = GET_PRIV (chat);
3366 DEBUG ("Finalized: %p", object);
3368 if (priv->update_misspelled_words_id != 0)
3369 g_source_remove (priv->update_misspelled_words_id);
3371 if (priv->save_paned_pos_id != 0)
3372 g_source_remove (priv->save_paned_pos_id);
3374 if (priv->contacts_visible_id != 0)
3375 g_source_remove (priv->contacts_visible_id);
3377 g_object_unref (priv->gsettings_chat);
3378 g_object_unref (priv->gsettings_ui);
3380 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3381 g_list_free (priv->input_history);
3383 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3384 g_list_free (priv->compositors);
3386 chat_composing_remove_timeout (chat);
3388 g_object_unref (priv->account_manager);
3389 g_object_unref (priv->log_manager);
3390 g_object_unref (priv->log_walker);
3392 if (priv->tp_chat) {
3393 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3394 chat_invalidated_cb, chat);
3395 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3396 chat_message_received_cb, chat);
3397 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3398 chat_message_acknowledged_cb, chat);
3399 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3400 chat_send_error_cb, chat);
3401 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3402 chat_state_changed_cb, chat);
3403 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3404 chat_members_changed_cb, chat);
3405 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3406 chat_self_contact_changed_cb, chat);
3407 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3408 chat_remote_contact_changed_cb, chat);
3409 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3410 chat_title_changed_cb, chat);
3411 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3412 chat_subject_changed_cb, chat);
3413 empathy_tp_chat_leave (priv->tp_chat, "");
3414 g_object_unref (priv->tp_chat);
3416 if (priv->account) {
3417 g_object_unref (priv->account);
3419 if (priv->self_contact) {
3420 g_signal_handlers_disconnect_by_func (priv->self_contact,
3421 chat_self_contact_alias_changed_cb,
3423 g_object_unref (priv->self_contact);
3425 if (priv->remote_contact) {
3426 g_object_unref (priv->remote_contact);
3429 if (priv->block_events_timeout_id) {
3430 g_source_remove (priv->block_events_timeout_id);
3434 g_free (priv->name);
3435 g_free (priv->subject);
3436 g_completion_free (priv->completion);
3438 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3440 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3444 chat_constructed (GObject *object)
3446 EmpathyChat *chat = EMPATHY_CHAT (object);
3447 EmpathyChatPriv *priv = GET_PRIV (chat);
3450 if (priv->tp_chat != NULL) {
3451 TpChannel *channel = TP_CHANNEL (priv->tp_chat);
3452 TpConnection *conn = tp_channel_get_connection (channel);
3453 gboolean supports_avatars =
3454 tp_proxy_has_interface_by_id (conn,
3455 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS);
3457 empathy_theme_adium_set_show_avatars (chat->view,
3461 /* Add messages from last conversation */
3462 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
3463 target = tpl_entity_new_from_room_id (priv->id);
3465 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
3467 priv->log_walker = tpl_log_manager_walk_filtered_events (priv->log_manager, priv->account, target,
3468 TPL_EVENT_MASK_TEXT, chat_log_filter, chat);
3469 g_object_unref (target);
3471 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3472 /* First display logs from the logger and then display pending messages */
3473 chat_add_logs (chat);
3476 /* Just display pending messages for rooms */
3477 priv->can_show_pending = TRUE;
3478 show_pending_messages (chat);
3483 empathy_chat_class_init (EmpathyChatClass *klass)
3485 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3487 object_class->finalize = chat_finalize;
3488 object_class->get_property = chat_get_property;
3489 object_class->set_property = chat_set_property;
3490 object_class->constructed = chat_constructed;
3492 g_object_class_install_property (object_class,
3494 g_param_spec_object ("tp-chat",
3496 "The tp chat object",
3497 EMPATHY_TYPE_TP_CHAT,
3500 G_PARAM_STATIC_STRINGS));
3501 g_object_class_install_property (object_class,
3503 g_param_spec_object ("account",
3504 "Account of the chat",
3505 "The account of the chat",
3508 G_PARAM_STATIC_STRINGS));
3509 g_object_class_install_property (object_class,
3511 g_param_spec_string ("id",
3513 "The id of the chat",
3516 G_PARAM_STATIC_STRINGS));
3517 g_object_class_install_property (object_class,
3519 g_param_spec_string ("name",
3521 "The name of the chat",
3524 G_PARAM_STATIC_STRINGS));
3525 g_object_class_install_property (object_class,
3527 g_param_spec_string ("subject",
3529 "The subject or topic of the chat",
3532 G_PARAM_STATIC_STRINGS));
3533 g_object_class_install_property (object_class,
3534 PROP_REMOTE_CONTACT,
3535 g_param_spec_object ("remote-contact",
3536 "The remote contact",
3537 "The remote contact is any",
3538 EMPATHY_TYPE_CONTACT,
3540 G_PARAM_STATIC_STRINGS));
3541 g_object_class_install_property (object_class,
3543 g_param_spec_boolean ("show-contacts",
3544 "Contacts' visibility",
3545 "The visibility of the contacts' list",
3548 G_PARAM_STATIC_STRINGS));
3550 g_object_class_install_property (object_class,
3552 g_param_spec_boolean ("sms-channel",
3554 "TRUE if this channel is for sending SMSes",
3556 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3558 g_object_class_install_property (object_class,
3559 PROP_N_MESSAGES_SENDING,
3560 g_param_spec_uint ("n-messages-sending",
3561 "Num Messages Sending",
3562 "The number of messages being sent",
3564 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3566 g_object_class_install_property (object_class,
3567 PROP_NB_UNREAD_MESSAGES,
3568 g_param_spec_uint ("nb-unread-messages",
3569 "Num Unread Messages",
3570 "The number of unread messages",
3572 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3574 signals[COMPOSING] =
3575 g_signal_new ("composing",
3576 G_OBJECT_CLASS_TYPE (object_class),
3580 g_cclosure_marshal_generic,
3585 * EmpathyChat::new-message:
3586 * @self: the #EmpathyChat
3587 * @message: the new message
3588 * @pending: whether the message was in the pending queue when @self
3590 * @should_highlight: %TRUE if the message mentions the local user
3592 signals[NEW_MESSAGE] =
3593 g_signal_new ("new-message",
3594 G_OBJECT_CLASS_TYPE (object_class),
3598 g_cclosure_marshal_generic,
3600 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3602 signals[PART_COMMAND_ENTERED] =
3603 g_signal_new ("part-command-entered",
3604 G_OBJECT_CLASS_TYPE (object_class),
3608 g_cclosure_marshal_generic,
3612 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3616 chat_block_events_timeout_cb (gpointer data)
3618 EmpathyChatPriv *priv = GET_PRIV (data);
3620 priv->block_events_timeout_id = 0;
3626 account_manager_prepared_cb (GObject *source_object,
3627 GAsyncResult *result,
3630 GList *accounts, *l;
3631 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3632 EmpathyChat *chat = user_data;
3633 GError *error = NULL;
3635 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3636 DEBUG ("Failed to prepare the account manager: %s", error->message);
3637 g_error_free (error);
3641 accounts = tp_account_manager_dup_valid_accounts (account_manager);
3643 for (l = accounts; l != NULL; l = l->next) {
3644 TpAccount *account = l->data;
3645 tp_g_signal_connect_object (account, "status-changed",
3646 G_CALLBACK (chat_new_connection_cb),
3650 g_list_free_full (accounts, g_object_unref);
3654 empathy_chat_init (EmpathyChat *chat)
3656 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3657 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3660 priv->log_manager = tpl_log_manager_dup_singleton ();
3661 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3662 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3664 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3665 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3666 priv->input_history = NULL;
3667 priv->input_history_current = NULL;
3668 priv->account_manager = tp_account_manager_dup ();
3670 tp_proxy_prepare_async (priv->account_manager, NULL,
3671 account_manager_prepared_cb, chat);
3673 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3674 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3676 /* Block events for some time to avoid having "has come online" or
3677 * "joined" messages. */
3678 priv->block_events_timeout_id =
3679 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3681 /* Add nick name completion */
3682 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3683 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3685 chat_create_ui (chat);
3689 empathy_chat_new (EmpathyTpChat *tp_chat)
3691 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3695 empathy_chat_get_tp_chat (EmpathyChat *chat)
3697 EmpathyChatPriv *priv = GET_PRIV (chat);
3699 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3701 return priv->tp_chat;
3707 GtkWidget *info_bar;
3717 passwd_remember_button_cb (GtkButton *button,
3720 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3724 passwd_not_now_button_cb (GtkButton *button,
3727 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3731 remember_password_infobar_response_cb (GtkWidget *info_bar,
3735 EmpathyChatPriv *priv = GET_PRIV (data->self);
3737 if (response_id == GTK_RESPONSE_OK) {
3738 DEBUG ("Saving room password");
3739 tpaw_keyring_set_room_password_async (priv->account,
3740 empathy_tp_chat_get_id (priv->tp_chat),
3745 gtk_widget_destroy (info_bar);
3746 g_free (data->password);
3747 g_slice_free (PasswordData, data);
3751 chat_prompt_to_save_password (EmpathyChat *self,
3754 GtkWidget *content_area;
3761 /* save the password in case it needs to be saved */
3762 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3764 /* Remove all previous widgets */
3765 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3766 gtk_container_forall (GTK_CONTAINER (content_area),
3767 (GtkCallback) gtk_widget_destroy, NULL);
3768 data->button = NULL;
3771 data->spinner = NULL;
3773 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3774 GTK_MESSAGE_QUESTION);
3776 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3777 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3780 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3781 GTK_ICON_SIZE_DIALOG);
3782 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3785 label = gtk_label_new (_("Would you like to store this password?"));
3786 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3788 /* Add 'Remember' button */
3789 alig = gtk_alignment_new (0, 0.5, 1, 0);
3791 button = gtk_button_new_with_label (_("Remember"));
3792 gtk_container_add (GTK_CONTAINER (alig), button);
3793 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3795 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3798 /* Add 'Not now' button */
3799 alig = gtk_alignment_new (0, 0.5, 1, 0);
3801 button = gtk_button_new_with_label (_("Not now"));
3802 gtk_container_add (GTK_CONTAINER (alig), button);
3803 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3805 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3809 g_signal_handler_disconnect (data->info_bar, data->response_id);
3810 g_signal_connect (data->info_bar, "response",
3811 G_CALLBACK (remember_password_infobar_response_cb), data);
3813 gtk_widget_show_all (data->info_bar);
3817 provide_password_cb (GObject *tp_chat,
3821 PasswordData *data = user_data;
3822 EmpathyChat *self = data->self;
3823 EmpathyChatPriv *priv = GET_PRIV (self);
3824 GError *error = NULL;
3826 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3828 DEBUG ("error: %s", error->message);
3829 /* FIXME: what should we do if that's another error? Close the channel?
3830 * Display the raw D-Bus error to the user isn't very useful */
3831 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3833 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3834 gtk_widget_set_sensitive (data->entry, TRUE);
3835 gtk_widget_grab_focus (data->entry);
3838 gtk_info_bar_set_message_type (
3839 GTK_INFO_BAR (data->info_bar),
3843 gtk_widget_set_sensitive (data->button, TRUE);
3844 gtk_button_set_label (GTK_BUTTON (data->button),
3848 gtk_label_set_text (GTK_LABEL (data->label),
3849 _("Wrong password; please try again:"));
3852 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3853 gtk_widget_hide (data->spinner);
3855 g_error_free (error);
3859 /* ask whether they want to save the password */
3860 chat_prompt_to_save_password (self, data);
3863 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3864 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3865 gtk_widget_grab_focus (self->input_text_view);
3869 password_infobar_response_cb (GtkWidget *info_bar,
3873 EmpathyChatPriv *priv = GET_PRIV (data->self);
3874 const gchar *password;
3876 if (response_id != GTK_RESPONSE_OK) {
3877 gtk_widget_destroy (info_bar);
3878 g_slice_free (PasswordData, data);
3882 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3884 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3885 provide_password_cb, data);
3887 gtk_widget_set_sensitive (data->button, FALSE);
3888 gtk_widget_set_sensitive (data->entry, FALSE);
3890 gtk_spinner_start (GTK_SPINNER (data->spinner));
3891 gtk_widget_show (data->spinner);
3895 password_entry_activate_cb (GtkWidget *entry,
3898 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3902 passwd_join_button_cb (GtkButton *button,
3905 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3909 clear_icon_released_cb (GtkEntry *entry,
3910 GtkEntryIconPosition icon_pos,
3914 gtk_entry_set_text (entry, "");
3918 password_entry_changed_cb (GtkEditable *entry,
3923 str = gtk_entry_get_text (GTK_ENTRY (entry));
3925 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3926 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3930 infobar_chat_invalidated_cb (TpProxy *proxy,
3934 gpointer password_infobar)
3936 /* Destroy the password infobar whenever a channel is invalidated
3937 * so we don't have multiple infobars when the MUC is rejoined */
3938 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3942 display_password_info_bar (EmpathyChat *self)
3944 EmpathyChatPriv *priv = GET_PRIV (self);
3945 GtkWidget *info_bar;
3946 GtkWidget *content_area;
3956 data = g_slice_new0 (PasswordData);
3958 info_bar = gtk_info_bar_new ();
3959 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3960 GTK_MESSAGE_QUESTION);
3962 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3964 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3965 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3968 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3969 GTK_ICON_SIZE_DIALOG);
3970 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3973 label = gtk_label_new (_("This room is protected by a password:"));
3974 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3976 /* Add password entry */
3977 entry = gtk_entry_new ();
3978 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3979 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3981 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3982 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3983 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3984 GTK_ENTRY_ICON_SECONDARY, FALSE);
3986 g_signal_connect (entry, "icon-release",
3987 G_CALLBACK (clear_icon_released_cb), data);
3988 g_signal_connect (entry, "changed",
3989 G_CALLBACK (password_entry_changed_cb), data);
3991 g_signal_connect (entry, "activate",
3992 G_CALLBACK (password_entry_activate_cb), data);
3994 /* Focus the password entry once it's realized */
3995 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3997 /* Add 'Join' button */
3998 alig = gtk_alignment_new (0, 0.5, 1, 0);
4000 button = gtk_button_new_with_label (_("Join"));
4001 gtk_container_add (GTK_CONTAINER (alig), button);
4002 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
4004 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
4008 spinner = gtk_spinner_new ();
4009 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
4011 /* Save some data for messing around with later */
4013 data->info_bar = info_bar;
4014 data->button = button;
4015 data->label = label;
4016 data->entry = entry;
4017 data->spinner = spinner;
4019 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
4021 gtk_widget_show_all (hbox);
4023 tp_g_signal_connect_object (priv->tp_chat,
4024 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
4027 data->response_id = g_signal_connect (info_bar, "response",
4028 G_CALLBACK (password_infobar_response_cb), data);
4030 gtk_widget_show_all (info_bar);
4031 /* ... but hide the spinner */
4032 gtk_widget_hide (spinner);
4034 /* prevent the user from typing anything */
4035 gtk_widget_set_sensitive (self->input_text_view, FALSE);
4039 provide_saved_password_cb (GObject *tp_chat,
4043 EmpathyChat *self = user_data;
4044 EmpathyChatPriv *priv = GET_PRIV (self);
4045 GError *error = NULL;
4047 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
4049 DEBUG ("error: %s", error->message);
4050 /* FIXME: what should we do if that's another error? Close the channel?
4051 * Display the raw D-Bus error to the user isn't very useful */
4052 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
4053 display_password_info_bar (self);
4054 gtk_widget_set_sensitive (priv->hpaned, FALSE);
4056 g_error_free (error);
4061 gtk_widget_set_sensitive (priv->hpaned, TRUE);
4062 gtk_widget_grab_focus (self->input_text_view);
4066 chat_room_got_password_cb (GObject *source,
4067 GAsyncResult *result,
4070 EmpathyChat *self = user_data;
4071 EmpathyChatPriv *priv = GET_PRIV (self);
4072 const gchar *password;
4073 GError *error = NULL;
4075 password = tpaw_keyring_get_room_password_finish (priv->account,
4078 if (error != NULL) {
4079 DEBUG ("Couldn't get room password: %s\n", error->message);
4080 g_clear_error (&error);
4082 display_password_info_bar (self);
4083 gtk_widget_set_sensitive (priv->hpaned, FALSE);
4087 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
4088 provide_saved_password_cb, self);
4092 chat_password_needed_changed_cb (EmpathyChat *self)
4094 EmpathyChatPriv *priv = GET_PRIV (self);
4096 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
4097 tpaw_keyring_get_room_password_async (priv->account,
4098 empathy_tp_chat_get_id (priv->tp_chat),
4099 chat_room_got_password_cb, self);
4104 chat_sms_channel_changed_cb (EmpathyChat *self)
4106 EmpathyChatPriv *priv = GET_PRIV (self);
4108 priv->sms_channel = tp_text_channel_is_sms_channel (
4109 (TpTextChannel *) priv->tp_chat);
4110 g_object_notify (G_OBJECT (self), "sms-channel");
4114 chat_n_messages_sending_changed_cb (EmpathyChat *self)
4116 g_object_notify (G_OBJECT (self), "n-messages-sending");
4120 empathy_chat_set_tp_chat (EmpathyChat *chat,
4121 EmpathyTpChat *tp_chat)
4123 EmpathyChatPriv *priv = GET_PRIV (chat);
4125 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4126 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4128 if (priv->tp_chat) {
4132 if (priv->account) {
4133 g_object_unref (priv->account);
4136 priv->tp_chat = g_object_ref (tp_chat);
4137 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4139 g_signal_connect (tp_chat, "invalidated",
4140 G_CALLBACK (chat_invalidated_cb),
4142 g_signal_connect (tp_chat, "message-received-empathy",
4143 G_CALLBACK (chat_message_received_cb),
4145 g_signal_connect (tp_chat, "message_acknowledged",
4146 G_CALLBACK (chat_message_acknowledged_cb),
4148 g_signal_connect (tp_chat, "send-error",
4149 G_CALLBACK (chat_send_error_cb),
4151 g_signal_connect (tp_chat, "contact-chat-state-changed",
4152 G_CALLBACK (chat_state_changed_cb),
4154 g_signal_connect (tp_chat, "members-changed",
4155 G_CALLBACK (chat_members_changed_cb),
4157 g_signal_connect (tp_chat, "member-renamed",
4158 G_CALLBACK (chat_member_renamed_cb),
4160 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4161 G_CALLBACK (chat_self_contact_changed_cb),
4163 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4164 G_CALLBACK (chat_remote_contact_changed_cb),
4166 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4167 G_CALLBACK (chat_password_needed_changed_cb),
4169 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4170 G_CALLBACK (chat_sms_channel_changed_cb),
4172 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4173 G_CALLBACK (chat_n_messages_sending_changed_cb),
4175 g_signal_connect_swapped (tp_chat, "notify::title",
4176 G_CALLBACK (chat_title_changed_cb),
4178 g_signal_connect_swapped (tp_chat, "notify::subject",
4179 G_CALLBACK (chat_subject_changed_cb),
4182 /* Get initial value of properties */
4183 chat_sms_channel_changed_cb (chat);
4184 chat_self_contact_changed_cb (chat);
4185 chat_remote_contact_changed_cb (chat);
4186 chat_title_changed_cb (chat);
4187 chat_subject_changed_cb (chat);
4189 if (chat->input_text_view) {
4190 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4191 if (priv->block_events_timeout_id == 0) {
4192 empathy_theme_adium_append_event (chat->view, _("Connected"));
4196 g_object_notify (G_OBJECT (chat), "tp-chat");
4197 g_object_notify (G_OBJECT (chat), "id");
4198 g_object_notify (G_OBJECT (chat), "account");
4200 /* This is a noop when tp-chat is set at object construction time and causes
4201 * the pending messages to be show when it's set on the object after it has
4203 show_pending_messages (chat);
4205 /* check if a password is needed */
4206 chat_password_needed_changed_cb (chat);
4210 empathy_chat_get_account (EmpathyChat *chat)
4212 EmpathyChatPriv *priv = GET_PRIV (chat);
4214 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4216 return priv->account;
4220 empathy_chat_get_id (EmpathyChat *chat)
4222 EmpathyChatPriv *priv = GET_PRIV (chat);
4224 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4230 empathy_chat_dup_name (EmpathyChat *chat)
4232 EmpathyChatPriv *priv = GET_PRIV (chat);
4235 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4239 if (!ret && priv->remote_contact) {
4240 ret = empathy_contact_get_alias (priv->remote_contact);
4247 ret = _("Conversation");
4249 if (priv->sms_channel)
4250 /* Translators: this string is a something like
4251 * "Escher Cat (SMS)" */
4252 return g_strdup_printf (_("%s (SMS)"), ret);
4254 return g_strdup (ret);
4258 empathy_chat_get_subject (EmpathyChat *chat)
4260 EmpathyChatPriv *priv = GET_PRIV (chat);
4262 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4264 return priv->subject;
4268 empathy_chat_get_remote_contact (EmpathyChat *chat)
4270 EmpathyChatPriv *priv = GET_PRIV (chat);
4272 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4274 return priv->remote_contact;
4278 empathy_chat_get_contact_menu (EmpathyChat *chat)
4280 EmpathyChatPriv *priv = GET_PRIV (chat);
4281 GtkWidget *menu = NULL;
4282 FolksIndividual *individual;
4285 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4287 if (priv->remote_contact == NULL)
4290 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4291 if (contact == NULL)
4294 individual = empathy_ensure_individual_from_tp_contact (contact);
4296 if (individual == NULL)
4299 menu = empathy_individual_menu_new (individual, NULL,
4300 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4301 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4302 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4303 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4305 g_object_unref (individual);
4311 empathy_chat_clear (EmpathyChat *chat)
4313 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4315 empathy_theme_adium_clear (chat->view);
4319 empathy_chat_scroll_down (EmpathyChat *chat)
4321 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4323 empathy_theme_adium_scroll_down (chat->view);
4327 empathy_chat_cut (EmpathyChat *chat)
4329 GtkTextBuffer *buffer;
4331 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4333 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4334 if (gtk_text_buffer_get_has_selection (buffer)) {
4335 GtkClipboard *clipboard;
4337 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4339 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4344 copy_from_chat_view (EmpathyChat *chat)
4346 if (!empathy_theme_adium_get_has_selection (chat->view))
4349 empathy_theme_adium_copy_clipboard (chat->view);
4354 copy_from_input (EmpathyChat *chat)
4356 GtkTextBuffer *buffer;
4357 GtkClipboard *clipboard;
4359 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4360 if (!gtk_text_buffer_get_has_selection (buffer))
4363 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4364 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4369 copy_from_topic (EmpathyChat *chat)
4371 EmpathyChatPriv *priv = GET_PRIV (chat);
4378 GtkClipboard *clipboard;
4380 if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4385 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4386 start = g_utf8_offset_to_pointer (topic, start_offset);
4387 end = g_utf8_offset_to_pointer (topic, end_offset);
4388 selection = g_strndup (start, end - start);
4390 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4391 gtk_clipboard_set_text (clipboard, selection, -1);
4398 empathy_chat_copy (EmpathyChat *chat)
4400 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4402 if (copy_from_chat_view (chat))
4405 if (copy_from_input (chat))
4408 copy_from_topic (chat);
4412 empathy_chat_paste (EmpathyChat *chat)
4414 GtkTextBuffer *buffer;
4415 GtkClipboard *clipboard;
4416 EmpathyChatPriv *priv;
4418 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4420 priv = GET_PRIV (chat);
4422 if (gtk_widget_get_visible (priv->search_bar)) {
4423 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4427 if (priv->tp_chat == NULL ||
4428 !gtk_widget_is_sensitive (chat->input_text_view))
4431 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4432 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4434 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4438 empathy_chat_find (EmpathyChat *chat)
4440 EmpathyChatPriv *priv;
4442 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4444 priv = GET_PRIV (chat);
4446 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4450 empathy_chat_correct_word (EmpathyChat *chat,
4453 const gchar *new_word)
4455 GtkTextBuffer *buffer;
4457 g_return_if_fail (chat != NULL);
4458 g_return_if_fail (new_word != NULL);
4460 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4462 gtk_text_buffer_delete (buffer, start, end);
4463 gtk_text_buffer_insert (buffer, start,
4469 empathy_chat_is_room (EmpathyChat *chat)
4471 EmpathyChatPriv *priv = GET_PRIV (chat);
4473 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4475 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4479 empathy_chat_is_highlighted (EmpathyChat *chat)
4481 EmpathyChatPriv *priv = GET_PRIV (chat);
4483 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4485 return priv->highlighted;
4489 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4491 EmpathyChatPriv *priv = GET_PRIV (self);
4493 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4495 return priv->unread_messages;
4498 /* called when the messages have been read by user */
4500 empathy_chat_messages_read (EmpathyChat *self)
4502 EmpathyChatPriv *priv = GET_PRIV (self);
4504 g_return_if_fail (EMPATHY_IS_CHAT (self));
4506 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4507 * definition. If we are still retrieving the backlogs, do not ACK */
4508 if (priv->retrieving_backlogs)
4511 if (priv->tp_chat != NULL) {
4512 tp_text_channel_ack_all_pending_messages_async (
4513 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4516 priv->highlighted = FALSE;
4518 if (priv->unread_messages_when_offline > 0) {
4519 /* We can't ack those as the connection has gone away so just consider
4521 priv->unread_messages -= priv->unread_messages_when_offline;
4522 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4523 priv->unread_messages_when_offline = 0;
4527 /* Return TRUE if on of the contacts in this chat is composing */
4529 empathy_chat_is_composing (EmpathyChat *chat)
4531 return chat->priv->compositors != NULL;
4535 empathy_chat_is_sms_channel (EmpathyChat *self)
4537 EmpathyChatPriv *priv = GET_PRIV (self);
4539 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4541 return priv->sms_channel;
4545 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4547 EmpathyChatPriv *priv;
4549 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4551 priv = GET_PRIV (self);
4553 if (priv->tp_chat == NULL) {
4558 g_object_get (priv->tp_chat,
4559 "n-messages-sending", &n_messages,
4567 empathy_chat_dup_text (EmpathyChat *self)
4569 GtkTextBuffer *buffer;
4570 GtkTextIter start, end;
4572 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4574 gtk_text_buffer_get_bounds (buffer, &start, &end);
4575 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4579 empathy_chat_set_text (EmpathyChat *self,
4582 GtkTextBuffer *buffer;
4584 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4586 gtk_text_buffer_set_text (buffer, text, -1);