1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
30 #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
35 #include <gdk/gdkkeysyms.h>
36 #include <glib/gi18n-lib.h>
39 #include <telepathy-glib/account-manager.h>
40 #include <telepathy-glib/util.h>
41 #include <telepathy-logger/telepathy-logger.h>
42 #include <libempathy/empathy-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 TpAccountManager *account_manager;
86 GList *input_history_current;
88 GCompletion *completion;
89 guint composing_stop_timeout_id;
90 guint block_events_timeout_id;
91 TpHandleType handle_type;
93 gboolean has_input_vscroll;
95 /* TRUE if spell checking is enabled, FALSE otherwise.
96 * This is to keep track of the last state of spell checking
98 gboolean spell_checking_enabled;
100 /* These store the signal handler ids for the enclosed text entry. */
101 gulong insert_text_id;
102 gulong delete_range_id;
103 gulong notify_cursor_position_id;
105 /* Source func ID for update_misspelled_words () */
106 guint update_misspelled_words_id;
107 /* Source func ID for save_paned_pos_timeout () */
108 guint save_paned_pos_id;
109 /* Source func ID for chat_contacts_visible_timeout_cb () */
110 guint contacts_visible_id;
114 GtkWidget *vbox_left;
115 GtkWidget *scrolled_window_chat;
116 GtkWidget *scrolled_window_input;
117 GtkWidget *scrolled_window_contacts;
118 GtkWidget *hbox_topic;
119 GtkWidget *expander_topic;
120 GtkWidget *label_topic;
121 GtkWidget *contact_list_view;
122 GtkWidget *info_bar_vbox;
123 GtkWidget *search_bar;
125 guint unread_messages;
126 guint unread_messages_when_offline;
127 /* TRUE if the pending messages can be displayed. This is to avoid to show
128 * pending messages *before* messages from logs. (#603980) */
129 gboolean can_show_pending;
131 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
132 * be differently handled since it introduces another race condition, which
133 * is really hard to occur, but still possible.
135 * With the current workaround (which has the race above), we need to be
136 * sure to ACK any pending messages only when the retrieval of backlogs is
137 * finished, that's why using retrieving_backlogs flag.
138 * empathy_chat_messages_read () will check this variable and not ACK
139 * anything when TRUE. It will be set TRUE at chat_constructed () and set
140 * back to FALSE when the backlog has been retrieved and the pending
141 * messages actually showed to the user.
143 * Race condition introduced with this workaround:
144 * Scenario: a message is pending, the user is notified and selects the tab.
145 * the tab with a pending message is focused before the messages are properly
146 * shown (since the preparation of the window is slower AND async WRT the
147 * tab showing), which means the user won't see any new messages (rare but
148 * possible), if he/she will change tab focus before the messages are
149 * properly shown, the tab will be set as 'seen' and the user won't be
150 * notified again about the already notified pending messages when the
151 * messages in tab will be properly shown */
152 gboolean retrieving_backlogs;
153 gboolean sms_channel;
155 /* we need to know whether populate-popup happened in response to
156 * the keyboard or the mouse. We can't ask GTK for the most recent
157 * event, because it will be a notify event. Instead we track it here */
158 GdkEventType most_recent_event_type;
160 /* A regex matching our own current nickname in the room, or %NULL if
161 * !empathy_chat_is_room (). */
162 GRegex *highlight_regex;
164 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
165 * Cleared by empathy_chat_messages_read (). */
166 gboolean highlighted;
170 gchar *text; /* Original message that was specified
171 * upon entry creation. */
172 gchar *modified_text; /* Message that was modified by user.
173 * When no modifications were made, it is NULL */
179 PART_COMMAND_ENTERED,
193 PROP_N_MESSAGES_SENDING,
194 PROP_NB_UNREAD_MESSAGES,
197 static guint signals[LAST_SIGNAL] = { 0 };
199 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
201 static gboolean update_misspelled_words (gpointer data);
204 chat_get_property (GObject *object,
209 EmpathyChat *chat = EMPATHY_CHAT (object);
210 EmpathyChatPriv *priv = GET_PRIV (object);
214 g_value_set_object (value, priv->tp_chat);
217 g_value_set_object (value, priv->account);
220 g_value_take_string (value, empathy_chat_dup_name (chat));
223 g_value_set_string (value, priv->id);
226 g_value_set_string (value, priv->subject);
228 case PROP_REMOTE_CONTACT:
229 g_value_set_object (value, priv->remote_contact);
231 case PROP_SHOW_CONTACTS:
232 g_value_set_boolean (value, priv->show_contacts);
234 case PROP_SMS_CHANNEL:
235 g_value_set_boolean (value, priv->sms_channel);
237 case PROP_N_MESSAGES_SENDING:
238 g_value_set_uint (value,
239 empathy_chat_get_n_messages_sending (chat));
241 case PROP_NB_UNREAD_MESSAGES:
242 g_value_set_uint (value,
243 empathy_chat_get_nb_unread_messages (chat));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
252 chat_set_property (GObject *object,
257 EmpathyChat *chat = EMPATHY_CHAT (object);
261 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
263 case PROP_SHOW_CONTACTS:
264 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
273 account_reconnected (EmpathyChat *chat,
276 EmpathyChatPriv *priv = GET_PRIV (chat);
278 DEBUG ("Account reconnected, request a new Text channel");
280 /* FIXME: Ideally we should ask to handle ourself the channel so we can
281 * report the error if any but this is blocked by
282 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
283 switch (priv->handle_type) {
284 case TP_HANDLE_TYPE_CONTACT:
285 if (priv->sms_channel)
286 empathy_sms_contact_id (
288 TP_USER_ACTION_TIME_NOT_USER_ACTION,
291 empathy_chat_with_contact_id (
293 TP_USER_ACTION_TIME_NOT_USER_ACTION,
296 case TP_HANDLE_TYPE_ROOM:
297 empathy_join_muc (account, priv->id,
298 TP_USER_ACTION_TIME_NOT_USER_ACTION);
300 case TP_HANDLE_TYPE_NONE:
301 case TP_HANDLE_TYPE_LIST:
302 case TP_HANDLE_TYPE_GROUP:
304 g_assert_not_reached ();
308 g_object_unref (chat);
312 chat_new_connection_cb (TpAccount *account,
316 gchar *dbus_error_name,
320 EmpathyChatPriv *priv = GET_PRIV (chat);
322 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
325 if (priv->tp_chat != NULL || account != priv->account ||
326 priv->handle_type == TP_HANDLE_TYPE_NONE ||
327 EMP_STR_EMPTY (priv->id))
332 account_reconnected (chat, account);
336 chat_composing_remove_timeout (EmpathyChat *chat)
338 EmpathyChatPriv *priv;
340 priv = GET_PRIV (chat);
342 if (priv->composing_stop_timeout_id) {
343 g_source_remove (priv->composing_stop_timeout_id);
344 priv->composing_stop_timeout_id = 0;
349 set_chate_state_cb (GObject *source,
350 GAsyncResult *result,
353 GError *error = NULL;
355 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
357 DEBUG ("Failed to set chat state: %s", error->message);
358 g_error_free (error);
363 set_chat_state (EmpathyChat *self,
364 TpChannelChatState state)
366 EmpathyChatPriv *priv = GET_PRIV (self);
368 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
369 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
372 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
373 set_chate_state_cb, self);
377 chat_composing_stop_timeout_cb (EmpathyChat *chat)
379 EmpathyChatPriv *priv;
380 gboolean send_chat_states;
382 priv = GET_PRIV (chat);
384 priv->composing_stop_timeout_id = 0;
385 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
386 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
387 if (!send_chat_states) {
388 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
390 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
397 chat_composing_start (EmpathyChat *chat)
399 EmpathyChatPriv *priv;
400 gboolean send_chat_states;
402 priv = GET_PRIV (chat);
404 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
405 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
406 if (!send_chat_states) {
410 if (priv->composing_stop_timeout_id) {
411 /* Just restart the timeout */
412 chat_composing_remove_timeout (chat);
414 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
417 priv->composing_stop_timeout_id = g_timeout_add_seconds (
418 COMPOSING_STOP_TIMEOUT,
419 (GSourceFunc) chat_composing_stop_timeout_cb,
424 chat_composing_stop (EmpathyChat *chat)
426 chat_composing_remove_timeout (chat);
427 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
431 chat_input_history_entry_cmp (InputHistoryEntry *entry,
434 if (!tp_strdiff (entry->text, text)) {
435 if (entry->modified_text != NULL) {
436 /* Modified entry and single string cannot be equal. */
444 static InputHistoryEntry *
445 chat_input_history_entry_new_with_text (const gchar *text)
447 InputHistoryEntry *entry;
448 entry = g_slice_new0 (InputHistoryEntry);
449 entry->text = g_strdup (text);
455 chat_input_history_entry_free (InputHistoryEntry *entry)
457 g_free (entry->text);
458 g_free (entry->modified_text);
459 g_slice_free (InputHistoryEntry, entry);
463 chat_input_history_entry_revert (InputHistoryEntry *entry)
465 g_free (entry->modified_text);
466 entry->modified_text = NULL;
470 chat_input_history_entry_update_text (InputHistoryEntry *entry,
475 if (!tp_strdiff (text, entry->text)) {
476 g_free (entry->modified_text);
477 entry->modified_text = NULL;
481 old = entry->modified_text;
482 entry->modified_text = g_strdup (text);
487 chat_input_history_entry_get_text (InputHistoryEntry *entry)
493 if (entry->modified_text != NULL) {
494 return entry->modified_text;
500 chat_input_history_remove_item (GList *list,
503 list = g_list_remove_link (list, item);
504 chat_input_history_entry_free (item->data);
505 g_list_free_1 (item);
510 chat_input_history_revert (EmpathyChat *chat)
512 EmpathyChatPriv *priv;
516 InputHistoryEntry *entry;
518 priv = GET_PRIV (chat);
519 list = priv->input_history;
522 DEBUG ("No input history");
526 /* Delete temporary entry */
527 if (priv->input_history_current != NULL) {
529 list = chat_input_history_remove_item (list, item1);
530 if (priv->input_history_current == item1) {
531 /* Removed temporary entry was current entry */
532 priv->input_history = list;
533 priv->input_history_current = NULL;
538 /* There is no entry to revert */
542 /* Restore the current history entry to original value */
543 item1 = priv->input_history_current;
545 chat_input_history_entry_revert (entry);
547 /* Remove restored entry if there is other occurance before this entry */
548 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
549 (GCompareFunc) chat_input_history_entry_cmp);
550 if (item2 != item1) {
551 list = chat_input_history_remove_item (list, item1);
554 /* Remove other occurance of the restored entry */
555 item2 = g_list_find_custom (item1->next,
556 chat_input_history_entry_get_text (entry),
557 (GCompareFunc) chat_input_history_entry_cmp);
559 list = chat_input_history_remove_item (list, item2);
563 priv->input_history_current = NULL;
564 priv->input_history = list;
568 chat_input_history_add (EmpathyChat *chat,
572 EmpathyChatPriv *priv;
575 InputHistoryEntry *entry;
577 priv = GET_PRIV (chat);
579 list = priv->input_history;
581 /* Remove any other occurances of this entry, if not temporary */
583 while ((item = g_list_find_custom (list, str,
584 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
585 list = chat_input_history_remove_item (list, item);
588 /* Trim the list to the last 10 items */
589 while (g_list_length (list) > 10) {
590 item = g_list_last (list);
592 list = chat_input_history_remove_item (list, item);
600 entry = chat_input_history_entry_new_with_text (str);
601 list = g_list_prepend (list, entry);
603 /* Set the list and the current item pointer */
604 priv->input_history = list;
606 priv->input_history_current = list;
609 priv->input_history_current = NULL;
614 chat_input_history_get_next (EmpathyChat *chat)
616 EmpathyChatPriv *priv;
620 priv = GET_PRIV (chat);
622 if (priv->input_history == NULL) {
623 DEBUG ("No input history, next entry is NULL");
626 g_assert (priv->input_history_current != NULL);
628 if ((item = g_list_next (priv->input_history_current)) == NULL)
630 item = priv->input_history_current;
633 msg = chat_input_history_entry_get_text (item->data);
635 DEBUG ("Returning next entry: '%s'", msg);
637 priv->input_history_current = item;
643 chat_input_history_get_prev (EmpathyChat *chat)
645 EmpathyChatPriv *priv;
649 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
651 priv = GET_PRIV (chat);
653 if (priv->input_history == NULL) {
654 DEBUG ("No input history, previous entry is NULL");
658 if (priv->input_history_current == NULL)
662 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
664 item = priv->input_history_current;
667 msg = chat_input_history_entry_get_text (item->data);
669 DEBUG ("Returning previous entry: '%s'", msg);
671 priv->input_history_current = item;
677 chat_input_history_update (EmpathyChat *chat,
678 GtkTextBuffer *buffer)
680 EmpathyChatPriv *priv;
681 GtkTextIter start, end;
683 InputHistoryEntry *entry;
685 priv = GET_PRIV (chat);
687 gtk_text_buffer_get_bounds (buffer, &start, &end);
688 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
690 if (priv->input_history_current == NULL) {
691 /* Add the current text temporarily to the history */
692 chat_input_history_add (chat, text, TRUE);
697 /* Save the changes in the history */
698 entry = priv->input_history_current->data;
699 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
700 chat_input_history_entry_update_text (entry, text);
709 } ChatCommandMsgData;
712 chat_command_msg_cb (GObject *source,
713 GAsyncResult *result,
716 ChatCommandMsgData *data = user_data;
717 GError *error = NULL;
720 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
721 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
723 if (channel == NULL) {
724 DEBUG ("Failed to get channel: %s", error->message);
725 g_error_free (error);
727 empathy_theme_adium_append_event (data->chat->view,
728 _("Failed to open private chat"));
732 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
733 TpTextChannel *text = (TpTextChannel *) channel;
736 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
739 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
741 g_object_unref (msg);
744 g_object_unref (channel);
747 g_free (data->message);
748 g_slice_free (ChatCommandMsgData, data);
752 nick_command_supported (EmpathyChat *chat)
754 EmpathyChatPriv * priv = GET_PRIV (chat);
755 TpConnection *connection;
757 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
758 return tp_proxy_has_interface_by_id (connection,
759 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
763 part_command_supported (EmpathyChat *chat)
765 EmpathyChatPriv * priv = GET_PRIV (chat);
767 return tp_proxy_has_interface_by_id (priv->tp_chat,
768 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
772 chat_command_clear (EmpathyChat *chat,
775 empathy_theme_adium_clear (chat->view);
779 chat_command_topic (EmpathyChat *chat,
782 EmpathyChatPriv *priv = GET_PRIV (chat);
784 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
785 empathy_theme_adium_append_event (chat->view,
786 _("Topic not supported on this conversation"));
790 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
791 empathy_theme_adium_append_event (chat->view,
792 _("You are not allowed to change the topic"));
796 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
800 empathy_chat_join_muc (EmpathyChat *chat,
803 EmpathyChatPriv *priv = GET_PRIV (chat);
805 empathy_join_muc (priv->account, room,
806 empathy_get_current_action_time ());
810 chat_command_join (EmpathyChat *chat,
815 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
817 /* FIXME: Ideally we should ask to handle ourself the channel so we can
818 * report the error if any but this is blocked by
819 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
820 while (rooms[i] != NULL) {
821 /* ignore empty strings */
822 if (!EMP_STR_EMPTY (rooms[i])) {
823 empathy_chat_join_muc (chat, rooms[i]);
831 chat_command_part (EmpathyChat *chat,
834 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
838 chat_command_msg_internal (EmpathyChat *chat,
839 const gchar *contact_id,
840 const gchar *message)
842 EmpathyChatPriv *priv = GET_PRIV (chat);
843 ChatCommandMsgData *data;
844 TpAccountChannelRequest *req;
847 request = tp_asv_new (
848 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
849 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
850 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
853 req = tp_account_channel_request_new (priv->account, request,
854 empathy_get_current_action_time ());
856 /* FIXME: We should probably search in members alias. But this
857 * is enough for IRC */
858 data = g_slice_new (ChatCommandMsgData);
860 data->message = g_strdup (message);
862 tp_account_channel_request_ensure_and_observe_channel_async (req,
863 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
865 g_object_unref (req);
866 g_hash_table_unref (request);
870 chat_command_query (EmpathyChat *chat,
873 /* If <message> part is not defined,
874 * strv[2] will be the terminal NULL */
875 chat_command_msg_internal (chat, strv[1], strv[2]);
879 chat_command_msg (EmpathyChat *chat,
882 chat_command_msg_internal (chat, strv[1], strv[2]);
886 callback_for_request_rename (TpProxy *proxy,
889 GObject *weak_object)
892 DEBUG ("Call to RequestRename method failed: %s",error->message);
897 chat_command_nick (EmpathyChat *chat,
900 EmpathyChatPriv *priv = GET_PRIV (chat);
903 proxy = TP_PROXY (tp_account_get_connection (priv->account));
905 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
906 strv[1], callback_for_request_rename, NULL, NULL, NULL);
910 chat_command_me (EmpathyChat *chat,
913 EmpathyChatPriv *priv = GET_PRIV (chat);
915 TpTextChannel *channel;
917 channel = (TpTextChannel *) (priv->tp_chat);
919 if (!tp_text_channel_supports_message_type (channel,
920 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
921 /* Action message are not supported, 'simulate' the action */
924 /* The TpChat can't be ready if it doesn't have the self contact */
925 g_assert (priv->self_contact != NULL);
927 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
929 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
934 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
938 empathy_tp_chat_send (priv->tp_chat, message);
939 g_object_unref (message);
943 chat_command_say (EmpathyChat *chat,
946 EmpathyChatPriv *priv = GET_PRIV (chat);
949 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
951 empathy_tp_chat_send (priv->tp_chat, message);
952 g_object_unref (message);
956 whois_got_contact_cb (GObject *source,
957 GAsyncResult *result,
960 EmpathyChat *chat = user_data;
961 EmpathyContact *contact;
962 FolksIndividual *individual;
964 contact = empathy_client_factory_dup_contact_by_id_finish (
965 EMPATHY_CLIENT_FACTORY (source), result, NULL);
967 if (contact == NULL) {
968 empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
972 individual = empathy_ensure_individual_from_tp_contact (
973 empathy_contact_get_tp_contact (contact));
974 empathy_display_individual_info (individual);
976 g_object_unref (individual);
977 g_object_unref (contact);
980 g_object_unref (chat);
984 chat_command_whois (EmpathyChat *chat,
987 EmpathyChatPriv *priv = GET_PRIV (chat);
989 EmpathyClientFactory *factory;
991 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
992 factory = empathy_client_factory_dup ();
994 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
995 * entered by the user (including spaces, if any). */
996 empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
997 whois_got_contact_cb, g_object_ref (chat));
999 g_object_unref (factory);
1003 chat_command_whale (EmpathyChat *chat,
1006 EmpathyChatPriv *priv = GET_PRIV (chat);
1009 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1011 "•_______________•");
1012 empathy_tp_chat_send (priv->tp_chat, message);
1013 g_object_unref (message);
1017 chat_command_babywhale (EmpathyChat *chat,
1020 EmpathyChatPriv *priv = GET_PRIV (chat);
1023 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1026 empathy_tp_chat_send (priv->tp_chat, message);
1027 g_object_unref (message);
1031 chat_command_inspector (EmpathyChat *chat,
1034 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1035 empathy_theme_adium_show_inspector (
1036 EMPATHY_THEME_ADIUM (chat->view));
1040 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1042 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1045 const gchar *prefix;
1048 ChatCommandFunc func;
1049 gboolean (*is_supported)(EmpathyChat *chat);
1053 static ChatCommandItem commands[] = {
1054 {"clear", 1, 1, chat_command_clear, NULL,
1055 N_("/clear: clear all messages from the current conversation")},
1057 {"topic", 2, 2, chat_command_topic, NULL,
1058 N_("/topic <topic>: set the topic of the current conversation")},
1060 {"join", 2, 2, chat_command_join, NULL,
1061 N_("/join <chat room ID>: join a new chat room")},
1063 {"j", 2, 2, chat_command_join, NULL,
1064 N_("/j <chat room ID>: join a new chat room")},
1066 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1067 {"part", 1, 3, chat_command_part, part_command_supported,
1068 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1069 "by default the current one")},
1071 {"query", 2, 3, chat_command_query, NULL,
1072 N_("/query <contact ID> [<message>]: open a private chat")},
1074 {"msg", 3, 3, chat_command_msg, NULL,
1075 N_("/msg <contact ID> <message>: open a private chat")},
1077 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1078 N_("/nick <nickname>: change your nickname on the current server")},
1080 {"me", 2, 2, chat_command_me, NULL,
1081 N_("/me <message>: send an ACTION message to the current conversation")},
1083 {"say", 2, 2, chat_command_say, NULL,
1084 N_("/say <message>: send <message> to the current conversation. "
1085 "This is used to send a message starting with a '/'. For example: "
1086 "\"/say /join is used to join a new chat room\"")},
1088 {"whois", 2, 2, chat_command_whois, NULL,
1089 N_("/whois <contact ID>: display information about a contact")},
1091 {"help", 1, 2, chat_command_help, NULL,
1092 N_("/help [<command>]: show all supported commands. "
1093 "If <command> is defined, show its usage.")},
1095 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1097 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1098 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1102 chat_command_show_help (EmpathyChat *chat,
1103 ChatCommandItem *item)
1107 if (item->help == NULL) {
1111 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1112 empathy_theme_adium_append_event (chat->view, str);
1117 chat_command_help (EmpathyChat *chat,
1122 /* If <command> part is not defined,
1123 * strv[1] will be the terminal NULL */
1124 if (strv[1] == NULL) {
1125 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1126 if (commands[i].is_supported != NULL) {
1127 if (!commands[i].is_supported (chat)) {
1131 if (commands[i].help == NULL) {
1134 empathy_theme_adium_append_event (chat->view,
1135 _(commands[i].help));
1140 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1141 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1142 if (commands[i].is_supported != NULL) {
1143 if (!commands[i].is_supported (chat)) {
1147 if (commands[i].help == NULL) {
1150 chat_command_show_help (chat, &commands[i]);
1155 empathy_theme_adium_append_event (chat->view,
1156 _("Unknown command"));
1160 chat_command_parse (const gchar *text, guint max_parts)
1165 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1167 array = g_ptr_array_sized_new (max_parts + 1);
1168 while (max_parts > 1) {
1171 /* Skip white spaces */
1172 while (g_ascii_isspace (*text)) {
1176 /* Search the end of this part, until first space. */
1177 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1183 item = g_strndup (text, end - text);
1184 g_ptr_array_add (array, item);
1185 DEBUG ("\tITEM: \"%s\"", item);
1191 /* Append last part if not empty */
1192 item = g_strstrip (g_strdup (text));
1193 if (!EMP_STR_EMPTY (item)) {
1194 g_ptr_array_add (array, item);
1195 DEBUG ("\tITEM: \"%s\"", item);
1200 /* Make the array NULL-terminated */
1201 g_ptr_array_add (array, NULL);
1203 return (GStrv) g_ptr_array_free (array, FALSE);
1207 has_prefix_case (const gchar *s,
1208 const gchar *prefix)
1210 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1214 chat_send (EmpathyChat *chat,
1217 EmpathyChatPriv *priv;
1221 if (EMP_STR_EMPTY (msg)) {
1225 priv = GET_PRIV (chat);
1227 chat_input_history_add (chat, msg, FALSE);
1229 if (msg[0] == '/') {
1230 gboolean second_slash = FALSE;
1231 const gchar *iter = msg + 1;
1233 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1238 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1241 c = *(msg + 1 + strlen (commands[i].prefix));
1242 if (c != '\0' && !g_ascii_isspace (c)) {
1245 if (commands[i].is_supported != NULL) {
1246 if (!commands[i].is_supported (chat)) {
1251 /* We can't use g_strsplit here because it does
1252 * not deal correctly if we have more than one space
1254 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1256 strv_len = g_strv_length (strv);
1257 if (strv_len < commands[i].min_parts ||
1258 strv_len > commands[i].max_parts) {
1259 chat_command_show_help (chat, &commands[i]);
1264 commands[i].func (chat, strv);
1269 /* Also allow messages with two slashes before the
1270 * first space, so it is possible to send a /unix/path.
1271 * This heuristic is kind of crap. */
1272 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1274 second_slash = TRUE;
1280 if (!second_slash) {
1281 empathy_theme_adium_append_event (chat->view,
1282 _("Unknown command; see /help for the available"
1288 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1290 empathy_tp_chat_send (priv->tp_chat, message);
1291 g_object_unref (message);
1295 chat_input_text_view_send (EmpathyChat *chat)
1297 GtkTextBuffer *buffer;
1298 GtkTextIter start, end;
1301 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1303 gtk_text_buffer_get_bounds (buffer, &start, &end);
1304 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1306 /* clear the input field */
1307 gtk_text_buffer_set_text (buffer, "", -1);
1308 /* delete input history modifications */
1309 chat_input_history_revert (chat);
1311 chat_send (chat, msg);
1316 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1317 TpContact *tp_contact,
1318 TpChannelChatState state,
1321 EmpathyChatPriv *priv;
1323 gboolean was_composing;
1324 EmpathyContact *contact;
1326 priv = GET_PRIV (chat);
1328 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1330 if (empathy_contact_is_user (contact)) {
1331 /* We don't care about our own chat state */
1335 was_composing = (priv->compositors != NULL);
1337 /* Find the contact in the list. After that l is the list elem or NULL */
1338 for (l = priv->compositors; l; l = l->next) {
1339 if (contact == l->data) {
1345 case TP_CHANNEL_CHAT_STATE_GONE:
1346 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1347 case TP_CHANNEL_CHAT_STATE_PAUSED:
1348 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1349 /* Contact is not composing */
1351 priv->compositors = g_list_remove_link (priv->compositors, l);
1352 g_object_unref (l->data);
1356 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1357 /* Contact is composing */
1359 priv->compositors = g_list_prepend (priv->compositors,
1360 g_object_ref (contact));
1364 g_assert_not_reached ();
1367 DEBUG ("Was composing: %s now composing: %s",
1368 was_composing ? "yes" : "no",
1369 priv->compositors ? "yes" : "no");
1371 if ((was_composing && !priv->compositors) ||
1372 (!was_composing && priv->compositors)) {
1373 /* Composing state changed */
1374 g_signal_emit (chat, signals[COMPOSING], 0,
1375 priv->compositors != NULL);
1379 g_object_unref (contact);
1383 get_highlight_regex_for (const gchar *name)
1386 gchar *name_esc, *pattern;
1387 GError *error = NULL;
1389 name_esc = g_regex_escape_string (name, -1);
1390 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1391 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1394 if (regex == NULL) {
1395 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1398 g_error_free (error);
1407 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1408 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1411 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1413 EmpathyChatPriv *priv = GET_PRIV (chat);
1415 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1417 if (priv->self_contact != NULL) {
1418 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1420 g_return_if_fail (alias != NULL);
1421 priv->highlight_regex = get_highlight_regex_for (alias);
1426 chat_should_highlight (EmpathyChat *chat,
1427 EmpathyMessage *message)
1429 EmpathyChatPriv *priv = GET_PRIV (chat);
1431 TpChannelTextMessageFlags flags;
1433 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1435 if (!empathy_chat_is_room (chat)) {
1439 if (!empathy_message_is_incoming (message)) {
1443 msg = empathy_message_get_body (message);
1448 flags = empathy_message_get_flags (message);
1449 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1450 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1451 * have already been received by the user before (and so are in the logs) */
1455 if (priv->highlight_regex == NULL) {
1459 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1463 chat_message_received (EmpathyChat *chat,
1464 EmpathyMessage *message,
1467 EmpathyChatPriv *priv = GET_PRIV (chat);
1468 EmpathyContact *sender;
1470 sender = empathy_message_get_sender (message);
1472 if (empathy_message_is_edit (message)) {
1473 DEBUG ("Editing message '%s' to '%s'",
1474 empathy_message_get_supersedes (message),
1475 empathy_message_get_body (message));
1477 empathy_theme_adium_edit_message (chat->view, message);
1479 gboolean should_highlight = chat_should_highlight (chat, message);
1481 if (should_highlight) {
1482 priv->highlighted = TRUE;
1485 DEBUG ("Appending new message '%s' from %s (%d)",
1486 empathy_message_get_token (message),
1487 empathy_contact_get_alias (sender),
1488 empathy_contact_get_handle (sender));
1490 empathy_theme_adium_append_message (chat->view, message, should_highlight);
1492 if (empathy_message_is_incoming (message)) {
1493 priv->unread_messages++;
1494 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1497 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1501 /* We received a message so the contact is no longer
1503 chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
1504 TP_CHANNEL_CHAT_STATE_ACTIVE,
1509 chat_message_received_cb (EmpathyTpChat *tp_chat,
1510 EmpathyMessage *message,
1513 chat_message_received (chat, message, FALSE);
1517 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1518 EmpathyMessage *message,
1521 EmpathyChatPriv *priv = GET_PRIV (chat);
1523 empathy_theme_adium_message_acknowledged (chat->view,
1526 if (!empathy_message_is_edit (message)) {
1527 priv->unread_messages--;
1528 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1533 append_balance_error (EmpathyChat *chat,
1534 const gchar *message_body)
1536 EmpathyChatPriv *priv = GET_PRIV (chat);
1537 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1538 const gchar *uri = tp_connection_get_balance_uri (conn);
1539 const gchar *error = _("insufficient balance to send message");
1540 gchar *str, *str_markup = NULL;
1542 if (message_body != NULL) {
1543 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1545 str = g_strdup_printf (_("Error sending message: %s"), error);
1548 if (!tp_str_empty (uri)) {
1549 /* translators: error used when user doesn't have enough credit on his
1550 * account to send the message. */
1551 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1552 " <a href='%s'>Top up</a>."), uri);
1554 if (message_body != NULL) {
1555 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1557 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1558 escaped_body, markup_error);
1560 g_free (escaped_body);
1562 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1565 g_free (markup_error);
1568 if (str_markup != NULL)
1569 empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
1571 empathy_theme_adium_append_event (chat->view, str);
1574 g_free (str_markup);
1578 chat_send_error_cb (EmpathyTpChat *tp_chat,
1579 const gchar *message_body,
1580 TpChannelTextSendError error_code,
1581 const gchar *dbus_error,
1584 const gchar *error = NULL;
1587 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1588 append_balance_error (chat, message_body);
1590 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1591 error = _("not capable");
1594 if (error == NULL) {
1595 /* if we didn't find a dbus-error, try the old error */
1596 switch (error_code) {
1597 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1598 error = _("offline");
1600 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1601 error = _("invalid contact");
1603 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1604 error = _("permission denied");
1606 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1607 error = _("too long message");
1609 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1610 error = _("not implemented");
1612 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1614 error = _("unknown");
1619 if (message_body != NULL) {
1620 str = g_strdup_printf (_("Error sending message '%s': %s"),
1621 message_body, error);
1624 str = g_strdup_printf (_("Error sending message: %s"), error);
1627 empathy_theme_adium_append_event (chat->view, str);
1632 chat_topic_label_size_allocate_cb (GtkLabel *label,
1633 GtkAllocation *allocation,
1636 EmpathyChatPriv *priv = GET_PRIV (chat);
1638 if (!gtk_label_get_line_wrap (label)) {
1639 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1640 gtk_widget_show (priv->expander_topic);
1642 gtk_widget_hide (priv->expander_topic);
1649 chat_topic_expander_activate_cb (GtkExpander *expander,
1650 GParamSpec *param_spec,
1653 EmpathyChatPriv *priv = GET_PRIV (chat);
1655 if (gtk_expander_get_expanded (expander)) {
1656 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1657 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1659 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1660 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1665 chat_subject_changed_cb (EmpathyChat *chat)
1667 EmpathyChatPriv *priv = GET_PRIV (chat);
1669 g_free (priv->subject);
1670 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1671 g_object_notify (G_OBJECT (chat), "subject");
1673 if (EMP_STR_EMPTY (priv->subject)) {
1674 gtk_widget_hide (priv->hbox_topic);
1676 gchar *markup_topic;
1679 markup_topic = empathy_add_link_markup (priv->subject);
1680 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1681 _("Topic:"), markup_topic);
1683 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1684 g_free (markup_text);
1685 g_free (markup_topic);
1687 gtk_widget_show (priv->hbox_topic);
1689 if (priv->block_events_timeout_id == 0) {
1692 if (!EMP_STR_EMPTY (priv->subject)) {
1693 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1695 if (tp_str_empty (actor)) {
1696 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1698 str = g_strdup_printf (_("Topic set by %s to: %s"),
1699 actor, priv->subject);
1701 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1702 /* No need to display this 'event' is no topic can be defined anyway */
1703 str = g_strdup (_("No topic defined"));
1707 empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
1714 chat_title_changed_cb (EmpathyChat *chat)
1716 EmpathyChatPriv *priv = GET_PRIV (chat);
1718 g_free (priv->name);
1719 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1720 g_object_notify (G_OBJECT (chat), "name");
1724 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1728 GtkTextIter word_start = *iter;
1729 GtkTextIter word_end = *iter;
1732 if (gtk_text_iter_inside_word (&word_end) &&
1733 !gtk_text_iter_ends_word (&word_end)) {
1734 gtk_text_iter_forward_word_end (&word_end);
1739 if (gtk_text_iter_get_char (&tmp) == '\'') {
1740 gtk_text_iter_forward_char (&tmp);
1742 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1743 gtk_text_iter_forward_word_end (&word_end);
1748 if (gtk_text_iter_inside_word (&word_start) ||
1749 gtk_text_iter_ends_word (&word_start)) {
1750 if (!gtk_text_iter_starts_word (&word_start) ||
1751 gtk_text_iter_equal (&word_start, &word_end)) {
1752 gtk_text_iter_backward_word_start (&word_start);
1756 gtk_text_iter_backward_char (&tmp);
1758 if (gtk_text_iter_get_char (&tmp) == '\'') {
1759 gtk_text_iter_backward_char (&tmp);
1761 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1762 gtk_text_iter_backward_word_start (&word_start);
1767 *start = word_start;
1773 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1774 GtkTextIter *location,
1779 GtkTextIter iter, pos;
1781 /* Remove all misspelled tags in the inserted text.
1782 * This happens when text is inserted within a misspelled word. */
1783 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1784 gtk_text_iter_get_offset (location) - len);
1785 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1788 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1791 GtkTextIter start, end;
1794 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1797 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1799 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1800 gtk_text_iter_equal (&pos, &end) ||
1801 empathy_spell_check (str)) {
1802 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1804 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1809 } while (gtk_text_iter_forward_word_end (&iter) &&
1810 gtk_text_iter_compare (&iter, location) <= 0);
1814 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1819 GtkTextIter word_start, word_end;
1821 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1822 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1823 &word_start, &word_end);
1828 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1831 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1832 chat_composing_stop (chat);
1834 chat_composing_start (chat);
1839 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1844 GtkTextIter prev_pos;
1845 GtkTextIter word_start;
1846 GtkTextIter word_end;
1850 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1852 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1853 gtk_text_buffer_get_insert (buffer));
1854 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1856 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1859 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1860 !gtk_text_iter_equal (&pos, &word_end)) {
1861 str = gtk_text_buffer_get_text (buffer,
1862 &word_start, &word_end, FALSE);
1864 if (!empathy_spell_check (str)) {
1865 gtk_text_buffer_apply_tag_by_name (buffer,
1866 "misspelled", &word_start, &word_end);
1868 gtk_text_buffer_remove_tag_by_name (buffer,
1869 "misspelled", &word_start, &word_end);
1876 gtk_text_buffer_move_mark (buffer, mark, &pos);
1880 empathy_isspace_cb (gunichar c,
1883 return g_unichar_isspace (c);
1887 chat_input_key_press_event_cb (GtkWidget *widget,
1891 EmpathyChatPriv *priv;
1894 GtkWidget *text_view_sw;
1896 priv = GET_PRIV (chat);
1898 priv->most_recent_event_type = event->type;
1900 /* Catch ctrl+up/down so we can traverse messages we sent */
1901 if ((event->state & GDK_CONTROL_MASK) &&
1902 (event->keyval == GDK_KEY_Up ||
1903 event->keyval == GDK_KEY_Down)) {
1904 GtkTextBuffer *buffer;
1907 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1908 chat_input_history_update (chat, buffer);
1910 if (event->keyval == GDK_KEY_Up) {
1911 str = chat_input_history_get_next (chat);
1913 str = chat_input_history_get_prev (chat);
1916 g_signal_handlers_block_by_func (buffer,
1917 chat_input_text_buffer_changed_cb,
1919 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1920 g_signal_handlers_unblock_by_func (buffer,
1921 chat_input_text_buffer_changed_cb,
1927 /* Catch enter but not ctrl/shift-enter */
1928 if (IS_ENTER (event->keyval) &&
1929 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1932 /* This is to make sure that kinput2 gets the enter. And if
1933 * it's handled there we shouldn't send on it. This is because
1934 * kinput2 uses Enter to commit letters. See:
1935 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1938 view = GTK_TEXT_VIEW (chat->input_text_view);
1939 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1940 gtk_text_view_reset_im_context (view);
1944 chat_input_text_view_send (chat);
1948 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1950 if (IS_ENTER (event->keyval) &&
1951 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1952 /* Newline for shift/control-enter. */
1955 if (!(event->state & GDK_CONTROL_MASK) &&
1956 event->keyval == GDK_KEY_Page_Up) {
1957 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1958 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1961 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1962 event->keyval == GDK_KEY_Page_Down) {
1963 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1964 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1965 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1966 gtk_adjustment_set_value (adj, val);
1969 if (event->keyval == GDK_KEY_Escape) {
1970 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1972 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1973 event->keyval == GDK_KEY_Tab) {
1974 GtkTextBuffer *buffer;
1975 GtkTextIter start, current;
1976 gchar *nick, *completed;
1977 GList *list, *completed_list;
1978 gboolean is_start_of_buffer;
1980 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1981 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1983 /* Get the start of the nick to complete. */
1984 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1985 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1986 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1988 is_start_of_buffer = gtk_text_iter_is_start (&start);
1990 list = empathy_tp_chat_get_members (priv->tp_chat);
1991 g_completion_add_items (priv->completion, list);
1993 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1994 completed_list = g_completion_complete (priv->completion,
2003 GString *message = NULL;
2006 gtk_text_buffer_delete (buffer, &start, ¤t);
2008 len = g_list_length (completed_list);
2011 /* If we only have one hit, use that text
2012 * instead of the text in completed since the
2013 * completed text will use the typed string
2014 * which might be cased all wrong.
2017 text = empathy_contact_get_alias (completed_list->data);
2021 /* Print all hits to the scrollback view, so the
2022 * user knows what possibilities he has.
2025 message = g_string_new ("");
2026 for (l = completed_list; l != NULL; l = l->next) {
2027 g_string_append (message, empathy_contact_get_alias (l->data));
2028 g_string_append (message, " - ");
2030 empathy_theme_adium_append_event (chat->view, message->str);
2031 g_string_free (message, TRUE);
2034 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2036 if (len == 1 && is_start_of_buffer) {
2037 gchar *complete_char;
2039 complete_char = g_settings_get_string (
2040 priv->gsettings_chat,
2041 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2043 if (complete_char != NULL) {
2044 gtk_text_buffer_insert_at_cursor (buffer,
2046 strlen (complete_char));
2047 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2048 g_free (complete_char);
2055 g_completion_clear_items (priv->completion);
2057 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2067 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2071 gtk_widget_grab_focus (chat->input_text_view);
2077 chat_input_realize_cb (GtkWidget *widget,
2080 DEBUG ("Setting focus to the input text view");
2081 if (gtk_widget_is_sensitive (widget)) {
2082 gtk_widget_grab_focus (widget);
2087 chat_input_has_focus_notify_cb (GtkWidget *widget,
2091 empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2095 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2096 EmpathySmiley *smiley,
2099 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2100 GtkTextBuffer *buffer;
2103 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2105 gtk_text_buffer_get_end_iter (buffer, &iter);
2106 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2108 gtk_text_buffer_get_end_iter (buffer, &iter);
2109 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2120 static EmpathyChatSpell *
2121 chat_spell_new (EmpathyChat *chat,
2126 EmpathyChatSpell *chat_spell;
2128 chat_spell = g_slice_new0 (EmpathyChatSpell);
2130 chat_spell->chat = g_object_ref (chat);
2131 chat_spell->word = g_strdup (word);
2132 chat_spell->start = start;
2133 chat_spell->end = end;
2139 chat_spell_free (EmpathyChatSpell *chat_spell)
2141 g_object_unref (chat_spell->chat);
2142 g_free (chat_spell->word);
2143 g_slice_free (EmpathyChatSpell, chat_spell);
2147 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2148 EmpathyChatSpell *chat_spell)
2150 empathy_chat_correct_word (chat_spell->chat,
2151 &(chat_spell->start),
2153 gtk_menu_item_get_label (menu_item));
2158 chat_spelling_build_suggestions_menu (const gchar *code,
2159 EmpathyChatSpell *chat_spell)
2161 GList *suggestions, *l;
2162 GtkWidget *menu, *menu_item;
2164 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2165 if (suggestions == NULL)
2168 menu = gtk_menu_new ();
2169 for (l = suggestions; l; l = l->next) {
2170 menu_item = gtk_menu_item_new_with_label (l->data);
2171 g_signal_connect (G_OBJECT (menu_item), "activate",
2172 G_CALLBACK (chat_spelling_menu_activate_cb),
2174 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2176 empathy_spell_free_suggestions (suggestions);
2178 gtk_widget_show_all (menu);
2184 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2186 GtkWidget *menu, *submenu, *item;
2189 codes = empathy_spell_get_enabled_language_codes ();
2190 g_assert (codes != NULL);
2192 if (g_list_length (codes) > 1) {
2193 menu = gtk_menu_new ();
2195 for (l = codes; l; l = l->next) {
2196 const gchar *code, *name;
2199 name = empathy_spell_get_language_name (code);
2203 item = gtk_image_menu_item_new_with_label (name);
2205 submenu = chat_spelling_build_suggestions_menu (
2207 if (submenu == NULL)
2208 gtk_widget_set_sensitive (item, FALSE);
2210 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2212 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2215 menu = chat_spelling_build_suggestions_menu (codes->data,
2218 menu = gtk_menu_new ();
2219 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2220 gtk_widget_set_sensitive (item, FALSE);
2221 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2224 g_list_free (codes);
2226 gtk_widget_show_all (menu);
2237 static EmpathyChatWord *
2238 chat_word_new (EmpathyChat *chat,
2242 EmpathyChatWord *chat_word;
2244 chat_word = g_slice_new0 (EmpathyChatWord);
2246 chat_word->chat = g_object_ref (chat);
2247 chat_word->word = g_strdup (word);
2248 chat_word->code = g_strdup (code);
2254 chat_word_free (EmpathyChatWord *chat_word)
2256 g_object_unref (chat_word->chat);
2257 g_free (chat_word->word);
2258 g_free (chat_word->code);
2259 g_slice_free (EmpathyChatWord, chat_word);
2263 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2264 EmpathyChatWord *chat_word)
2266 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2268 empathy_spell_add_to_dictionary (chat_word->code,
2270 priv->update_misspelled_words_id =
2271 g_idle_add (update_misspelled_words, chat_word->chat);
2275 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2277 GtkWidget *menu, *item, *lang_item, *image;
2280 const gchar *code, *name;
2281 EmpathyChatWord *chat_word;
2283 codes = empathy_spell_get_enabled_language_codes ();
2284 g_assert (codes != NULL);
2285 if (g_list_length (codes) > 1) {
2286 /* translators: %s is the selected word */
2287 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2289 item = gtk_image_menu_item_new_with_mnemonic (label);
2291 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2292 GTK_ICON_SIZE_MENU);
2293 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2296 menu = gtk_menu_new ();
2298 for (l = codes; l; l = l->next) {
2300 name = empathy_spell_get_language_name (code);
2304 lang_item = gtk_image_menu_item_new_with_label (name);
2306 chat_word= chat_word_new (chat_spell->chat,
2307 chat_spell->word, code);
2308 g_object_set_data_full (G_OBJECT (lang_item),
2309 "chat-word", chat_word,
2310 (GDestroyNotify) chat_word_free);
2312 g_signal_connect (G_OBJECT (lang_item), "activate",
2313 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2315 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2317 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2320 name = empathy_spell_get_language_name (code);
2321 g_assert (name != NULL);
2322 /* translators: first %s is the selected word,
2323 * second %s is the language name of the target dictionary */
2324 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2325 chat_spell->word, name);
2326 item = gtk_image_menu_item_new_with_mnemonic (label);
2328 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2329 GTK_ICON_SIZE_MENU);
2330 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2332 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2334 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2335 (GDestroyNotify) chat_word_free);
2337 g_signal_connect (G_OBJECT (item), "activate",
2338 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2341 g_list_free (codes);
2343 gtk_widget_show_all (item);
2349 chat_text_send_cb (GtkMenuItem *menuitem,
2352 chat_input_text_view_send (chat);
2356 chat_input_button_press_event_cb (GtkTextView *view,
2357 GdkEventButton *event,
2360 EmpathyChatPriv *priv = GET_PRIV (chat);
2362 priv->most_recent_event_type = event->type;
2368 chat_input_populate_popup_cb (GtkTextView *view,
2372 EmpathyChatPriv *priv = GET_PRIV (chat);
2373 GtkTextBuffer *buffer;
2374 GtkTextTagTable *table;
2377 GtkTextIter iter, start, end;
2380 EmpathyChatSpell *chat_spell;
2381 GtkWidget *spell_menu;
2382 GtkWidget *spell_item;
2383 EmpathySmileyManager *smiley_manager;
2384 GtkWidget *smiley_menu;
2387 buffer = gtk_text_view_get_buffer (view);
2389 /* Add the emoticon menu. */
2390 item = gtk_separator_menu_item_new ();
2391 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2392 gtk_widget_show (item);
2394 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2395 image = gtk_image_new_from_icon_name ("face-smile",
2396 GTK_ICON_SIZE_MENU);
2397 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2398 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2399 gtk_widget_show (item);
2401 smiley_manager = empathy_smiley_manager_dup_singleton ();
2402 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2403 chat_insert_smiley_activate_cb,
2405 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2406 g_object_unref (smiley_manager);
2408 /* Add the Send menu item. */
2409 gtk_text_buffer_get_bounds (buffer, &start, &end);
2410 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2411 if (!EMP_STR_EMPTY (str)) {
2412 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2413 g_signal_connect (G_OBJECT (item), "activate",
2414 G_CALLBACK (chat_text_send_cb), chat);
2415 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2416 gtk_widget_show (item);
2420 /* Add the spell check menu item. */
2421 table = gtk_text_buffer_get_tag_table (buffer);
2422 tag = gtk_text_tag_table_lookup (table, "misspelled");
2424 switch (priv->most_recent_event_type) {
2425 case GDK_BUTTON_PRESS:
2426 /* get the location from the pointer */
2427 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2428 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2429 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2431 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2432 GTK_TEXT_WINDOW_WIDGET,
2435 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2440 g_warn_if_reached ();
2441 /* assume the KEY_PRESS case */
2444 /* get the location from the cursor */
2445 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2446 gtk_text_buffer_get_insert (buffer));
2452 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2453 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2455 str = gtk_text_buffer_get_text (buffer,
2456 &start, &end, FALSE);
2458 if (!EMP_STR_EMPTY (str)) {
2459 chat_spell = chat_spell_new (chat, str, start, end);
2460 g_object_set_data_full (G_OBJECT (menu),
2461 "chat-spell", chat_spell,
2462 (GDestroyNotify) chat_spell_free);
2464 item = gtk_separator_menu_item_new ();
2465 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2466 gtk_widget_show (item);
2468 /* Spelling suggestions */
2469 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2470 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2471 GTK_ICON_SIZE_MENU);
2472 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2474 spell_menu = chat_spelling_build_menu (chat_spell);
2475 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2478 spell_item = gtk_separator_menu_item_new ();
2479 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2480 gtk_widget_show (spell_item);
2482 /* Add to dictionary */
2483 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2485 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2486 gtk_widget_show (spell_item);
2488 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2489 gtk_widget_show (item);
2495 chat_log_filter (TplEvent *event,
2498 TpWeakRef *wr = user_data;
2499 EmpathyChat *chat = tp_weak_ref_dup_object (wr);
2500 EmpathyMessage *message;
2501 EmpathyChatPriv *priv;
2502 const GList *pending;
2503 bool retval = FALSE;
2508 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2509 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2511 priv = GET_PRIV (chat);
2513 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2514 message = empathy_message_from_tpl_log_event (event);
2516 for (; pending; pending = g_list_next (pending)) {
2517 if (empathy_message_equal (message, pending->data))
2524 g_object_unref (message);
2525 g_object_unref (chat);
2531 show_pending_messages (EmpathyChat *chat) {
2532 EmpathyChatPriv *priv = GET_PRIV (chat);
2533 const GList *messages, *l;
2535 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2537 if (chat->view == NULL || priv->tp_chat == NULL)
2540 if (!priv->can_show_pending)
2543 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2545 for (l = messages; l != NULL ; l = g_list_next (l)) {
2546 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2547 chat_message_received (chat, message, TRUE);
2553 got_filtered_messages_cb (GObject *manager,
2554 GAsyncResult *result,
2559 TpWeakRef *wr = user_data;
2560 EmpathyChat *chat = tp_weak_ref_dup_object (wr);
2561 EmpathyChatPriv *priv;
2562 GError *error = NULL;
2565 tp_weak_ref_destroy (wr);
2569 priv = GET_PRIV (chat);
2571 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2572 result, &messages, &error)) {
2573 DEBUG ("%s. Aborting.", error->message);
2574 empathy_theme_adium_append_event (chat->view,
2575 _("Failed to retrieve recent logs"));
2576 g_error_free (error);
2580 for (l = messages; l; l = g_list_next (l)) {
2581 EmpathyMessage *message;
2583 g_assert (TPL_IS_EVENT (l->data));
2585 message = empathy_message_from_tpl_log_event (l->data);
2586 g_object_unref (l->data);
2588 if (empathy_message_is_edit (message)) {
2589 /* this is an edited message, create a synthetic event
2590 * using the supersedes token and
2591 * original-message-sent timestamp, that we can then
2593 EmpathyMessage *syn_msg = g_object_new (
2594 EMPATHY_TYPE_MESSAGE,
2596 "token", empathy_message_get_supersedes (message),
2597 "type", empathy_message_get_tptype (message),
2598 "timestamp", empathy_message_get_original_timestamp (message),
2599 "incoming", empathy_message_is_incoming (message),
2601 "receiver", empathy_message_get_receiver (message),
2602 "sender", empathy_message_get_sender (message),
2605 empathy_theme_adium_append_message (chat->view, syn_msg,
2606 chat_should_highlight (chat, syn_msg));
2607 empathy_theme_adium_edit_message (chat->view, message);
2609 g_object_unref (syn_msg);
2611 /* append the latest message */
2612 empathy_theme_adium_append_message (chat->view, message,
2613 chat_should_highlight (chat, message));
2616 g_object_unref (message);
2618 g_list_free (messages);
2621 /* in case of TPL error, skip backlog and show pending messages */
2622 priv->can_show_pending = TRUE;
2623 show_pending_messages (chat);
2625 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2626 * about it in EmpathyChatPriv definition */
2627 priv->retrieving_backlogs = FALSE;
2628 empathy_chat_messages_read (chat);
2630 /* Turn back on scrolling */
2631 empathy_theme_adium_scroll (chat->view, TRUE);
2633 g_object_unref (chat);
2634 tp_weak_ref_destroy (wr);
2638 chat_add_logs (EmpathyChat *chat)
2640 EmpathyChatPriv *priv = GET_PRIV (chat);
2648 /* Turn off scrolling temporarily */
2649 empathy_theme_adium_scroll (chat->view, FALSE);
2651 /* Add messages from last conversation */
2652 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2653 target = tpl_entity_new_from_room_id (priv->id);
2655 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2657 priv->retrieving_backlogs = TRUE;
2658 wr = tp_weak_ref_new (chat, NULL, NULL);
2659 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2662 TPL_EVENT_MASK_TEXT,
2666 got_filtered_messages_cb,
2669 g_object_unref (target);
2673 chat_contacts_completion_func (const gchar *s1,
2677 gchar *tmp, *nick1, *nick2;
2684 return s1 ? -1 : +1;
2687 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2688 nick1 = g_utf8_casefold (tmp, -1);
2691 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2692 nick2 = g_utf8_casefold (tmp, -1);
2695 ret = strncmp (nick1, nick2, n);
2704 build_part_message (guint reason,
2706 EmpathyContact *actor,
2707 const gchar *message)
2709 GString *s = g_string_new ("");
2710 const gchar *actor_name = NULL;
2712 if (actor != NULL) {
2713 actor_name = empathy_contact_get_alias (actor);
2716 /* Having an actor only really makes sense for a few actions... */
2718 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2719 g_string_append_printf (s, _("%s has disconnected"), name);
2721 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2722 if (actor_name != NULL) {
2723 /* translators: reverse the order of these arguments
2724 * if the kicked should come before the kicker in your locale.
2726 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2729 g_string_append_printf (s, _("%s was kicked"), name);
2732 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2733 if (actor_name != NULL) {
2734 /* translators: reverse the order of these arguments
2735 * if the banned should come before the banner in your locale.
2737 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2740 g_string_append_printf (s, _("%s was banned"), name);
2744 g_string_append_printf (s, _("%s has left the room"), name);
2747 if (!EMP_STR_EMPTY (message)) {
2748 /* Note to translators: this string is appended to
2749 * notifications like "foo has left the room", with the message
2750 * given by the user living the room. If this poses a problem,
2751 * please let us know. :-)
2753 g_string_append_printf (s, _(" (%s)"), message);
2756 return g_string_free (s, FALSE);
2760 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2761 EmpathyContact *contact,
2762 EmpathyContact *actor,
2768 EmpathyChatPriv *priv = GET_PRIV (chat);
2769 const gchar *name = empathy_contact_get_alias (contact);
2772 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2774 if (priv->block_events_timeout_id != 0)
2778 str = g_strdup_printf (_("%s has joined the room"),
2781 str = build_part_message (reason, name, actor, message);
2784 empathy_theme_adium_append_event (chat->view, str);
2789 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2790 EmpathyContact *old_contact,
2791 EmpathyContact *new_contact,
2796 EmpathyChatPriv *priv = GET_PRIV (chat);
2798 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2800 if (priv->block_events_timeout_id == 0) {
2803 str = g_strdup_printf (_("%s is now known as %s"),
2804 empathy_contact_get_alias (old_contact),
2805 empathy_contact_get_alias (new_contact));
2806 empathy_theme_adium_append_event (chat->view, str);
2813 chat_contacts_visible_timeout_cb (gpointer chat)
2815 EmpathyChatPriv *priv = GET_PRIV (chat);
2817 /* Relax the size request */
2818 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2820 /* Set the position of the slider. This must be done here because
2821 * GtkPaned need to know its size allocation and it will be settled only
2822 * after the gtk_window_resize () tough effect. */
2823 if (priv->contacts_width > 0) {
2824 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2825 priv->contacts_width);
2828 priv->contacts_visible_id = 0;
2834 chat_update_contacts_visibility (EmpathyChat *chat,
2837 EmpathyChatPriv *priv = GET_PRIV (chat);
2839 if (!priv->scrolled_window_contacts) {
2843 if (priv->remote_contact != NULL) {
2847 if (show && priv->contact_list_view == NULL) {
2848 EmpathyIndividualStore *store;
2850 GtkAllocation allocation;
2852 /* We are adding the contact list to the chat, we don't want the
2853 * chat view to become too small. If the chat view is already
2854 * smaller than 250 make sure that size won't change. If the
2855 * chat view is bigger the contact list will take some space on
2856 * it but we make sure the chat view don't become smaller than
2857 * 250. Relax the size request once the resize is done */
2858 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2859 min_width = MIN (allocation.width, 250);
2860 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2862 /* There is no way to know when the window resize will happen
2863 * since it is WM's decision. Let's hope it won't be longer. */
2864 if (priv->contacts_visible_id != 0)
2865 g_source_remove (priv->contacts_visible_id);
2866 priv->contacts_visible_id = g_timeout_add (500,
2867 chat_contacts_visible_timeout_cb, chat);
2869 store = EMPATHY_INDIVIDUAL_STORE (
2870 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2872 empathy_individual_store_set_show_groups (store, FALSE);
2874 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2875 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2876 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2877 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2878 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2879 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2880 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2882 empathy_individual_view_set_show_offline (
2883 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2884 empathy_individual_view_set_show_uninteresting (
2885 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2887 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2888 priv->contact_list_view);
2890 gtk_widget_show (priv->contact_list_view);
2891 gtk_widget_show (priv->scrolled_window_contacts);
2892 g_object_unref (store);
2894 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2895 gtk_widget_hide (priv->scrolled_window_contacts);
2896 if (priv->contact_list_view != NULL) {
2897 gtk_widget_destroy (priv->contact_list_view);
2898 priv->contact_list_view = NULL;
2904 empathy_chat_set_show_contacts (EmpathyChat *chat,
2907 EmpathyChatPriv *priv = GET_PRIV (chat);
2909 priv->show_contacts = show;
2911 chat_update_contacts_visibility (chat, show);
2913 g_object_notify (G_OBJECT (chat), "show-contacts");
2917 chat_self_contact_changed_cb (EmpathyChat *chat)
2919 EmpathyChatPriv *priv = GET_PRIV (chat);
2921 if (priv->self_contact != NULL) {
2922 g_signal_handlers_disconnect_by_func (priv->self_contact,
2923 chat_self_contact_alias_changed_cb,
2926 g_clear_object (&priv->self_contact);
2928 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2929 if (priv->self_contact != NULL) {
2930 g_object_ref (priv->self_contact);
2932 if (empathy_chat_is_room (chat)) {
2933 g_signal_connect_swapped (priv->self_contact, "notify::alias",
2934 G_CALLBACK (chat_self_contact_alias_changed_cb),
2939 chat_self_contact_alias_changed_cb (chat);
2943 chat_remote_contact_changed_cb (EmpathyChat *chat)
2945 EmpathyChatPriv *priv = GET_PRIV (chat);
2947 if (priv->remote_contact != NULL) {
2948 g_object_unref (priv->remote_contact);
2949 priv->remote_contact = NULL;
2954 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2955 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2956 if (priv->remote_contact != NULL) {
2957 g_object_ref (priv->remote_contact);
2958 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2960 else if (priv->tp_chat != NULL) {
2961 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2964 chat_update_contacts_visibility (chat, priv->show_contacts);
2966 g_object_notify (G_OBJECT (chat), "remote-contact");
2967 g_object_notify (G_OBJECT (chat), "id");
2971 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2977 EmpathyChatPriv *priv;
2979 priv = GET_PRIV (chat);
2981 if (!priv->tp_chat) {
2985 chat_composing_remove_timeout (chat);
2986 g_object_unref (priv->tp_chat);
2987 priv->tp_chat = NULL;
2988 g_object_notify (G_OBJECT (chat), "tp-chat");
2990 empathy_theme_adium_append_event (chat->view, _("Disconnected"));
2991 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2993 chat_update_contacts_visibility (chat, FALSE);
2995 priv->unread_messages_when_offline = priv->unread_messages;
2999 update_misspelled_words (gpointer data)
3001 EmpathyChat *chat = EMPATHY_CHAT (data);
3002 EmpathyChatPriv *priv = GET_PRIV (chat);
3003 GtkTextBuffer *buffer;
3007 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3009 gtk_text_buffer_get_end_iter (buffer, &iter);
3010 length = gtk_text_iter_get_offset (&iter);
3011 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3012 NULL, length, chat);
3014 priv->update_misspelled_words_id = 0;
3020 conf_spell_checking_cb (GSettings *gsettings_chat,
3024 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3025 EmpathyChatPriv *priv = GET_PRIV (chat);
3026 gboolean spell_checker;
3027 GtkTextBuffer *buffer;
3029 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3032 spell_checker = g_settings_get_boolean (gsettings_chat,
3033 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3035 if (!empathy_spell_supported ()) {
3036 spell_checker = FALSE;
3039 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3041 if (spell_checker == priv->spell_checking_enabled) {
3042 if (spell_checker) {
3043 /* Possibly changed dictionaries,
3044 * update misspelled words. Need to do so in idle
3045 * so the spell checker is updated. */
3046 priv->update_misspelled_words_id =
3047 g_idle_add (update_misspelled_words, chat);
3053 if (spell_checker) {
3056 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3057 buffer, "notify::cursor-position",
3058 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3060 priv->insert_text_id = tp_g_signal_connect_object (
3061 buffer, "insert-text",
3062 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3063 chat, G_CONNECT_AFTER);
3064 priv->delete_range_id = tp_g_signal_connect_object (
3065 buffer, "delete-range",
3066 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3067 chat, G_CONNECT_AFTER);
3069 gtk_text_buffer_create_tag (buffer, "misspelled",
3070 "underline", PANGO_UNDERLINE_ERROR,
3073 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3074 gtk_text_buffer_get_insert (buffer));
3075 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3078 /* Mark misspelled words in the existing buffer.
3079 * Need to do so in idle so the spell checker is updated. */
3080 priv->update_misspelled_words_id =
3081 g_idle_add (update_misspelled_words, chat);
3083 GtkTextTagTable *table;
3086 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3087 priv->notify_cursor_position_id = 0;
3088 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3089 priv->insert_text_id = 0;
3090 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3091 priv->delete_range_id = 0;
3093 table = gtk_text_buffer_get_tag_table (buffer);
3094 tag = gtk_text_tag_table_lookup (table, "misspelled");
3095 gtk_text_tag_table_remove (table, tag);
3097 gtk_text_buffer_delete_mark_by_name (buffer,
3098 "previous-cursor-position");
3101 priv->spell_checking_enabled = spell_checker;
3105 save_paned_pos_timeout (gpointer data)
3107 EmpathyChat *self = data;
3110 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3112 g_settings_set_int (self->priv->gsettings_ui,
3113 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3120 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3124 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3126 if (chat->priv->save_paned_pos_id != 0)
3127 g_source_remove (chat->priv->save_paned_pos_id);
3129 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3130 save_paned_pos_timeout, chat);
3136 chat_create_ui (EmpathyChat *chat)
3138 EmpathyChatPriv *priv = GET_PRIV (chat);
3142 GtkTextBuffer *buffer;
3143 EmpathyThemeManager *theme_mgr;
3145 filename = empathy_file_lookup ("empathy-chat.ui",
3147 gui = empathy_builder_get_file (filename,
3148 "chat_widget", &priv->widget,
3149 "hpaned", &priv->hpaned,
3150 "vbox_left", &priv->vbox_left,
3151 "scrolled_window_chat", &priv->scrolled_window_chat,
3152 "scrolled_window_input", &priv->scrolled_window_input,
3153 "hbox_topic", &priv->hbox_topic,
3154 "expander_topic", &priv->expander_topic,
3155 "label_topic", &priv->label_topic,
3156 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3157 "info_bar_vbox", &priv->info_bar_vbox,
3160 empathy_builder_connect (gui, chat,
3161 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3162 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3167 /* Add message view. */
3168 theme_mgr = empathy_theme_manager_dup_singleton ();
3169 chat->view = empathy_theme_manager_create_view (theme_mgr);
3170 g_object_unref (theme_mgr);
3171 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3172 and other types, even though it's non-editable and doesn't accept any
3173 drags. This steals drag motion for anything inside the scrollbars,
3174 making drag destinations on chat windows far less useful.
3176 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3177 g_signal_connect (chat->view, "focus_in_event",
3178 G_CALLBACK (chat_text_view_focus_in_event_cb),
3180 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3181 GTK_WIDGET (chat->view));
3182 gtk_widget_show (GTK_WIDGET (chat->view));
3184 /* Add input GtkTextView */
3185 chat->input_text_view = empathy_input_text_view_new ();
3186 g_signal_connect (chat->input_text_view, "notify::has-focus",
3187 G_CALLBACK (chat_input_has_focus_notify_cb),
3189 g_signal_connect (chat->input_text_view, "key-press-event",
3190 G_CALLBACK (chat_input_key_press_event_cb),
3192 g_signal_connect (chat->input_text_view, "realize",
3193 G_CALLBACK (chat_input_realize_cb),
3195 g_signal_connect (chat->input_text_view, "button-press-event",
3196 G_CALLBACK (chat_input_button_press_event_cb),
3198 g_signal_connect (chat->input_text_view, "populate-popup",
3199 G_CALLBACK (chat_input_populate_popup_cb),
3201 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3202 tp_g_signal_connect_object (buffer, "changed",
3203 G_CALLBACK (chat_input_text_buffer_changed_cb),
3205 tp_g_signal_connect_object (priv->gsettings_chat,
3206 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3207 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3208 conf_spell_checking_cb (priv->gsettings_chat,
3209 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3210 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3211 chat->input_text_view);
3212 gtk_widget_show (chat->input_text_view);
3214 /* Add the (invisible) search bar */
3215 priv->search_bar = empathy_search_bar_new (chat->view);
3216 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3219 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3221 /* Initialy hide the topic, will be shown if not empty */
3222 gtk_widget_hide (priv->hbox_topic);
3224 g_signal_connect (priv->hpaned, "notify::position",
3225 G_CALLBACK (chat_hpaned_pos_changed_cb),
3228 /* Set widget focus order */
3229 list = g_list_append (NULL, priv->search_bar);
3230 list = g_list_append (list, priv->scrolled_window_input);
3231 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3234 list = g_list_append (NULL, priv->vbox_left);
3235 list = g_list_append (list, priv->scrolled_window_contacts);
3236 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3239 list = g_list_append (NULL, priv->hpaned);
3240 list = g_list_append (list, priv->hbox_topic);
3241 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3244 /* Add the main widget in the chat widget */
3245 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3246 g_object_unref (gui);
3250 chat_finalize (GObject *object)
3253 EmpathyChatPriv *priv;
3255 chat = EMPATHY_CHAT (object);
3256 priv = GET_PRIV (chat);
3258 DEBUG ("Finalized: %p", object);
3260 if (priv->update_misspelled_words_id != 0)
3261 g_source_remove (priv->update_misspelled_words_id);
3263 if (priv->save_paned_pos_id != 0)
3264 g_source_remove (priv->save_paned_pos_id);
3266 if (priv->contacts_visible_id != 0)
3267 g_source_remove (priv->contacts_visible_id);
3269 g_object_unref (priv->gsettings_chat);
3270 g_object_unref (priv->gsettings_ui);
3272 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3273 g_list_free (priv->input_history);
3275 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3276 g_list_free (priv->compositors);
3278 chat_composing_remove_timeout (chat);
3280 g_object_unref (priv->account_manager);
3281 g_object_unref (priv->log_manager);
3283 if (priv->tp_chat) {
3284 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3285 chat_invalidated_cb, chat);
3286 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3287 chat_message_received_cb, chat);
3288 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3289 chat_message_acknowledged_cb, chat);
3290 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3291 chat_send_error_cb, chat);
3292 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3293 chat_state_changed_cb, chat);
3294 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3295 chat_members_changed_cb, chat);
3296 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3297 chat_self_contact_changed_cb, chat);
3298 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3299 chat_remote_contact_changed_cb, chat);
3300 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3301 chat_title_changed_cb, chat);
3302 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3303 chat_subject_changed_cb, chat);
3304 empathy_tp_chat_leave (priv->tp_chat, "");
3305 g_object_unref (priv->tp_chat);
3307 if (priv->account) {
3308 g_object_unref (priv->account);
3310 if (priv->self_contact) {
3311 g_signal_handlers_disconnect_by_func (priv->self_contact,
3312 chat_self_contact_alias_changed_cb,
3314 g_object_unref (priv->self_contact);
3316 if (priv->remote_contact) {
3317 g_object_unref (priv->remote_contact);
3320 if (priv->block_events_timeout_id) {
3321 g_source_remove (priv->block_events_timeout_id);
3325 g_free (priv->name);
3326 g_free (priv->subject);
3327 g_completion_free (priv->completion);
3329 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3331 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3335 chat_constructed (GObject *object)
3337 EmpathyChat *chat = EMPATHY_CHAT (object);
3338 EmpathyChatPriv *priv = GET_PRIV (chat);
3340 if (priv->tp_chat != NULL) {
3341 TpChannel *channel = TP_CHANNEL (priv->tp_chat);
3342 TpConnection *conn = tp_channel_borrow_connection (channel);
3343 gboolean supports_avatars =
3344 tp_proxy_has_interface_by_id (conn,
3345 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS);
3347 empathy_theme_adium_set_show_avatars (chat->view,
3351 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3352 /* First display logs from the logger and then display pending messages */
3353 chat_add_logs (chat);
3356 /* Just display pending messages for rooms */
3357 priv->can_show_pending = TRUE;
3358 show_pending_messages (chat);
3363 empathy_chat_class_init (EmpathyChatClass *klass)
3365 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3367 object_class->finalize = chat_finalize;
3368 object_class->get_property = chat_get_property;
3369 object_class->set_property = chat_set_property;
3370 object_class->constructed = chat_constructed;
3372 g_object_class_install_property (object_class,
3374 g_param_spec_object ("tp-chat",
3376 "The tp chat object",
3377 EMPATHY_TYPE_TP_CHAT,
3380 G_PARAM_STATIC_STRINGS));
3381 g_object_class_install_property (object_class,
3383 g_param_spec_object ("account",
3384 "Account of the chat",
3385 "The account of the chat",
3388 G_PARAM_STATIC_STRINGS));
3389 g_object_class_install_property (object_class,
3391 g_param_spec_string ("id",
3393 "The id of the chat",
3396 G_PARAM_STATIC_STRINGS));
3397 g_object_class_install_property (object_class,
3399 g_param_spec_string ("name",
3401 "The name of the chat",
3404 G_PARAM_STATIC_STRINGS));
3405 g_object_class_install_property (object_class,
3407 g_param_spec_string ("subject",
3409 "The subject or topic of the chat",
3412 G_PARAM_STATIC_STRINGS));
3413 g_object_class_install_property (object_class,
3414 PROP_REMOTE_CONTACT,
3415 g_param_spec_object ("remote-contact",
3416 "The remote contact",
3417 "The remote contact is any",
3418 EMPATHY_TYPE_CONTACT,
3420 G_PARAM_STATIC_STRINGS));
3421 g_object_class_install_property (object_class,
3423 g_param_spec_boolean ("show-contacts",
3424 "Contacts' visibility",
3425 "The visibility of the contacts' list",
3428 G_PARAM_STATIC_STRINGS));
3430 g_object_class_install_property (object_class,
3432 g_param_spec_boolean ("sms-channel",
3434 "TRUE if this channel is for sending SMSes",
3436 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3438 g_object_class_install_property (object_class,
3439 PROP_N_MESSAGES_SENDING,
3440 g_param_spec_uint ("n-messages-sending",
3441 "Num Messages Sending",
3442 "The number of messages being sent",
3444 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3446 g_object_class_install_property (object_class,
3447 PROP_NB_UNREAD_MESSAGES,
3448 g_param_spec_uint ("nb-unread-messages",
3449 "Num Unread Messages",
3450 "The number of unread messages",
3452 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3454 signals[COMPOSING] =
3455 g_signal_new ("composing",
3456 G_OBJECT_CLASS_TYPE (object_class),
3460 g_cclosure_marshal_generic,
3465 * EmpathyChat::new-message:
3466 * @self: the #EmpathyChat
3467 * @message: the new message
3468 * @pending: whether the message was in the pending queue when @self
3470 * @should_highlight: %TRUE if the message mentions the local user
3472 signals[NEW_MESSAGE] =
3473 g_signal_new ("new-message",
3474 G_OBJECT_CLASS_TYPE (object_class),
3478 g_cclosure_marshal_generic,
3480 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3482 signals[PART_COMMAND_ENTERED] =
3483 g_signal_new ("part-command-entered",
3484 G_OBJECT_CLASS_TYPE (object_class),
3488 g_cclosure_marshal_generic,
3492 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3496 chat_block_events_timeout_cb (gpointer data)
3498 EmpathyChatPriv *priv = GET_PRIV (data);
3500 priv->block_events_timeout_id = 0;
3506 account_manager_prepared_cb (GObject *source_object,
3507 GAsyncResult *result,
3510 GList *accounts, *l;
3511 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3512 EmpathyChat *chat = user_data;
3513 GError *error = NULL;
3515 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3516 DEBUG ("Failed to prepare the account manager: %s", error->message);
3517 g_error_free (error);
3521 accounts = tp_account_manager_get_valid_accounts (account_manager);
3523 for (l = accounts; l != NULL; l = l->next) {
3524 TpAccount *account = l->data;
3525 tp_g_signal_connect_object (account, "status-changed",
3526 G_CALLBACK (chat_new_connection_cb),
3530 g_list_free (accounts);
3534 empathy_chat_init (EmpathyChat *chat)
3536 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3537 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3540 priv->log_manager = tpl_log_manager_dup_singleton ();
3541 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3542 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3544 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3545 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3546 priv->input_history = NULL;
3547 priv->input_history_current = NULL;
3548 priv->account_manager = tp_account_manager_dup ();
3550 tp_proxy_prepare_async (priv->account_manager, NULL,
3551 account_manager_prepared_cb, chat);
3553 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3554 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3556 /* Block events for some time to avoid having "has come online" or
3557 * "joined" messages. */
3558 priv->block_events_timeout_id =
3559 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3561 /* Add nick name completion */
3562 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3563 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3565 chat_create_ui (chat);
3569 empathy_chat_new (EmpathyTpChat *tp_chat)
3571 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3575 empathy_chat_get_tp_chat (EmpathyChat *chat)
3577 EmpathyChatPriv *priv = GET_PRIV (chat);
3579 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3581 return priv->tp_chat;
3587 GtkWidget *info_bar;
3597 passwd_remember_button_cb (GtkButton *button,
3600 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3604 passwd_not_now_button_cb (GtkButton *button,
3607 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3611 remember_password_infobar_response_cb (GtkWidget *info_bar,
3615 EmpathyChatPriv *priv = GET_PRIV (data->self);
3617 if (response_id == GTK_RESPONSE_OK) {
3618 DEBUG ("Saving room password");
3619 empathy_keyring_set_room_password_async (priv->account,
3620 empathy_tp_chat_get_id (priv->tp_chat),
3625 gtk_widget_destroy (info_bar);
3626 g_free (data->password);
3627 g_slice_free (PasswordData, data);
3631 chat_prompt_to_save_password (EmpathyChat *self,
3634 GtkWidget *content_area;
3641 /* save the password in case it needs to be saved */
3642 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3644 /* Remove all previous widgets */
3645 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3646 gtk_container_forall (GTK_CONTAINER (content_area),
3647 (GtkCallback) gtk_widget_destroy, NULL);
3648 data->button = NULL;
3651 data->spinner = NULL;
3653 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3654 GTK_MESSAGE_QUESTION);
3656 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3657 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3660 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3661 GTK_ICON_SIZE_DIALOG);
3662 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3665 label = gtk_label_new (_("Would you like to store this password?"));
3666 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3668 /* Add 'Remember' button */
3669 alig = gtk_alignment_new (0, 0.5, 1, 0);
3671 button = gtk_button_new_with_label (_("Remember"));
3672 gtk_container_add (GTK_CONTAINER (alig), button);
3673 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3675 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3678 /* Add 'Not now' button */
3679 alig = gtk_alignment_new (0, 0.5, 1, 0);
3681 button = gtk_button_new_with_label (_("Not now"));
3682 gtk_container_add (GTK_CONTAINER (alig), button);
3683 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3685 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3689 g_signal_handler_disconnect (data->info_bar, data->response_id);
3690 g_signal_connect (data->info_bar, "response",
3691 G_CALLBACK (remember_password_infobar_response_cb), data);
3693 gtk_widget_show_all (data->info_bar);
3697 provide_password_cb (GObject *tp_chat,
3701 PasswordData *data = user_data;
3702 EmpathyChat *self = data->self;
3703 EmpathyChatPriv *priv = GET_PRIV (self);
3704 GError *error = NULL;
3706 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3708 DEBUG ("error: %s", error->message);
3709 /* FIXME: what should we do if that's another error? Close the channel?
3710 * Display the raw D-Bus error to the user isn't very useful */
3711 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3713 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3714 gtk_widget_set_sensitive (data->entry, TRUE);
3715 gtk_widget_grab_focus (data->entry);
3718 gtk_info_bar_set_message_type (
3719 GTK_INFO_BAR (data->info_bar),
3723 gtk_widget_set_sensitive (data->button, TRUE);
3724 gtk_button_set_label (GTK_BUTTON (data->button),
3728 gtk_label_set_text (GTK_LABEL (data->label),
3729 _("Wrong password; please try again:"));
3732 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3733 gtk_widget_hide (data->spinner);
3735 g_error_free (error);
3739 if (empathy_keyring_is_available ()) {
3740 /* ask whether they want to save the password */
3741 chat_prompt_to_save_password (self, data);
3743 /* Get rid of the password info bar finally */
3744 gtk_widget_destroy (data->info_bar);
3745 g_slice_free (PasswordData, data);
3749 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3750 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3751 gtk_widget_grab_focus (self->input_text_view);
3755 password_infobar_response_cb (GtkWidget *info_bar,
3759 EmpathyChatPriv *priv = GET_PRIV (data->self);
3760 const gchar *password;
3762 if (response_id != GTK_RESPONSE_OK) {
3763 gtk_widget_destroy (info_bar);
3764 g_slice_free (PasswordData, data);
3768 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3770 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3771 provide_password_cb, data);
3773 gtk_widget_set_sensitive (data->button, FALSE);
3774 gtk_widget_set_sensitive (data->entry, FALSE);
3776 gtk_spinner_start (GTK_SPINNER (data->spinner));
3777 gtk_widget_show (data->spinner);
3781 password_entry_activate_cb (GtkWidget *entry,
3784 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3788 passwd_join_button_cb (GtkButton *button,
3791 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3795 clear_icon_released_cb (GtkEntry *entry,
3796 GtkEntryIconPosition icon_pos,
3800 gtk_entry_set_text (entry, "");
3804 password_entry_changed_cb (GtkEditable *entry,
3809 str = gtk_entry_get_text (GTK_ENTRY (entry));
3811 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3812 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3816 infobar_chat_invalidated_cb (TpProxy *proxy,
3820 gpointer password_infobar)
3822 /* Destroy the password infobar whenever a channel is invalidated
3823 * so we don't have multiple infobars when the MUC is rejoined */
3824 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3828 display_password_info_bar (EmpathyChat *self)
3830 EmpathyChatPriv *priv = GET_PRIV (self);
3831 GtkWidget *info_bar;
3832 GtkWidget *content_area;
3842 data = g_slice_new0 (PasswordData);
3844 info_bar = gtk_info_bar_new ();
3845 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3846 GTK_MESSAGE_QUESTION);
3848 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3850 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3851 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3854 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3855 GTK_ICON_SIZE_DIALOG);
3856 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3859 label = gtk_label_new (_("This room is protected by a password:"));
3860 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3862 /* Add password entry */
3863 entry = gtk_entry_new ();
3864 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3865 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3867 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3868 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3869 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3870 GTK_ENTRY_ICON_SECONDARY, FALSE);
3872 g_signal_connect (entry, "icon-release",
3873 G_CALLBACK (clear_icon_released_cb), data);
3874 g_signal_connect (entry, "changed",
3875 G_CALLBACK (password_entry_changed_cb), data);
3877 g_signal_connect (entry, "activate",
3878 G_CALLBACK (password_entry_activate_cb), data);
3880 /* Focus the password entry once it's realized */
3881 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3883 /* Add 'Join' button */
3884 alig = gtk_alignment_new (0, 0.5, 1, 0);
3886 button = gtk_button_new_with_label (_("Join"));
3887 gtk_container_add (GTK_CONTAINER (alig), button);
3888 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3890 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3894 spinner = gtk_spinner_new ();
3895 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3897 /* Save some data for messing around with later */
3899 data->info_bar = info_bar;
3900 data->button = button;
3901 data->label = label;
3902 data->entry = entry;
3903 data->spinner = spinner;
3905 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3907 gtk_widget_show_all (hbox);
3909 tp_g_signal_connect_object (priv->tp_chat,
3910 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3913 data->response_id = g_signal_connect (info_bar, "response",
3914 G_CALLBACK (password_infobar_response_cb), data);
3916 gtk_widget_show_all (info_bar);
3917 /* ... but hide the spinner */
3918 gtk_widget_hide (spinner);
3920 /* prevent the user from typing anything */
3921 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3925 provide_saved_password_cb (GObject *tp_chat,
3929 EmpathyChat *self = user_data;
3930 EmpathyChatPriv *priv = GET_PRIV (self);
3931 GError *error = NULL;
3933 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3935 DEBUG ("error: %s", error->message);
3936 /* FIXME: what should we do if that's another error? Close the channel?
3937 * Display the raw D-Bus error to the user isn't very useful */
3938 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3939 display_password_info_bar (self);
3940 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3942 g_error_free (error);
3947 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3948 gtk_widget_grab_focus (self->input_text_view);
3952 chat_room_got_password_cb (GObject *source,
3953 GAsyncResult *result,
3956 EmpathyChat *self = user_data;
3957 EmpathyChatPriv *priv = GET_PRIV (self);
3958 const gchar *password;
3959 GError *error = NULL;
3961 password = empathy_keyring_get_room_password_finish (priv->account,
3964 if (error != NULL) {
3965 DEBUG ("Couldn't get room password: %s\n", error->message);
3966 g_clear_error (&error);
3968 display_password_info_bar (self);
3969 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3973 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3974 provide_saved_password_cb, self);
3978 chat_password_needed_changed_cb (EmpathyChat *self)
3980 EmpathyChatPriv *priv = GET_PRIV (self);
3982 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3983 empathy_keyring_get_room_password_async (priv->account,
3984 empathy_tp_chat_get_id (priv->tp_chat),
3985 chat_room_got_password_cb, self);
3990 chat_sms_channel_changed_cb (EmpathyChat *self)
3992 EmpathyChatPriv *priv = GET_PRIV (self);
3994 priv->sms_channel = tp_text_channel_is_sms_channel (
3995 (TpTextChannel *) priv->tp_chat);
3996 g_object_notify (G_OBJECT (self), "sms-channel");
4000 chat_n_messages_sending_changed_cb (EmpathyChat *self)
4002 g_object_notify (G_OBJECT (self), "n-messages-sending");
4006 empathy_chat_set_tp_chat (EmpathyChat *chat,
4007 EmpathyTpChat *tp_chat)
4009 EmpathyChatPriv *priv = GET_PRIV (chat);
4011 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4012 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4014 if (priv->tp_chat) {
4018 if (priv->account) {
4019 g_object_unref (priv->account);
4022 priv->tp_chat = g_object_ref (tp_chat);
4023 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4025 g_signal_connect (tp_chat, "invalidated",
4026 G_CALLBACK (chat_invalidated_cb),
4028 g_signal_connect (tp_chat, "message-received-empathy",
4029 G_CALLBACK (chat_message_received_cb),
4031 g_signal_connect (tp_chat, "message_acknowledged",
4032 G_CALLBACK (chat_message_acknowledged_cb),
4034 g_signal_connect (tp_chat, "send-error",
4035 G_CALLBACK (chat_send_error_cb),
4037 g_signal_connect (tp_chat, "contact-chat-state-changed",
4038 G_CALLBACK (chat_state_changed_cb),
4040 g_signal_connect (tp_chat, "members-changed",
4041 G_CALLBACK (chat_members_changed_cb),
4043 g_signal_connect (tp_chat, "member-renamed",
4044 G_CALLBACK (chat_member_renamed_cb),
4046 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4047 G_CALLBACK (chat_self_contact_changed_cb),
4049 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4050 G_CALLBACK (chat_remote_contact_changed_cb),
4052 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4053 G_CALLBACK (chat_password_needed_changed_cb),
4055 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4056 G_CALLBACK (chat_sms_channel_changed_cb),
4058 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4059 G_CALLBACK (chat_n_messages_sending_changed_cb),
4061 g_signal_connect_swapped (tp_chat, "notify::title",
4062 G_CALLBACK (chat_title_changed_cb),
4064 g_signal_connect_swapped (tp_chat, "notify::subject",
4065 G_CALLBACK (chat_subject_changed_cb),
4068 /* Get initial value of properties */
4069 chat_sms_channel_changed_cb (chat);
4070 chat_self_contact_changed_cb (chat);
4071 chat_remote_contact_changed_cb (chat);
4072 chat_title_changed_cb (chat);
4073 chat_subject_changed_cb (chat);
4075 if (chat->input_text_view) {
4076 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4077 if (priv->block_events_timeout_id == 0) {
4078 empathy_theme_adium_append_event (chat->view, _("Connected"));
4082 g_object_notify (G_OBJECT (chat), "tp-chat");
4083 g_object_notify (G_OBJECT (chat), "id");
4084 g_object_notify (G_OBJECT (chat), "account");
4086 /* This is a noop when tp-chat is set at object construction time and causes
4087 * the pending messages to be show when it's set on the object after it has
4089 show_pending_messages (chat);
4091 /* check if a password is needed */
4092 chat_password_needed_changed_cb (chat);
4096 empathy_chat_get_account (EmpathyChat *chat)
4098 EmpathyChatPriv *priv = GET_PRIV (chat);
4100 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4102 return priv->account;
4106 empathy_chat_get_id (EmpathyChat *chat)
4108 EmpathyChatPriv *priv = GET_PRIV (chat);
4110 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4116 empathy_chat_dup_name (EmpathyChat *chat)
4118 EmpathyChatPriv *priv = GET_PRIV (chat);
4121 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4125 if (!ret && priv->remote_contact) {
4126 ret = empathy_contact_get_alias (priv->remote_contact);
4133 ret = _("Conversation");
4135 if (priv->sms_channel)
4136 /* Translators: this string is a something like
4137 * "Escher Cat (SMS)" */
4138 return g_strdup_printf (_("%s (SMS)"), ret);
4140 return g_strdup (ret);
4144 empathy_chat_get_subject (EmpathyChat *chat)
4146 EmpathyChatPriv *priv = GET_PRIV (chat);
4148 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4150 return priv->subject;
4154 empathy_chat_get_remote_contact (EmpathyChat *chat)
4156 EmpathyChatPriv *priv = GET_PRIV (chat);
4158 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4160 return priv->remote_contact;
4164 empathy_chat_get_contact_menu (EmpathyChat *chat)
4166 EmpathyChatPriv *priv = GET_PRIV (chat);
4167 GtkWidget *menu = NULL;
4168 FolksIndividual *individual;
4171 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4173 if (priv->remote_contact == NULL)
4176 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4177 if (contact == NULL)
4180 individual = empathy_ensure_individual_from_tp_contact (contact);
4182 if (individual == NULL)
4185 menu = empathy_individual_menu_new (individual,
4186 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4187 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4188 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4189 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4191 g_object_unref (individual);
4197 empathy_chat_clear (EmpathyChat *chat)
4199 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4201 empathy_theme_adium_clear (chat->view);
4205 empathy_chat_scroll_down (EmpathyChat *chat)
4207 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4209 empathy_theme_adium_scroll_down (chat->view);
4213 empathy_chat_cut (EmpathyChat *chat)
4215 GtkTextBuffer *buffer;
4217 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4219 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4220 if (gtk_text_buffer_get_has_selection (buffer)) {
4221 GtkClipboard *clipboard;
4223 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4225 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4230 copy_from_chat_view (EmpathyChat *chat)
4232 if (!empathy_theme_adium_get_has_selection (chat->view))
4235 empathy_theme_adium_copy_clipboard (chat->view);
4240 copy_from_input (EmpathyChat *chat)
4242 GtkTextBuffer *buffer;
4243 GtkClipboard *clipboard;
4245 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4246 if (!gtk_text_buffer_get_has_selection (buffer))
4249 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4250 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4255 copy_from_topic (EmpathyChat *chat)
4257 EmpathyChatPriv *priv = GET_PRIV (chat);
4264 GtkClipboard *clipboard;
4266 if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4271 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4272 start = g_utf8_offset_to_pointer (topic, start_offset);
4273 end = g_utf8_offset_to_pointer (topic, end_offset);
4274 selection = g_strndup (start, end - start);
4276 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4277 gtk_clipboard_set_text (clipboard, selection, -1);
4284 empathy_chat_copy (EmpathyChat *chat)
4286 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4288 if (copy_from_chat_view (chat))
4291 if (copy_from_input (chat))
4294 copy_from_topic (chat);
4298 empathy_chat_paste (EmpathyChat *chat)
4300 GtkTextBuffer *buffer;
4301 GtkClipboard *clipboard;
4302 EmpathyChatPriv *priv;
4304 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4306 priv = GET_PRIV (chat);
4308 if (gtk_widget_get_visible (priv->search_bar)) {
4309 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4313 if (priv->tp_chat == NULL ||
4314 !gtk_widget_is_sensitive (chat->input_text_view))
4317 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4318 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4320 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4324 empathy_chat_find (EmpathyChat *chat)
4326 EmpathyChatPriv *priv;
4328 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4330 priv = GET_PRIV (chat);
4332 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4336 empathy_chat_correct_word (EmpathyChat *chat,
4339 const gchar *new_word)
4341 GtkTextBuffer *buffer;
4343 g_return_if_fail (chat != NULL);
4344 g_return_if_fail (new_word != NULL);
4346 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4348 gtk_text_buffer_delete (buffer, start, end);
4349 gtk_text_buffer_insert (buffer, start,
4355 empathy_chat_is_room (EmpathyChat *chat)
4357 EmpathyChatPriv *priv = GET_PRIV (chat);
4359 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4361 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4365 empathy_chat_is_highlighted (EmpathyChat *chat)
4367 EmpathyChatPriv *priv = GET_PRIV (chat);
4369 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4371 return priv->highlighted;
4375 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4377 EmpathyChatPriv *priv = GET_PRIV (self);
4379 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4381 return priv->unread_messages;
4384 /* called when the messages have been read by user */
4386 empathy_chat_messages_read (EmpathyChat *self)
4388 EmpathyChatPriv *priv = GET_PRIV (self);
4390 g_return_if_fail (EMPATHY_IS_CHAT (self));
4392 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4393 * definition. If we are still retrieving the backlogs, do not ACK */
4394 if (priv->retrieving_backlogs)
4397 if (priv->tp_chat != NULL) {
4398 tp_text_channel_ack_all_pending_messages_async (
4399 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4402 priv->highlighted = FALSE;
4404 if (priv->unread_messages_when_offline > 0) {
4405 /* We can't ack those as the connection has gone away so just consider
4407 priv->unread_messages -= priv->unread_messages_when_offline;
4408 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4409 priv->unread_messages_when_offline = 0;
4413 /* Return TRUE if on of the contacts in this chat is composing */
4415 empathy_chat_is_composing (EmpathyChat *chat)
4417 return chat->priv->compositors != NULL;
4421 empathy_chat_is_sms_channel (EmpathyChat *self)
4423 EmpathyChatPriv *priv = GET_PRIV (self);
4425 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4427 return priv->sms_channel;
4431 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4433 EmpathyChatPriv *priv;
4435 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4437 priv = GET_PRIV (self);
4439 if (priv->tp_chat == NULL) {
4444 g_object_get (priv->tp_chat,
4445 "n-messages-sending", &n_messages,
4453 empathy_chat_dup_text (EmpathyChat *self)
4455 GtkTextBuffer *buffer;
4456 GtkTextIter start, end;
4458 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4460 gtk_text_buffer_get_bounds (buffer, &start, &end);
4461 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4465 empathy_chat_set_text (EmpathyChat *self,
4468 GtkTextBuffer *buffer;
4470 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4472 gtk_text_buffer_set_text (buffer, text, -1);