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-contact-dialogs.h"
52 #include "empathy-individual-store-channel.h"
53 #include "empathy-individual-view.h"
54 #include "empathy-input-text-view.h"
55 #include "empathy-search-bar.h"
56 #include "empathy-theme-manager.h"
57 #include "empathy-theme-adium.h"
58 #include "empathy-smiley-manager.h"
59 #include "empathy-ui-utils.h"
60 #include "empathy-string-parser.h"
61 #include "extensions/extensions.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
64 #include <libempathy/empathy-debug.h>
66 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
67 #define COMPOSING_STOP_TIMEOUT 5
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
70 struct _EmpathyChatPriv {
71 EmpathyTpChat *tp_chat;
76 EmpathyContact *self_contact;
77 EmpathyContact *remote_contact;
78 gboolean show_contacts;
80 GSettings *gsettings_chat;
81 GSettings *gsettings_ui;
83 TplLogManager *log_manager;
84 TpAccountManager *account_manager;
86 GList *input_history_current;
88 GCompletion *completion;
89 guint composing_stop_timeout_id;
90 guint block_events_timeout_id;
91 TpHandleType handle_type;
93 gboolean has_input_vscroll;
95 /* TRUE if spell checking is enabled, FALSE otherwise.
96 * This is to keep track of the last state of spell checking
98 gboolean spell_checking_enabled;
100 /* These store the signal handler ids for the enclosed text entry. */
101 gulong insert_text_id;
102 gulong delete_range_id;
103 gulong notify_cursor_position_id;
105 /* Source func ID for update_misspelled_words () */
106 guint update_misspelled_words_id;
107 /* Source func ID for save_paned_pos_timeout () */
108 guint save_paned_pos_id;
109 /* Source func ID for chat_contacts_visible_timeout_cb () */
110 guint contacts_visible_id;
114 GtkWidget *vbox_left;
115 GtkWidget *scrolled_window_chat;
116 GtkWidget *scrolled_window_input;
117 GtkWidget *scrolled_window_contacts;
118 GtkWidget *hbox_topic;
119 GtkWidget *expander_topic;
120 GtkWidget *label_topic;
121 GtkWidget *contact_list_view;
122 GtkWidget *info_bar_vbox;
123 GtkWidget *search_bar;
125 guint unread_messages;
126 guint unread_messages_when_offline;
127 /* TRUE if the pending messages can be displayed. This is to avoid to show
128 * pending messages *before* messages from logs. (#603980) */
129 gboolean can_show_pending;
131 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
132 * be differently handled since it introduces another race condition, which
133 * is really hard to occur, but still possible.
135 * With the current workaround (which has the race above), we need to be
136 * sure to ACK any pending messages only when the retrieval of backlogs is
137 * finished, that's why using retrieving_backlogs flag.
138 * empathy_chat_messages_read () will check this variable and not ACK
139 * anything when TRUE. It will be set TRUE at chat_constructed () and set
140 * back to FALSE when the backlog has been retrieved and the pending
141 * messages actually showed to the user.
143 * Race condition introduced with this workaround:
144 * Scenario: a message is pending, the user is notified and selects the tab.
145 * the tab with a pending message is focused before the messages are properly
146 * shown (since the preparation of the window is slower AND async WRT the
147 * tab showing), which means the user won't see any new messages (rare but
148 * possible), if he/she will change tab focus before the messages are
149 * properly shown, the tab will be set as 'seen' and the user won't be
150 * notified again about the already notified pending messages when the
151 * messages in tab will be properly shown */
152 gboolean retrieving_backlogs;
153 gboolean sms_channel;
155 /* we need to know whether populate-popup happened in response to
156 * the keyboard or the mouse. We can't ask GTK for the most recent
157 * event, because it will be a notify event. Instead we track it here */
158 GdkEventType most_recent_event_type;
160 /* A regex matching our own current nickname in the room, or %NULL if
161 * !empathy_chat_is_room (). */
162 GRegex *highlight_regex;
164 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
165 * Cleared by empathy_chat_messages_read (). */
166 gboolean highlighted;
170 gchar *text; /* Original message that was specified
171 * upon entry creation. */
172 gchar *modified_text; /* Message that was modified by user.
173 * When no modifications were made, it is NULL */
179 PART_COMMAND_ENTERED,
193 PROP_N_MESSAGES_SENDING,
194 PROP_NB_UNREAD_MESSAGES,
197 static guint signals[LAST_SIGNAL] = { 0 };
199 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
201 static gboolean update_misspelled_words (gpointer data);
204 chat_get_property (GObject *object,
209 EmpathyChat *chat = EMPATHY_CHAT (object);
210 EmpathyChatPriv *priv = GET_PRIV (object);
214 g_value_set_object (value, priv->tp_chat);
217 g_value_set_object (value, priv->account);
220 g_value_take_string (value, empathy_chat_dup_name (chat));
223 g_value_set_string (value, priv->id);
226 g_value_set_string (value, priv->subject);
228 case PROP_REMOTE_CONTACT:
229 g_value_set_object (value, priv->remote_contact);
231 case PROP_SHOW_CONTACTS:
232 g_value_set_boolean (value, priv->show_contacts);
234 case PROP_SMS_CHANNEL:
235 g_value_set_boolean (value, priv->sms_channel);
237 case PROP_N_MESSAGES_SENDING:
238 g_value_set_uint (value,
239 empathy_chat_get_n_messages_sending (chat));
241 case PROP_NB_UNREAD_MESSAGES:
242 g_value_set_uint (value,
243 empathy_chat_get_nb_unread_messages (chat));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
252 chat_set_property (GObject *object,
257 EmpathyChat *chat = EMPATHY_CHAT (object);
261 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
263 case PROP_SHOW_CONTACTS:
264 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
273 account_reconnected (EmpathyChat *chat,
276 EmpathyChatPriv *priv = GET_PRIV (chat);
278 DEBUG ("Account reconnected, request a new Text channel");
280 /* FIXME: Ideally we should ask to handle ourself the channel so we can
281 * report the error if any but this is blocked by
282 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
283 switch (priv->handle_type) {
284 case TP_HANDLE_TYPE_CONTACT:
285 if (priv->sms_channel)
286 empathy_sms_contact_id (
288 TP_USER_ACTION_TIME_NOT_USER_ACTION,
291 empathy_chat_with_contact_id (
293 TP_USER_ACTION_TIME_NOT_USER_ACTION,
296 case TP_HANDLE_TYPE_ROOM:
297 empathy_join_muc (account, priv->id,
298 TP_USER_ACTION_TIME_NOT_USER_ACTION);
300 case TP_HANDLE_TYPE_NONE:
301 case TP_HANDLE_TYPE_LIST:
302 case TP_HANDLE_TYPE_GROUP:
304 g_assert_not_reached ();
308 g_object_unref (chat);
312 chat_new_connection_cb (TpAccount *account,
316 gchar *dbus_error_name,
320 EmpathyChatPriv *priv = GET_PRIV (chat);
322 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
325 if (priv->tp_chat != NULL || account != priv->account ||
326 priv->handle_type == TP_HANDLE_TYPE_NONE ||
327 EMP_STR_EMPTY (priv->id))
332 account_reconnected (chat, account);
336 chat_composing_remove_timeout (EmpathyChat *chat)
338 EmpathyChatPriv *priv;
340 priv = GET_PRIV (chat);
342 if (priv->composing_stop_timeout_id) {
343 g_source_remove (priv->composing_stop_timeout_id);
344 priv->composing_stop_timeout_id = 0;
349 set_chate_state_cb (GObject *source,
350 GAsyncResult *result,
353 GError *error = NULL;
355 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
357 DEBUG ("Failed to set chat state: %s", error->message);
358 g_error_free (error);
363 set_chat_state (EmpathyChat *self,
364 TpChannelChatState state)
366 EmpathyChatPriv *priv = GET_PRIV (self);
368 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
369 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
372 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
373 set_chate_state_cb, self);
377 chat_composing_stop_timeout_cb (EmpathyChat *chat)
379 EmpathyChatPriv *priv;
380 gboolean send_chat_states;
382 priv = GET_PRIV (chat);
384 priv->composing_stop_timeout_id = 0;
385 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
386 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
387 if (!send_chat_states) {
388 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
390 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
397 chat_composing_start (EmpathyChat *chat)
399 EmpathyChatPriv *priv;
400 gboolean send_chat_states;
402 priv = GET_PRIV (chat);
404 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
405 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
406 if (!send_chat_states) {
410 if (priv->composing_stop_timeout_id) {
411 /* Just restart the timeout */
412 chat_composing_remove_timeout (chat);
414 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
417 priv->composing_stop_timeout_id = g_timeout_add_seconds (
418 COMPOSING_STOP_TIMEOUT,
419 (GSourceFunc) chat_composing_stop_timeout_cb,
424 chat_composing_stop (EmpathyChat *chat)
426 chat_composing_remove_timeout (chat);
427 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
431 chat_input_history_entry_cmp (InputHistoryEntry *entry,
434 if (!tp_strdiff (entry->text, text)) {
435 if (entry->modified_text != NULL) {
436 /* Modified entry and single string cannot be equal. */
444 static InputHistoryEntry *
445 chat_input_history_entry_new_with_text (const gchar *text)
447 InputHistoryEntry *entry;
448 entry = g_slice_new0 (InputHistoryEntry);
449 entry->text = g_strdup (text);
455 chat_input_history_entry_free (InputHistoryEntry *entry)
457 g_free (entry->text);
458 g_free (entry->modified_text);
459 g_slice_free (InputHistoryEntry, entry);
463 chat_input_history_entry_revert (InputHistoryEntry *entry)
465 g_free (entry->modified_text);
466 entry->modified_text = NULL;
470 chat_input_history_entry_update_text (InputHistoryEntry *entry,
475 if (!tp_strdiff (text, entry->text)) {
476 g_free (entry->modified_text);
477 entry->modified_text = NULL;
481 old = entry->modified_text;
482 entry->modified_text = g_strdup (text);
487 chat_input_history_entry_get_text (InputHistoryEntry *entry)
493 if (entry->modified_text != NULL) {
494 return entry->modified_text;
500 chat_input_history_remove_item (GList *list,
503 list = g_list_remove_link (list, item);
504 chat_input_history_entry_free (item->data);
505 g_list_free_1 (item);
510 chat_input_history_revert (EmpathyChat *chat)
512 EmpathyChatPriv *priv;
516 InputHistoryEntry *entry;
518 priv = GET_PRIV (chat);
519 list = priv->input_history;
522 DEBUG ("No input history");
526 /* Delete temporary entry */
527 if (priv->input_history_current != NULL) {
529 list = chat_input_history_remove_item (list, item1);
530 if (priv->input_history_current == item1) {
531 /* Removed temporary entry was current entry */
532 priv->input_history = list;
533 priv->input_history_current = NULL;
538 /* There is no entry to revert */
542 /* Restore the current history entry to original value */
543 item1 = priv->input_history_current;
545 chat_input_history_entry_revert (entry);
547 /* Remove restored entry if there is other occurance before this entry */
548 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
549 (GCompareFunc) chat_input_history_entry_cmp);
550 if (item2 != item1) {
551 list = chat_input_history_remove_item (list, item1);
554 /* Remove other occurance of the restored entry */
555 item2 = g_list_find_custom (item1->next,
556 chat_input_history_entry_get_text (entry),
557 (GCompareFunc) chat_input_history_entry_cmp);
559 list = chat_input_history_remove_item (list, item2);
563 priv->input_history_current = NULL;
564 priv->input_history = list;
568 chat_input_history_add (EmpathyChat *chat,
572 EmpathyChatPriv *priv;
575 InputHistoryEntry *entry;
577 priv = GET_PRIV (chat);
579 list = priv->input_history;
581 /* Remove any other occurances of this entry, if not temporary */
583 while ((item = g_list_find_custom (list, str,
584 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
585 list = chat_input_history_remove_item (list, item);
588 /* Trim the list to the last 10 items */
589 while (g_list_length (list) > 10) {
590 item = g_list_last (list);
592 list = chat_input_history_remove_item (list, item);
600 entry = chat_input_history_entry_new_with_text (str);
601 list = g_list_prepend (list, entry);
603 /* Set the list and the current item pointer */
604 priv->input_history = list;
606 priv->input_history_current = list;
609 priv->input_history_current = NULL;
614 chat_input_history_get_next (EmpathyChat *chat)
616 EmpathyChatPriv *priv;
620 priv = GET_PRIV (chat);
622 if (priv->input_history == NULL) {
623 DEBUG ("No input history, next entry is NULL");
626 g_assert (priv->input_history_current != NULL);
628 if ((item = g_list_next (priv->input_history_current)) == NULL)
630 item = priv->input_history_current;
633 msg = chat_input_history_entry_get_text (item->data);
635 DEBUG ("Returning next entry: '%s'", msg);
637 priv->input_history_current = item;
643 chat_input_history_get_prev (EmpathyChat *chat)
645 EmpathyChatPriv *priv;
649 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
651 priv = GET_PRIV (chat);
653 if (priv->input_history == NULL) {
654 DEBUG ("No input history, previous entry is NULL");
658 if (priv->input_history_current == NULL)
662 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
664 item = priv->input_history_current;
667 msg = chat_input_history_entry_get_text (item->data);
669 DEBUG ("Returning previous entry: '%s'", msg);
671 priv->input_history_current = item;
677 chat_input_history_update (EmpathyChat *chat,
678 GtkTextBuffer *buffer)
680 EmpathyChatPriv *priv;
681 GtkTextIter start, end;
683 InputHistoryEntry *entry;
685 priv = GET_PRIV (chat);
687 gtk_text_buffer_get_bounds (buffer, &start, &end);
688 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
690 if (priv->input_history_current == NULL) {
691 /* Add the current text temporarily to the history */
692 chat_input_history_add (chat, text, TRUE);
697 /* Save the changes in the history */
698 entry = priv->input_history_current->data;
699 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
700 chat_input_history_entry_update_text (entry, text);
709 } ChatCommandMsgData;
712 chat_command_msg_cb (GObject *source,
713 GAsyncResult *result,
716 ChatCommandMsgData *data = user_data;
717 GError *error = NULL;
720 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
721 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
723 if (channel == NULL) {
724 DEBUG ("Failed to get channel: %s", error->message);
725 g_error_free (error);
727 empathy_chat_view_append_event (data->chat->view,
728 _("Failed to open private chat"));
732 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
733 TpTextChannel *text = (TpTextChannel *) channel;
736 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
739 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
741 g_object_unref (msg);
744 g_object_unref (channel);
747 g_free (data->message);
748 g_slice_free (ChatCommandMsgData, data);
752 nick_command_supported (EmpathyChat *chat)
754 EmpathyChatPriv * priv = GET_PRIV (chat);
755 TpConnection *connection;
757 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
758 return tp_proxy_has_interface_by_id (connection,
759 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
763 part_command_supported (EmpathyChat *chat)
765 EmpathyChatPriv * priv = GET_PRIV (chat);
767 return tp_proxy_has_interface_by_id (priv->tp_chat,
768 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
772 chat_command_clear (EmpathyChat *chat,
775 empathy_chat_view_clear (chat->view);
779 chat_command_topic (EmpathyChat *chat,
782 EmpathyChatPriv *priv = GET_PRIV (chat);
784 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
785 empathy_chat_view_append_event (chat->view,
786 _("Topic not supported on this conversation"));
790 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
791 empathy_chat_view_append_event (chat->view,
792 _("You are not allowed to change the topic"));
796 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
800 empathy_chat_join_muc (EmpathyChat *chat,
803 EmpathyChatPriv *priv = GET_PRIV (chat);
805 empathy_join_muc (priv->account, room,
806 empathy_get_current_action_time ());
810 chat_command_join (EmpathyChat *chat,
815 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
817 /* FIXME: Ideally we should ask to handle ourself the channel so we can
818 * report the error if any but this is blocked by
819 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
820 while (rooms[i] != NULL) {
821 /* ignore empty strings */
822 if (!EMP_STR_EMPTY (rooms[i])) {
823 empathy_chat_join_muc (chat, rooms[i]);
831 chat_command_part (EmpathyChat *chat,
834 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
838 chat_command_msg_internal (EmpathyChat *chat,
839 const gchar *contact_id,
840 const gchar *message)
842 EmpathyChatPriv *priv = GET_PRIV (chat);
843 ChatCommandMsgData *data;
844 TpAccountChannelRequest *req;
847 request = tp_asv_new (
848 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
849 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
850 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
853 req = tp_account_channel_request_new (priv->account, request,
854 empathy_get_current_action_time ());
856 /* FIXME: We should probably search in members alias. But this
857 * is enough for IRC */
858 data = g_slice_new (ChatCommandMsgData);
860 data->message = g_strdup (message);
862 tp_account_channel_request_ensure_and_observe_channel_async (req,
863 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
865 g_object_unref (req);
866 g_hash_table_unref (request);
870 chat_command_query (EmpathyChat *chat,
873 /* If <message> part is not defined,
874 * strv[2] will be the terminal NULL */
875 chat_command_msg_internal (chat, strv[1], strv[2]);
879 chat_command_msg (EmpathyChat *chat,
882 chat_command_msg_internal (chat, strv[1], strv[2]);
886 callback_for_request_rename (TpProxy *proxy,
889 GObject *weak_object)
892 DEBUG ("Call to RequestRename method failed: %s",error->message);
897 chat_command_nick (EmpathyChat *chat,
900 EmpathyChatPriv *priv = GET_PRIV (chat);
903 proxy = TP_PROXY (tp_account_get_connection (priv->account));
905 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
906 strv[1], callback_for_request_rename, NULL, NULL, NULL);
910 chat_command_me (EmpathyChat *chat,
913 EmpathyChatPriv *priv = GET_PRIV (chat);
915 TpTextChannel *channel;
917 channel = (TpTextChannel *) (priv->tp_chat);
919 if (!tp_text_channel_supports_message_type (channel,
920 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
921 /* Action message are not supported, 'simulate' the action */
924 /* The TpChat can't be ready if it doesn't have the self contact */
925 g_assert (priv->self_contact != NULL);
927 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
929 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
934 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
938 empathy_tp_chat_send (priv->tp_chat, message);
939 g_object_unref (message);
943 chat_command_say (EmpathyChat *chat,
946 EmpathyChatPriv *priv = GET_PRIV (chat);
949 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
951 empathy_tp_chat_send (priv->tp_chat, message);
952 g_object_unref (message);
956 whois_got_contact_cb (GObject *source,
957 GAsyncResult *result,
960 EmpathyChat *chat = user_data;
961 EmpathyContact *contact;
964 contact = empathy_client_factory_dup_contact_by_id_finish (
965 EMPATHY_CLIENT_FACTORY (source), result, NULL);
967 if (contact == NULL) {
968 empathy_chat_view_append_event (chat->view, _("Invalid contact ID"));
972 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
973 g_return_if_fail (window != NULL);
974 g_return_if_fail (gtk_widget_is_toplevel (window));
975 empathy_contact_information_dialog_show (contact,
976 GTK_WINDOW (window));
978 g_object_unref (contact);
981 g_object_unref (chat);
985 chat_command_whois (EmpathyChat *chat,
988 EmpathyChatPriv *priv = GET_PRIV (chat);
990 EmpathyClientFactory *factory;
992 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
993 factory = empathy_client_factory_dup ();
995 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
996 * entered by the user (including spaces, if any). */
997 empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
998 whois_got_contact_cb, g_object_ref (chat));
1000 g_object_unref (factory);
1004 chat_command_whale (EmpathyChat *chat,
1007 EmpathyChatPriv *priv = GET_PRIV (chat);
1010 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1012 "•_______________•");
1013 empathy_tp_chat_send (priv->tp_chat, message);
1014 g_object_unref (message);
1018 chat_command_babywhale (EmpathyChat *chat,
1021 EmpathyChatPriv *priv = GET_PRIV (chat);
1024 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1027 empathy_tp_chat_send (priv->tp_chat, message);
1028 g_object_unref (message);
1032 chat_command_inspector (EmpathyChat *chat,
1035 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1036 empathy_theme_adium_show_inspector (
1037 EMPATHY_THEME_ADIUM (chat->view));
1041 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1043 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1046 const gchar *prefix;
1049 ChatCommandFunc func;
1050 gboolean (*is_supported)(EmpathyChat *chat);
1054 static ChatCommandItem commands[] = {
1055 {"clear", 1, 1, chat_command_clear, NULL,
1056 N_("/clear: clear all messages from the current conversation")},
1058 {"topic", 2, 2, chat_command_topic, NULL,
1059 N_("/topic <topic>: set the topic of the current conversation")},
1061 {"join", 2, 2, chat_command_join, NULL,
1062 N_("/join <chat room ID>: join a new chat room")},
1064 {"j", 2, 2, chat_command_join, NULL,
1065 N_("/j <chat room ID>: join a new chat room")},
1067 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1068 {"part", 1, 3, chat_command_part, part_command_supported,
1069 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1070 "by default the current one")},
1072 {"query", 2, 3, chat_command_query, NULL,
1073 N_("/query <contact ID> [<message>]: open a private chat")},
1075 {"msg", 3, 3, chat_command_msg, NULL,
1076 N_("/msg <contact ID> <message>: open a private chat")},
1078 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1079 N_("/nick <nickname>: change your nickname on the current server")},
1081 {"me", 2, 2, chat_command_me, NULL,
1082 N_("/me <message>: send an ACTION message to the current conversation")},
1084 {"say", 2, 2, chat_command_say, NULL,
1085 N_("/say <message>: send <message> to the current conversation. "
1086 "This is used to send a message starting with a '/'. For example: "
1087 "\"/say /join is used to join a new chat room\"")},
1089 {"whois", 2, 2, chat_command_whois, NULL,
1090 N_("/whois <contact ID>: display information about a contact")},
1092 {"help", 1, 2, chat_command_help, NULL,
1093 N_("/help [<command>]: show all supported commands. "
1094 "If <command> is defined, show its usage.")},
1096 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1098 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1099 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1103 chat_command_show_help (EmpathyChat *chat,
1104 ChatCommandItem *item)
1108 if (item->help == NULL) {
1112 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1113 empathy_chat_view_append_event (chat->view, str);
1118 chat_command_help (EmpathyChat *chat,
1123 /* If <command> part is not defined,
1124 * strv[1] will be the terminal NULL */
1125 if (strv[1] == NULL) {
1126 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1127 if (commands[i].is_supported != NULL) {
1128 if (!commands[i].is_supported (chat)) {
1132 if (commands[i].help == NULL) {
1135 empathy_chat_view_append_event (chat->view,
1136 _(commands[i].help));
1141 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1142 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1143 if (commands[i].is_supported != NULL) {
1144 if (!commands[i].is_supported (chat)) {
1148 if (commands[i].help == NULL) {
1151 chat_command_show_help (chat, &commands[i]);
1156 empathy_chat_view_append_event (chat->view,
1157 _("Unknown command"));
1161 chat_command_parse (const gchar *text, guint max_parts)
1166 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1168 array = g_ptr_array_sized_new (max_parts + 1);
1169 while (max_parts > 1) {
1172 /* Skip white spaces */
1173 while (g_ascii_isspace (*text)) {
1177 /* Search the end of this part, until first space. */
1178 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1184 item = g_strndup (text, end - text);
1185 g_ptr_array_add (array, item);
1186 DEBUG ("\tITEM: \"%s\"", item);
1192 /* Append last part if not empty */
1193 item = g_strstrip (g_strdup (text));
1194 if (!EMP_STR_EMPTY (item)) {
1195 g_ptr_array_add (array, item);
1196 DEBUG ("\tITEM: \"%s\"", item);
1201 /* Make the array NULL-terminated */
1202 g_ptr_array_add (array, NULL);
1204 return (GStrv) g_ptr_array_free (array, FALSE);
1208 has_prefix_case (const gchar *s,
1209 const gchar *prefix)
1211 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1215 chat_send (EmpathyChat *chat,
1218 EmpathyChatPriv *priv;
1222 if (EMP_STR_EMPTY (msg)) {
1226 priv = GET_PRIV (chat);
1228 chat_input_history_add (chat, msg, FALSE);
1230 if (msg[0] == '/') {
1231 gboolean second_slash = FALSE;
1232 const gchar *iter = msg + 1;
1234 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1239 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1242 c = *(msg + 1 + strlen (commands[i].prefix));
1243 if (c != '\0' && !g_ascii_isspace (c)) {
1246 if (commands[i].is_supported != NULL) {
1247 if (!commands[i].is_supported (chat)) {
1252 /* We can't use g_strsplit here because it does
1253 * not deal correctly if we have more than one space
1255 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1257 strv_len = g_strv_length (strv);
1258 if (strv_len < commands[i].min_parts ||
1259 strv_len > commands[i].max_parts) {
1260 chat_command_show_help (chat, &commands[i]);
1265 commands[i].func (chat, strv);
1270 /* Also allow messages with two slashes before the
1271 * first space, so it is possible to send a /unix/path.
1272 * This heuristic is kind of crap. */
1273 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1275 second_slash = TRUE;
1281 if (!second_slash) {
1282 empathy_chat_view_append_event (chat->view,
1283 _("Unknown command; see /help for the available"
1289 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1291 empathy_tp_chat_send (priv->tp_chat, message);
1292 g_object_unref (message);
1296 chat_input_text_view_send (EmpathyChat *chat)
1298 GtkTextBuffer *buffer;
1299 GtkTextIter start, end;
1302 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1304 gtk_text_buffer_get_bounds (buffer, &start, &end);
1305 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1307 /* clear the input field */
1308 gtk_text_buffer_set_text (buffer, "", -1);
1309 /* delete input history modifications */
1310 chat_input_history_revert (chat);
1312 chat_send (chat, msg);
1317 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1318 TpContact *tp_contact,
1319 TpChannelChatState state,
1322 EmpathyChatPriv *priv;
1324 gboolean was_composing;
1325 EmpathyContact *contact;
1327 priv = GET_PRIV (chat);
1329 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1331 if (empathy_contact_is_user (contact)) {
1332 /* We don't care about our own chat state */
1336 was_composing = (priv->compositors != NULL);
1338 /* Find the contact in the list. After that l is the list elem or NULL */
1339 for (l = priv->compositors; l; l = l->next) {
1340 if (contact == l->data) {
1346 case TP_CHANNEL_CHAT_STATE_GONE:
1347 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1348 case TP_CHANNEL_CHAT_STATE_PAUSED:
1349 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1350 /* Contact is not composing */
1352 priv->compositors = g_list_remove_link (priv->compositors, l);
1353 g_object_unref (l->data);
1357 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1358 /* Contact is composing */
1360 priv->compositors = g_list_prepend (priv->compositors,
1361 g_object_ref (contact));
1365 g_assert_not_reached ();
1368 DEBUG ("Was composing: %s now composing: %s",
1369 was_composing ? "yes" : "no",
1370 priv->compositors ? "yes" : "no");
1372 if ((was_composing && !priv->compositors) ||
1373 (!was_composing && priv->compositors)) {
1374 /* Composing state changed */
1375 g_signal_emit (chat, signals[COMPOSING], 0,
1376 priv->compositors != NULL);
1380 g_object_unref (contact);
1384 get_highlight_regex_for (const gchar *name)
1387 gchar *name_esc, *pattern;
1388 GError *error = NULL;
1390 name_esc = g_regex_escape_string (name, -1);
1391 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1392 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1395 if (regex == NULL) {
1396 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1399 g_error_free (error);
1408 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1409 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1412 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1414 EmpathyChatPriv *priv = GET_PRIV (chat);
1416 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1418 if (priv->self_contact != NULL) {
1419 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1421 g_return_if_fail (alias != NULL);
1422 priv->highlight_regex = get_highlight_regex_for (alias);
1427 chat_should_highlight (EmpathyChat *chat,
1428 EmpathyMessage *message)
1430 EmpathyChatPriv *priv = GET_PRIV (chat);
1432 TpChannelTextMessageFlags flags;
1434 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1436 if (!empathy_chat_is_room (chat)) {
1440 if (!empathy_message_is_incoming (message)) {
1444 msg = empathy_message_get_body (message);
1449 flags = empathy_message_get_flags (message);
1450 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1451 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1452 * have already been received by the user before (and so are in the logs) */
1456 if (priv->highlight_regex == NULL) {
1460 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1464 chat_message_received (EmpathyChat *chat,
1465 EmpathyMessage *message,
1468 EmpathyChatPriv *priv = GET_PRIV (chat);
1469 EmpathyContact *sender;
1471 sender = empathy_message_get_sender (message);
1473 if (empathy_message_is_edit (message)) {
1474 DEBUG ("Editing message '%s' to '%s'",
1475 empathy_message_get_supersedes (message),
1476 empathy_message_get_body (message));
1478 empathy_chat_view_edit_message (chat->view, message);
1480 gboolean should_highlight = chat_should_highlight (chat, message);
1482 if (should_highlight) {
1483 priv->highlighted = TRUE;
1486 DEBUG ("Appending new message '%s' from %s (%d)",
1487 empathy_message_get_token (message),
1488 empathy_contact_get_alias (sender),
1489 empathy_contact_get_handle (sender));
1491 empathy_chat_view_append_message (chat->view, message, should_highlight);
1493 if (empathy_message_is_incoming (message)) {
1494 priv->unread_messages++;
1495 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1498 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1502 /* We received a message so the contact is no longer
1504 chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
1505 TP_CHANNEL_CHAT_STATE_ACTIVE,
1510 chat_message_received_cb (EmpathyTpChat *tp_chat,
1511 EmpathyMessage *message,
1514 chat_message_received (chat, message, FALSE);
1518 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1519 EmpathyMessage *message,
1522 EmpathyChatPriv *priv = GET_PRIV (chat);
1524 empathy_chat_view_message_acknowledged (chat->view,
1527 if (!empathy_message_is_edit (message)) {
1528 priv->unread_messages--;
1529 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1534 append_balance_error (EmpathyChat *chat,
1535 const gchar *message_body)
1537 EmpathyChatPriv *priv = GET_PRIV (chat);
1538 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1539 const gchar *uri = tp_connection_get_balance_uri (conn);
1540 const gchar *error = _("insufficient balance to send message");
1541 gchar *str, *str_markup = NULL;
1543 if (message_body != NULL) {
1544 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1546 str = g_strdup_printf (_("Error sending message: %s"), error);
1549 if (!tp_str_empty (uri)) {
1550 /* translators: error used when user doesn't have enough credit on his
1551 * account to send the message. */
1552 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1553 " <a href='%s'>Top up</a>."), uri);
1555 if (message_body != NULL) {
1556 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1558 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1559 escaped_body, markup_error);
1561 g_free (escaped_body);
1563 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1566 g_free (markup_error);
1569 if (str_markup != NULL)
1570 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1572 empathy_chat_view_append_event (chat->view, str);
1575 g_free (str_markup);
1579 chat_send_error_cb (EmpathyTpChat *tp_chat,
1580 const gchar *message_body,
1581 TpChannelTextSendError error_code,
1582 const gchar *dbus_error,
1585 const gchar *error = NULL;
1588 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1589 append_balance_error (chat, message_body);
1591 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1592 error = _("not capable");
1595 if (error == NULL) {
1596 /* if we didn't find a dbus-error, try the old error */
1597 switch (error_code) {
1598 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1599 error = _("offline");
1601 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1602 error = _("invalid contact");
1604 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1605 error = _("permission denied");
1607 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1608 error = _("too long message");
1610 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1611 error = _("not implemented");
1613 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1615 error = _("unknown");
1620 if (message_body != NULL) {
1621 str = g_strdup_printf (_("Error sending message '%s': %s"),
1622 message_body, error);
1625 str = g_strdup_printf (_("Error sending message: %s"), error);
1628 empathy_chat_view_append_event (chat->view, str);
1633 chat_topic_label_size_allocate_cb (GtkLabel *label,
1634 GtkAllocation *allocation,
1637 EmpathyChatPriv *priv = GET_PRIV (chat);
1639 if (!gtk_label_get_line_wrap (label)) {
1640 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1641 gtk_widget_show (priv->expander_topic);
1643 gtk_widget_hide (priv->expander_topic);
1650 chat_topic_expander_activate_cb (GtkExpander *expander,
1651 GParamSpec *param_spec,
1654 EmpathyChatPriv *priv = GET_PRIV (chat);
1656 if (gtk_expander_get_expanded (expander)) {
1657 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1658 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1660 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1661 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1666 chat_subject_changed_cb (EmpathyChat *chat)
1668 EmpathyChatPriv *priv = GET_PRIV (chat);
1670 g_free (priv->subject);
1671 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1672 g_object_notify (G_OBJECT (chat), "subject");
1674 if (EMP_STR_EMPTY (priv->subject)) {
1675 gtk_widget_hide (priv->hbox_topic);
1677 gchar *markup_topic;
1680 markup_topic = empathy_add_link_markup (priv->subject);
1681 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1682 _("Topic:"), markup_topic);
1684 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1685 g_free (markup_text);
1686 g_free (markup_topic);
1688 gtk_widget_show (priv->hbox_topic);
1690 if (priv->block_events_timeout_id == 0) {
1693 if (!EMP_STR_EMPTY (priv->subject)) {
1694 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1696 if (tp_str_empty (actor)) {
1697 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1699 str = g_strdup_printf (_("Topic set by %s to: %s"),
1700 actor, priv->subject);
1702 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1703 /* No need to display this 'event' is no topic can be defined anyway */
1704 str = g_strdup (_("No topic defined"));
1708 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1715 chat_title_changed_cb (EmpathyChat *chat)
1717 EmpathyChatPriv *priv = GET_PRIV (chat);
1719 g_free (priv->name);
1720 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1721 g_object_notify (G_OBJECT (chat), "name");
1725 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1729 GtkTextIter word_start = *iter;
1730 GtkTextIter word_end = *iter;
1733 if (gtk_text_iter_inside_word (&word_end) &&
1734 !gtk_text_iter_ends_word (&word_end)) {
1735 gtk_text_iter_forward_word_end (&word_end);
1740 if (gtk_text_iter_get_char (&tmp) == '\'') {
1741 gtk_text_iter_forward_char (&tmp);
1743 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1744 gtk_text_iter_forward_word_end (&word_end);
1749 if (gtk_text_iter_inside_word (&word_start) ||
1750 gtk_text_iter_ends_word (&word_start)) {
1751 if (!gtk_text_iter_starts_word (&word_start) ||
1752 gtk_text_iter_equal (&word_start, &word_end)) {
1753 gtk_text_iter_backward_word_start (&word_start);
1757 gtk_text_iter_backward_char (&tmp);
1759 if (gtk_text_iter_get_char (&tmp) == '\'') {
1760 gtk_text_iter_backward_char (&tmp);
1762 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1763 gtk_text_iter_backward_word_start (&word_start);
1768 *start = word_start;
1774 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1775 GtkTextIter *location,
1780 GtkTextIter iter, pos;
1782 /* Remove all misspelled tags in the inserted text.
1783 * This happens when text is inserted within a misspelled word. */
1784 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1785 gtk_text_iter_get_offset (location) - len);
1786 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1789 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1792 GtkTextIter start, end;
1795 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1798 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1800 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1801 gtk_text_iter_equal (&pos, &end) ||
1802 empathy_spell_check (str)) {
1803 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1805 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1810 } while (gtk_text_iter_forward_word_end (&iter) &&
1811 gtk_text_iter_compare (&iter, location) <= 0);
1815 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1820 GtkTextIter word_start, word_end;
1822 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1823 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1824 &word_start, &word_end);
1829 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1832 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1833 chat_composing_stop (chat);
1835 chat_composing_start (chat);
1840 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1845 GtkTextIter prev_pos;
1846 GtkTextIter word_start;
1847 GtkTextIter word_end;
1851 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1853 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1854 gtk_text_buffer_get_insert (buffer));
1855 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1857 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1860 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1861 !gtk_text_iter_equal (&pos, &word_end)) {
1862 str = gtk_text_buffer_get_text (buffer,
1863 &word_start, &word_end, FALSE);
1865 if (!empathy_spell_check (str)) {
1866 gtk_text_buffer_apply_tag_by_name (buffer,
1867 "misspelled", &word_start, &word_end);
1869 gtk_text_buffer_remove_tag_by_name (buffer,
1870 "misspelled", &word_start, &word_end);
1877 gtk_text_buffer_move_mark (buffer, mark, &pos);
1881 empathy_isspace_cb (gunichar c,
1884 return g_unichar_isspace (c);
1888 chat_input_key_press_event_cb (GtkWidget *widget,
1892 EmpathyChatPriv *priv;
1895 GtkWidget *text_view_sw;
1897 priv = GET_PRIV (chat);
1899 priv->most_recent_event_type = event->type;
1901 /* Catch ctrl+up/down so we can traverse messages we sent */
1902 if ((event->state & GDK_CONTROL_MASK) &&
1903 (event->keyval == GDK_KEY_Up ||
1904 event->keyval == GDK_KEY_Down)) {
1905 GtkTextBuffer *buffer;
1908 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1909 chat_input_history_update (chat, buffer);
1911 if (event->keyval == GDK_KEY_Up) {
1912 str = chat_input_history_get_next (chat);
1914 str = chat_input_history_get_prev (chat);
1917 g_signal_handlers_block_by_func (buffer,
1918 chat_input_text_buffer_changed_cb,
1920 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1921 g_signal_handlers_unblock_by_func (buffer,
1922 chat_input_text_buffer_changed_cb,
1928 /* Catch enter but not ctrl/shift-enter */
1929 if (IS_ENTER (event->keyval) &&
1930 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1933 /* This is to make sure that kinput2 gets the enter. And if
1934 * it's handled there we shouldn't send on it. This is because
1935 * kinput2 uses Enter to commit letters. See:
1936 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1939 view = GTK_TEXT_VIEW (chat->input_text_view);
1940 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1941 gtk_text_view_reset_im_context (view);
1945 chat_input_text_view_send (chat);
1949 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1951 if (IS_ENTER (event->keyval) &&
1952 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1953 /* Newline for shift/control-enter. */
1956 if (!(event->state & GDK_CONTROL_MASK) &&
1957 event->keyval == GDK_KEY_Page_Up) {
1958 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1959 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1962 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1963 event->keyval == GDK_KEY_Page_Down) {
1964 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1965 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1966 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1967 gtk_adjustment_set_value (adj, val);
1970 if (event->keyval == GDK_KEY_Escape) {
1971 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1973 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1974 event->keyval == GDK_KEY_Tab) {
1975 GtkTextBuffer *buffer;
1976 GtkTextIter start, current;
1977 gchar *nick, *completed;
1978 GList *list, *completed_list;
1979 gboolean is_start_of_buffer;
1981 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1982 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1984 /* Get the start of the nick to complete. */
1985 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1986 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1987 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1989 is_start_of_buffer = gtk_text_iter_is_start (&start);
1991 list = empathy_tp_chat_get_members (priv->tp_chat);
1992 g_completion_add_items (priv->completion, list);
1994 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1995 completed_list = g_completion_complete (priv->completion,
2004 GString *message = NULL;
2007 gtk_text_buffer_delete (buffer, &start, ¤t);
2009 len = g_list_length (completed_list);
2012 /* If we only have one hit, use that text
2013 * instead of the text in completed since the
2014 * completed text will use the typed string
2015 * which might be cased all wrong.
2018 text = empathy_contact_get_alias (completed_list->data);
2022 /* Print all hits to the scrollback view, so the
2023 * user knows what possibilities he has.
2026 message = g_string_new ("");
2027 for (l = completed_list; l != NULL; l = l->next) {
2028 g_string_append (message, empathy_contact_get_alias (l->data));
2029 g_string_append (message, " - ");
2031 empathy_chat_view_append_event (chat->view, message->str);
2032 g_string_free (message, TRUE);
2035 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2037 if (len == 1 && is_start_of_buffer) {
2038 gchar *complete_char;
2040 complete_char = g_settings_get_string (
2041 priv->gsettings_chat,
2042 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2044 if (complete_char != NULL) {
2045 gtk_text_buffer_insert_at_cursor (buffer,
2047 strlen (complete_char));
2048 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2049 g_free (complete_char);
2056 g_completion_clear_items (priv->completion);
2058 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2068 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2072 gtk_widget_grab_focus (chat->input_text_view);
2078 chat_input_realize_cb (GtkWidget *widget,
2081 DEBUG ("Setting focus to the input text view");
2082 if (gtk_widget_is_sensitive (widget)) {
2083 gtk_widget_grab_focus (widget);
2088 chat_input_has_focus_notify_cb (GtkWidget *widget,
2092 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2096 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2097 EmpathySmiley *smiley,
2100 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2101 GtkTextBuffer *buffer;
2104 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2106 gtk_text_buffer_get_end_iter (buffer, &iter);
2107 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2109 gtk_text_buffer_get_end_iter (buffer, &iter);
2110 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2121 static EmpathyChatSpell *
2122 chat_spell_new (EmpathyChat *chat,
2127 EmpathyChatSpell *chat_spell;
2129 chat_spell = g_slice_new0 (EmpathyChatSpell);
2131 chat_spell->chat = g_object_ref (chat);
2132 chat_spell->word = g_strdup (word);
2133 chat_spell->start = start;
2134 chat_spell->end = end;
2140 chat_spell_free (EmpathyChatSpell *chat_spell)
2142 g_object_unref (chat_spell->chat);
2143 g_free (chat_spell->word);
2144 g_slice_free (EmpathyChatSpell, chat_spell);
2148 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2149 EmpathyChatSpell *chat_spell)
2151 empathy_chat_correct_word (chat_spell->chat,
2152 &(chat_spell->start),
2154 gtk_menu_item_get_label (menu_item));
2159 chat_spelling_build_suggestions_menu (const gchar *code,
2160 EmpathyChatSpell *chat_spell)
2162 GList *suggestions, *l;
2163 GtkWidget *menu, *menu_item;
2165 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2166 if (suggestions == NULL)
2169 menu = gtk_menu_new ();
2170 for (l = suggestions; l; l = l->next) {
2171 menu_item = gtk_menu_item_new_with_label (l->data);
2172 g_signal_connect (G_OBJECT (menu_item), "activate",
2173 G_CALLBACK (chat_spelling_menu_activate_cb),
2175 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2177 empathy_spell_free_suggestions (suggestions);
2179 gtk_widget_show_all (menu);
2185 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2187 GtkWidget *menu, *submenu, *item;
2190 codes = empathy_spell_get_enabled_language_codes ();
2191 g_assert (codes != NULL);
2193 if (g_list_length (codes) > 1) {
2194 menu = gtk_menu_new ();
2196 for (l = codes; l; l = l->next) {
2197 const gchar *code, *name;
2200 name = empathy_spell_get_language_name (code);
2204 item = gtk_image_menu_item_new_with_label (name);
2206 submenu = chat_spelling_build_suggestions_menu (
2208 if (submenu == NULL)
2209 gtk_widget_set_sensitive (item, FALSE);
2211 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2213 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2216 menu = chat_spelling_build_suggestions_menu (codes->data,
2219 menu = gtk_menu_new ();
2220 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2221 gtk_widget_set_sensitive (item, FALSE);
2222 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2225 g_list_free (codes);
2227 gtk_widget_show_all (menu);
2238 static EmpathyChatWord *
2239 chat_word_new (EmpathyChat *chat,
2243 EmpathyChatWord *chat_word;
2245 chat_word = g_slice_new0 (EmpathyChatWord);
2247 chat_word->chat = g_object_ref (chat);
2248 chat_word->word = g_strdup (word);
2249 chat_word->code = g_strdup (code);
2255 chat_word_free (EmpathyChatWord *chat_word)
2257 g_object_unref (chat_word->chat);
2258 g_free (chat_word->word);
2259 g_free (chat_word->code);
2260 g_slice_free (EmpathyChatWord, chat_word);
2264 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2265 EmpathyChatWord *chat_word)
2267 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2269 empathy_spell_add_to_dictionary (chat_word->code,
2271 priv->update_misspelled_words_id =
2272 g_idle_add (update_misspelled_words, chat_word->chat);
2276 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2278 GtkWidget *menu, *item, *lang_item, *image;
2281 const gchar *code, *name;
2282 EmpathyChatWord *chat_word;
2284 codes = empathy_spell_get_enabled_language_codes ();
2285 g_assert (codes != NULL);
2286 if (g_list_length (codes) > 1) {
2287 /* translators: %s is the selected word */
2288 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2290 item = gtk_image_menu_item_new_with_mnemonic (label);
2292 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2293 GTK_ICON_SIZE_MENU);
2294 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2297 menu = gtk_menu_new ();
2299 for (l = codes; l; l = l->next) {
2301 name = empathy_spell_get_language_name (code);
2305 lang_item = gtk_image_menu_item_new_with_label (name);
2307 chat_word= chat_word_new (chat_spell->chat,
2308 chat_spell->word, code);
2309 g_object_set_data_full (G_OBJECT (lang_item),
2310 "chat-word", chat_word,
2311 (GDestroyNotify) chat_word_free);
2313 g_signal_connect (G_OBJECT (lang_item), "activate",
2314 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2316 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2318 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2321 name = empathy_spell_get_language_name (code);
2322 g_assert (name != NULL);
2323 /* translators: first %s is the selected word,
2324 * second %s is the language name of the target dictionary */
2325 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2326 chat_spell->word, name);
2327 item = gtk_image_menu_item_new_with_mnemonic (label);
2329 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2330 GTK_ICON_SIZE_MENU);
2331 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2333 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2335 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2336 (GDestroyNotify) chat_word_free);
2338 g_signal_connect (G_OBJECT (item), "activate",
2339 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2342 g_list_free (codes);
2344 gtk_widget_show_all (item);
2350 chat_text_send_cb (GtkMenuItem *menuitem,
2353 chat_input_text_view_send (chat);
2357 chat_input_button_press_event_cb (GtkTextView *view,
2358 GdkEventButton *event,
2361 EmpathyChatPriv *priv = GET_PRIV (chat);
2363 priv->most_recent_event_type = event->type;
2369 chat_input_populate_popup_cb (GtkTextView *view,
2373 EmpathyChatPriv *priv = GET_PRIV (chat);
2374 GtkTextBuffer *buffer;
2375 GtkTextTagTable *table;
2378 GtkTextIter iter, start, end;
2381 EmpathyChatSpell *chat_spell;
2382 GtkWidget *spell_menu;
2383 GtkWidget *spell_item;
2384 EmpathySmileyManager *smiley_manager;
2385 GtkWidget *smiley_menu;
2388 buffer = gtk_text_view_get_buffer (view);
2390 /* Add the emoticon menu. */
2391 item = gtk_separator_menu_item_new ();
2392 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2393 gtk_widget_show (item);
2395 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2396 image = gtk_image_new_from_icon_name ("face-smile",
2397 GTK_ICON_SIZE_MENU);
2398 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2399 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2400 gtk_widget_show (item);
2402 smiley_manager = empathy_smiley_manager_dup_singleton ();
2403 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2404 chat_insert_smiley_activate_cb,
2406 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2407 g_object_unref (smiley_manager);
2409 /* Add the Send menu item. */
2410 gtk_text_buffer_get_bounds (buffer, &start, &end);
2411 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2412 if (!EMP_STR_EMPTY (str)) {
2413 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2414 g_signal_connect (G_OBJECT (item), "activate",
2415 G_CALLBACK (chat_text_send_cb), chat);
2416 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2417 gtk_widget_show (item);
2421 /* Add the spell check menu item. */
2422 table = gtk_text_buffer_get_tag_table (buffer);
2423 tag = gtk_text_tag_table_lookup (table, "misspelled");
2425 switch (priv->most_recent_event_type) {
2426 case GDK_BUTTON_PRESS:
2427 /* get the location from the pointer */
2428 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2429 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2430 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2432 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2433 GTK_TEXT_WINDOW_WIDGET,
2436 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2441 g_warn_if_reached ();
2442 /* assume the KEY_PRESS case */
2445 /* get the location from the cursor */
2446 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2447 gtk_text_buffer_get_insert (buffer));
2453 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2454 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2456 str = gtk_text_buffer_get_text (buffer,
2457 &start, &end, FALSE);
2459 if (!EMP_STR_EMPTY (str)) {
2460 chat_spell = chat_spell_new (chat, str, start, end);
2461 g_object_set_data_full (G_OBJECT (menu),
2462 "chat-spell", chat_spell,
2463 (GDestroyNotify) chat_spell_free);
2465 item = gtk_separator_menu_item_new ();
2466 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2467 gtk_widget_show (item);
2469 /* Spelling suggestions */
2470 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2471 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2472 GTK_ICON_SIZE_MENU);
2473 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2475 spell_menu = chat_spelling_build_menu (chat_spell);
2476 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2479 spell_item = gtk_separator_menu_item_new ();
2480 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2481 gtk_widget_show (spell_item);
2483 /* Add to dictionary */
2484 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2486 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2487 gtk_widget_show (spell_item);
2489 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2490 gtk_widget_show (item);
2496 chat_log_filter (TplEvent *event,
2499 EmpathyChat *chat = user_data;
2500 EmpathyMessage *message;
2501 EmpathyChatPriv *priv = GET_PRIV (chat);
2502 const GList *pending;
2504 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2505 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2507 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2508 message = empathy_message_from_tpl_log_event (event);
2510 for (; pending; pending = g_list_next (pending)) {
2511 if (empathy_message_equal (message, pending->data)) {
2512 g_object_unref (message);
2517 g_object_unref (message);
2523 show_pending_messages (EmpathyChat *chat) {
2524 EmpathyChatPriv *priv = GET_PRIV (chat);
2525 const GList *messages, *l;
2527 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2529 if (chat->view == NULL || priv->tp_chat == NULL)
2532 if (!priv->can_show_pending)
2535 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2537 for (l = messages; l != NULL ; l = g_list_next (l)) {
2538 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2539 chat_message_received (chat, message, TRUE);
2545 got_filtered_messages_cb (GObject *manager,
2546 GAsyncResult *result,
2551 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2552 EmpathyChatPriv *priv = GET_PRIV (chat);
2553 GError *error = NULL;
2555 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2556 result, &messages, &error)) {
2557 DEBUG ("%s. Aborting.", error->message);
2558 empathy_chat_view_append_event (chat->view,
2559 _("Failed to retrieve recent logs"));
2560 g_error_free (error);
2564 for (l = messages; l; l = g_list_next (l)) {
2565 EmpathyMessage *message;
2567 g_assert (TPL_IS_EVENT (l->data));
2569 message = empathy_message_from_tpl_log_event (l->data);
2570 g_object_unref (l->data);
2572 if (empathy_message_is_edit (message)) {
2573 /* this is an edited message, create a synthetic event
2574 * using the supersedes token and
2575 * original-message-sent timestamp, that we can then
2577 EmpathyMessage *syn_msg = g_object_new (
2578 EMPATHY_TYPE_MESSAGE,
2580 "token", empathy_message_get_supersedes (message),
2581 "type", empathy_message_get_tptype (message),
2582 "timestamp", empathy_message_get_original_timestamp (message),
2583 "incoming", empathy_message_is_incoming (message),
2585 "receiver", empathy_message_get_receiver (message),
2586 "sender", empathy_message_get_sender (message),
2589 empathy_chat_view_append_message (chat->view, syn_msg,
2590 chat_should_highlight (chat, syn_msg));
2591 empathy_chat_view_edit_message (chat->view, message);
2593 g_object_unref (syn_msg);
2595 /* append the latest message */
2596 empathy_chat_view_append_message (chat->view, message,
2597 chat_should_highlight (chat, message));
2600 g_object_unref (message);
2602 g_list_free (messages);
2605 /* in case of TPL error, skip backlog and show pending messages */
2606 priv->can_show_pending = TRUE;
2607 show_pending_messages (chat);
2609 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2610 * about it in EmpathyChatPriv definition */
2611 priv->retrieving_backlogs = FALSE;
2612 empathy_chat_messages_read (chat);
2614 /* Turn back on scrolling */
2615 empathy_chat_view_scroll (chat->view, TRUE);
2619 chat_add_logs (EmpathyChat *chat)
2621 EmpathyChatPriv *priv = GET_PRIV (chat);
2628 /* Turn off scrolling temporarily */
2629 empathy_chat_view_scroll (chat->view, FALSE);
2631 /* Add messages from last conversation */
2632 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2633 target = tpl_entity_new_from_room_id (priv->id);
2635 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2637 priv->retrieving_backlogs = TRUE;
2638 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2641 TPL_EVENT_MASK_TEXT,
2645 got_filtered_messages_cb,
2648 g_object_unref (target);
2652 chat_contacts_completion_func (const gchar *s1,
2656 gchar *tmp, *nick1, *nick2;
2663 return s1 ? -1 : +1;
2666 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2667 nick1 = g_utf8_casefold (tmp, -1);
2670 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2671 nick2 = g_utf8_casefold (tmp, -1);
2674 ret = strncmp (nick1, nick2, n);
2683 build_part_message (guint reason,
2685 EmpathyContact *actor,
2686 const gchar *message)
2688 GString *s = g_string_new ("");
2689 const gchar *actor_name = NULL;
2691 if (actor != NULL) {
2692 actor_name = empathy_contact_get_alias (actor);
2695 /* Having an actor only really makes sense for a few actions... */
2697 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2698 g_string_append_printf (s, _("%s has disconnected"), name);
2700 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2701 if (actor_name != NULL) {
2702 /* translators: reverse the order of these arguments
2703 * if the kicked should come before the kicker in your locale.
2705 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2708 g_string_append_printf (s, _("%s was kicked"), name);
2711 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2712 if (actor_name != NULL) {
2713 /* translators: reverse the order of these arguments
2714 * if the banned should come before the banner in your locale.
2716 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2719 g_string_append_printf (s, _("%s was banned"), name);
2723 g_string_append_printf (s, _("%s has left the room"), name);
2726 if (!EMP_STR_EMPTY (message)) {
2727 /* Note to translators: this string is appended to
2728 * notifications like "foo has left the room", with the message
2729 * given by the user living the room. If this poses a problem,
2730 * please let us know. :-)
2732 g_string_append_printf (s, _(" (%s)"), message);
2735 return g_string_free (s, FALSE);
2739 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2740 EmpathyContact *contact,
2741 EmpathyContact *actor,
2747 EmpathyChatPriv *priv = GET_PRIV (chat);
2748 const gchar *name = empathy_contact_get_alias (contact);
2751 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2753 if (priv->block_events_timeout_id != 0)
2757 str = g_strdup_printf (_("%s has joined the room"),
2760 str = build_part_message (reason, name, actor, message);
2763 empathy_chat_view_append_event (chat->view, str);
2768 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2769 EmpathyContact *old_contact,
2770 EmpathyContact *new_contact,
2775 EmpathyChatPriv *priv = GET_PRIV (chat);
2777 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2779 if (priv->block_events_timeout_id == 0) {
2782 str = g_strdup_printf (_("%s is now known as %s"),
2783 empathy_contact_get_alias (old_contact),
2784 empathy_contact_get_alias (new_contact));
2785 empathy_chat_view_append_event (chat->view, str);
2792 chat_contacts_visible_timeout_cb (gpointer chat)
2794 EmpathyChatPriv *priv = GET_PRIV (chat);
2796 /* Relax the size request */
2797 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2799 /* Set the position of the slider. This must be done here because
2800 * GtkPaned need to know its size allocation and it will be settled only
2801 * after the gtk_window_resize () tough effect. */
2802 if (priv->contacts_width > 0) {
2803 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2804 priv->contacts_width);
2807 priv->contacts_visible_id = 0;
2813 chat_update_contacts_visibility (EmpathyChat *chat,
2816 EmpathyChatPriv *priv = GET_PRIV (chat);
2818 if (!priv->scrolled_window_contacts) {
2822 if (priv->remote_contact != NULL) {
2826 if (show && priv->contact_list_view == NULL) {
2827 EmpathyIndividualStore *store;
2829 GtkAllocation allocation;
2831 /* We are adding the contact list to the chat, we don't want the
2832 * chat view to become too small. If the chat view is already
2833 * smaller than 250 make sure that size won't change. If the
2834 * chat view is bigger the contact list will take some space on
2835 * it but we make sure the chat view don't become smaller than
2836 * 250. Relax the size request once the resize is done */
2837 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2838 min_width = MIN (allocation.width, 250);
2839 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2841 /* There is no way to know when the window resize will happen
2842 * since it is WM's decision. Let's hope it won't be longer. */
2843 if (priv->contacts_visible_id != 0)
2844 g_source_remove (priv->contacts_visible_id);
2845 priv->contacts_visible_id = g_timeout_add (500,
2846 chat_contacts_visible_timeout_cb, chat);
2848 store = EMPATHY_INDIVIDUAL_STORE (
2849 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2851 empathy_individual_store_set_show_groups (store, FALSE);
2853 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2854 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2855 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2856 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2857 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2858 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2859 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2861 empathy_individual_view_set_show_offline (
2862 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2863 empathy_individual_view_set_show_uninteresting (
2864 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2866 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2867 priv->contact_list_view);
2869 gtk_widget_show (priv->contact_list_view);
2870 gtk_widget_show (priv->scrolled_window_contacts);
2871 g_object_unref (store);
2873 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2874 gtk_widget_hide (priv->scrolled_window_contacts);
2875 if (priv->contact_list_view != NULL) {
2876 gtk_widget_destroy (priv->contact_list_view);
2877 priv->contact_list_view = NULL;
2883 empathy_chat_set_show_contacts (EmpathyChat *chat,
2886 EmpathyChatPriv *priv = GET_PRIV (chat);
2888 priv->show_contacts = show;
2890 chat_update_contacts_visibility (chat, show);
2892 g_object_notify (G_OBJECT (chat), "show-contacts");
2896 chat_self_contact_changed_cb (EmpathyChat *chat)
2898 EmpathyChatPriv *priv = GET_PRIV (chat);
2900 if (priv->self_contact != NULL) {
2901 g_signal_handlers_disconnect_by_func (priv->self_contact,
2902 chat_self_contact_alias_changed_cb,
2905 g_clear_object (&priv->self_contact);
2907 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2908 if (priv->self_contact != NULL) {
2909 g_object_ref (priv->self_contact);
2911 if (empathy_chat_is_room (chat)) {
2912 g_signal_connect_swapped (priv->self_contact, "notify::alias",
2913 G_CALLBACK (chat_self_contact_alias_changed_cb),
2918 chat_self_contact_alias_changed_cb (chat);
2922 chat_remote_contact_changed_cb (EmpathyChat *chat)
2924 EmpathyChatPriv *priv = GET_PRIV (chat);
2926 if (priv->remote_contact != NULL) {
2927 g_object_unref (priv->remote_contact);
2928 priv->remote_contact = NULL;
2933 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2934 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2935 if (priv->remote_contact != NULL) {
2936 g_object_ref (priv->remote_contact);
2937 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2939 else if (priv->tp_chat != NULL) {
2940 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2943 chat_update_contacts_visibility (chat, priv->show_contacts);
2945 g_object_notify (G_OBJECT (chat), "remote-contact");
2946 g_object_notify (G_OBJECT (chat), "id");
2950 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2956 EmpathyChatPriv *priv;
2958 priv = GET_PRIV (chat);
2960 if (!priv->tp_chat) {
2964 chat_composing_remove_timeout (chat);
2965 g_object_unref (priv->tp_chat);
2966 priv->tp_chat = NULL;
2967 g_object_notify (G_OBJECT (chat), "tp-chat");
2969 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2970 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2972 chat_update_contacts_visibility (chat, FALSE);
2974 priv->unread_messages_when_offline = priv->unread_messages;
2978 update_misspelled_words (gpointer data)
2980 EmpathyChat *chat = EMPATHY_CHAT (data);
2981 EmpathyChatPriv *priv = GET_PRIV (chat);
2982 GtkTextBuffer *buffer;
2986 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2988 gtk_text_buffer_get_end_iter (buffer, &iter);
2989 length = gtk_text_iter_get_offset (&iter);
2990 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2991 NULL, length, chat);
2993 priv->update_misspelled_words_id = 0;
2999 conf_spell_checking_cb (GSettings *gsettings_chat,
3003 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3004 EmpathyChatPriv *priv = GET_PRIV (chat);
3005 gboolean spell_checker;
3006 GtkTextBuffer *buffer;
3008 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3011 spell_checker = g_settings_get_boolean (gsettings_chat,
3012 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3014 if (!empathy_spell_supported ()) {
3015 spell_checker = FALSE;
3018 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3020 if (spell_checker == priv->spell_checking_enabled) {
3021 if (spell_checker) {
3022 /* Possibly changed dictionaries,
3023 * update misspelled words. Need to do so in idle
3024 * so the spell checker is updated. */
3025 priv->update_misspelled_words_id =
3026 g_idle_add (update_misspelled_words, chat);
3032 if (spell_checker) {
3035 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3036 buffer, "notify::cursor-position",
3037 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3039 priv->insert_text_id = tp_g_signal_connect_object (
3040 buffer, "insert-text",
3041 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3042 chat, G_CONNECT_AFTER);
3043 priv->delete_range_id = tp_g_signal_connect_object (
3044 buffer, "delete-range",
3045 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3046 chat, G_CONNECT_AFTER);
3048 gtk_text_buffer_create_tag (buffer, "misspelled",
3049 "underline", PANGO_UNDERLINE_ERROR,
3052 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3053 gtk_text_buffer_get_insert (buffer));
3054 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3057 /* Mark misspelled words in the existing buffer.
3058 * Need to do so in idle so the spell checker is updated. */
3059 priv->update_misspelled_words_id =
3060 g_idle_add (update_misspelled_words, chat);
3062 GtkTextTagTable *table;
3065 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3066 priv->notify_cursor_position_id = 0;
3067 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3068 priv->insert_text_id = 0;
3069 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3070 priv->delete_range_id = 0;
3072 table = gtk_text_buffer_get_tag_table (buffer);
3073 tag = gtk_text_tag_table_lookup (table, "misspelled");
3074 gtk_text_tag_table_remove (table, tag);
3076 gtk_text_buffer_delete_mark_by_name (buffer,
3077 "previous-cursor-position");
3080 priv->spell_checking_enabled = spell_checker;
3084 save_paned_pos_timeout (gpointer data)
3086 EmpathyChat *self = data;
3089 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3091 g_settings_set_int (self->priv->gsettings_ui,
3092 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3099 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3103 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3105 if (chat->priv->save_paned_pos_id != 0)
3106 g_source_remove (chat->priv->save_paned_pos_id);
3108 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3109 save_paned_pos_timeout, chat);
3115 chat_create_ui (EmpathyChat *chat)
3117 EmpathyChatPriv *priv = GET_PRIV (chat);
3121 GtkTextBuffer *buffer;
3122 EmpathyThemeManager *theme_mgr;
3124 filename = empathy_file_lookup ("empathy-chat.ui",
3126 gui = empathy_builder_get_file (filename,
3127 "chat_widget", &priv->widget,
3128 "hpaned", &priv->hpaned,
3129 "vbox_left", &priv->vbox_left,
3130 "scrolled_window_chat", &priv->scrolled_window_chat,
3131 "scrolled_window_input", &priv->scrolled_window_input,
3132 "hbox_topic", &priv->hbox_topic,
3133 "expander_topic", &priv->expander_topic,
3134 "label_topic", &priv->label_topic,
3135 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3136 "info_bar_vbox", &priv->info_bar_vbox,
3139 empathy_builder_connect (gui, chat,
3140 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3141 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3146 /* Add message view. */
3147 theme_mgr = empathy_theme_manager_dup_singleton ();
3148 chat->view = empathy_theme_manager_create_view (theme_mgr);
3149 g_object_unref (theme_mgr);
3150 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3151 and other types, even though it's non-editable and doesn't accept any
3152 drags. This steals drag motion for anything inside the scrollbars,
3153 making drag destinations on chat windows far less useful.
3155 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3156 g_signal_connect (chat->view, "focus_in_event",
3157 G_CALLBACK (chat_text_view_focus_in_event_cb),
3159 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3160 GTK_WIDGET (chat->view));
3161 gtk_widget_show (GTK_WIDGET (chat->view));
3163 /* Add input GtkTextView */
3164 chat->input_text_view = empathy_input_text_view_new ();
3165 g_signal_connect (chat->input_text_view, "notify::has-focus",
3166 G_CALLBACK (chat_input_has_focus_notify_cb),
3168 g_signal_connect (chat->input_text_view, "key-press-event",
3169 G_CALLBACK (chat_input_key_press_event_cb),
3171 g_signal_connect (chat->input_text_view, "realize",
3172 G_CALLBACK (chat_input_realize_cb),
3174 g_signal_connect (chat->input_text_view, "button-press-event",
3175 G_CALLBACK (chat_input_button_press_event_cb),
3177 g_signal_connect (chat->input_text_view, "populate-popup",
3178 G_CALLBACK (chat_input_populate_popup_cb),
3180 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3181 tp_g_signal_connect_object (buffer, "changed",
3182 G_CALLBACK (chat_input_text_buffer_changed_cb),
3184 tp_g_signal_connect_object (priv->gsettings_chat,
3185 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3186 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3187 conf_spell_checking_cb (priv->gsettings_chat,
3188 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3189 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3190 chat->input_text_view);
3191 gtk_widget_show (chat->input_text_view);
3193 /* Add the (invisible) search bar */
3194 priv->search_bar = empathy_search_bar_new (chat->view);
3195 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3198 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3200 /* Initialy hide the topic, will be shown if not empty */
3201 gtk_widget_hide (priv->hbox_topic);
3203 g_signal_connect (priv->hpaned, "notify::position",
3204 G_CALLBACK (chat_hpaned_pos_changed_cb),
3207 /* Set widget focus order */
3208 list = g_list_append (NULL, priv->search_bar);
3209 list = g_list_append (list, priv->scrolled_window_input);
3210 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3213 list = g_list_append (NULL, priv->vbox_left);
3214 list = g_list_append (list, priv->scrolled_window_contacts);
3215 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3218 list = g_list_append (NULL, priv->hpaned);
3219 list = g_list_append (list, priv->hbox_topic);
3220 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3223 /* Add the main widget in the chat widget */
3224 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3225 g_object_unref (gui);
3229 chat_finalize (GObject *object)
3232 EmpathyChatPriv *priv;
3234 chat = EMPATHY_CHAT (object);
3235 priv = GET_PRIV (chat);
3237 DEBUG ("Finalized: %p", object);
3239 if (priv->update_misspelled_words_id != 0)
3240 g_source_remove (priv->update_misspelled_words_id);
3242 if (priv->save_paned_pos_id != 0)
3243 g_source_remove (priv->save_paned_pos_id);
3245 if (priv->contacts_visible_id != 0)
3246 g_source_remove (priv->contacts_visible_id);
3248 g_object_unref (priv->gsettings_chat);
3249 g_object_unref (priv->gsettings_ui);
3251 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3252 g_list_free (priv->input_history);
3254 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3255 g_list_free (priv->compositors);
3257 chat_composing_remove_timeout (chat);
3259 g_object_unref (priv->account_manager);
3260 g_object_unref (priv->log_manager);
3262 if (priv->tp_chat) {
3263 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3264 chat_invalidated_cb, chat);
3265 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3266 chat_message_received_cb, chat);
3267 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3268 chat_message_acknowledged_cb, chat);
3269 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3270 chat_send_error_cb, chat);
3271 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3272 chat_state_changed_cb, chat);
3273 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3274 chat_members_changed_cb, chat);
3275 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3276 chat_self_contact_changed_cb, chat);
3277 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3278 chat_remote_contact_changed_cb, chat);
3279 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3280 chat_title_changed_cb, chat);
3281 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3282 chat_subject_changed_cb, chat);
3283 empathy_tp_chat_leave (priv->tp_chat, "");
3284 g_object_unref (priv->tp_chat);
3286 if (priv->account) {
3287 g_object_unref (priv->account);
3289 if (priv->self_contact) {
3290 g_signal_handlers_disconnect_by_func (priv->self_contact,
3291 chat_self_contact_alias_changed_cb,
3293 g_object_unref (priv->self_contact);
3295 if (priv->remote_contact) {
3296 g_object_unref (priv->remote_contact);
3299 if (priv->block_events_timeout_id) {
3300 g_source_remove (priv->block_events_timeout_id);
3304 g_free (priv->name);
3305 g_free (priv->subject);
3306 g_completion_free (priv->completion);
3308 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3310 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3314 chat_constructed (GObject *object)
3316 EmpathyChat *chat = EMPATHY_CHAT (object);
3317 EmpathyChatPriv *priv = GET_PRIV (chat);
3319 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3320 /* First display logs from the logger and then display pending messages */
3321 chat_add_logs (chat);
3324 /* Just display pending messages for rooms */
3325 priv->can_show_pending = TRUE;
3326 show_pending_messages (chat);
3331 empathy_chat_class_init (EmpathyChatClass *klass)
3333 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3335 object_class->finalize = chat_finalize;
3336 object_class->get_property = chat_get_property;
3337 object_class->set_property = chat_set_property;
3338 object_class->constructed = chat_constructed;
3340 g_object_class_install_property (object_class,
3342 g_param_spec_object ("tp-chat",
3344 "The tp chat object",
3345 EMPATHY_TYPE_TP_CHAT,
3348 G_PARAM_STATIC_STRINGS));
3349 g_object_class_install_property (object_class,
3351 g_param_spec_object ("account",
3352 "Account of the chat",
3353 "The account of the chat",
3356 G_PARAM_STATIC_STRINGS));
3357 g_object_class_install_property (object_class,
3359 g_param_spec_string ("id",
3361 "The id of the chat",
3364 G_PARAM_STATIC_STRINGS));
3365 g_object_class_install_property (object_class,
3367 g_param_spec_string ("name",
3369 "The name of the chat",
3372 G_PARAM_STATIC_STRINGS));
3373 g_object_class_install_property (object_class,
3375 g_param_spec_string ("subject",
3377 "The subject or topic of the chat",
3380 G_PARAM_STATIC_STRINGS));
3381 g_object_class_install_property (object_class,
3382 PROP_REMOTE_CONTACT,
3383 g_param_spec_object ("remote-contact",
3384 "The remote contact",
3385 "The remote contact is any",
3386 EMPATHY_TYPE_CONTACT,
3388 G_PARAM_STATIC_STRINGS));
3389 g_object_class_install_property (object_class,
3391 g_param_spec_boolean ("show-contacts",
3392 "Contacts' visibility",
3393 "The visibility of the contacts' list",
3396 G_PARAM_STATIC_STRINGS));
3398 g_object_class_install_property (object_class,
3400 g_param_spec_boolean ("sms-channel",
3402 "TRUE if this channel is for sending SMSes",
3404 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3406 g_object_class_install_property (object_class,
3407 PROP_N_MESSAGES_SENDING,
3408 g_param_spec_uint ("n-messages-sending",
3409 "Num Messages Sending",
3410 "The number of messages being sent",
3412 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3414 g_object_class_install_property (object_class,
3415 PROP_NB_UNREAD_MESSAGES,
3416 g_param_spec_uint ("nb-unread-messages",
3417 "Num Unread Messages",
3418 "The number of unread messages",
3420 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3422 signals[COMPOSING] =
3423 g_signal_new ("composing",
3424 G_OBJECT_CLASS_TYPE (object_class),
3428 g_cclosure_marshal_generic,
3433 * EmpathyChat::new-message:
3434 * @self: the #EmpathyChat
3435 * @message: the new message
3436 * @pending: whether the message was in the pending queue when @self
3438 * @should_highlight: %TRUE if the message mentions the local user
3440 signals[NEW_MESSAGE] =
3441 g_signal_new ("new-message",
3442 G_OBJECT_CLASS_TYPE (object_class),
3446 g_cclosure_marshal_generic,
3448 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3450 signals[PART_COMMAND_ENTERED] =
3451 g_signal_new ("part-command-entered",
3452 G_OBJECT_CLASS_TYPE (object_class),
3456 g_cclosure_marshal_generic,
3460 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3464 chat_block_events_timeout_cb (gpointer data)
3466 EmpathyChatPriv *priv = GET_PRIV (data);
3468 priv->block_events_timeout_id = 0;
3474 account_manager_prepared_cb (GObject *source_object,
3475 GAsyncResult *result,
3478 GList *accounts, *l;
3479 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3480 EmpathyChat *chat = user_data;
3481 GError *error = NULL;
3483 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3484 DEBUG ("Failed to prepare the account manager: %s", error->message);
3485 g_error_free (error);
3489 accounts = tp_account_manager_get_valid_accounts (account_manager);
3491 for (l = accounts; l != NULL; l = l->next) {
3492 TpAccount *account = l->data;
3493 tp_g_signal_connect_object (account, "status-changed",
3494 G_CALLBACK (chat_new_connection_cb),
3498 g_list_free (accounts);
3502 empathy_chat_init (EmpathyChat *chat)
3504 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3505 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3508 priv->log_manager = tpl_log_manager_dup_singleton ();
3509 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3510 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3512 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3513 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3514 priv->input_history = NULL;
3515 priv->input_history_current = NULL;
3516 priv->account_manager = tp_account_manager_dup ();
3518 tp_proxy_prepare_async (priv->account_manager, NULL,
3519 account_manager_prepared_cb, chat);
3521 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3522 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3524 /* Block events for some time to avoid having "has come online" or
3525 * "joined" messages. */
3526 priv->block_events_timeout_id =
3527 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3529 /* Add nick name completion */
3530 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3531 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3533 chat_create_ui (chat);
3537 empathy_chat_new (EmpathyTpChat *tp_chat)
3539 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3543 empathy_chat_get_tp_chat (EmpathyChat *chat)
3545 EmpathyChatPriv *priv = GET_PRIV (chat);
3547 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3549 return priv->tp_chat;
3555 GtkWidget *info_bar;
3565 passwd_remember_button_cb (GtkButton *button,
3568 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3572 passwd_not_now_button_cb (GtkButton *button,
3575 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3579 remember_password_infobar_response_cb (GtkWidget *info_bar,
3583 EmpathyChatPriv *priv = GET_PRIV (data->self);
3585 if (response_id == GTK_RESPONSE_OK) {
3586 DEBUG ("Saving room password");
3587 empathy_keyring_set_room_password_async (priv->account,
3588 empathy_tp_chat_get_id (priv->tp_chat),
3593 gtk_widget_destroy (info_bar);
3594 g_free (data->password);
3595 g_slice_free (PasswordData, data);
3599 chat_prompt_to_save_password (EmpathyChat *self,
3602 GtkWidget *content_area;
3609 /* save the password in case it needs to be saved */
3610 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3612 /* Remove all previous widgets */
3613 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3614 gtk_container_forall (GTK_CONTAINER (content_area),
3615 (GtkCallback) gtk_widget_destroy, NULL);
3616 data->button = NULL;
3619 data->spinner = NULL;
3621 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3622 GTK_MESSAGE_QUESTION);
3624 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3625 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3628 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3629 GTK_ICON_SIZE_DIALOG);
3630 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3633 label = gtk_label_new (_("Would you like to store this password?"));
3634 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3636 /* Add 'Remember' button */
3637 alig = gtk_alignment_new (0, 0.5, 1, 0);
3639 button = gtk_button_new_with_label (_("Remember"));
3640 gtk_container_add (GTK_CONTAINER (alig), button);
3641 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3643 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3646 /* Add 'Not now' button */
3647 alig = gtk_alignment_new (0, 0.5, 1, 0);
3649 button = gtk_button_new_with_label (_("Not now"));
3650 gtk_container_add (GTK_CONTAINER (alig), button);
3651 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3653 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3657 g_signal_handler_disconnect (data->info_bar, data->response_id);
3658 g_signal_connect (data->info_bar, "response",
3659 G_CALLBACK (remember_password_infobar_response_cb), data);
3661 gtk_widget_show_all (data->info_bar);
3665 provide_password_cb (GObject *tp_chat,
3669 PasswordData *data = user_data;
3670 EmpathyChat *self = data->self;
3671 EmpathyChatPriv *priv = GET_PRIV (self);
3672 GError *error = NULL;
3674 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3676 DEBUG ("error: %s", error->message);
3677 /* FIXME: what should we do if that's another error? Close the channel?
3678 * Display the raw D-Bus error to the user isn't very useful */
3679 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3681 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3682 gtk_widget_set_sensitive (data->entry, TRUE);
3683 gtk_widget_grab_focus (data->entry);
3686 gtk_info_bar_set_message_type (
3687 GTK_INFO_BAR (data->info_bar),
3691 gtk_widget_set_sensitive (data->button, TRUE);
3692 gtk_button_set_label (GTK_BUTTON (data->button),
3696 gtk_label_set_text (GTK_LABEL (data->label),
3697 _("Wrong password; please try again:"));
3700 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3701 gtk_widget_hide (data->spinner);
3703 g_error_free (error);
3707 if (empathy_keyring_is_available ()) {
3708 /* ask whether they want to save the password */
3709 chat_prompt_to_save_password (self, data);
3711 /* Get rid of the password info bar finally */
3712 gtk_widget_destroy (data->info_bar);
3713 g_slice_free (PasswordData, data);
3717 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3718 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3719 gtk_widget_grab_focus (self->input_text_view);
3723 password_infobar_response_cb (GtkWidget *info_bar,
3727 EmpathyChatPriv *priv = GET_PRIV (data->self);
3728 const gchar *password;
3730 if (response_id != GTK_RESPONSE_OK) {
3731 gtk_widget_destroy (info_bar);
3732 g_slice_free (PasswordData, data);
3736 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3738 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3739 provide_password_cb, data);
3741 gtk_widget_set_sensitive (data->button, FALSE);
3742 gtk_widget_set_sensitive (data->entry, FALSE);
3744 gtk_spinner_start (GTK_SPINNER (data->spinner));
3745 gtk_widget_show (data->spinner);
3749 password_entry_activate_cb (GtkWidget *entry,
3752 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3756 passwd_join_button_cb (GtkButton *button,
3759 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3763 clear_icon_released_cb (GtkEntry *entry,
3764 GtkEntryIconPosition icon_pos,
3768 gtk_entry_set_text (entry, "");
3772 password_entry_changed_cb (GtkEditable *entry,
3777 str = gtk_entry_get_text (GTK_ENTRY (entry));
3779 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3780 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3784 infobar_chat_invalidated_cb (TpProxy *proxy,
3788 gpointer password_infobar)
3790 /* Destroy the password infobar whenever a channel is invalidated
3791 * so we don't have multiple infobars when the MUC is rejoined */
3792 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3796 display_password_info_bar (EmpathyChat *self)
3798 EmpathyChatPriv *priv = GET_PRIV (self);
3799 GtkWidget *info_bar;
3800 GtkWidget *content_area;
3810 data = g_slice_new0 (PasswordData);
3812 info_bar = gtk_info_bar_new ();
3813 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3814 GTK_MESSAGE_QUESTION);
3816 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3818 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3819 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3822 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3823 GTK_ICON_SIZE_DIALOG);
3824 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3827 label = gtk_label_new (_("This room is protected by a password:"));
3828 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3830 /* Add password entry */
3831 entry = gtk_entry_new ();
3832 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3833 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3835 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3836 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3837 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3838 GTK_ENTRY_ICON_SECONDARY, FALSE);
3840 g_signal_connect (entry, "icon-release",
3841 G_CALLBACK (clear_icon_released_cb), data);
3842 g_signal_connect (entry, "changed",
3843 G_CALLBACK (password_entry_changed_cb), data);
3845 g_signal_connect (entry, "activate",
3846 G_CALLBACK (password_entry_activate_cb), data);
3848 /* Focus the password entry once it's realized */
3849 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3851 /* Add 'Join' button */
3852 alig = gtk_alignment_new (0, 0.5, 1, 0);
3854 button = gtk_button_new_with_label (_("Join"));
3855 gtk_container_add (GTK_CONTAINER (alig), button);
3856 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3858 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3862 spinner = gtk_spinner_new ();
3863 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3865 /* Save some data for messing around with later */
3867 data->info_bar = info_bar;
3868 data->button = button;
3869 data->label = label;
3870 data->entry = entry;
3871 data->spinner = spinner;
3873 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3875 gtk_widget_show_all (hbox);
3877 tp_g_signal_connect_object (priv->tp_chat,
3878 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3881 data->response_id = g_signal_connect (info_bar, "response",
3882 G_CALLBACK (password_infobar_response_cb), data);
3884 gtk_widget_show_all (info_bar);
3885 /* ... but hide the spinner */
3886 gtk_widget_hide (spinner);
3888 /* prevent the user from typing anything */
3889 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3893 provide_saved_password_cb (GObject *tp_chat,
3897 EmpathyChat *self = user_data;
3898 EmpathyChatPriv *priv = GET_PRIV (self);
3899 GError *error = NULL;
3901 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3903 DEBUG ("error: %s", error->message);
3904 /* FIXME: what should we do if that's another error? Close the channel?
3905 * Display the raw D-Bus error to the user isn't very useful */
3906 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3907 display_password_info_bar (self);
3908 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3910 g_error_free (error);
3915 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3916 gtk_widget_grab_focus (self->input_text_view);
3920 chat_room_got_password_cb (GObject *source,
3921 GAsyncResult *result,
3924 EmpathyChat *self = user_data;
3925 EmpathyChatPriv *priv = GET_PRIV (self);
3926 const gchar *password;
3927 GError *error = NULL;
3929 password = empathy_keyring_get_room_password_finish (priv->account,
3932 if (error != NULL) {
3933 DEBUG ("Couldn't get room password: %s\n", error->message);
3934 g_clear_error (&error);
3936 display_password_info_bar (self);
3937 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3941 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3942 provide_saved_password_cb, self);
3946 chat_password_needed_changed_cb (EmpathyChat *self)
3948 EmpathyChatPriv *priv = GET_PRIV (self);
3950 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3951 empathy_keyring_get_room_password_async (priv->account,
3952 empathy_tp_chat_get_id (priv->tp_chat),
3953 chat_room_got_password_cb, self);
3958 chat_sms_channel_changed_cb (EmpathyChat *self)
3960 EmpathyChatPriv *priv = GET_PRIV (self);
3962 priv->sms_channel = tp_text_channel_is_sms_channel (
3963 (TpTextChannel *) priv->tp_chat);
3964 g_object_notify (G_OBJECT (self), "sms-channel");
3968 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3970 g_object_notify (G_OBJECT (self), "n-messages-sending");
3974 empathy_chat_set_tp_chat (EmpathyChat *chat,
3975 EmpathyTpChat *tp_chat)
3977 EmpathyChatPriv *priv = GET_PRIV (chat);
3979 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3980 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3982 if (priv->tp_chat) {
3986 if (priv->account) {
3987 g_object_unref (priv->account);
3990 priv->tp_chat = g_object_ref (tp_chat);
3991 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3993 g_signal_connect (tp_chat, "invalidated",
3994 G_CALLBACK (chat_invalidated_cb),
3996 g_signal_connect (tp_chat, "message-received-empathy",
3997 G_CALLBACK (chat_message_received_cb),
3999 g_signal_connect (tp_chat, "message_acknowledged",
4000 G_CALLBACK (chat_message_acknowledged_cb),
4002 g_signal_connect (tp_chat, "send-error",
4003 G_CALLBACK (chat_send_error_cb),
4005 g_signal_connect (tp_chat, "contact-chat-state-changed",
4006 G_CALLBACK (chat_state_changed_cb),
4008 g_signal_connect (tp_chat, "members-changed",
4009 G_CALLBACK (chat_members_changed_cb),
4011 g_signal_connect (tp_chat, "member-renamed",
4012 G_CALLBACK (chat_member_renamed_cb),
4014 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4015 G_CALLBACK (chat_self_contact_changed_cb),
4017 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4018 G_CALLBACK (chat_remote_contact_changed_cb),
4020 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4021 G_CALLBACK (chat_password_needed_changed_cb),
4023 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4024 G_CALLBACK (chat_sms_channel_changed_cb),
4026 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4027 G_CALLBACK (chat_n_messages_sending_changed_cb),
4029 g_signal_connect_swapped (tp_chat, "notify::title",
4030 G_CALLBACK (chat_title_changed_cb),
4032 g_signal_connect_swapped (tp_chat, "notify::subject",
4033 G_CALLBACK (chat_subject_changed_cb),
4036 /* Get initial value of properties */
4037 chat_sms_channel_changed_cb (chat);
4038 chat_self_contact_changed_cb (chat);
4039 chat_remote_contact_changed_cb (chat);
4040 chat_title_changed_cb (chat);
4041 chat_subject_changed_cb (chat);
4043 if (chat->input_text_view) {
4044 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4045 if (priv->block_events_timeout_id == 0) {
4046 empathy_chat_view_append_event (chat->view, _("Connected"));
4050 g_object_notify (G_OBJECT (chat), "tp-chat");
4051 g_object_notify (G_OBJECT (chat), "id");
4052 g_object_notify (G_OBJECT (chat), "account");
4054 /* This is a noop when tp-chat is set at object construction time and causes
4055 * the pending messages to be show when it's set on the object after it has
4057 show_pending_messages (chat);
4059 /* check if a password is needed */
4060 chat_password_needed_changed_cb (chat);
4064 empathy_chat_get_account (EmpathyChat *chat)
4066 EmpathyChatPriv *priv = GET_PRIV (chat);
4068 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4070 return priv->account;
4074 empathy_chat_get_id (EmpathyChat *chat)
4076 EmpathyChatPriv *priv = GET_PRIV (chat);
4078 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4084 empathy_chat_dup_name (EmpathyChat *chat)
4086 EmpathyChatPriv *priv = GET_PRIV (chat);
4089 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4093 if (!ret && priv->remote_contact) {
4094 ret = empathy_contact_get_alias (priv->remote_contact);
4101 ret = _("Conversation");
4103 if (priv->sms_channel)
4104 /* Translators: this string is a something like
4105 * "Escher Cat (SMS)" */
4106 return g_strdup_printf (_("%s (SMS)"), ret);
4108 return g_strdup (ret);
4112 empathy_chat_get_subject (EmpathyChat *chat)
4114 EmpathyChatPriv *priv = GET_PRIV (chat);
4116 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4118 return priv->subject;
4122 empathy_chat_get_remote_contact (EmpathyChat *chat)
4124 EmpathyChatPriv *priv = GET_PRIV (chat);
4126 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4128 return priv->remote_contact;
4132 empathy_chat_get_contact_menu (EmpathyChat *chat)
4134 EmpathyChatPriv *priv = GET_PRIV (chat);
4135 GtkWidget *menu = NULL;
4136 FolksIndividual *individual;
4139 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4141 if (priv->remote_contact == NULL)
4144 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4145 if (contact == NULL)
4148 individual = empathy_ensure_individual_from_tp_contact (contact);
4150 if (individual == NULL)
4153 menu = empathy_individual_menu_new (individual,
4154 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4155 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4156 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4157 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4159 g_object_unref (individual);
4165 empathy_chat_clear (EmpathyChat *chat)
4167 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4169 empathy_chat_view_clear (chat->view);
4173 empathy_chat_scroll_down (EmpathyChat *chat)
4175 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4177 empathy_chat_view_scroll_down (chat->view);
4181 empathy_chat_cut (EmpathyChat *chat)
4183 GtkTextBuffer *buffer;
4185 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4187 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4188 if (gtk_text_buffer_get_has_selection (buffer)) {
4189 GtkClipboard *clipboard;
4191 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4193 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4198 empathy_chat_copy (EmpathyChat *chat)
4200 GtkTextBuffer *buffer;
4202 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4204 if (empathy_chat_view_get_has_selection (chat->view)) {
4205 empathy_chat_view_copy_clipboard (chat->view);
4209 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4210 if (gtk_text_buffer_get_has_selection (buffer)) {
4211 GtkClipboard *clipboard;
4213 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4215 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4220 EmpathyChatPriv *priv = GET_PRIV (chat);
4222 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4229 GtkClipboard *clipboard;
4231 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4232 start = g_utf8_offset_to_pointer (topic, start_offset);
4233 end = g_utf8_offset_to_pointer (topic, end_offset);
4234 selection = g_strndup (start, end - start);
4236 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4237 gtk_clipboard_set_text (clipboard, selection, -1);
4245 empathy_chat_paste (EmpathyChat *chat)
4247 GtkTextBuffer *buffer;
4248 GtkClipboard *clipboard;
4249 EmpathyChatPriv *priv;
4251 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4253 priv = GET_PRIV (chat);
4255 if (gtk_widget_get_visible (priv->search_bar)) {
4256 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4260 if (priv->tp_chat == NULL ||
4261 !gtk_widget_is_sensitive (chat->input_text_view))
4264 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4265 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4267 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4271 empathy_chat_find (EmpathyChat *chat)
4273 EmpathyChatPriv *priv;
4275 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4277 priv = GET_PRIV (chat);
4279 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4283 empathy_chat_correct_word (EmpathyChat *chat,
4286 const gchar *new_word)
4288 GtkTextBuffer *buffer;
4290 g_return_if_fail (chat != NULL);
4291 g_return_if_fail (new_word != NULL);
4293 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4295 gtk_text_buffer_delete (buffer, start, end);
4296 gtk_text_buffer_insert (buffer, start,
4302 empathy_chat_is_room (EmpathyChat *chat)
4304 EmpathyChatPriv *priv = GET_PRIV (chat);
4306 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4308 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4312 empathy_chat_is_highlighted (EmpathyChat *chat)
4314 EmpathyChatPriv *priv = GET_PRIV (chat);
4316 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4318 return priv->highlighted;
4322 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4324 EmpathyChatPriv *priv = GET_PRIV (self);
4326 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4328 return priv->unread_messages;
4331 /* called when the messages have been read by user */
4333 empathy_chat_messages_read (EmpathyChat *self)
4335 EmpathyChatPriv *priv = GET_PRIV (self);
4337 g_return_if_fail (EMPATHY_IS_CHAT (self));
4339 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4340 * definition. If we are still retrieving the backlogs, do not ACK */
4341 if (priv->retrieving_backlogs)
4344 if (priv->tp_chat != NULL) {
4345 tp_text_channel_ack_all_pending_messages_async (
4346 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4349 priv->highlighted = FALSE;
4351 if (priv->unread_messages_when_offline > 0) {
4352 /* We can't ack those as the connection has gone away so just consider
4354 priv->unread_messages -= priv->unread_messages_when_offline;
4355 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4356 priv->unread_messages_when_offline = 0;
4360 /* Return TRUE if on of the contacts in this chat is composing */
4362 empathy_chat_is_composing (EmpathyChat *chat)
4364 return chat->priv->compositors != NULL;
4368 empathy_chat_is_sms_channel (EmpathyChat *self)
4370 EmpathyChatPriv *priv = GET_PRIV (self);
4372 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4374 return priv->sms_channel;
4378 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4380 EmpathyChatPriv *priv;
4382 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4384 priv = GET_PRIV (self);
4386 if (priv->tp_chat == NULL) {
4391 g_object_get (priv->tp_chat,
4392 "n-messages-sending", &n_messages,
4400 empathy_chat_dup_text (EmpathyChat *self)
4402 GtkTextBuffer *buffer;
4403 GtkTextIter start, end;
4405 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4407 gtk_text_buffer_get_bounds (buffer, &start, &end);
4408 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4412 empathy_chat_set_text (EmpathyChat *self,
4415 GtkTextBuffer *buffer;
4417 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4419 gtk_text_buffer_set_text (buffer, text, -1);