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>
31 #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
36 #include <gdk/gdkkeysyms.h>
37 #include <glib/gi18n-lib.h>
40 #include <telepathy-glib/telepathy-glib.h>
41 #include <telepathy-logger/telepathy-logger.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-keyring.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-request-util.h>
46 #include <libempathy/empathy-chatroom-manager.h>
47 #include <libempathy/empathy-client-factory.h>
49 #include "empathy-chat.h"
50 #include "empathy-spell.h"
51 #include "empathy-individual-information-dialog.h"
52 #include "empathy-individual-store-channel.h"
53 #include "empathy-individual-view.h"
54 #include "empathy-input-text-view.h"
55 #include "empathy-search-bar.h"
56 #include "empathy-theme-manager.h"
57 #include "empathy-theme-adium.h"
58 #include "empathy-smiley-manager.h"
59 #include "empathy-ui-utils.h"
60 #include "empathy-string-parser.h"
61 #include "extensions/extensions.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
64 #include <libempathy/empathy-debug.h>
66 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
67 #define COMPOSING_STOP_TIMEOUT 5
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
70 struct _EmpathyChatPriv {
71 EmpathyTpChat *tp_chat;
76 EmpathyContact *self_contact;
77 EmpathyContact *remote_contact;
78 gboolean show_contacts;
80 GSettings *gsettings_chat;
81 GSettings *gsettings_ui;
83 TplLogManager *log_manager;
84 TplLogWalker *log_walker;
85 /* Are we watching for scrolling movements? */
86 gboolean watch_scroll;
87 /* Maximum page size of the chat->view. */
89 /* The offset from the lower edge of the chat->view before it
90 * expanded to fit in the newly fetched logs. This is to
91 * restore the chat->view to the page it was on before the
92 * latest batch of logs were inserted. */
95 TpAccountManager *account_manager;
97 GList *input_history_current;
99 GCompletion *completion;
100 guint composing_stop_timeout_id;
101 guint block_events_timeout_id;
102 TpHandleType handle_type;
104 gboolean has_input_vscroll;
106 /* TRUE if spell checking is enabled, FALSE otherwise.
107 * This is to keep track of the last state of spell checking
108 * when it changes. */
109 gboolean spell_checking_enabled;
111 /* These store the signal handler ids for the enclosed text entry. */
112 gulong insert_text_id;
113 gulong delete_range_id;
114 gulong notify_cursor_position_id;
116 /* Source func ID for update_misspelled_words () */
117 guint update_misspelled_words_id;
118 /* Source func ID for save_paned_pos_timeout () */
119 guint save_paned_pos_id;
120 /* Source func ID for chat_contacts_visible_timeout_cb () */
121 guint contacts_visible_id;
125 GtkWidget *vbox_left;
126 GtkWidget *scrolled_window_chat;
127 GtkWidget *scrolled_window_input;
128 GtkWidget *scrolled_window_contacts;
129 GtkWidget *hbox_topic;
130 GtkWidget *expander_topic;
131 GtkWidget *label_topic;
132 GtkWidget *contact_list_view;
133 GtkWidget *info_bar_vbox;
134 GtkWidget *search_bar;
136 guint unread_messages;
137 guint unread_messages_when_offline;
138 /* TRUE if the pending messages can be displayed. This is to avoid to show
139 * pending messages *before* messages from logs. (#603980) */
140 gboolean can_show_pending;
142 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
143 * be differently handled since it introduces another race condition, which
144 * is really hard to occur, but still possible.
146 * With the current workaround (which has the race above), we need to be
147 * sure to ACK any pending messages only when the retrieval of backlogs is
148 * finished, that's why using retrieving_backlogs flag.
149 * empathy_chat_messages_read () will check this variable and not ACK
150 * anything when TRUE. It will be set TRUE at chat_constructed () and set
151 * back to FALSE when the backlog has been retrieved and the pending
152 * messages actually showed to the user.
154 * Race condition introduced with this workaround:
155 * Scenario: a message is pending, the user is notified and selects the tab.
156 * the tab with a pending message is focused before the messages are properly
157 * shown (since the preparation of the window is slower AND async WRT the
158 * tab showing), which means the user won't see any new messages (rare but
159 * possible), if he/she will change tab focus before the messages are
160 * properly shown, the tab will be set as 'seen' and the user won't be
161 * notified again about the already notified pending messages when the
162 * messages in tab will be properly shown */
163 gboolean retrieving_backlogs;
164 gboolean sms_channel;
166 /* we need to know whether populate-popup happened in response to
167 * the keyboard or the mouse. We can't ask GTK for the most recent
168 * event, because it will be a notify event. Instead we track it here */
169 GdkEventType most_recent_event_type;
171 /* A regex matching our own current nickname in the room, or %NULL if
172 * !empathy_chat_is_room (). */
173 GRegex *highlight_regex;
175 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
176 * Cleared by empathy_chat_messages_read (). */
177 gboolean highlighted;
181 gchar *text; /* Original message that was specified
182 * upon entry creation. */
183 gchar *modified_text; /* Message that was modified by user.
184 * When no modifications were made, it is NULL */
190 PART_COMMAND_ENTERED,
204 PROP_N_MESSAGES_SENDING,
205 PROP_NB_UNREAD_MESSAGES,
208 static guint signals[LAST_SIGNAL] = { 0 };
210 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
212 static gboolean chat_scrollable_connect (gpointer user_data);
213 static gboolean update_misspelled_words (gpointer data);
216 chat_get_property (GObject *object,
221 EmpathyChat *chat = EMPATHY_CHAT (object);
222 EmpathyChatPriv *priv = GET_PRIV (object);
226 g_value_set_object (value, priv->tp_chat);
229 g_value_set_object (value, priv->account);
232 g_value_take_string (value, empathy_chat_dup_name (chat));
235 g_value_set_string (value, priv->id);
238 g_value_set_string (value, priv->subject);
240 case PROP_REMOTE_CONTACT:
241 g_value_set_object (value, priv->remote_contact);
243 case PROP_SHOW_CONTACTS:
244 g_value_set_boolean (value, priv->show_contacts);
246 case PROP_SMS_CHANNEL:
247 g_value_set_boolean (value, priv->sms_channel);
249 case PROP_N_MESSAGES_SENDING:
250 g_value_set_uint (value,
251 empathy_chat_get_n_messages_sending (chat));
253 case PROP_NB_UNREAD_MESSAGES:
254 g_value_set_uint (value,
255 empathy_chat_get_nb_unread_messages (chat));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
264 chat_set_property (GObject *object,
269 EmpathyChat *chat = EMPATHY_CHAT (object);
273 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
275 case PROP_SHOW_CONTACTS:
276 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
285 account_reconnected (EmpathyChat *chat,
288 EmpathyChatPriv *priv = GET_PRIV (chat);
290 DEBUG ("Account reconnected, request a new Text channel");
292 /* FIXME: Ideally we should ask to handle ourself the channel so we can
293 * report the error if any but this is blocked by
294 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
295 switch (priv->handle_type) {
296 case TP_HANDLE_TYPE_CONTACT:
297 if (priv->sms_channel)
298 empathy_sms_contact_id (
300 TP_USER_ACTION_TIME_NOT_USER_ACTION,
303 empathy_chat_with_contact_id (
305 TP_USER_ACTION_TIME_NOT_USER_ACTION,
308 case TP_HANDLE_TYPE_ROOM:
309 empathy_join_muc (account, priv->id,
310 TP_USER_ACTION_TIME_NOT_USER_ACTION);
312 case TP_HANDLE_TYPE_NONE:
313 case TP_HANDLE_TYPE_LIST:
314 case TP_HANDLE_TYPE_GROUP:
316 g_assert_not_reached ();
320 g_object_unref (chat);
324 chat_new_connection_cb (TpAccount *account,
328 gchar *dbus_error_name,
332 EmpathyChatPriv *priv = GET_PRIV (chat);
334 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
337 if (priv->tp_chat != NULL || account != priv->account ||
338 priv->handle_type == TP_HANDLE_TYPE_NONE ||
339 EMP_STR_EMPTY (priv->id))
344 account_reconnected (chat, account);
348 chat_composing_remove_timeout (EmpathyChat *chat)
350 EmpathyChatPriv *priv;
352 priv = GET_PRIV (chat);
354 if (priv->composing_stop_timeout_id) {
355 g_source_remove (priv->composing_stop_timeout_id);
356 priv->composing_stop_timeout_id = 0;
361 set_chate_state_cb (GObject *source,
362 GAsyncResult *result,
365 GError *error = NULL;
367 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
369 DEBUG ("Failed to set chat state: %s", error->message);
370 g_error_free (error);
375 set_chat_state (EmpathyChat *self,
376 TpChannelChatState state)
378 EmpathyChatPriv *priv = GET_PRIV (self);
380 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
381 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
384 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
385 set_chate_state_cb, self);
389 chat_composing_stop_timeout_cb (EmpathyChat *chat)
391 EmpathyChatPriv *priv;
392 gboolean send_chat_states;
394 priv = GET_PRIV (chat);
396 priv->composing_stop_timeout_id = 0;
397 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
398 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
399 if (!send_chat_states) {
400 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
402 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
409 chat_composing_start (EmpathyChat *chat)
411 EmpathyChatPriv *priv;
412 gboolean send_chat_states;
414 priv = GET_PRIV (chat);
416 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
417 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
418 if (!send_chat_states) {
422 if (priv->composing_stop_timeout_id) {
423 /* Just restart the timeout */
424 chat_composing_remove_timeout (chat);
426 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
429 priv->composing_stop_timeout_id = g_timeout_add_seconds (
430 COMPOSING_STOP_TIMEOUT,
431 (GSourceFunc) chat_composing_stop_timeout_cb,
436 chat_composing_stop (EmpathyChat *chat)
438 chat_composing_remove_timeout (chat);
439 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
443 chat_input_history_entry_cmp (InputHistoryEntry *entry,
446 if (!tp_strdiff (entry->text, text)) {
447 if (entry->modified_text != NULL) {
448 /* Modified entry and single string cannot be equal. */
456 static InputHistoryEntry *
457 chat_input_history_entry_new_with_text (const gchar *text)
459 InputHistoryEntry *entry;
460 entry = g_slice_new0 (InputHistoryEntry);
461 entry->text = g_strdup (text);
467 chat_input_history_entry_free (InputHistoryEntry *entry)
469 g_free (entry->text);
470 g_free (entry->modified_text);
471 g_slice_free (InputHistoryEntry, entry);
475 chat_input_history_entry_revert (InputHistoryEntry *entry)
477 g_free (entry->modified_text);
478 entry->modified_text = NULL;
482 chat_input_history_entry_update_text (InputHistoryEntry *entry,
487 if (!tp_strdiff (text, entry->text)) {
488 g_free (entry->modified_text);
489 entry->modified_text = NULL;
493 old = entry->modified_text;
494 entry->modified_text = g_strdup (text);
499 chat_input_history_entry_get_text (InputHistoryEntry *entry)
505 if (entry->modified_text != NULL) {
506 return entry->modified_text;
512 chat_input_history_remove_item (GList *list,
515 list = g_list_remove_link (list, item);
516 chat_input_history_entry_free (item->data);
517 g_list_free_1 (item);
522 chat_input_history_revert (EmpathyChat *chat)
524 EmpathyChatPriv *priv;
528 InputHistoryEntry *entry;
530 priv = GET_PRIV (chat);
531 list = priv->input_history;
534 DEBUG ("No input history");
538 /* Delete temporary entry */
539 if (priv->input_history_current != NULL) {
541 list = chat_input_history_remove_item (list, item1);
542 if (priv->input_history_current == item1) {
543 /* Removed temporary entry was current entry */
544 priv->input_history = list;
545 priv->input_history_current = NULL;
550 /* There is no entry to revert */
554 /* Restore the current history entry to original value */
555 item1 = priv->input_history_current;
557 chat_input_history_entry_revert (entry);
559 /* Remove restored entry if there is other occurance before this entry */
560 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
561 (GCompareFunc) chat_input_history_entry_cmp);
562 if (item2 != item1) {
563 list = chat_input_history_remove_item (list, item1);
566 /* Remove other occurance of the restored entry */
567 item2 = g_list_find_custom (item1->next,
568 chat_input_history_entry_get_text (entry),
569 (GCompareFunc) chat_input_history_entry_cmp);
571 list = chat_input_history_remove_item (list, item2);
575 priv->input_history_current = NULL;
576 priv->input_history = list;
580 chat_input_history_add (EmpathyChat *chat,
584 EmpathyChatPriv *priv;
587 InputHistoryEntry *entry;
589 priv = GET_PRIV (chat);
591 list = priv->input_history;
593 /* Remove any other occurances of this entry, if not temporary */
595 while ((item = g_list_find_custom (list, str,
596 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
597 list = chat_input_history_remove_item (list, item);
600 /* Trim the list to the last 10 items */
601 while (g_list_length (list) > 10) {
602 item = g_list_last (list);
604 list = chat_input_history_remove_item (list, item);
612 entry = chat_input_history_entry_new_with_text (str);
613 list = g_list_prepend (list, entry);
615 /* Set the list and the current item pointer */
616 priv->input_history = list;
618 priv->input_history_current = list;
621 priv->input_history_current = NULL;
626 chat_input_history_get_next (EmpathyChat *chat)
628 EmpathyChatPriv *priv;
632 priv = GET_PRIV (chat);
634 if (priv->input_history == NULL) {
635 DEBUG ("No input history, next entry is NULL");
638 g_assert (priv->input_history_current != NULL);
640 if ((item = g_list_next (priv->input_history_current)) == NULL)
642 item = priv->input_history_current;
645 msg = chat_input_history_entry_get_text (item->data);
647 DEBUG ("Returning next entry: '%s'", msg);
649 priv->input_history_current = item;
655 chat_input_history_get_prev (EmpathyChat *chat)
657 EmpathyChatPriv *priv;
661 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
663 priv = GET_PRIV (chat);
665 if (priv->input_history == NULL) {
666 DEBUG ("No input history, previous entry is NULL");
670 if (priv->input_history_current == NULL)
674 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
676 item = priv->input_history_current;
679 msg = chat_input_history_entry_get_text (item->data);
681 DEBUG ("Returning previous entry: '%s'", msg);
683 priv->input_history_current = item;
689 chat_input_history_update (EmpathyChat *chat,
690 GtkTextBuffer *buffer)
692 EmpathyChatPriv *priv;
693 GtkTextIter start, end;
695 InputHistoryEntry *entry;
697 priv = GET_PRIV (chat);
699 gtk_text_buffer_get_bounds (buffer, &start, &end);
700 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
702 if (priv->input_history_current == NULL) {
703 /* Add the current text temporarily to the history */
704 chat_input_history_add (chat, text, TRUE);
709 /* Save the changes in the history */
710 entry = priv->input_history_current->data;
711 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
712 chat_input_history_entry_update_text (entry, text);
721 } ChatCommandMsgData;
724 chat_command_msg_cb (GObject *source,
725 GAsyncResult *result,
728 ChatCommandMsgData *data = user_data;
729 GError *error = NULL;
732 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
733 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
735 if (channel == NULL) {
736 DEBUG ("Failed to get channel: %s", error->message);
737 g_error_free (error);
739 empathy_theme_adium_append_event (data->chat->view,
740 _("Failed to open private chat"));
744 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
745 TpTextChannel *text = (TpTextChannel *) channel;
748 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
751 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
753 g_object_unref (msg);
756 g_object_unref (channel);
759 g_free (data->message);
760 g_slice_free (ChatCommandMsgData, data);
764 nick_command_supported (EmpathyChat *chat)
766 EmpathyChatPriv * priv = GET_PRIV (chat);
767 TpConnection *connection;
769 connection = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
770 return tp_proxy_has_interface_by_id (connection,
771 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
775 part_command_supported (EmpathyChat *chat)
777 EmpathyChatPriv * priv = GET_PRIV (chat);
779 return tp_proxy_has_interface_by_id (priv->tp_chat,
780 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
784 chat_command_clear (EmpathyChat *chat,
787 empathy_theme_adium_clear (chat->view);
791 chat_command_topic (EmpathyChat *chat,
794 EmpathyChatPriv *priv = GET_PRIV (chat);
796 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
797 empathy_theme_adium_append_event (chat->view,
798 _("Topic not supported on this conversation"));
802 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
803 empathy_theme_adium_append_event (chat->view,
804 _("You are not allowed to change the topic"));
808 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
812 empathy_chat_join_muc (EmpathyChat *chat,
815 EmpathyChatPriv *priv = GET_PRIV (chat);
817 empathy_join_muc (priv->account, room,
818 empathy_get_current_action_time ());
822 chat_command_join (EmpathyChat *chat,
827 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
829 /* FIXME: Ideally we should ask to handle ourself the channel so we can
830 * report the error if any but this is blocked by
831 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
832 while (rooms[i] != NULL) {
833 /* ignore empty strings */
834 if (!EMP_STR_EMPTY (rooms[i])) {
835 empathy_chat_join_muc (chat, rooms[i]);
843 chat_command_part (EmpathyChat *chat,
846 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
850 chat_command_msg_internal (EmpathyChat *chat,
851 const gchar *contact_id,
852 const gchar *message)
854 EmpathyChatPriv *priv = GET_PRIV (chat);
855 ChatCommandMsgData *data;
856 TpAccountChannelRequest *req;
859 request = tp_asv_new (
860 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
861 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
862 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
865 req = tp_account_channel_request_new (priv->account, request,
866 empathy_get_current_action_time ());
868 /* FIXME: We should probably search in members alias. But this
869 * is enough for IRC */
870 data = g_slice_new (ChatCommandMsgData);
872 data->message = g_strdup (message);
874 tp_account_channel_request_ensure_and_observe_channel_async (req,
875 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
877 g_object_unref (req);
878 g_hash_table_unref (request);
882 chat_command_query (EmpathyChat *chat,
885 /* If <message> part is not defined,
886 * strv[2] will be the terminal NULL */
887 chat_command_msg_internal (chat, strv[1], strv[2]);
891 chat_command_msg (EmpathyChat *chat,
894 chat_command_msg_internal (chat, strv[1], strv[2]);
898 callback_for_request_rename (TpProxy *proxy,
901 GObject *weak_object)
904 DEBUG ("Call to RequestRename method failed: %s",error->message);
909 chat_command_nick (EmpathyChat *chat,
912 EmpathyChatPriv *priv = GET_PRIV (chat);
915 proxy = TP_PROXY (tp_account_get_connection (priv->account));
917 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
918 strv[1], callback_for_request_rename, NULL, NULL, NULL);
922 chat_command_me (EmpathyChat *chat,
925 EmpathyChatPriv *priv = GET_PRIV (chat);
927 TpTextChannel *channel;
929 channel = (TpTextChannel *) (priv->tp_chat);
931 if (!tp_text_channel_supports_message_type (channel,
932 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
933 /* Action message are not supported, 'simulate' the action */
936 /* The TpChat can't be ready if it doesn't have the self contact */
937 g_assert (priv->self_contact != NULL);
939 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
941 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
946 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
950 empathy_tp_chat_send (priv->tp_chat, message);
951 g_object_unref (message);
955 chat_command_say (EmpathyChat *chat,
958 EmpathyChatPriv *priv = GET_PRIV (chat);
961 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
963 empathy_tp_chat_send (priv->tp_chat, message);
964 g_object_unref (message);
968 whois_got_contact_cb (GObject *source,
969 GAsyncResult *result,
972 EmpathyChat *chat = user_data;
973 EmpathyContact *contact;
974 FolksIndividual *individual;
976 contact = empathy_client_factory_dup_contact_by_id_finish (
977 EMPATHY_CLIENT_FACTORY (source), result, NULL);
979 if (contact == NULL) {
980 empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
984 individual = empathy_ensure_individual_from_tp_contact (
985 empathy_contact_get_tp_contact (contact));
986 empathy_display_individual_info (individual);
988 g_object_unref (individual);
989 g_object_unref (contact);
992 g_object_unref (chat);
996 chat_command_whois (EmpathyChat *chat,
999 EmpathyChatPriv *priv = GET_PRIV (chat);
1001 EmpathyClientFactory *factory;
1003 conn = tp_channel_get_connection ((TpChannel *) priv->tp_chat);
1004 factory = empathy_client_factory_dup ();
1006 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1007 * entered by the user (including spaces, if any). */
1008 empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
1009 whois_got_contact_cb, g_object_ref (chat));
1011 g_object_unref (factory);
1015 chat_command_whale (EmpathyChat *chat,
1018 EmpathyChatPriv *priv = GET_PRIV (chat);
1021 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1023 "•_______________•");
1024 empathy_tp_chat_send (priv->tp_chat, message);
1025 g_object_unref (message);
1029 chat_command_babywhale (EmpathyChat *chat,
1032 EmpathyChatPriv *priv = GET_PRIV (chat);
1035 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1038 empathy_tp_chat_send (priv->tp_chat, message);
1039 g_object_unref (message);
1043 chat_command_inspector (EmpathyChat *chat,
1046 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1047 empathy_theme_adium_show_inspector (
1048 EMPATHY_THEME_ADIUM (chat->view));
1052 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1054 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1057 const gchar *prefix;
1060 ChatCommandFunc func;
1061 gboolean (*is_supported)(EmpathyChat *chat);
1065 static ChatCommandItem commands[] = {
1066 {"clear", 1, 1, chat_command_clear, NULL,
1067 N_("/clear: clear all messages from the current conversation")},
1069 {"topic", 2, 2, chat_command_topic, NULL,
1070 N_("/topic <topic>: set the topic of the current conversation")},
1072 {"join", 2, 2, chat_command_join, NULL,
1073 N_("/join <chat room ID>: join a new chat room")},
1075 {"j", 2, 2, chat_command_join, NULL,
1076 N_("/j <chat room ID>: join a new chat room")},
1078 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1079 {"part", 1, 3, chat_command_part, part_command_supported,
1080 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1081 "by default the current one")},
1083 {"query", 2, 3, chat_command_query, NULL,
1084 N_("/query <contact ID> [<message>]: open a private chat")},
1086 {"msg", 3, 3, chat_command_msg, NULL,
1087 N_("/msg <contact ID> <message>: open a private chat")},
1089 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1090 N_("/nick <nickname>: change your nickname on the current server")},
1092 {"me", 2, 2, chat_command_me, NULL,
1093 N_("/me <message>: send an ACTION message to the current conversation")},
1095 {"say", 2, 2, chat_command_say, NULL,
1096 N_("/say <message>: send <message> to the current conversation. "
1097 "This is used to send a message starting with a '/'. For example: "
1098 "\"/say /join is used to join a new chat room\"")},
1100 {"whois", 2, 2, chat_command_whois, NULL,
1101 N_("/whois <contact ID>: display information about a contact")},
1103 {"help", 1, 2, chat_command_help, NULL,
1104 N_("/help [<command>]: show all supported commands. "
1105 "If <command> is defined, show its usage.")},
1107 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1109 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1110 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1114 chat_command_show_help (EmpathyChat *chat,
1115 ChatCommandItem *item)
1119 if (item->help == NULL) {
1123 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1124 empathy_theme_adium_append_event (chat->view, str);
1129 chat_command_help (EmpathyChat *chat,
1134 /* If <command> part is not defined,
1135 * strv[1] will be the terminal NULL */
1136 if (strv[1] == NULL) {
1137 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1138 if (commands[i].is_supported != NULL) {
1139 if (!commands[i].is_supported (chat)) {
1143 if (commands[i].help == NULL) {
1146 empathy_theme_adium_append_event (chat->view,
1147 _(commands[i].help));
1152 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1153 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1154 if (commands[i].is_supported != NULL) {
1155 if (!commands[i].is_supported (chat)) {
1159 if (commands[i].help == NULL) {
1162 chat_command_show_help (chat, &commands[i]);
1167 empathy_theme_adium_append_event (chat->view,
1168 _("Unknown command"));
1172 chat_command_parse (const gchar *text, guint max_parts)
1177 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1179 array = g_ptr_array_sized_new (max_parts + 1);
1180 while (max_parts > 1) {
1183 /* Skip white spaces */
1184 while (g_ascii_isspace (*text)) {
1188 /* Search the end of this part, until first space. */
1189 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1195 item = g_strndup (text, end - text);
1196 g_ptr_array_add (array, item);
1197 DEBUG ("\tITEM: \"%s\"", item);
1203 /* Append last part if not empty */
1204 item = g_strstrip (g_strdup (text));
1205 if (!EMP_STR_EMPTY (item)) {
1206 g_ptr_array_add (array, item);
1207 DEBUG ("\tITEM: \"%s\"", item);
1212 /* Make the array NULL-terminated */
1213 g_ptr_array_add (array, NULL);
1215 return (GStrv) g_ptr_array_free (array, FALSE);
1219 has_prefix_case (const gchar *s,
1220 const gchar *prefix)
1222 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1226 chat_send (EmpathyChat *chat,
1229 EmpathyChatPriv *priv;
1233 if (EMP_STR_EMPTY (msg)) {
1237 priv = GET_PRIV (chat);
1239 chat_input_history_add (chat, msg, FALSE);
1241 if (msg[0] == '/') {
1242 gboolean second_slash = FALSE;
1243 const gchar *iter = msg + 1;
1245 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1250 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1253 c = *(msg + 1 + strlen (commands[i].prefix));
1254 if (c != '\0' && !g_ascii_isspace (c)) {
1257 if (commands[i].is_supported != NULL) {
1258 if (!commands[i].is_supported (chat)) {
1263 /* We can't use g_strsplit here because it does
1264 * not deal correctly if we have more than one space
1266 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1268 strv_len = g_strv_length (strv);
1269 if (strv_len < commands[i].min_parts ||
1270 strv_len > commands[i].max_parts) {
1271 chat_command_show_help (chat, &commands[i]);
1276 commands[i].func (chat, strv);
1281 /* Also allow messages with two slashes before the
1282 * first space, so it is possible to send a /unix/path.
1283 * This heuristic is kind of crap. */
1284 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1286 second_slash = TRUE;
1292 if (!second_slash) {
1293 empathy_theme_adium_append_event (chat->view,
1294 _("Unknown command; see /help for the available"
1300 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1302 empathy_tp_chat_send (priv->tp_chat, message);
1303 g_object_unref (message);
1307 chat_input_text_view_send (EmpathyChat *chat)
1309 GtkTextBuffer *buffer;
1310 GtkTextIter start, end;
1313 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1315 gtk_text_buffer_get_bounds (buffer, &start, &end);
1316 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1318 /* clear the input field */
1319 gtk_text_buffer_set_text (buffer, "", -1);
1320 /* delete input history modifications */
1321 chat_input_history_revert (chat);
1323 chat_send (chat, msg);
1328 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1329 TpContact *tp_contact,
1330 TpChannelChatState state,
1333 EmpathyChatPriv *priv;
1335 gboolean was_composing;
1336 EmpathyContact *contact;
1338 priv = GET_PRIV (chat);
1340 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1342 if (empathy_contact_is_user (contact)) {
1343 /* We don't care about our own chat state */
1347 was_composing = (priv->compositors != NULL);
1349 /* Find the contact in the list. After that l is the list elem or NULL */
1350 for (l = priv->compositors; l; l = l->next) {
1351 if (contact == l->data) {
1357 case TP_CHANNEL_CHAT_STATE_GONE:
1358 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1359 case TP_CHANNEL_CHAT_STATE_PAUSED:
1360 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1361 /* Contact is not composing */
1363 priv->compositors = g_list_remove_link (priv->compositors, l);
1364 g_object_unref (l->data);
1368 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1369 /* Contact is composing */
1371 priv->compositors = g_list_prepend (priv->compositors,
1372 g_object_ref (contact));
1376 g_assert_not_reached ();
1379 DEBUG ("Was composing: %s now composing: %s",
1380 was_composing ? "yes" : "no",
1381 priv->compositors ? "yes" : "no");
1383 if ((was_composing && !priv->compositors) ||
1384 (!was_composing && priv->compositors)) {
1385 /* Composing state changed */
1386 g_signal_emit (chat, signals[COMPOSING], 0,
1387 priv->compositors != NULL);
1391 g_object_unref (contact);
1395 get_highlight_regex_for (const gchar *name)
1398 gchar *name_esc, *pattern;
1399 GError *error = NULL;
1401 name_esc = g_regex_escape_string (name, -1);
1402 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1403 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1406 if (regex == NULL) {
1407 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1410 g_error_free (error);
1419 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1420 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1423 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1425 EmpathyChatPriv *priv = GET_PRIV (chat);
1427 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1429 if (priv->self_contact != NULL) {
1430 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1432 g_return_if_fail (alias != NULL);
1433 priv->highlight_regex = get_highlight_regex_for (alias);
1438 chat_should_highlight (EmpathyChat *chat,
1439 EmpathyMessage *message)
1441 EmpathyChatPriv *priv = GET_PRIV (chat);
1443 TpChannelTextMessageFlags flags;
1445 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1447 if (!empathy_chat_is_room (chat)) {
1451 if (!empathy_message_is_incoming (message)) {
1455 msg = empathy_message_get_body (message);
1460 flags = empathy_message_get_flags (message);
1461 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1462 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1463 * have already been received by the user before (and so are in the logs) */
1467 if (priv->highlight_regex == NULL) {
1471 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1475 chat_message_received (EmpathyChat *chat,
1476 EmpathyMessage *message,
1479 EmpathyChatPriv *priv = GET_PRIV (chat);
1480 EmpathyContact *sender;
1482 sender = empathy_message_get_sender (message);
1484 if (empathy_message_is_edit (message)) {
1485 DEBUG ("Editing message '%s' to '%s'",
1486 empathy_message_get_supersedes (message),
1487 empathy_message_get_body (message));
1489 empathy_theme_adium_edit_message (chat->view, message);
1491 gboolean should_highlight = chat_should_highlight (chat, message);
1493 if (should_highlight) {
1494 priv->highlighted = TRUE;
1497 DEBUG ("Appending new message '%s' from %s (%d)",
1498 empathy_message_get_token (message),
1499 empathy_contact_get_alias (sender),
1500 empathy_contact_get_handle (sender));
1502 empathy_theme_adium_append_message (chat->view, message, should_highlight);
1504 if (empathy_message_is_incoming (message)) {
1505 priv->unread_messages++;
1506 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1509 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1513 /* We received a message so the contact is no longer
1515 chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
1516 TP_CHANNEL_CHAT_STATE_ACTIVE,
1521 chat_message_received_cb (EmpathyTpChat *tp_chat,
1522 EmpathyMessage *message,
1525 chat_message_received (chat, message, FALSE);
1529 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1530 EmpathyMessage *message,
1533 EmpathyChatPriv *priv = GET_PRIV (chat);
1535 empathy_theme_adium_message_acknowledged (chat->view,
1538 if (!empathy_message_is_edit (message)) {
1539 priv->unread_messages--;
1540 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1545 append_balance_error (EmpathyChat *chat,
1546 const gchar *message_body)
1548 EmpathyChatPriv *priv = GET_PRIV (chat);
1549 TpConnection *conn = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
1550 const gchar *uri = tp_connection_get_balance_uri (conn);
1551 const gchar *error = _("insufficient balance to send message");
1552 gchar *str, *str_markup = NULL;
1554 if (message_body != NULL) {
1555 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1557 str = g_strdup_printf (_("Error sending message: %s"), error);
1560 if (!tp_str_empty (uri)) {
1561 /* translators: error used when user doesn't have enough credit on his
1562 * account to send the message. */
1563 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1564 " <a href='%s'>Top up</a>."), uri);
1566 if (message_body != NULL) {
1567 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1569 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1570 escaped_body, markup_error);
1572 g_free (escaped_body);
1574 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1577 g_free (markup_error);
1580 if (str_markup != NULL)
1581 empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
1583 empathy_theme_adium_append_event (chat->view, str);
1586 g_free (str_markup);
1590 chat_send_error_cb (EmpathyTpChat *tp_chat,
1591 const gchar *message_body,
1592 TpChannelTextSendError error_code,
1593 const gchar *dbus_error,
1596 const gchar *error = NULL;
1599 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1600 append_balance_error (chat, message_body);
1602 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1603 error = _("not capable");
1606 if (error == NULL) {
1607 /* if we didn't find a dbus-error, try the old error */
1608 switch (error_code) {
1609 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1610 error = _("offline");
1612 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1613 error = _("invalid contact");
1615 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1616 error = _("permission denied");
1618 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1619 error = _("too long message");
1621 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1622 error = _("not implemented");
1624 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1626 error = _("unknown");
1631 if (message_body != NULL) {
1632 str = g_strdup_printf (_("Error sending message '%s': %s"),
1633 message_body, error);
1636 str = g_strdup_printf (_("Error sending message: %s"), error);
1639 empathy_theme_adium_append_event (chat->view, str);
1644 chat_topic_label_size_allocate_cb (GtkLabel *label,
1645 GtkAllocation *allocation,
1648 EmpathyChatPriv *priv = GET_PRIV (chat);
1650 if (!gtk_label_get_line_wrap (label)) {
1651 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1652 gtk_widget_show (priv->expander_topic);
1654 gtk_widget_hide (priv->expander_topic);
1661 chat_topic_expander_activate_cb (GtkExpander *expander,
1662 GParamSpec *param_spec,
1665 EmpathyChatPriv *priv = GET_PRIV (chat);
1667 if (gtk_expander_get_expanded (expander)) {
1668 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1669 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1671 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1672 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1677 chat_subject_changed_cb (EmpathyChat *chat)
1679 EmpathyChatPriv *priv = GET_PRIV (chat);
1681 g_free (priv->subject);
1682 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1683 g_object_notify (G_OBJECT (chat), "subject");
1685 if (EMP_STR_EMPTY (priv->subject)) {
1686 gtk_widget_hide (priv->hbox_topic);
1688 gchar *markup_topic;
1691 markup_topic = empathy_add_link_markup (priv->subject);
1692 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1693 _("Topic:"), markup_topic);
1695 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1696 g_free (markup_text);
1697 g_free (markup_topic);
1699 gtk_widget_show (priv->hbox_topic);
1701 if (priv->block_events_timeout_id == 0) {
1704 if (!EMP_STR_EMPTY (priv->subject)) {
1705 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1707 if (tp_str_empty (actor)) {
1708 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1710 str = g_strdup_printf (_("Topic set by %s to: %s"),
1711 actor, priv->subject);
1713 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1714 /* No need to display this 'event' is no topic can be defined anyway */
1715 str = g_strdup (_("No topic defined"));
1719 empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
1726 chat_title_changed_cb (EmpathyChat *chat)
1728 EmpathyChatPriv *priv = GET_PRIV (chat);
1730 g_free (priv->name);
1731 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1732 g_object_notify (G_OBJECT (chat), "name");
1736 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1740 GtkTextIter word_start = *iter;
1741 GtkTextIter word_end = *iter;
1744 if (gtk_text_iter_inside_word (&word_end) &&
1745 !gtk_text_iter_ends_word (&word_end)) {
1746 gtk_text_iter_forward_word_end (&word_end);
1751 if (gtk_text_iter_get_char (&tmp) == '\'') {
1752 gtk_text_iter_forward_char (&tmp);
1754 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1755 gtk_text_iter_forward_word_end (&word_end);
1760 if (gtk_text_iter_inside_word (&word_start) ||
1761 gtk_text_iter_ends_word (&word_start)) {
1762 if (!gtk_text_iter_starts_word (&word_start) ||
1763 gtk_text_iter_equal (&word_start, &word_end)) {
1764 gtk_text_iter_backward_word_start (&word_start);
1768 gtk_text_iter_backward_char (&tmp);
1770 if (gtk_text_iter_get_char (&tmp) == '\'') {
1771 gtk_text_iter_backward_char (&tmp);
1773 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1774 gtk_text_iter_backward_word_start (&word_start);
1779 *start = word_start;
1785 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1786 GtkTextIter *location,
1791 GtkTextIter iter, pos;
1793 /* Remove all misspelled tags in the inserted text.
1794 * This happens when text is inserted within a misspelled word. */
1795 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1796 gtk_text_iter_get_offset (location) - len);
1797 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1800 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1803 GtkTextIter start, end;
1806 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1809 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1811 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1812 gtk_text_iter_equal (&pos, &end) ||
1813 empathy_spell_check (str)) {
1814 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1816 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1821 } while (gtk_text_iter_forward_word_end (&iter) &&
1822 gtk_text_iter_compare (&iter, location) <= 0);
1826 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1831 GtkTextIter word_start, word_end;
1833 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1834 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1835 &word_start, &word_end);
1840 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1843 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1844 chat_composing_stop (chat);
1846 chat_composing_start (chat);
1851 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1856 GtkTextIter prev_pos;
1857 GtkTextIter word_start;
1858 GtkTextIter word_end;
1862 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1864 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1865 gtk_text_buffer_get_insert (buffer));
1866 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1868 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1871 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1872 !gtk_text_iter_equal (&pos, &word_end)) {
1873 str = gtk_text_buffer_get_text (buffer,
1874 &word_start, &word_end, FALSE);
1876 if (!empathy_spell_check (str)) {
1877 gtk_text_buffer_apply_tag_by_name (buffer,
1878 "misspelled", &word_start, &word_end);
1880 gtk_text_buffer_remove_tag_by_name (buffer,
1881 "misspelled", &word_start, &word_end);
1888 gtk_text_buffer_move_mark (buffer, mark, &pos);
1892 empathy_isspace_cb (gunichar c,
1895 return g_unichar_isspace (c);
1899 chat_input_key_press_event_cb (GtkWidget *widget,
1903 EmpathyChatPriv *priv;
1906 GtkWidget *text_view_sw;
1908 priv = GET_PRIV (chat);
1910 priv->most_recent_event_type = event->type;
1912 /* Catch ctrl+up/down so we can traverse messages we sent */
1913 if ((event->state & GDK_CONTROL_MASK) &&
1914 (event->keyval == GDK_KEY_Up ||
1915 event->keyval == GDK_KEY_Down)) {
1916 GtkTextBuffer *buffer;
1919 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1920 chat_input_history_update (chat, buffer);
1922 if (event->keyval == GDK_KEY_Up) {
1923 str = chat_input_history_get_next (chat);
1925 str = chat_input_history_get_prev (chat);
1928 g_signal_handlers_block_by_func (buffer,
1929 chat_input_text_buffer_changed_cb,
1931 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1932 g_signal_handlers_unblock_by_func (buffer,
1933 chat_input_text_buffer_changed_cb,
1939 /* Catch enter but not ctrl/shift-enter */
1940 if (IS_ENTER (event->keyval) &&
1941 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1944 /* This is to make sure that kinput2 gets the enter. And if
1945 * it's handled there we shouldn't send on it. This is because
1946 * kinput2 uses Enter to commit letters. See:
1947 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1950 view = GTK_TEXT_VIEW (chat->input_text_view);
1951 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1952 gtk_text_view_reset_im_context (view);
1956 chat_input_text_view_send (chat);
1960 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1962 if (IS_ENTER (event->keyval) &&
1963 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1964 /* Newline for shift/control-enter. */
1967 if (!(event->state & GDK_CONTROL_MASK) &&
1968 event->keyval == GDK_KEY_Page_Up) {
1969 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1970 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1973 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1974 event->keyval == GDK_KEY_Page_Down) {
1975 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1976 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1977 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1978 gtk_adjustment_set_value (adj, val);
1981 if (event->keyval == GDK_KEY_Escape) {
1982 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1984 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1985 event->keyval == GDK_KEY_Tab) {
1986 GtkTextBuffer *buffer;
1987 GtkTextIter start, current;
1988 gchar *nick, *completed;
1989 GList *list, *completed_list;
1990 gboolean is_start_of_buffer;
1992 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1993 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1995 /* Get the start of the nick to complete. */
1996 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1997 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1998 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
2000 is_start_of_buffer = gtk_text_iter_is_start (&start);
2002 list = empathy_tp_chat_get_members (priv->tp_chat);
2003 g_completion_add_items (priv->completion, list);
2005 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
2006 completed_list = g_completion_complete (priv->completion,
2015 GString *message = NULL;
2018 gtk_text_buffer_delete (buffer, &start, ¤t);
2020 len = g_list_length (completed_list);
2023 /* If we only have one hit, use that text
2024 * instead of the text in completed since the
2025 * completed text will use the typed string
2026 * which might be cased all wrong.
2029 text = empathy_contact_get_alias (completed_list->data);
2033 /* Print all hits to the scrollback view, so the
2034 * user knows what possibilities he has.
2037 message = g_string_new ("");
2038 for (l = completed_list; l != NULL; l = l->next) {
2039 g_string_append (message, empathy_contact_get_alias (l->data));
2040 g_string_append (message, " - ");
2042 empathy_theme_adium_append_event (chat->view, message->str);
2043 g_string_free (message, TRUE);
2046 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2048 if (len == 1 && is_start_of_buffer) {
2049 gchar *complete_char;
2051 complete_char = g_settings_get_string (
2052 priv->gsettings_chat,
2053 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2055 if (complete_char != NULL) {
2056 gtk_text_buffer_insert_at_cursor (buffer,
2058 strlen (complete_char));
2059 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2060 g_free (complete_char);
2067 g_completion_clear_items (priv->completion);
2069 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2079 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2083 gtk_widget_grab_focus (chat->input_text_view);
2089 chat_input_realize_cb (GtkWidget *widget,
2092 DEBUG ("Setting focus to the input text view");
2093 if (gtk_widget_is_sensitive (widget)) {
2094 gtk_widget_grab_focus (widget);
2099 chat_input_has_focus_notify_cb (GtkWidget *widget,
2103 empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2107 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2108 EmpathySmiley *smiley,
2111 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2112 GtkTextBuffer *buffer;
2115 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2117 gtk_text_buffer_get_end_iter (buffer, &iter);
2118 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2120 gtk_text_buffer_get_end_iter (buffer, &iter);
2121 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2132 static EmpathyChatSpell *
2133 chat_spell_new (EmpathyChat *chat,
2138 EmpathyChatSpell *chat_spell;
2140 chat_spell = g_slice_new0 (EmpathyChatSpell);
2142 chat_spell->chat = g_object_ref (chat);
2143 chat_spell->word = g_strdup (word);
2144 chat_spell->start = start;
2145 chat_spell->end = end;
2151 chat_spell_free (EmpathyChatSpell *chat_spell)
2153 g_object_unref (chat_spell->chat);
2154 g_free (chat_spell->word);
2155 g_slice_free (EmpathyChatSpell, chat_spell);
2159 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2160 EmpathyChatSpell *chat_spell)
2162 empathy_chat_correct_word (chat_spell->chat,
2163 &(chat_spell->start),
2165 gtk_menu_item_get_label (menu_item));
2170 chat_spelling_build_suggestions_menu (const gchar *code,
2171 EmpathyChatSpell *chat_spell)
2173 GList *suggestions, *l;
2174 GtkWidget *menu, *menu_item;
2176 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2177 if (suggestions == NULL)
2180 menu = gtk_menu_new ();
2181 for (l = suggestions; l; l = l->next) {
2182 menu_item = gtk_menu_item_new_with_label (l->data);
2183 g_signal_connect (G_OBJECT (menu_item), "activate",
2184 G_CALLBACK (chat_spelling_menu_activate_cb),
2186 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2188 empathy_spell_free_suggestions (suggestions);
2190 gtk_widget_show_all (menu);
2196 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2198 GtkWidget *menu, *submenu, *item;
2201 codes = empathy_spell_get_enabled_language_codes ();
2202 g_assert (codes != NULL);
2204 if (g_list_length (codes) > 1) {
2205 menu = gtk_menu_new ();
2207 for (l = codes; l; l = l->next) {
2208 const gchar *code, *name;
2211 name = empathy_spell_get_language_name (code);
2215 item = gtk_image_menu_item_new_with_label (name);
2217 submenu = chat_spelling_build_suggestions_menu (
2219 if (submenu == NULL)
2220 gtk_widget_set_sensitive (item, FALSE);
2222 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2224 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2227 menu = chat_spelling_build_suggestions_menu (codes->data,
2230 menu = gtk_menu_new ();
2231 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2232 gtk_widget_set_sensitive (item, FALSE);
2233 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2236 g_list_free (codes);
2238 gtk_widget_show_all (menu);
2249 static EmpathyChatWord *
2250 chat_word_new (EmpathyChat *chat,
2254 EmpathyChatWord *chat_word;
2256 chat_word = g_slice_new0 (EmpathyChatWord);
2258 chat_word->chat = g_object_ref (chat);
2259 chat_word->word = g_strdup (word);
2260 chat_word->code = g_strdup (code);
2266 chat_word_free (EmpathyChatWord *chat_word)
2268 g_object_unref (chat_word->chat);
2269 g_free (chat_word->word);
2270 g_free (chat_word->code);
2271 g_slice_free (EmpathyChatWord, chat_word);
2275 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2276 EmpathyChatWord *chat_word)
2278 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2280 empathy_spell_add_to_dictionary (chat_word->code,
2282 priv->update_misspelled_words_id =
2283 g_idle_add (update_misspelled_words, chat_word->chat);
2287 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2289 GtkWidget *menu, *item, *lang_item, *image;
2292 const gchar *code, *name;
2293 EmpathyChatWord *chat_word;
2295 codes = empathy_spell_get_enabled_language_codes ();
2296 g_assert (codes != NULL);
2297 if (g_list_length (codes) > 1) {
2298 /* translators: %s is the selected word */
2299 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2301 item = gtk_image_menu_item_new_with_mnemonic (label);
2303 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2304 GTK_ICON_SIZE_MENU);
2305 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2308 menu = gtk_menu_new ();
2310 for (l = codes; l; l = l->next) {
2312 name = empathy_spell_get_language_name (code);
2316 lang_item = gtk_image_menu_item_new_with_label (name);
2318 chat_word= chat_word_new (chat_spell->chat,
2319 chat_spell->word, code);
2320 g_object_set_data_full (G_OBJECT (lang_item),
2321 "chat-word", chat_word,
2322 (GDestroyNotify) chat_word_free);
2324 g_signal_connect (G_OBJECT (lang_item), "activate",
2325 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2327 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2329 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2332 name = empathy_spell_get_language_name (code);
2333 g_assert (name != NULL);
2334 /* translators: first %s is the selected word,
2335 * second %s is the language name of the target dictionary */
2336 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2337 chat_spell->word, name);
2338 item = gtk_image_menu_item_new_with_mnemonic (label);
2340 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2341 GTK_ICON_SIZE_MENU);
2342 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2344 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2346 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2347 (GDestroyNotify) chat_word_free);
2349 g_signal_connect (G_OBJECT (item), "activate",
2350 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2353 g_list_free (codes);
2355 gtk_widget_show_all (item);
2361 chat_text_send_cb (GtkMenuItem *menuitem,
2364 chat_input_text_view_send (chat);
2368 chat_input_button_press_event_cb (GtkTextView *view,
2369 GdkEventButton *event,
2372 EmpathyChatPriv *priv = GET_PRIV (chat);
2374 priv->most_recent_event_type = event->type;
2380 chat_input_populate_popup_cb (GtkTextView *view,
2384 EmpathyChatPriv *priv = GET_PRIV (chat);
2385 GtkTextBuffer *buffer;
2386 GtkTextTagTable *table;
2389 GtkTextIter iter, start, end;
2392 EmpathyChatSpell *chat_spell;
2393 GtkWidget *spell_menu;
2394 GtkWidget *spell_item;
2395 EmpathySmileyManager *smiley_manager;
2396 GtkWidget *smiley_menu;
2399 buffer = gtk_text_view_get_buffer (view);
2401 /* Add the emoticon menu. */
2402 item = gtk_separator_menu_item_new ();
2403 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2404 gtk_widget_show (item);
2406 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2407 image = gtk_image_new_from_icon_name ("face-smile",
2408 GTK_ICON_SIZE_MENU);
2409 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2410 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2411 gtk_widget_show (item);
2413 smiley_manager = empathy_smiley_manager_dup_singleton ();
2414 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2415 chat_insert_smiley_activate_cb,
2417 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2418 g_object_unref (smiley_manager);
2420 /* Add the Send menu item. */
2421 gtk_text_buffer_get_bounds (buffer, &start, &end);
2422 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2423 if (!EMP_STR_EMPTY (str)) {
2424 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2425 g_signal_connect (G_OBJECT (item), "activate",
2426 G_CALLBACK (chat_text_send_cb), chat);
2427 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2428 gtk_widget_show (item);
2432 /* Add the spell check menu item. */
2433 table = gtk_text_buffer_get_tag_table (buffer);
2434 tag = gtk_text_tag_table_lookup (table, "misspelled");
2436 switch (priv->most_recent_event_type) {
2437 case GDK_BUTTON_PRESS:
2438 /* get the location from the pointer */
2439 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2440 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2441 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2443 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2444 GTK_TEXT_WINDOW_WIDGET,
2447 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2452 g_warn_if_reached ();
2453 /* assume the KEY_PRESS case */
2456 /* get the location from the cursor */
2457 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2458 gtk_text_buffer_get_insert (buffer));
2464 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2465 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2467 str = gtk_text_buffer_get_text (buffer,
2468 &start, &end, FALSE);
2470 if (!EMP_STR_EMPTY (str)) {
2471 chat_spell = chat_spell_new (chat, str, start, end);
2472 g_object_set_data_full (G_OBJECT (menu),
2473 "chat-spell", chat_spell,
2474 (GDestroyNotify) chat_spell_free);
2476 item = gtk_separator_menu_item_new ();
2477 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2478 gtk_widget_show (item);
2480 /* Spelling suggestions */
2481 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2482 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2483 GTK_ICON_SIZE_MENU);
2484 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2486 spell_menu = chat_spelling_build_menu (chat_spell);
2487 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2490 spell_item = gtk_separator_menu_item_new ();
2491 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2492 gtk_widget_show (spell_item);
2494 /* Add to dictionary */
2495 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2497 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2498 gtk_widget_show (spell_item);
2500 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2501 gtk_widget_show (item);
2507 chat_log_filter (TplEvent *event,
2510 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2511 EmpathyChatPriv *priv = GET_PRIV (chat);
2512 EmpathyMessage *message;
2513 const GList *pending;
2514 bool retval = FALSE;
2516 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2517 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2519 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2520 message = empathy_message_from_tpl_log_event (event);
2522 for (; pending; pending = g_list_next (pending)) {
2523 if (empathy_message_equal (message, pending->data))
2530 g_object_unref (message);
2536 show_pending_messages (EmpathyChat *chat) {
2537 EmpathyChatPriv *priv = GET_PRIV (chat);
2538 const GList *messages, *l;
2540 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2542 if (chat->view == NULL || priv->tp_chat == NULL)
2545 if (!priv->can_show_pending)
2548 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2550 for (l = messages; l != NULL ; l = g_list_next (l)) {
2551 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2552 chat_message_received (chat, message, TRUE);
2558 chat_scrollable_set_value (gpointer user_data)
2560 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2561 EmpathyChatPriv *priv = GET_PRIV (chat);
2562 GtkAdjustment *adjustment;
2565 adjustment = gtk_scrollable_get_vadjustment (
2566 GTK_SCROLLABLE (chat->view));
2568 /* Set the chat->view's adjustment back to the value it had
2569 * before it grew as a result of new logs being inserted.
2571 upper = (guint) gtk_adjustment_get_upper (adjustment);
2572 gtk_adjustment_set_value (adjustment, upper - priv->scroll_offset);
2574 return G_SOURCE_REMOVE;
2578 got_filtered_messages_cb (GObject *walker,
2579 GAsyncResult *result,
2584 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2585 EmpathyChatPriv *priv = GET_PRIV (chat);
2586 GError *error = NULL;
2588 if (!tpl_log_walker_get_events_finish (TPL_LOG_WALKER (walker),
2589 result, &messages, &error)) {
2590 DEBUG ("%s. Aborting.", error->message);
2591 empathy_theme_adium_append_event (chat->view,
2592 _("Failed to retrieve recent logs"));
2593 g_error_free (error);
2597 for (l = g_list_last (messages); l; l = g_list_previous (l)) {
2598 EmpathyMessage *message;
2600 g_assert (TPL_IS_EVENT (l->data));
2602 message = empathy_message_from_tpl_log_event (l->data);
2603 g_object_unref (l->data);
2605 if (empathy_message_is_edit (message)) {
2606 /* this is an edited message, create a synthetic event
2607 * using the supersedes token and
2608 * original-message-sent timestamp, that we can then
2610 EmpathyMessage *syn_msg = g_object_new (
2611 EMPATHY_TYPE_MESSAGE,
2613 "token", empathy_message_get_supersedes (message),
2614 "type", empathy_message_get_tptype (message),
2615 "timestamp", empathy_message_get_original_timestamp (message),
2616 "incoming", empathy_message_is_incoming (message),
2618 "receiver", empathy_message_get_receiver (message),
2619 "sender", empathy_message_get_sender (message),
2622 empathy_theme_adium_prepend_message (chat->view, syn_msg,
2623 chat_should_highlight (chat, syn_msg));
2624 empathy_theme_adium_edit_message (chat->view, message);
2626 g_object_unref (syn_msg);
2628 /* append the latest message */
2629 empathy_theme_adium_prepend_message (chat->view, message,
2630 chat_should_highlight (chat, message));
2633 g_object_unref (message);
2635 g_list_free (messages);
2638 /* in case of TPL error, skip backlog and show pending messages */
2639 priv->can_show_pending = TRUE;
2640 show_pending_messages (chat);
2642 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2643 * about it in EmpathyChatPriv definition */
2644 priv->retrieving_backlogs = FALSE;
2645 empathy_chat_messages_read (chat);
2647 /* Turn back on scrolling */
2648 empathy_theme_adium_scroll (chat->view, TRUE);
2650 /* We start watching the scrolling movements only after the first
2651 * batch of logs have been fetched. Otherwise, if the
2652 * chat->view's page size is too small the scrollbar might hit
2653 * the upper edge and trigger another batch of logs to be
2656 if (G_UNLIKELY (!priv->watch_scroll &&
2657 !tpl_log_walker_is_end (priv->log_walker))) {
2658 priv->watch_scroll = TRUE;
2659 g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_connect,
2660 g_object_ref (chat), g_object_unref);
2663 GtkAdjustment *adjustment;
2667 /* The chat->view's adjustment won't change unless we
2668 * return to the main loop. Save the current offset
2669 * from the lower edge (or the upper value of the
2670 * adjustment) so that we can restore it later once the
2673 adjustment = gtk_scrollable_get_vadjustment (
2674 GTK_SCROLLABLE (chat->view));
2675 upper = (guint) gtk_adjustment_get_upper (adjustment);
2676 value = (guint) gtk_adjustment_get_value (adjustment);
2677 priv->scroll_offset = upper - value;
2679 g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_set_value,
2680 g_object_ref (chat), g_object_unref);
2683 g_object_unref (chat);
2687 chat_add_logs (EmpathyChat *chat)
2689 EmpathyChatPriv *priv = GET_PRIV (chat);
2692 return G_SOURCE_REMOVE;
2695 /* Turn off scrolling temporarily */
2696 empathy_theme_adium_scroll (chat->view, FALSE);
2698 tpl_log_walker_get_events_async (priv->log_walker, 5,
2699 got_filtered_messages_cb, g_object_ref (chat));
2701 return G_SOURCE_REMOVE;
2705 chat_schedule_logs (EmpathyChat *chat)
2707 EmpathyChatPriv *priv = GET_PRIV (chat);
2709 if (priv->retrieving_backlogs)
2712 priv->retrieving_backlogs = TRUE;
2713 g_timeout_add_full (G_PRIORITY_LOW, 500, /* ms */
2714 (GSourceFunc) chat_add_logs, g_object_ref (chat), g_object_unref);
2718 chat_view_adjustment_changed_cb (GtkAdjustment *adjustment,
2721 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2722 EmpathyChatPriv *priv = GET_PRIV (chat);
2725 if (tpl_log_walker_is_end (priv->log_walker)) {
2726 g_signal_handlers_disconnect_by_func (adjustment,
2727 chat_view_adjustment_changed_cb, user_data);
2731 page_size = (guint) gtk_adjustment_get_page_size (adjustment);
2732 if (page_size <= priv->max_page_size)
2735 /* We need to fetch more logs if the page size of the view
2736 * increases, so that there is no empty space at the top.
2738 if (G_LIKELY (priv->max_page_size != 0))
2739 chat_schedule_logs (chat);
2741 priv->max_page_size = page_size;
2745 chat_view_adjustment_value_changed_cb (GtkAdjustment *adjustment,
2748 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2749 EmpathyChatPriv *priv = GET_PRIV (chat);
2753 if (tpl_log_walker_is_end (priv->log_walker)) {
2754 g_signal_handlers_disconnect_by_func (adjustment,
2755 chat_view_adjustment_value_changed_cb, user_data);
2759 lower = (guint) gtk_adjustment_get_lower (adjustment);
2760 value = (guint) gtk_adjustment_get_value (adjustment);
2764 /* Request for more logs to be fetched if the user hit the
2765 * upper edge of the chat->view.
2767 chat_schedule_logs (chat);
2771 chat_scrollable_connect (gpointer user_data)
2773 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2774 GtkAdjustment *adjustment;
2776 adjustment = gtk_scrollable_get_vadjustment (
2777 GTK_SCROLLABLE (chat->view));
2779 g_signal_connect (adjustment, "changed",
2780 G_CALLBACK (chat_view_adjustment_changed_cb), chat);
2781 g_signal_connect (adjustment, "value-changed",
2782 G_CALLBACK (chat_view_adjustment_value_changed_cb), chat);
2784 return G_SOURCE_REMOVE;
2788 chat_contacts_completion_func (const gchar *s1,
2792 gchar *tmp, *nick1, *nick2;
2799 return s1 ? -1 : +1;
2802 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2803 nick1 = g_utf8_casefold (tmp, -1);
2806 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2807 nick2 = g_utf8_casefold (tmp, -1);
2810 ret = strncmp (nick1, nick2, n);
2819 build_part_message (guint reason,
2821 EmpathyContact *actor,
2822 const gchar *message)
2824 GString *s = g_string_new ("");
2825 const gchar *actor_name = NULL;
2827 if (actor != NULL) {
2828 actor_name = empathy_contact_get_alias (actor);
2831 /* Having an actor only really makes sense for a few actions... */
2833 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2834 g_string_append_printf (s, _("%s has disconnected"), name);
2836 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2837 if (actor_name != NULL) {
2838 /* translators: reverse the order of these arguments
2839 * if the kicked should come before the kicker in your locale.
2841 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2844 g_string_append_printf (s, _("%s was kicked"), name);
2847 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2848 if (actor_name != NULL) {
2849 /* translators: reverse the order of these arguments
2850 * if the banned should come before the banner in your locale.
2852 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2855 g_string_append_printf (s, _("%s was banned"), name);
2859 g_string_append_printf (s, _("%s has left the room"), name);
2862 if (!EMP_STR_EMPTY (message)) {
2863 /* Note to translators: this string is appended to
2864 * notifications like "foo has left the room", with the message
2865 * given by the user living the room. If this poses a problem,
2866 * please let us know. :-)
2868 g_string_append_printf (s, _(" (%s)"), message);
2871 return g_string_free (s, FALSE);
2875 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2876 EmpathyContact *contact,
2877 EmpathyContact *actor,
2883 EmpathyChatPriv *priv = GET_PRIV (chat);
2884 const gchar *name = empathy_contact_get_alias (contact);
2887 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2889 if (priv->block_events_timeout_id != 0)
2893 str = g_strdup_printf (_("%s has joined the room"),
2896 str = build_part_message (reason, name, actor, message);
2899 empathy_theme_adium_append_event (chat->view, str);
2904 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2905 EmpathyContact *old_contact,
2906 EmpathyContact *new_contact,
2911 EmpathyChatPriv *priv = GET_PRIV (chat);
2913 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2915 if (priv->block_events_timeout_id == 0) {
2918 str = g_strdup_printf (_("%s is now known as %s"),
2919 empathy_contact_get_alias (old_contact),
2920 empathy_contact_get_alias (new_contact));
2921 empathy_theme_adium_append_event (chat->view, str);
2928 chat_contacts_visible_timeout_cb (gpointer chat)
2930 EmpathyChatPriv *priv = GET_PRIV (chat);
2932 /* Relax the size request */
2933 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2935 /* Set the position of the slider. This must be done here because
2936 * GtkPaned need to know its size allocation and it will be settled only
2937 * after the gtk_window_resize () tough effect. */
2938 if (priv->contacts_width > 0) {
2939 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2940 priv->contacts_width);
2943 priv->contacts_visible_id = 0;
2949 chat_update_contacts_visibility (EmpathyChat *chat,
2952 EmpathyChatPriv *priv = GET_PRIV (chat);
2954 if (!priv->scrolled_window_contacts) {
2958 if (priv->remote_contact != NULL) {
2962 if (show && priv->contact_list_view == NULL) {
2963 EmpathyIndividualStore *store;
2965 GtkAllocation allocation;
2967 /* We are adding the contact list to the chat, we don't want the
2968 * chat view to become too small. If the chat view is already
2969 * smaller than 250 make sure that size won't change. If the
2970 * chat view is bigger the contact list will take some space on
2971 * it but we make sure the chat view don't become smaller than
2972 * 250. Relax the size request once the resize is done */
2973 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2974 min_width = MIN (allocation.width, 250);
2975 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2977 /* There is no way to know when the window resize will happen
2978 * since it is WM's decision. Let's hope it won't be longer. */
2979 if (priv->contacts_visible_id != 0)
2980 g_source_remove (priv->contacts_visible_id);
2981 priv->contacts_visible_id = g_timeout_add (500,
2982 chat_contacts_visible_timeout_cb, chat);
2984 store = EMPATHY_INDIVIDUAL_STORE (
2985 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2987 empathy_individual_store_set_show_groups (store, FALSE);
2989 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2990 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2991 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2992 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2993 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2994 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2995 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2997 empathy_individual_view_set_show_offline (
2998 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2999 empathy_individual_view_set_show_uninteresting (
3000 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
3002 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
3003 priv->contact_list_view);
3005 gtk_widget_show (priv->contact_list_view);
3006 gtk_widget_show (priv->scrolled_window_contacts);
3007 g_object_unref (store);
3009 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
3010 gtk_widget_hide (priv->scrolled_window_contacts);
3011 if (priv->contact_list_view != NULL) {
3012 gtk_widget_destroy (priv->contact_list_view);
3013 priv->contact_list_view = NULL;
3019 empathy_chat_set_show_contacts (EmpathyChat *chat,
3022 EmpathyChatPriv *priv = GET_PRIV (chat);
3024 priv->show_contacts = show;
3026 chat_update_contacts_visibility (chat, show);
3028 g_object_notify (G_OBJECT (chat), "show-contacts");
3032 chat_self_contact_changed_cb (EmpathyChat *chat)
3034 EmpathyChatPriv *priv = GET_PRIV (chat);
3036 if (priv->self_contact != NULL) {
3037 g_signal_handlers_disconnect_by_func (priv->self_contact,
3038 chat_self_contact_alias_changed_cb,
3041 g_clear_object (&priv->self_contact);
3043 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
3044 if (priv->self_contact != NULL) {
3045 g_object_ref (priv->self_contact);
3047 if (empathy_chat_is_room (chat)) {
3048 g_signal_connect_swapped (priv->self_contact, "notify::alias",
3049 G_CALLBACK (chat_self_contact_alias_changed_cb),
3054 chat_self_contact_alias_changed_cb (chat);
3058 chat_remote_contact_changed_cb (EmpathyChat *chat)
3060 EmpathyChatPriv *priv = GET_PRIV (chat);
3062 if (priv->remote_contact != NULL) {
3063 g_object_unref (priv->remote_contact);
3064 priv->remote_contact = NULL;
3069 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
3070 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
3071 if (priv->remote_contact != NULL) {
3072 g_object_ref (priv->remote_contact);
3073 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
3075 else if (priv->tp_chat != NULL) {
3076 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
3079 chat_update_contacts_visibility (chat, priv->show_contacts);
3081 g_object_notify (G_OBJECT (chat), "remote-contact");
3082 g_object_notify (G_OBJECT (chat), "id");
3086 chat_invalidated_cb (EmpathyTpChat *tp_chat,
3092 EmpathyChatPriv *priv;
3094 priv = GET_PRIV (chat);
3096 if (!priv->tp_chat) {
3100 chat_composing_remove_timeout (chat);
3101 g_object_unref (priv->tp_chat);
3102 priv->tp_chat = NULL;
3103 g_object_notify (G_OBJECT (chat), "tp-chat");
3105 empathy_theme_adium_append_event (chat->view, _("Disconnected"));
3106 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
3108 chat_update_contacts_visibility (chat, FALSE);
3110 priv->unread_messages_when_offline = priv->unread_messages;
3114 update_misspelled_words (gpointer data)
3116 EmpathyChat *chat = EMPATHY_CHAT (data);
3117 EmpathyChatPriv *priv = GET_PRIV (chat);
3118 GtkTextBuffer *buffer;
3122 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3124 gtk_text_buffer_get_end_iter (buffer, &iter);
3125 length = gtk_text_iter_get_offset (&iter);
3126 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3127 NULL, length, chat);
3129 priv->update_misspelled_words_id = 0;
3135 conf_spell_checking_cb (GSettings *gsettings_chat,
3139 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3140 EmpathyChatPriv *priv = GET_PRIV (chat);
3141 gboolean spell_checker;
3142 GtkTextBuffer *buffer;
3144 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3147 spell_checker = g_settings_get_boolean (gsettings_chat,
3148 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3150 if (!empathy_spell_supported ()) {
3151 spell_checker = FALSE;
3154 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3156 if (spell_checker == priv->spell_checking_enabled) {
3157 if (spell_checker) {
3158 /* Possibly changed dictionaries,
3159 * update misspelled words. Need to do so in idle
3160 * so the spell checker is updated. */
3161 priv->update_misspelled_words_id =
3162 g_idle_add (update_misspelled_words, chat);
3168 if (spell_checker) {
3171 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3172 buffer, "notify::cursor-position",
3173 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3175 priv->insert_text_id = tp_g_signal_connect_object (
3176 buffer, "insert-text",
3177 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3178 chat, G_CONNECT_AFTER);
3179 priv->delete_range_id = tp_g_signal_connect_object (
3180 buffer, "delete-range",
3181 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3182 chat, G_CONNECT_AFTER);
3184 gtk_text_buffer_create_tag (buffer, "misspelled",
3185 "underline", PANGO_UNDERLINE_ERROR,
3188 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3189 gtk_text_buffer_get_insert (buffer));
3190 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3193 /* Mark misspelled words in the existing buffer.
3194 * Need to do so in idle so the spell checker is updated. */
3195 priv->update_misspelled_words_id =
3196 g_idle_add (update_misspelled_words, chat);
3198 GtkTextTagTable *table;
3201 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3202 priv->notify_cursor_position_id = 0;
3203 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3204 priv->insert_text_id = 0;
3205 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3206 priv->delete_range_id = 0;
3208 table = gtk_text_buffer_get_tag_table (buffer);
3209 tag = gtk_text_tag_table_lookup (table, "misspelled");
3210 gtk_text_tag_table_remove (table, tag);
3212 gtk_text_buffer_delete_mark_by_name (buffer,
3213 "previous-cursor-position");
3216 priv->spell_checking_enabled = spell_checker;
3220 save_paned_pos_timeout (gpointer data)
3222 EmpathyChat *self = data;
3225 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3227 g_settings_set_int (self->priv->gsettings_ui,
3228 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3235 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3239 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3241 if (chat->priv->save_paned_pos_id != 0)
3242 g_source_remove (chat->priv->save_paned_pos_id);
3244 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3245 save_paned_pos_timeout, chat);
3251 chat_create_ui (EmpathyChat *chat)
3253 EmpathyChatPriv *priv = GET_PRIV (chat);
3257 GtkTextBuffer *buffer;
3258 EmpathyThemeManager *theme_mgr;
3260 filename = empathy_file_lookup ("empathy-chat.ui",
3262 gui = empathy_builder_get_file (filename,
3263 "chat_widget", &priv->widget,
3264 "hpaned", &priv->hpaned,
3265 "vbox_left", &priv->vbox_left,
3266 "scrolled_window_chat", &priv->scrolled_window_chat,
3267 "scrolled_window_input", &priv->scrolled_window_input,
3268 "hbox_topic", &priv->hbox_topic,
3269 "expander_topic", &priv->expander_topic,
3270 "label_topic", &priv->label_topic,
3271 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3272 "info_bar_vbox", &priv->info_bar_vbox,
3275 empathy_builder_connect (gui, chat,
3276 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3277 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3282 /* Add message view. */
3283 theme_mgr = empathy_theme_manager_dup_singleton ();
3284 chat->view = empathy_theme_manager_create_view (theme_mgr);
3285 g_object_unref (theme_mgr);
3286 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3287 and other types, even though it's non-editable and doesn't accept any
3288 drags. This steals drag motion for anything inside the scrollbars,
3289 making drag destinations on chat windows far less useful.
3291 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3292 g_signal_connect (chat->view, "focus_in_event",
3293 G_CALLBACK (chat_text_view_focus_in_event_cb),
3295 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3296 GTK_WIDGET (chat->view));
3297 gtk_widget_show (GTK_WIDGET (chat->view));
3299 /* Add input GtkTextView */
3300 chat->input_text_view = empathy_input_text_view_new ();
3301 g_signal_connect (chat->input_text_view, "notify::has-focus",
3302 G_CALLBACK (chat_input_has_focus_notify_cb),
3304 g_signal_connect (chat->input_text_view, "key-press-event",
3305 G_CALLBACK (chat_input_key_press_event_cb),
3307 g_signal_connect (chat->input_text_view, "realize",
3308 G_CALLBACK (chat_input_realize_cb),
3310 g_signal_connect (chat->input_text_view, "button-press-event",
3311 G_CALLBACK (chat_input_button_press_event_cb),
3313 g_signal_connect (chat->input_text_view, "populate-popup",
3314 G_CALLBACK (chat_input_populate_popup_cb),
3316 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3317 tp_g_signal_connect_object (buffer, "changed",
3318 G_CALLBACK (chat_input_text_buffer_changed_cb),
3320 tp_g_signal_connect_object (priv->gsettings_chat,
3321 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3322 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3323 conf_spell_checking_cb (priv->gsettings_chat,
3324 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3325 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3326 chat->input_text_view);
3327 gtk_widget_show (chat->input_text_view);
3329 /* Add the (invisible) search bar */
3330 priv->search_bar = empathy_search_bar_new (chat->view);
3331 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3334 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3336 /* Initialy hide the topic, will be shown if not empty */
3337 gtk_widget_hide (priv->hbox_topic);
3339 g_signal_connect (priv->hpaned, "notify::position",
3340 G_CALLBACK (chat_hpaned_pos_changed_cb),
3343 /* Set widget focus order */
3344 list = g_list_append (NULL, priv->search_bar);
3345 list = g_list_append (list, priv->scrolled_window_input);
3346 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3349 list = g_list_append (NULL, priv->vbox_left);
3350 list = g_list_append (list, priv->scrolled_window_contacts);
3351 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3354 list = g_list_append (NULL, priv->hpaned);
3355 list = g_list_append (list, priv->hbox_topic);
3356 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3359 /* Add the main widget in the chat widget */
3360 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3361 g_object_unref (gui);
3365 chat_finalize (GObject *object)
3368 EmpathyChatPriv *priv;
3370 chat = EMPATHY_CHAT (object);
3371 priv = GET_PRIV (chat);
3373 DEBUG ("Finalized: %p", object);
3375 if (priv->update_misspelled_words_id != 0)
3376 g_source_remove (priv->update_misspelled_words_id);
3378 if (priv->save_paned_pos_id != 0)
3379 g_source_remove (priv->save_paned_pos_id);
3381 if (priv->contacts_visible_id != 0)
3382 g_source_remove (priv->contacts_visible_id);
3384 g_object_unref (priv->gsettings_chat);
3385 g_object_unref (priv->gsettings_ui);
3387 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3388 g_list_free (priv->input_history);
3390 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3391 g_list_free (priv->compositors);
3393 chat_composing_remove_timeout (chat);
3395 g_object_unref (priv->account_manager);
3396 g_object_unref (priv->log_manager);
3397 g_object_unref (priv->log_walker);
3399 if (priv->tp_chat) {
3400 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3401 chat_invalidated_cb, chat);
3402 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3403 chat_message_received_cb, chat);
3404 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3405 chat_message_acknowledged_cb, chat);
3406 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3407 chat_send_error_cb, chat);
3408 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3409 chat_state_changed_cb, chat);
3410 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3411 chat_members_changed_cb, chat);
3412 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3413 chat_self_contact_changed_cb, chat);
3414 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3415 chat_remote_contact_changed_cb, chat);
3416 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3417 chat_title_changed_cb, chat);
3418 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3419 chat_subject_changed_cb, chat);
3420 empathy_tp_chat_leave (priv->tp_chat, "");
3421 g_object_unref (priv->tp_chat);
3423 if (priv->account) {
3424 g_object_unref (priv->account);
3426 if (priv->self_contact) {
3427 g_signal_handlers_disconnect_by_func (priv->self_contact,
3428 chat_self_contact_alias_changed_cb,
3430 g_object_unref (priv->self_contact);
3432 if (priv->remote_contact) {
3433 g_object_unref (priv->remote_contact);
3436 if (priv->block_events_timeout_id) {
3437 g_source_remove (priv->block_events_timeout_id);
3441 g_free (priv->name);
3442 g_free (priv->subject);
3443 g_completion_free (priv->completion);
3445 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3447 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3451 chat_constructed (GObject *object)
3453 EmpathyChat *chat = EMPATHY_CHAT (object);
3454 EmpathyChatPriv *priv = GET_PRIV (chat);
3457 if (priv->tp_chat != NULL) {
3458 TpChannel *channel = TP_CHANNEL (priv->tp_chat);
3459 TpConnection *conn = tp_channel_get_connection (channel);
3460 gboolean supports_avatars =
3461 tp_proxy_has_interface_by_id (conn,
3462 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS);
3464 empathy_theme_adium_set_show_avatars (chat->view,
3468 /* Add messages from last conversation */
3469 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
3470 target = tpl_entity_new_from_room_id (priv->id);
3472 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
3474 priv->log_walker = tpl_log_manager_walk_filtered_events (priv->log_manager, priv->account, target,
3475 TPL_EVENT_MASK_TEXT, chat_log_filter, chat);
3476 g_object_unref (target);
3478 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3479 /* First display logs from the logger and then display pending messages */
3480 chat_add_logs (chat);
3483 /* Just display pending messages for rooms */
3484 priv->can_show_pending = TRUE;
3485 show_pending_messages (chat);
3490 empathy_chat_class_init (EmpathyChatClass *klass)
3492 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3494 object_class->finalize = chat_finalize;
3495 object_class->get_property = chat_get_property;
3496 object_class->set_property = chat_set_property;
3497 object_class->constructed = chat_constructed;
3499 g_object_class_install_property (object_class,
3501 g_param_spec_object ("tp-chat",
3503 "The tp chat object",
3504 EMPATHY_TYPE_TP_CHAT,
3507 G_PARAM_STATIC_STRINGS));
3508 g_object_class_install_property (object_class,
3510 g_param_spec_object ("account",
3511 "Account of the chat",
3512 "The account of the chat",
3515 G_PARAM_STATIC_STRINGS));
3516 g_object_class_install_property (object_class,
3518 g_param_spec_string ("id",
3520 "The id of the chat",
3523 G_PARAM_STATIC_STRINGS));
3524 g_object_class_install_property (object_class,
3526 g_param_spec_string ("name",
3528 "The name of the chat",
3531 G_PARAM_STATIC_STRINGS));
3532 g_object_class_install_property (object_class,
3534 g_param_spec_string ("subject",
3536 "The subject or topic of the chat",
3539 G_PARAM_STATIC_STRINGS));
3540 g_object_class_install_property (object_class,
3541 PROP_REMOTE_CONTACT,
3542 g_param_spec_object ("remote-contact",
3543 "The remote contact",
3544 "The remote contact is any",
3545 EMPATHY_TYPE_CONTACT,
3547 G_PARAM_STATIC_STRINGS));
3548 g_object_class_install_property (object_class,
3550 g_param_spec_boolean ("show-contacts",
3551 "Contacts' visibility",
3552 "The visibility of the contacts' list",
3555 G_PARAM_STATIC_STRINGS));
3557 g_object_class_install_property (object_class,
3559 g_param_spec_boolean ("sms-channel",
3561 "TRUE if this channel is for sending SMSes",
3563 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3565 g_object_class_install_property (object_class,
3566 PROP_N_MESSAGES_SENDING,
3567 g_param_spec_uint ("n-messages-sending",
3568 "Num Messages Sending",
3569 "The number of messages being sent",
3571 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3573 g_object_class_install_property (object_class,
3574 PROP_NB_UNREAD_MESSAGES,
3575 g_param_spec_uint ("nb-unread-messages",
3576 "Num Unread Messages",
3577 "The number of unread messages",
3579 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3581 signals[COMPOSING] =
3582 g_signal_new ("composing",
3583 G_OBJECT_CLASS_TYPE (object_class),
3587 g_cclosure_marshal_generic,
3592 * EmpathyChat::new-message:
3593 * @self: the #EmpathyChat
3594 * @message: the new message
3595 * @pending: whether the message was in the pending queue when @self
3597 * @should_highlight: %TRUE if the message mentions the local user
3599 signals[NEW_MESSAGE] =
3600 g_signal_new ("new-message",
3601 G_OBJECT_CLASS_TYPE (object_class),
3605 g_cclosure_marshal_generic,
3607 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3609 signals[PART_COMMAND_ENTERED] =
3610 g_signal_new ("part-command-entered",
3611 G_OBJECT_CLASS_TYPE (object_class),
3615 g_cclosure_marshal_generic,
3619 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3623 chat_block_events_timeout_cb (gpointer data)
3625 EmpathyChatPriv *priv = GET_PRIV (data);
3627 priv->block_events_timeout_id = 0;
3633 account_manager_prepared_cb (GObject *source_object,
3634 GAsyncResult *result,
3637 GList *accounts, *l;
3638 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3639 EmpathyChat *chat = user_data;
3640 GError *error = NULL;
3642 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3643 DEBUG ("Failed to prepare the account manager: %s", error->message);
3644 g_error_free (error);
3648 accounts = tp_account_manager_dup_valid_accounts (account_manager);
3650 for (l = accounts; l != NULL; l = l->next) {
3651 TpAccount *account = l->data;
3652 tp_g_signal_connect_object (account, "status-changed",
3653 G_CALLBACK (chat_new_connection_cb),
3657 g_list_free_full (accounts, g_object_unref);
3661 empathy_chat_init (EmpathyChat *chat)
3663 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3664 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3667 priv->log_manager = tpl_log_manager_dup_singleton ();
3668 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3669 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3671 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3672 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3673 priv->input_history = NULL;
3674 priv->input_history_current = NULL;
3675 priv->account_manager = tp_account_manager_dup ();
3677 tp_proxy_prepare_async (priv->account_manager, NULL,
3678 account_manager_prepared_cb, chat);
3680 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3681 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3683 /* Block events for some time to avoid having "has come online" or
3684 * "joined" messages. */
3685 priv->block_events_timeout_id =
3686 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3688 /* Add nick name completion */
3689 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3690 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3692 chat_create_ui (chat);
3696 empathy_chat_new (EmpathyTpChat *tp_chat)
3698 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3702 empathy_chat_get_tp_chat (EmpathyChat *chat)
3704 EmpathyChatPriv *priv = GET_PRIV (chat);
3706 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3708 return priv->tp_chat;
3714 GtkWidget *info_bar;
3724 passwd_remember_button_cb (GtkButton *button,
3727 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3731 passwd_not_now_button_cb (GtkButton *button,
3734 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3738 remember_password_infobar_response_cb (GtkWidget *info_bar,
3742 EmpathyChatPriv *priv = GET_PRIV (data->self);
3744 if (response_id == GTK_RESPONSE_OK) {
3745 DEBUG ("Saving room password");
3746 empathy_keyring_set_room_password_async (priv->account,
3747 empathy_tp_chat_get_id (priv->tp_chat),
3752 gtk_widget_destroy (info_bar);
3753 g_free (data->password);
3754 g_slice_free (PasswordData, data);
3758 chat_prompt_to_save_password (EmpathyChat *self,
3761 GtkWidget *content_area;
3768 /* save the password in case it needs to be saved */
3769 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3771 /* Remove all previous widgets */
3772 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3773 gtk_container_forall (GTK_CONTAINER (content_area),
3774 (GtkCallback) gtk_widget_destroy, NULL);
3775 data->button = NULL;
3778 data->spinner = NULL;
3780 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3781 GTK_MESSAGE_QUESTION);
3783 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3784 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3787 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3788 GTK_ICON_SIZE_DIALOG);
3789 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3792 label = gtk_label_new (_("Would you like to store this password?"));
3793 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3795 /* Add 'Remember' button */
3796 alig = gtk_alignment_new (0, 0.5, 1, 0);
3798 button = gtk_button_new_with_label (_("Remember"));
3799 gtk_container_add (GTK_CONTAINER (alig), button);
3800 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3802 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3805 /* Add 'Not now' button */
3806 alig = gtk_alignment_new (0, 0.5, 1, 0);
3808 button = gtk_button_new_with_label (_("Not now"));
3809 gtk_container_add (GTK_CONTAINER (alig), button);
3810 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3812 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3816 g_signal_handler_disconnect (data->info_bar, data->response_id);
3817 g_signal_connect (data->info_bar, "response",
3818 G_CALLBACK (remember_password_infobar_response_cb), data);
3820 gtk_widget_show_all (data->info_bar);
3824 provide_password_cb (GObject *tp_chat,
3828 PasswordData *data = user_data;
3829 EmpathyChat *self = data->self;
3830 EmpathyChatPriv *priv = GET_PRIV (self);
3831 GError *error = NULL;
3833 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3835 DEBUG ("error: %s", error->message);
3836 /* FIXME: what should we do if that's another error? Close the channel?
3837 * Display the raw D-Bus error to the user isn't very useful */
3838 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3840 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3841 gtk_widget_set_sensitive (data->entry, TRUE);
3842 gtk_widget_grab_focus (data->entry);
3845 gtk_info_bar_set_message_type (
3846 GTK_INFO_BAR (data->info_bar),
3850 gtk_widget_set_sensitive (data->button, TRUE);
3851 gtk_button_set_label (GTK_BUTTON (data->button),
3855 gtk_label_set_text (GTK_LABEL (data->label),
3856 _("Wrong password; please try again:"));
3859 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3860 gtk_widget_hide (data->spinner);
3862 g_error_free (error);
3866 if (empathy_keyring_is_available ()) {
3867 /* ask whether they want to save the password */
3868 chat_prompt_to_save_password (self, data);
3870 /* Get rid of the password info bar finally */
3871 gtk_widget_destroy (data->info_bar);
3872 g_slice_free (PasswordData, data);
3876 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3877 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3878 gtk_widget_grab_focus (self->input_text_view);
3882 password_infobar_response_cb (GtkWidget *info_bar,
3886 EmpathyChatPriv *priv = GET_PRIV (data->self);
3887 const gchar *password;
3889 if (response_id != GTK_RESPONSE_OK) {
3890 gtk_widget_destroy (info_bar);
3891 g_slice_free (PasswordData, data);
3895 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3897 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3898 provide_password_cb, data);
3900 gtk_widget_set_sensitive (data->button, FALSE);
3901 gtk_widget_set_sensitive (data->entry, FALSE);
3903 gtk_spinner_start (GTK_SPINNER (data->spinner));
3904 gtk_widget_show (data->spinner);
3908 password_entry_activate_cb (GtkWidget *entry,
3911 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3915 passwd_join_button_cb (GtkButton *button,
3918 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3922 clear_icon_released_cb (GtkEntry *entry,
3923 GtkEntryIconPosition icon_pos,
3927 gtk_entry_set_text (entry, "");
3931 password_entry_changed_cb (GtkEditable *entry,
3936 str = gtk_entry_get_text (GTK_ENTRY (entry));
3938 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3939 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3943 infobar_chat_invalidated_cb (TpProxy *proxy,
3947 gpointer password_infobar)
3949 /* Destroy the password infobar whenever a channel is invalidated
3950 * so we don't have multiple infobars when the MUC is rejoined */
3951 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3955 display_password_info_bar (EmpathyChat *self)
3957 EmpathyChatPriv *priv = GET_PRIV (self);
3958 GtkWidget *info_bar;
3959 GtkWidget *content_area;
3969 data = g_slice_new0 (PasswordData);
3971 info_bar = gtk_info_bar_new ();
3972 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3973 GTK_MESSAGE_QUESTION);
3975 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3977 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3978 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3981 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3982 GTK_ICON_SIZE_DIALOG);
3983 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3986 label = gtk_label_new (_("This room is protected by a password:"));
3987 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3989 /* Add password entry */
3990 entry = gtk_entry_new ();
3991 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3992 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3994 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3995 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3996 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3997 GTK_ENTRY_ICON_SECONDARY, FALSE);
3999 g_signal_connect (entry, "icon-release",
4000 G_CALLBACK (clear_icon_released_cb), data);
4001 g_signal_connect (entry, "changed",
4002 G_CALLBACK (password_entry_changed_cb), data);
4004 g_signal_connect (entry, "activate",
4005 G_CALLBACK (password_entry_activate_cb), data);
4007 /* Focus the password entry once it's realized */
4008 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
4010 /* Add 'Join' button */
4011 alig = gtk_alignment_new (0, 0.5, 1, 0);
4013 button = gtk_button_new_with_label (_("Join"));
4014 gtk_container_add (GTK_CONTAINER (alig), button);
4015 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
4017 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
4021 spinner = gtk_spinner_new ();
4022 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
4024 /* Save some data for messing around with later */
4026 data->info_bar = info_bar;
4027 data->button = button;
4028 data->label = label;
4029 data->entry = entry;
4030 data->spinner = spinner;
4032 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
4034 gtk_widget_show_all (hbox);
4036 tp_g_signal_connect_object (priv->tp_chat,
4037 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
4040 data->response_id = g_signal_connect (info_bar, "response",
4041 G_CALLBACK (password_infobar_response_cb), data);
4043 gtk_widget_show_all (info_bar);
4044 /* ... but hide the spinner */
4045 gtk_widget_hide (spinner);
4047 /* prevent the user from typing anything */
4048 gtk_widget_set_sensitive (self->input_text_view, FALSE);
4052 provide_saved_password_cb (GObject *tp_chat,
4056 EmpathyChat *self = user_data;
4057 EmpathyChatPriv *priv = GET_PRIV (self);
4058 GError *error = NULL;
4060 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
4062 DEBUG ("error: %s", error->message);
4063 /* FIXME: what should we do if that's another error? Close the channel?
4064 * Display the raw D-Bus error to the user isn't very useful */
4065 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
4066 display_password_info_bar (self);
4067 gtk_widget_set_sensitive (priv->hpaned, FALSE);
4069 g_error_free (error);
4074 gtk_widget_set_sensitive (priv->hpaned, TRUE);
4075 gtk_widget_grab_focus (self->input_text_view);
4079 chat_room_got_password_cb (GObject *source,
4080 GAsyncResult *result,
4083 EmpathyChat *self = user_data;
4084 EmpathyChatPriv *priv = GET_PRIV (self);
4085 const gchar *password;
4086 GError *error = NULL;
4088 password = empathy_keyring_get_room_password_finish (priv->account,
4091 if (error != NULL) {
4092 DEBUG ("Couldn't get room password: %s\n", error->message);
4093 g_clear_error (&error);
4095 display_password_info_bar (self);
4096 gtk_widget_set_sensitive (priv->hpaned, FALSE);
4100 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
4101 provide_saved_password_cb, self);
4105 chat_password_needed_changed_cb (EmpathyChat *self)
4107 EmpathyChatPriv *priv = GET_PRIV (self);
4109 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
4110 empathy_keyring_get_room_password_async (priv->account,
4111 empathy_tp_chat_get_id (priv->tp_chat),
4112 chat_room_got_password_cb, self);
4117 chat_sms_channel_changed_cb (EmpathyChat *self)
4119 EmpathyChatPriv *priv = GET_PRIV (self);
4121 priv->sms_channel = tp_text_channel_is_sms_channel (
4122 (TpTextChannel *) priv->tp_chat);
4123 g_object_notify (G_OBJECT (self), "sms-channel");
4127 chat_n_messages_sending_changed_cb (EmpathyChat *self)
4129 g_object_notify (G_OBJECT (self), "n-messages-sending");
4133 empathy_chat_set_tp_chat (EmpathyChat *chat,
4134 EmpathyTpChat *tp_chat)
4136 EmpathyChatPriv *priv = GET_PRIV (chat);
4138 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4139 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4141 if (priv->tp_chat) {
4145 if (priv->account) {
4146 g_object_unref (priv->account);
4149 priv->tp_chat = g_object_ref (tp_chat);
4150 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4152 g_signal_connect (tp_chat, "invalidated",
4153 G_CALLBACK (chat_invalidated_cb),
4155 g_signal_connect (tp_chat, "message-received-empathy",
4156 G_CALLBACK (chat_message_received_cb),
4158 g_signal_connect (tp_chat, "message_acknowledged",
4159 G_CALLBACK (chat_message_acknowledged_cb),
4161 g_signal_connect (tp_chat, "send-error",
4162 G_CALLBACK (chat_send_error_cb),
4164 g_signal_connect (tp_chat, "contact-chat-state-changed",
4165 G_CALLBACK (chat_state_changed_cb),
4167 g_signal_connect (tp_chat, "members-changed",
4168 G_CALLBACK (chat_members_changed_cb),
4170 g_signal_connect (tp_chat, "member-renamed",
4171 G_CALLBACK (chat_member_renamed_cb),
4173 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4174 G_CALLBACK (chat_self_contact_changed_cb),
4176 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4177 G_CALLBACK (chat_remote_contact_changed_cb),
4179 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4180 G_CALLBACK (chat_password_needed_changed_cb),
4182 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4183 G_CALLBACK (chat_sms_channel_changed_cb),
4185 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4186 G_CALLBACK (chat_n_messages_sending_changed_cb),
4188 g_signal_connect_swapped (tp_chat, "notify::title",
4189 G_CALLBACK (chat_title_changed_cb),
4191 g_signal_connect_swapped (tp_chat, "notify::subject",
4192 G_CALLBACK (chat_subject_changed_cb),
4195 /* Get initial value of properties */
4196 chat_sms_channel_changed_cb (chat);
4197 chat_self_contact_changed_cb (chat);
4198 chat_remote_contact_changed_cb (chat);
4199 chat_title_changed_cb (chat);
4200 chat_subject_changed_cb (chat);
4202 if (chat->input_text_view) {
4203 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4204 if (priv->block_events_timeout_id == 0) {
4205 empathy_theme_adium_append_event (chat->view, _("Connected"));
4209 g_object_notify (G_OBJECT (chat), "tp-chat");
4210 g_object_notify (G_OBJECT (chat), "id");
4211 g_object_notify (G_OBJECT (chat), "account");
4213 /* This is a noop when tp-chat is set at object construction time and causes
4214 * the pending messages to be show when it's set on the object after it has
4216 show_pending_messages (chat);
4218 /* check if a password is needed */
4219 chat_password_needed_changed_cb (chat);
4223 empathy_chat_get_account (EmpathyChat *chat)
4225 EmpathyChatPriv *priv = GET_PRIV (chat);
4227 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4229 return priv->account;
4233 empathy_chat_get_id (EmpathyChat *chat)
4235 EmpathyChatPriv *priv = GET_PRIV (chat);
4237 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4243 empathy_chat_dup_name (EmpathyChat *chat)
4245 EmpathyChatPriv *priv = GET_PRIV (chat);
4248 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4252 if (!ret && priv->remote_contact) {
4253 ret = empathy_contact_get_alias (priv->remote_contact);
4260 ret = _("Conversation");
4262 if (priv->sms_channel)
4263 /* Translators: this string is a something like
4264 * "Escher Cat (SMS)" */
4265 return g_strdup_printf (_("%s (SMS)"), ret);
4267 return g_strdup (ret);
4271 empathy_chat_get_subject (EmpathyChat *chat)
4273 EmpathyChatPriv *priv = GET_PRIV (chat);
4275 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4277 return priv->subject;
4281 empathy_chat_get_remote_contact (EmpathyChat *chat)
4283 EmpathyChatPriv *priv = GET_PRIV (chat);
4285 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4287 return priv->remote_contact;
4291 empathy_chat_get_contact_menu (EmpathyChat *chat)
4293 EmpathyChatPriv *priv = GET_PRIV (chat);
4294 GtkWidget *menu = NULL;
4295 FolksIndividual *individual;
4298 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4300 if (priv->remote_contact == NULL)
4303 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4304 if (contact == NULL)
4307 individual = empathy_ensure_individual_from_tp_contact (contact);
4309 if (individual == NULL)
4312 menu = empathy_individual_menu_new (individual,
4313 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4314 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4315 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4316 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4318 g_object_unref (individual);
4324 empathy_chat_clear (EmpathyChat *chat)
4326 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4328 empathy_theme_adium_clear (chat->view);
4332 empathy_chat_scroll_down (EmpathyChat *chat)
4334 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4336 empathy_theme_adium_scroll_down (chat->view);
4340 empathy_chat_cut (EmpathyChat *chat)
4342 GtkTextBuffer *buffer;
4344 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4346 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4347 if (gtk_text_buffer_get_has_selection (buffer)) {
4348 GtkClipboard *clipboard;
4350 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4352 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4357 copy_from_chat_view (EmpathyChat *chat)
4359 if (!empathy_theme_adium_get_has_selection (chat->view))
4362 empathy_theme_adium_copy_clipboard (chat->view);
4367 copy_from_input (EmpathyChat *chat)
4369 GtkTextBuffer *buffer;
4370 GtkClipboard *clipboard;
4372 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4373 if (!gtk_text_buffer_get_has_selection (buffer))
4376 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4377 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4382 copy_from_topic (EmpathyChat *chat)
4384 EmpathyChatPriv *priv = GET_PRIV (chat);
4391 GtkClipboard *clipboard;
4393 if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4398 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4399 start = g_utf8_offset_to_pointer (topic, start_offset);
4400 end = g_utf8_offset_to_pointer (topic, end_offset);
4401 selection = g_strndup (start, end - start);
4403 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4404 gtk_clipboard_set_text (clipboard, selection, -1);
4411 empathy_chat_copy (EmpathyChat *chat)
4413 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4415 if (copy_from_chat_view (chat))
4418 if (copy_from_input (chat))
4421 copy_from_topic (chat);
4425 empathy_chat_paste (EmpathyChat *chat)
4427 GtkTextBuffer *buffer;
4428 GtkClipboard *clipboard;
4429 EmpathyChatPriv *priv;
4431 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4433 priv = GET_PRIV (chat);
4435 if (gtk_widget_get_visible (priv->search_bar)) {
4436 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4440 if (priv->tp_chat == NULL ||
4441 !gtk_widget_is_sensitive (chat->input_text_view))
4444 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4445 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4447 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4451 empathy_chat_find (EmpathyChat *chat)
4453 EmpathyChatPriv *priv;
4455 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4457 priv = GET_PRIV (chat);
4459 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4463 empathy_chat_correct_word (EmpathyChat *chat,
4466 const gchar *new_word)
4468 GtkTextBuffer *buffer;
4470 g_return_if_fail (chat != NULL);
4471 g_return_if_fail (new_word != NULL);
4473 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4475 gtk_text_buffer_delete (buffer, start, end);
4476 gtk_text_buffer_insert (buffer, start,
4482 empathy_chat_is_room (EmpathyChat *chat)
4484 EmpathyChatPriv *priv = GET_PRIV (chat);
4486 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4488 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4492 empathy_chat_is_highlighted (EmpathyChat *chat)
4494 EmpathyChatPriv *priv = GET_PRIV (chat);
4496 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4498 return priv->highlighted;
4502 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4504 EmpathyChatPriv *priv = GET_PRIV (self);
4506 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4508 return priv->unread_messages;
4511 /* called when the messages have been read by user */
4513 empathy_chat_messages_read (EmpathyChat *self)
4515 EmpathyChatPriv *priv = GET_PRIV (self);
4517 g_return_if_fail (EMPATHY_IS_CHAT (self));
4519 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4520 * definition. If we are still retrieving the backlogs, do not ACK */
4521 if (priv->retrieving_backlogs)
4524 if (priv->tp_chat != NULL) {
4525 tp_text_channel_ack_all_pending_messages_async (
4526 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4529 priv->highlighted = FALSE;
4531 if (priv->unread_messages_when_offline > 0) {
4532 /* We can't ack those as the connection has gone away so just consider
4534 priv->unread_messages -= priv->unread_messages_when_offline;
4535 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4536 priv->unread_messages_when_offline = 0;
4540 /* Return TRUE if on of the contacts in this chat is composing */
4542 empathy_chat_is_composing (EmpathyChat *chat)
4544 return chat->priv->compositors != NULL;
4548 empathy_chat_is_sms_channel (EmpathyChat *self)
4550 EmpathyChatPriv *priv = GET_PRIV (self);
4552 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4554 return priv->sms_channel;
4558 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4560 EmpathyChatPriv *priv;
4562 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4564 priv = GET_PRIV (self);
4566 if (priv->tp_chat == NULL) {
4571 g_object_get (priv->tp_chat,
4572 "n-messages-sending", &n_messages,
4580 empathy_chat_dup_text (EmpathyChat *self)
4582 GtkTextBuffer *buffer;
4583 GtkTextIter start, end;
4585 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4587 gtk_text_buffer_get_bounds (buffer, &start, &end);
4588 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4592 empathy_chat_set_text (EmpathyChat *self,
4595 GtkTextBuffer *buffer;
4597 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4599 gtk_text_buffer_set_text (buffer, text, -1);