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/log-manager.h>
42 #include <telepathy-logger/text-event.h>
43 #include <libempathy/empathy-contact-list.h>
44 #include <libempathy/empathy-gsettings.h>
45 #include <libempathy/empathy-keyring.h>
46 #include <libempathy/empathy-utils.h>
47 #include <libempathy/empathy-request-util.h>
48 #include <libempathy/empathy-chatroom-manager.h>
50 #include "empathy-chat.h"
51 #include "empathy-spell.h"
52 #include "empathy-contact-dialogs.h"
53 #include "empathy-individual-store-channel.h"
54 #include "empathy-individual-view.h"
55 #include "empathy-input-text-view.h"
56 #include "empathy-search-bar.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-theme-adium.h"
59 #include "empathy-smiley-manager.h"
60 #include "empathy-ui-utils.h"
61 #include "empathy-string-parser.h"
62 #include "extensions/extensions.h"
64 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
65 #include <libempathy/empathy-debug.h>
67 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
68 #define COMPOSING_STOP_TIMEOUT 5
70 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
71 struct _EmpathyChatPriv {
72 EmpathyTpChat *tp_chat;
77 EmpathyContact *self_contact;
78 EmpathyContact *remote_contact;
79 gboolean show_contacts;
81 GSettings *gsettings_chat;
82 GSettings *gsettings_ui;
84 TplLogManager *log_manager;
85 TpAccountManager *account_manager;
87 GList *input_history_current;
89 GCompletion *completion;
90 guint composing_stop_timeout_id;
91 guint block_events_timeout_id;
92 TpHandleType handle_type;
94 gboolean has_input_vscroll;
96 /* TRUE if spell checking is enabled, FALSE otherwise.
97 * This is to keep track of the last state of spell checking
99 gboolean spell_checking_enabled;
101 /* These store the signal handler ids for the enclosed text entry. */
102 gulong insert_text_id;
103 gulong delete_range_id;
104 gulong notify_cursor_position_id;
106 /* Source func ID for update_misspelled_words () */
107 guint update_misspelled_words_id;
108 /* Source func ID for save_paned_pos_timeout () */
109 guint save_paned_pos_id;
110 /* Source func ID for chat_contacts_visible_timeout_cb () */
111 guint contacts_visible_id;
115 GtkWidget *vbox_left;
116 GtkWidget *scrolled_window_chat;
117 GtkWidget *scrolled_window_input;
118 GtkWidget *scrolled_window_contacts;
119 GtkWidget *hbox_topic;
120 GtkWidget *expander_topic;
121 GtkWidget *label_topic;
122 GtkWidget *contact_list_view;
123 GtkWidget *info_bar_vbox;
124 GtkWidget *search_bar;
126 guint unread_messages;
127 guint unread_messages_when_offline;
128 /* TRUE if the pending messages can be displayed. This is to avoid to show
129 * pending messages *before* messages from logs. (#603980) */
130 gboolean can_show_pending;
132 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
133 * be differently handled since it introduces another race condition, which
134 * is really hard to occur, but still possible.
136 * With the current workaround (which has the race above), we need to be
137 * sure to ACK any pending messages only when the retrieval of backlogs is
138 * finished, that's why using retrieving_backlogs flag.
139 * empathy_chat_messages_read () will check this variable and not ACK
140 * anything when TRUE. It will be set TRUE at chat_constructed () and set
141 * back to FALSE when the backlog has been retrieved and the pending
142 * messages actually showed to the user.
144 * Race condition introduced with this workaround:
145 * Scenario: a message is pending, the user is notified and selects the tab.
146 * the tab with a pending message is focused before the messages are properly
147 * shown (since the preparation of the window is slower AND async WRT the
148 * tab showing), which means the user won't see any new messages (rare but
149 * possible), if he/she will change tab focus before the messages are
150 * properly shown, the tab will be set as 'seen' and the user won't be
151 * notified again about the already notified pending messages when the
152 * messages in tab will be properly shown */
153 gboolean retrieving_backlogs;
154 gboolean sms_channel;
156 /* we need to know whether populate-popup happened in response to
157 * the keyboard or the mouse. We can't ask GTK for the most recent
158 * event, because it will be a notify event. Instead we track it here */
159 GdkEventType most_recent_event_type;
161 /* A regex matching our own current nickname in the room, or %NULL if
162 * !empathy_chat_is_room (). */
163 GRegex *highlight_regex;
165 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
166 * Cleared by empathy_chat_messages_read (). */
167 gboolean highlighted;
171 gchar *text; /* Original message that was specified
172 * upon entry creation. */
173 gchar *modified_text; /* Message that was modified by user.
174 * When no modifications were made, it is NULL */
180 PART_COMMAND_ENTERED,
194 PROP_N_MESSAGES_SENDING,
195 PROP_NB_UNREAD_MESSAGES,
198 static guint signals[LAST_SIGNAL] = { 0 };
200 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
202 static gboolean update_misspelled_words (gpointer data);
205 chat_get_property (GObject *object,
210 EmpathyChat *chat = EMPATHY_CHAT (object);
211 EmpathyChatPriv *priv = GET_PRIV (object);
215 g_value_set_object (value, priv->tp_chat);
218 g_value_set_object (value, priv->account);
221 g_value_take_string (value, empathy_chat_dup_name (chat));
224 g_value_set_string (value, priv->id);
227 g_value_set_string (value, priv->subject);
229 case PROP_REMOTE_CONTACT:
230 g_value_set_object (value, priv->remote_contact);
232 case PROP_SHOW_CONTACTS:
233 g_value_set_boolean (value, priv->show_contacts);
235 case PROP_SMS_CHANNEL:
236 g_value_set_boolean (value, priv->sms_channel);
238 case PROP_N_MESSAGES_SENDING:
239 g_value_set_uint (value,
240 empathy_chat_get_n_messages_sending (chat));
242 case PROP_NB_UNREAD_MESSAGES:
243 g_value_set_uint (value,
244 empathy_chat_get_nb_unread_messages (chat));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
253 chat_set_property (GObject *object,
258 EmpathyChat *chat = EMPATHY_CHAT (object);
262 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
264 case PROP_SHOW_CONTACTS:
265 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
274 account_reconnected (EmpathyChat *chat,
277 EmpathyChatPriv *priv = GET_PRIV (chat);
279 DEBUG ("Account reconnected, request a new Text channel");
281 /* FIXME: Ideally we should ask to handle ourself the channel so we can
282 * report the error if any but this is blocked by
283 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
284 switch (priv->handle_type) {
285 case TP_HANDLE_TYPE_CONTACT:
286 if (priv->sms_channel)
287 empathy_sms_contact_id (
289 TP_USER_ACTION_TIME_NOT_USER_ACTION,
292 empathy_chat_with_contact_id (
294 TP_USER_ACTION_TIME_NOT_USER_ACTION,
297 case TP_HANDLE_TYPE_ROOM:
298 empathy_join_muc (account, priv->id,
299 TP_USER_ACTION_TIME_NOT_USER_ACTION);
301 case TP_HANDLE_TYPE_NONE:
302 case TP_HANDLE_TYPE_LIST:
303 case TP_HANDLE_TYPE_GROUP:
305 g_assert_not_reached ();
309 g_object_unref (chat);
313 chat_new_connection_cb (TpAccount *account,
317 gchar *dbus_error_name,
321 EmpathyChatPriv *priv = GET_PRIV (chat);
323 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
326 if (priv->tp_chat != NULL || account != priv->account ||
327 priv->handle_type == TP_HANDLE_TYPE_NONE ||
328 EMP_STR_EMPTY (priv->id))
333 account_reconnected (chat, account);
337 chat_composing_remove_timeout (EmpathyChat *chat)
339 EmpathyChatPriv *priv;
341 priv = GET_PRIV (chat);
343 if (priv->composing_stop_timeout_id) {
344 g_source_remove (priv->composing_stop_timeout_id);
345 priv->composing_stop_timeout_id = 0;
350 set_chate_state_cb (GObject *source,
351 GAsyncResult *result,
354 GError *error = NULL;
356 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
358 DEBUG ("Failed to set chat state: %s", error->message);
359 g_error_free (error);
364 set_chat_state (EmpathyChat *self,
365 TpChannelChatState state)
367 EmpathyChatPriv *priv = GET_PRIV (self);
369 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
370 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
373 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
374 set_chate_state_cb, self);
378 chat_composing_stop_timeout_cb (EmpathyChat *chat)
380 EmpathyChatPriv *priv;
381 gboolean send_chat_states;
383 priv = GET_PRIV (chat);
385 priv->composing_stop_timeout_id = 0;
386 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
387 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
388 if (!send_chat_states) {
389 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
391 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
398 chat_composing_start (EmpathyChat *chat)
400 EmpathyChatPriv *priv;
401 gboolean send_chat_states;
403 priv = GET_PRIV (chat);
405 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
406 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
407 if (!send_chat_states) {
411 if (priv->composing_stop_timeout_id) {
412 /* Just restart the timeout */
413 chat_composing_remove_timeout (chat);
415 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
418 priv->composing_stop_timeout_id = g_timeout_add_seconds (
419 COMPOSING_STOP_TIMEOUT,
420 (GSourceFunc) chat_composing_stop_timeout_cb,
425 chat_composing_stop (EmpathyChat *chat)
427 chat_composing_remove_timeout (chat);
428 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
432 chat_input_history_entry_cmp (InputHistoryEntry *entry,
435 if (!tp_strdiff (entry->text, text)) {
436 if (entry->modified_text != NULL) {
437 /* Modified entry and single string cannot be equal. */
445 static InputHistoryEntry *
446 chat_input_history_entry_new_with_text (const gchar *text)
448 InputHistoryEntry *entry;
449 entry = g_slice_new0 (InputHistoryEntry);
450 entry->text = g_strdup (text);
456 chat_input_history_entry_free (InputHistoryEntry *entry)
458 g_free (entry->text);
459 g_free (entry->modified_text);
460 g_slice_free (InputHistoryEntry, entry);
464 chat_input_history_entry_revert (InputHistoryEntry *entry)
466 g_free (entry->modified_text);
467 entry->modified_text = NULL;
471 chat_input_history_entry_update_text (InputHistoryEntry *entry,
476 if (!tp_strdiff (text, entry->text)) {
477 g_free (entry->modified_text);
478 entry->modified_text = NULL;
482 old = entry->modified_text;
483 entry->modified_text = g_strdup (text);
488 chat_input_history_entry_get_text (InputHistoryEntry *entry)
494 if (entry->modified_text != NULL) {
495 return entry->modified_text;
501 chat_input_history_remove_item (GList *list,
504 list = g_list_remove_link (list, item);
505 chat_input_history_entry_free (item->data);
506 g_list_free_1 (item);
511 chat_input_history_revert (EmpathyChat *chat)
513 EmpathyChatPriv *priv;
517 InputHistoryEntry *entry;
519 priv = GET_PRIV (chat);
520 list = priv->input_history;
523 DEBUG ("No input history");
527 /* Delete temporary entry */
528 if (priv->input_history_current != NULL) {
530 list = chat_input_history_remove_item (list, item1);
531 if (priv->input_history_current == item1) {
532 /* Removed temporary entry was current entry */
533 priv->input_history = list;
534 priv->input_history_current = NULL;
539 /* There is no entry to revert */
543 /* Restore the current history entry to original value */
544 item1 = priv->input_history_current;
546 chat_input_history_entry_revert (entry);
548 /* Remove restored entry if there is other occurance before this entry */
549 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
550 (GCompareFunc) chat_input_history_entry_cmp);
551 if (item2 != item1) {
552 list = chat_input_history_remove_item (list, item1);
555 /* Remove other occurance of the restored entry */
556 item2 = g_list_find_custom (item1->next,
557 chat_input_history_entry_get_text (entry),
558 (GCompareFunc) chat_input_history_entry_cmp);
560 list = chat_input_history_remove_item (list, item2);
564 priv->input_history_current = NULL;
565 priv->input_history = list;
569 chat_input_history_add (EmpathyChat *chat,
573 EmpathyChatPriv *priv;
576 InputHistoryEntry *entry;
578 priv = GET_PRIV (chat);
580 list = priv->input_history;
582 /* Remove any other occurances of this entry, if not temporary */
584 while ((item = g_list_find_custom (list, str,
585 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
586 list = chat_input_history_remove_item (list, item);
589 /* Trim the list to the last 10 items */
590 while (g_list_length (list) > 10) {
591 item = g_list_last (list);
593 list = chat_input_history_remove_item (list, item);
601 entry = chat_input_history_entry_new_with_text (str);
602 list = g_list_prepend (list, entry);
604 /* Set the list and the current item pointer */
605 priv->input_history = list;
607 priv->input_history_current = list;
610 priv->input_history_current = NULL;
615 chat_input_history_get_next (EmpathyChat *chat)
617 EmpathyChatPriv *priv;
621 priv = GET_PRIV (chat);
623 if (priv->input_history == NULL) {
624 DEBUG ("No input history, next entry is NULL");
627 g_assert (priv->input_history_current != NULL);
629 if ((item = g_list_next (priv->input_history_current)) == NULL)
631 item = priv->input_history_current;
634 msg = chat_input_history_entry_get_text (item->data);
636 DEBUG ("Returning next entry: '%s'", msg);
638 priv->input_history_current = item;
644 chat_input_history_get_prev (EmpathyChat *chat)
646 EmpathyChatPriv *priv;
650 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
652 priv = GET_PRIV (chat);
654 if (priv->input_history == NULL) {
655 DEBUG ("No input history, previous entry is NULL");
659 if (priv->input_history_current == NULL)
663 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
665 item = priv->input_history_current;
668 msg = chat_input_history_entry_get_text (item->data);
670 DEBUG ("Returning previous entry: '%s'", msg);
672 priv->input_history_current = item;
678 chat_input_history_update (EmpathyChat *chat,
679 GtkTextBuffer *buffer)
681 EmpathyChatPriv *priv;
682 GtkTextIter start, end;
684 InputHistoryEntry *entry;
686 priv = GET_PRIV (chat);
688 gtk_text_buffer_get_bounds (buffer, &start, &end);
689 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
691 if (priv->input_history_current == NULL) {
692 /* Add the current text temporarily to the history */
693 chat_input_history_add (chat, text, TRUE);
698 /* Save the changes in the history */
699 entry = priv->input_history_current->data;
700 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
701 chat_input_history_entry_update_text (entry, text);
710 } ChatCommandMsgData;
713 chat_command_msg_cb (GObject *source,
714 GAsyncResult *result,
717 ChatCommandMsgData *data = user_data;
718 GError *error = NULL;
721 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
722 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
724 if (channel == NULL) {
725 DEBUG ("Failed to get channel: %s", error->message);
726 g_error_free (error);
728 empathy_chat_view_append_event (data->chat->view,
729 _("Failed to open private chat"));
733 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
734 TpTextChannel *text = (TpTextChannel *) channel;
737 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
740 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
742 g_object_unref (msg);
745 g_object_unref (channel);
748 g_free (data->message);
749 g_slice_free (ChatCommandMsgData, data);
753 nick_command_supported (EmpathyChat *chat)
755 EmpathyChatPriv * priv = GET_PRIV (chat);
756 TpConnection *connection;
758 connection = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
759 return tp_proxy_has_interface_by_id (connection,
760 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
764 part_command_supported (EmpathyChat *chat)
766 EmpathyChatPriv * priv = GET_PRIV (chat);
768 return tp_proxy_has_interface_by_id (priv->tp_chat,
769 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
773 chat_command_clear (EmpathyChat *chat,
776 empathy_chat_view_clear (chat->view);
780 chat_command_topic (EmpathyChat *chat,
783 EmpathyChatPriv *priv = GET_PRIV (chat);
785 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
786 empathy_chat_view_append_event (chat->view,
787 _("Topic not supported on this conversation"));
791 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
792 empathy_chat_view_append_event (chat->view,
793 _("You are not allowed to change the topic"));
797 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
801 empathy_chat_join_muc (EmpathyChat *chat,
804 EmpathyChatPriv *priv = GET_PRIV (chat);
806 empathy_join_muc (priv->account, room,
807 empathy_get_current_action_time ());
811 chat_command_join (EmpathyChat *chat,
816 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
818 /* FIXME: Ideally we should ask to handle ourself the channel so we can
819 * report the error if any but this is blocked by
820 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
821 while (rooms[i] != NULL) {
822 /* ignore empty strings */
823 if (!EMP_STR_EMPTY (rooms[i])) {
824 empathy_chat_join_muc (chat, rooms[i]);
832 chat_command_part (EmpathyChat *chat,
835 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
839 chat_command_msg_internal (EmpathyChat *chat,
840 const gchar *contact_id,
841 const gchar *message)
843 EmpathyChatPriv *priv = GET_PRIV (chat);
844 ChatCommandMsgData *data;
845 TpAccountChannelRequest *req;
848 request = tp_asv_new (
849 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
850 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
851 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
854 req = tp_account_channel_request_new (priv->account, request,
855 empathy_get_current_action_time ());
857 /* FIXME: We should probably search in members alias. But this
858 * is enough for IRC */
859 data = g_slice_new (ChatCommandMsgData);
861 data->message = g_strdup (message);
863 tp_account_channel_request_ensure_and_observe_channel_async (req,
864 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
866 g_object_unref (req);
867 g_hash_table_unref (request);
871 chat_command_query (EmpathyChat *chat,
874 /* If <message> part is not defined,
875 * strv[2] will be the terminal NULL */
876 chat_command_msg_internal (chat, strv[1], strv[2]);
880 chat_command_msg (EmpathyChat *chat,
883 chat_command_msg_internal (chat, strv[1], strv[2]);
887 callback_for_request_rename (TpProxy *proxy,
890 GObject *weak_object)
893 DEBUG ("Call to RequestRename method failed: %s",error->message);
898 chat_command_nick (EmpathyChat *chat,
901 EmpathyChatPriv *priv = GET_PRIV (chat);
904 proxy = TP_PROXY (tp_account_get_connection (priv->account));
906 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
907 strv[1], callback_for_request_rename, NULL, NULL, NULL);
911 chat_command_me (EmpathyChat *chat,
914 EmpathyChatPriv *priv = GET_PRIV (chat);
916 TpTextChannel *channel;
918 channel = (TpTextChannel *) (priv->tp_chat);
920 if (!tp_text_channel_supports_message_type (channel,
921 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
922 /* Action message are not supported, 'simulate' the action */
925 /* The TpChat can't be ready if it doesn't have the self contact */
926 g_assert (priv->self_contact != NULL);
928 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
930 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
935 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
939 empathy_tp_chat_send (priv->tp_chat, message);
940 g_object_unref (message);
944 chat_command_say (EmpathyChat *chat,
947 EmpathyChatPriv *priv = GET_PRIV (chat);
950 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
952 empathy_tp_chat_send (priv->tp_chat, message);
953 g_object_unref (message);
957 whois_got_contact_cb (TpConnection *connection,
959 TpContact * const *contacts,
960 const gchar * const *requested_ids,
961 GHashTable *failed_id_errors,
964 GObject *weak_object)
966 EmpathyChat *chat = EMPATHY_CHAT (weak_object);
968 g_return_if_fail (n_contacts <= 1);
970 if (n_contacts == 0) {
972 gpointer key = NULL, value = NULL;
976 /* tp-glib guarantees that the contacts you requested will be
977 * in failed_id_errors regardless of whether the individual
978 * contact was invalid or the whole operation failed.
980 g_hash_table_iter_init (&iter, failed_id_errors);
981 g_hash_table_iter_next (&iter, &key, &value);
985 DEBUG ("Error getting TpContact for '%s': %s %u %s",
986 id, g_quark_to_string (id_error->domain),
987 id_error->code, id_error->message);
990 /* The specific ID failed. */
991 gchar *event = g_strdup_printf (
992 _("“%s” is not a valid contact ID"), id);
993 empathy_chat_view_append_event (chat->view, event);
996 /* Otherwise we're disconnected or something; so the window
997 * will already say ‘Disconnected’, so let's not show anything.
1000 EmpathyContact *empathy_contact;
1003 g_return_if_fail (contacts[0] != NULL);
1004 empathy_contact = empathy_contact_dup_from_tp_contact (
1007 window = gtk_widget_get_toplevel (GTK_WIDGET (chat));
1008 /* If we're alive and this command is running, we'd better be
1010 g_return_if_fail (window != NULL);
1011 g_return_if_fail (gtk_widget_is_toplevel (window));
1012 empathy_contact_information_dialog_show (empathy_contact,
1013 GTK_WINDOW (window));
1014 g_object_unref (empathy_contact);
1019 chat_command_whois (EmpathyChat *chat,
1022 EmpathyChatPriv *priv = GET_PRIV (chat);
1025 conn = tp_channel_borrow_connection ((TpChannel *) priv->tp_chat);
1026 tp_connection_get_contacts_by_id (conn,
1027 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
1028 * entered by the user (including spaces, if any). */
1029 1, (const gchar * const *) strv + 1,
1031 whois_got_contact_cb, NULL, NULL, G_OBJECT (chat));
1035 chat_command_whale (EmpathyChat *chat,
1038 EmpathyChatPriv *priv = GET_PRIV (chat);
1041 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1043 "•_______________•");
1044 empathy_tp_chat_send (priv->tp_chat, message);
1045 g_object_unref (message);
1049 chat_command_babywhale (EmpathyChat *chat,
1052 EmpathyChatPriv *priv = GET_PRIV (chat);
1055 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1058 empathy_tp_chat_send (priv->tp_chat, message);
1059 g_object_unref (message);
1063 chat_command_inspector (EmpathyChat *chat,
1066 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1067 empathy_theme_adium_show_inspector (
1068 EMPATHY_THEME_ADIUM (chat->view));
1072 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1074 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1077 const gchar *prefix;
1080 ChatCommandFunc func;
1081 gboolean (*is_supported)(EmpathyChat *chat);
1085 static ChatCommandItem commands[] = {
1086 {"clear", 1, 1, chat_command_clear, NULL,
1087 N_("/clear: clear all messages from the current conversation")},
1089 {"topic", 2, 2, chat_command_topic, NULL,
1090 N_("/topic <topic>: set the topic of the current conversation")},
1092 {"join", 2, 2, chat_command_join, NULL,
1093 N_("/join <chat room ID>: join a new chat room")},
1095 {"j", 2, 2, chat_command_join, NULL,
1096 N_("/j <chat room ID>: join a new chat room")},
1098 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1099 {"part", 1, 3, chat_command_part, part_command_supported,
1100 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1101 "by default the current one")},
1103 {"query", 2, 3, chat_command_query, NULL,
1104 N_("/query <contact ID> [<message>]: open a private chat")},
1106 {"msg", 3, 3, chat_command_msg, NULL,
1107 N_("/msg <contact ID> <message>: open a private chat")},
1109 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1110 N_("/nick <nickname>: change your nickname on the current server")},
1112 {"me", 2, 2, chat_command_me, NULL,
1113 N_("/me <message>: send an ACTION message to the current conversation")},
1115 {"say", 2, 2, chat_command_say, NULL,
1116 N_("/say <message>: send <message> to the current conversation. "
1117 "This is used to send a message starting with a '/'. For example: "
1118 "\"/say /join is used to join a new chat room\"")},
1120 {"whois", 2, 2, chat_command_whois, NULL,
1121 N_("/whois <contact ID>: display information about a contact")},
1123 {"help", 1, 2, chat_command_help, NULL,
1124 N_("/help [<command>]: show all supported commands. "
1125 "If <command> is defined, show its usage.")},
1127 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1129 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1130 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1134 chat_command_show_help (EmpathyChat *chat,
1135 ChatCommandItem *item)
1139 if (item->help == NULL) {
1143 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1144 empathy_chat_view_append_event (chat->view, str);
1149 chat_command_help (EmpathyChat *chat,
1154 /* If <command> part is not defined,
1155 * strv[1] will be the terminal NULL */
1156 if (strv[1] == NULL) {
1157 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1158 if (commands[i].is_supported != NULL) {
1159 if (!commands[i].is_supported (chat)) {
1163 if (commands[i].help == NULL) {
1166 empathy_chat_view_append_event (chat->view,
1167 _(commands[i].help));
1172 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1173 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1174 if (commands[i].is_supported != NULL) {
1175 if (!commands[i].is_supported (chat)) {
1179 if (commands[i].help == NULL) {
1182 chat_command_show_help (chat, &commands[i]);
1187 empathy_chat_view_append_event (chat->view,
1188 _("Unknown command"));
1192 chat_command_parse (const gchar *text, guint max_parts)
1197 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1199 array = g_ptr_array_sized_new (max_parts + 1);
1200 while (max_parts > 1) {
1203 /* Skip white spaces */
1204 while (g_ascii_isspace (*text)) {
1208 /* Search the end of this part, until first space. */
1209 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1215 item = g_strndup (text, end - text);
1216 g_ptr_array_add (array, item);
1217 DEBUG ("\tITEM: \"%s\"", item);
1223 /* Append last part if not empty */
1224 item = g_strstrip (g_strdup (text));
1225 if (!EMP_STR_EMPTY (item)) {
1226 g_ptr_array_add (array, item);
1227 DEBUG ("\tITEM: \"%s\"", item);
1232 /* Make the array NULL-terminated */
1233 g_ptr_array_add (array, NULL);
1235 return (GStrv) g_ptr_array_free (array, FALSE);
1239 has_prefix_case (const gchar *s,
1240 const gchar *prefix)
1242 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1246 chat_send (EmpathyChat *chat,
1249 EmpathyChatPriv *priv;
1253 if (EMP_STR_EMPTY (msg)) {
1257 priv = GET_PRIV (chat);
1259 chat_input_history_add (chat, msg, FALSE);
1261 if (msg[0] == '/') {
1262 gboolean second_slash = FALSE;
1263 const gchar *iter = msg + 1;
1265 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1270 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1273 c = *(msg + 1 + strlen (commands[i].prefix));
1274 if (c != '\0' && !g_ascii_isspace (c)) {
1277 if (commands[i].is_supported != NULL) {
1278 if (!commands[i].is_supported (chat)) {
1283 /* We can't use g_strsplit here because it does
1284 * not deal correctly if we have more than one space
1286 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1288 strv_len = g_strv_length (strv);
1289 if (strv_len < commands[i].min_parts ||
1290 strv_len > commands[i].max_parts) {
1291 chat_command_show_help (chat, &commands[i]);
1296 commands[i].func (chat, strv);
1301 /* Also allow messages with two slashes before the
1302 * first space, so it is possible to send a /unix/path.
1303 * This heuristic is kind of crap. */
1304 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1306 second_slash = TRUE;
1312 if (!second_slash) {
1313 empathy_chat_view_append_event (chat->view,
1314 _("Unknown command; see /help for the available"
1320 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1322 empathy_tp_chat_send (priv->tp_chat, message);
1323 g_object_unref (message);
1327 chat_input_text_view_send (EmpathyChat *chat)
1329 GtkTextBuffer *buffer;
1330 GtkTextIter start, end;
1333 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1335 gtk_text_buffer_get_bounds (buffer, &start, &end);
1336 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1338 /* clear the input field */
1339 gtk_text_buffer_set_text (buffer, "", -1);
1340 /* delete input history modifications */
1341 chat_input_history_revert (chat);
1343 chat_send (chat, msg);
1348 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1349 EmpathyContact *contact,
1350 TpChannelChatState state,
1353 EmpathyChatPriv *priv;
1355 gboolean was_composing;
1357 priv = GET_PRIV (chat);
1359 if (empathy_contact_is_user (contact)) {
1360 /* We don't care about our own chat state */
1364 was_composing = (priv->compositors != NULL);
1366 /* Find the contact in the list. After that l is the list elem or NULL */
1367 for (l = priv->compositors; l; l = l->next) {
1368 if (contact == l->data) {
1374 case TP_CHANNEL_CHAT_STATE_GONE:
1375 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1376 case TP_CHANNEL_CHAT_STATE_PAUSED:
1377 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1378 /* Contact is not composing */
1380 priv->compositors = g_list_remove_link (priv->compositors, l);
1381 g_object_unref (l->data);
1385 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1386 /* Contact is composing */
1388 priv->compositors = g_list_prepend (priv->compositors,
1389 g_object_ref (contact));
1393 g_assert_not_reached ();
1396 DEBUG ("Was composing: %s now composing: %s",
1397 was_composing ? "yes" : "no",
1398 priv->compositors ? "yes" : "no");
1400 if ((was_composing && !priv->compositors) ||
1401 (!was_composing && priv->compositors)) {
1402 /* Composing state changed */
1403 g_signal_emit (chat, signals[COMPOSING], 0,
1404 priv->compositors != NULL);
1409 get_highlight_regex_for (const gchar *name)
1412 gchar *name_esc, *pattern;
1413 GError *error = NULL;
1415 name_esc = g_regex_escape_string (name, -1);
1416 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1417 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1420 if (regex == NULL) {
1421 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1424 g_error_free (error);
1433 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1434 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1437 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1439 EmpathyChatPriv *priv = GET_PRIV (chat);
1441 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1443 if (priv->self_contact != NULL) {
1444 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1446 g_return_if_fail (alias != NULL);
1447 priv->highlight_regex = get_highlight_regex_for (alias);
1452 chat_should_highlight (EmpathyChat *chat,
1453 EmpathyMessage *message)
1455 EmpathyChatPriv *priv = GET_PRIV (chat);
1457 TpChannelTextMessageFlags flags;
1459 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1461 if (!empathy_chat_is_room (chat)) {
1465 if (!empathy_message_is_incoming (message)) {
1469 msg = empathy_message_get_body (message);
1474 flags = empathy_message_get_flags (message);
1475 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1476 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1477 * have already been received by the user before (and so are in the logs) */
1481 if (priv->highlight_regex == NULL) {
1485 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1489 chat_message_received (EmpathyChat *chat,
1490 EmpathyMessage *message,
1493 EmpathyChatPriv *priv = GET_PRIV (chat);
1494 EmpathyContact *sender;
1496 sender = empathy_message_get_sender (message);
1498 if (empathy_message_is_edit (message)) {
1499 DEBUG ("Editing message '%s' to '%s'",
1500 empathy_message_get_supersedes (message),
1501 empathy_message_get_body (message));
1503 empathy_chat_view_edit_message (chat->view, message);
1505 gboolean should_highlight = chat_should_highlight (chat, message);
1507 if (should_highlight) {
1508 priv->highlighted = TRUE;
1511 DEBUG ("Appending new message '%s' from %s (%d)",
1512 empathy_message_get_token (message),
1513 empathy_contact_get_alias (sender),
1514 empathy_contact_get_handle (sender));
1516 empathy_chat_view_append_message (chat->view, message, should_highlight);
1518 if (empathy_message_is_incoming (message)) {
1519 priv->unread_messages++;
1520 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1523 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1527 /* We received a message so the contact is no longer
1529 chat_state_changed_cb (priv->tp_chat, sender,
1530 TP_CHANNEL_CHAT_STATE_ACTIVE,
1535 chat_message_received_cb (EmpathyTpChat *tp_chat,
1536 EmpathyMessage *message,
1539 chat_message_received (chat, message, FALSE);
1543 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1544 EmpathyMessage *message,
1547 EmpathyChatPriv *priv = GET_PRIV (chat);
1549 empathy_chat_view_message_acknowledged (chat->view,
1552 if (!empathy_message_is_edit (message)) {
1553 priv->unread_messages--;
1554 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1559 append_balance_error (EmpathyChat *chat,
1560 const gchar *message_body)
1562 EmpathyChatPriv *priv = GET_PRIV (chat);
1563 TpConnection *conn = tp_channel_borrow_connection (TP_CHANNEL (priv->tp_chat));
1564 const gchar *uri = tp_connection_get_balance_uri (conn);
1565 const gchar *error = _("insufficient balance to send message");
1566 gchar *str, *str_markup = NULL;
1568 if (message_body != NULL) {
1569 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1571 str = g_strdup_printf (_("Error sending message: %s"), error);
1574 if (!tp_str_empty (uri)) {
1575 /* translators: error used when user doesn't have enough credit on his
1576 * account to send the message. */
1577 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1578 " <a href='%s'>Top up</a>."), uri);
1580 if (message_body != NULL) {
1581 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1583 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1584 escaped_body, markup_error);
1586 g_free (escaped_body);
1588 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1591 g_free (markup_error);
1594 if (str_markup != NULL)
1595 empathy_chat_view_append_event_markup (chat->view, str_markup, str);
1597 empathy_chat_view_append_event (chat->view, str);
1600 g_free (str_markup);
1604 chat_send_error_cb (EmpathyTpChat *tp_chat,
1605 const gchar *message_body,
1606 TpChannelTextSendError error_code,
1607 const gchar *dbus_error,
1610 const gchar *error = NULL;
1613 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1614 append_balance_error (chat, message_body);
1616 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1617 error = _("not capable");
1620 if (error == NULL) {
1621 /* if we didn't find a dbus-error, try the old error */
1622 switch (error_code) {
1623 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1624 error = _("offline");
1626 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1627 error = _("invalid contact");
1629 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1630 error = _("permission denied");
1632 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1633 error = _("too long message");
1635 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1636 error = _("not implemented");
1638 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1640 error = _("unknown");
1645 if (message_body != NULL) {
1646 str = g_strdup_printf (_("Error sending message '%s': %s"),
1647 message_body, error);
1650 str = g_strdup_printf (_("Error sending message: %s"), error);
1653 empathy_chat_view_append_event (chat->view, str);
1658 chat_topic_label_size_allocate_cb (GtkLabel *label,
1659 GtkAllocation *allocation,
1662 EmpathyChatPriv *priv = GET_PRIV (chat);
1664 if (!gtk_label_get_line_wrap (label)) {
1665 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1666 gtk_widget_show (priv->expander_topic);
1668 gtk_widget_hide (priv->expander_topic);
1675 chat_topic_expander_activate_cb (GtkExpander *expander,
1676 GParamSpec *param_spec,
1679 EmpathyChatPriv *priv = GET_PRIV (chat);
1681 if (gtk_expander_get_expanded (expander)) {
1682 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1683 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1685 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1686 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1691 chat_subject_changed_cb (EmpathyChat *chat)
1693 EmpathyChatPriv *priv = GET_PRIV (chat);
1695 g_free (priv->subject);
1696 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1697 g_object_notify (G_OBJECT (chat), "subject");
1699 if (EMP_STR_EMPTY (priv->subject)) {
1700 gtk_widget_hide (priv->hbox_topic);
1702 gchar *markup_topic;
1705 markup_topic = empathy_add_link_markup (priv->subject);
1706 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1707 _("Topic:"), markup_topic);
1709 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1710 g_free (markup_text);
1711 g_free (markup_topic);
1713 gtk_widget_show (priv->hbox_topic);
1715 if (priv->block_events_timeout_id == 0) {
1718 if (!EMP_STR_EMPTY (priv->subject)) {
1719 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1721 if (tp_str_empty (actor)) {
1722 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1724 str = g_strdup_printf (_("Topic set by %s to: %s"),
1725 actor, priv->subject);
1727 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1728 /* No need to display this 'event' is no topic can be defined anyway */
1729 str = g_strdup (_("No topic defined"));
1733 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1740 chat_title_changed_cb (EmpathyChat *chat)
1742 EmpathyChatPriv *priv = GET_PRIV (chat);
1744 g_free (priv->name);
1745 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1746 g_object_notify (G_OBJECT (chat), "name");
1750 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1754 GtkTextIter word_start = *iter;
1755 GtkTextIter word_end = *iter;
1758 if (gtk_text_iter_inside_word (&word_end) &&
1759 !gtk_text_iter_ends_word (&word_end)) {
1760 gtk_text_iter_forward_word_end (&word_end);
1765 if (gtk_text_iter_get_char (&tmp) == '\'') {
1766 gtk_text_iter_forward_char (&tmp);
1768 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1769 gtk_text_iter_forward_word_end (&word_end);
1774 if (gtk_text_iter_inside_word (&word_start) ||
1775 gtk_text_iter_ends_word (&word_start)) {
1776 if (!gtk_text_iter_starts_word (&word_start) ||
1777 gtk_text_iter_equal (&word_start, &word_end)) {
1778 gtk_text_iter_backward_word_start (&word_start);
1782 gtk_text_iter_backward_char (&tmp);
1784 if (gtk_text_iter_get_char (&tmp) == '\'') {
1785 gtk_text_iter_backward_char (&tmp);
1787 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1788 gtk_text_iter_backward_word_start (&word_start);
1793 *start = word_start;
1799 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1800 GtkTextIter *location,
1805 GtkTextIter iter, pos;
1807 /* Remove all misspelled tags in the inserted text.
1808 * This happens when text is inserted within a misspelled word. */
1809 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1810 gtk_text_iter_get_offset (location) - len);
1811 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1814 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1817 GtkTextIter start, end;
1820 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1823 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1825 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1826 gtk_text_iter_equal (&pos, &end) ||
1827 empathy_spell_check (str)) {
1828 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1830 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1835 } while (gtk_text_iter_forward_word_end (&iter) &&
1836 gtk_text_iter_compare (&iter, location) <= 0);
1840 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1845 GtkTextIter word_start, word_end;
1847 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1848 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1849 &word_start, &word_end);
1854 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1857 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1858 chat_composing_stop (chat);
1860 chat_composing_start (chat);
1865 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1870 GtkTextIter prev_pos;
1871 GtkTextIter word_start;
1872 GtkTextIter word_end;
1876 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1878 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1879 gtk_text_buffer_get_insert (buffer));
1880 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1882 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1885 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1886 !gtk_text_iter_equal (&pos, &word_end)) {
1887 str = gtk_text_buffer_get_text (buffer,
1888 &word_start, &word_end, FALSE);
1890 if (!empathy_spell_check (str)) {
1891 gtk_text_buffer_apply_tag_by_name (buffer,
1892 "misspelled", &word_start, &word_end);
1894 gtk_text_buffer_remove_tag_by_name (buffer,
1895 "misspelled", &word_start, &word_end);
1902 gtk_text_buffer_move_mark (buffer, mark, &pos);
1906 empathy_isspace_cb (gunichar c,
1909 return g_unichar_isspace (c);
1913 chat_input_key_press_event_cb (GtkWidget *widget,
1917 EmpathyChatPriv *priv;
1920 GtkWidget *text_view_sw;
1922 priv = GET_PRIV (chat);
1924 priv->most_recent_event_type = event->type;
1926 /* Catch ctrl+up/down so we can traverse messages we sent */
1927 if ((event->state & GDK_CONTROL_MASK) &&
1928 (event->keyval == GDK_KEY_Up ||
1929 event->keyval == GDK_KEY_Down)) {
1930 GtkTextBuffer *buffer;
1933 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1934 chat_input_history_update (chat, buffer);
1936 if (event->keyval == GDK_KEY_Up) {
1937 str = chat_input_history_get_next (chat);
1939 str = chat_input_history_get_prev (chat);
1942 g_signal_handlers_block_by_func (buffer,
1943 chat_input_text_buffer_changed_cb,
1945 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1946 g_signal_handlers_unblock_by_func (buffer,
1947 chat_input_text_buffer_changed_cb,
1953 /* Catch enter but not ctrl/shift-enter */
1954 if (IS_ENTER (event->keyval) &&
1955 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1958 /* This is to make sure that kinput2 gets the enter. And if
1959 * it's handled there we shouldn't send on it. This is because
1960 * kinput2 uses Enter to commit letters. See:
1961 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1964 view = GTK_TEXT_VIEW (chat->input_text_view);
1965 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1966 gtk_text_view_reset_im_context (view);
1970 chat_input_text_view_send (chat);
1974 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1976 if (IS_ENTER (event->keyval) &&
1977 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1978 /* Newline for shift/control-enter. */
1981 if (!(event->state & GDK_CONTROL_MASK) &&
1982 event->keyval == GDK_KEY_Page_Up) {
1983 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1984 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1987 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1988 event->keyval == GDK_KEY_Page_Down) {
1989 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1990 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1991 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1992 gtk_adjustment_set_value (adj, val);
1995 if (event->keyval == GDK_KEY_Escape) {
1996 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1998 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1999 event->keyval == GDK_KEY_Tab) {
2000 GtkTextBuffer *buffer;
2001 GtkTextIter start, current;
2002 gchar *nick, *completed;
2003 GList *list, *completed_list;
2004 gboolean is_start_of_buffer;
2006 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
2007 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
2009 /* Get the start of the nick to complete. */
2010 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
2011 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
2012 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
2014 is_start_of_buffer = gtk_text_iter_is_start (&start);
2016 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
2017 g_completion_add_items (priv->completion, list);
2019 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
2020 completed_list = g_completion_complete (priv->completion,
2029 GString *message = NULL;
2032 gtk_text_buffer_delete (buffer, &start, ¤t);
2034 len = g_list_length (completed_list);
2037 /* If we only have one hit, use that text
2038 * instead of the text in completed since the
2039 * completed text will use the typed string
2040 * which might be cased all wrong.
2043 text = empathy_contact_get_alias (completed_list->data);
2047 /* Print all hits to the scrollback view, so the
2048 * user knows what possibilities he has.
2051 message = g_string_new ("");
2052 for (l = completed_list; l != NULL; l = l->next) {
2053 g_string_append (message, empathy_contact_get_alias (l->data));
2054 g_string_append (message, " - ");
2056 empathy_chat_view_append_event (chat->view, message->str);
2057 g_string_free (message, TRUE);
2060 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2062 if (len == 1 && is_start_of_buffer) {
2063 gchar *complete_char;
2065 complete_char = g_settings_get_string (
2066 priv->gsettings_chat,
2067 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2069 if (complete_char != NULL) {
2070 gtk_text_buffer_insert_at_cursor (buffer,
2072 strlen (complete_char));
2073 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2074 g_free (complete_char);
2081 g_completion_clear_items (priv->completion);
2083 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2093 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2097 gtk_widget_grab_focus (chat->input_text_view);
2103 chat_input_realize_cb (GtkWidget *widget,
2106 DEBUG ("Setting focus to the input text view");
2107 if (gtk_widget_is_sensitive (widget)) {
2108 gtk_widget_grab_focus (widget);
2113 chat_input_has_focus_notify_cb (GtkWidget *widget,
2117 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2121 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2122 EmpathySmiley *smiley,
2125 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2126 GtkTextBuffer *buffer;
2129 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2131 gtk_text_buffer_get_end_iter (buffer, &iter);
2132 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2134 gtk_text_buffer_get_end_iter (buffer, &iter);
2135 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2146 static EmpathyChatSpell *
2147 chat_spell_new (EmpathyChat *chat,
2152 EmpathyChatSpell *chat_spell;
2154 chat_spell = g_slice_new0 (EmpathyChatSpell);
2156 chat_spell->chat = g_object_ref (chat);
2157 chat_spell->word = g_strdup (word);
2158 chat_spell->start = start;
2159 chat_spell->end = end;
2165 chat_spell_free (EmpathyChatSpell *chat_spell)
2167 g_object_unref (chat_spell->chat);
2168 g_free (chat_spell->word);
2169 g_slice_free (EmpathyChatSpell, chat_spell);
2173 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2174 EmpathyChatSpell *chat_spell)
2176 empathy_chat_correct_word (chat_spell->chat,
2177 &(chat_spell->start),
2179 gtk_menu_item_get_label (menu_item));
2184 chat_spelling_build_suggestions_menu (const gchar *code,
2185 EmpathyChatSpell *chat_spell)
2187 GList *suggestions, *l;
2188 GtkWidget *menu, *menu_item;
2190 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2191 if (suggestions == NULL)
2194 menu = gtk_menu_new ();
2195 for (l = suggestions; l; l = l->next) {
2196 menu_item = gtk_menu_item_new_with_label (l->data);
2197 g_signal_connect (G_OBJECT (menu_item), "activate",
2198 G_CALLBACK (chat_spelling_menu_activate_cb),
2200 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2202 empathy_spell_free_suggestions (suggestions);
2204 gtk_widget_show_all (menu);
2210 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2212 GtkWidget *menu, *submenu, *item;
2215 codes = empathy_spell_get_enabled_language_codes ();
2216 g_assert (codes != NULL);
2218 if (g_list_length (codes) > 1) {
2219 menu = gtk_menu_new ();
2221 for (l = codes; l; l = l->next) {
2222 const gchar *code, *name;
2225 name = empathy_spell_get_language_name (code);
2229 item = gtk_image_menu_item_new_with_label (name);
2231 submenu = chat_spelling_build_suggestions_menu (
2233 if (submenu == NULL)
2234 gtk_widget_set_sensitive (item, FALSE);
2236 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2238 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2241 menu = chat_spelling_build_suggestions_menu (codes->data,
2244 menu = gtk_menu_new ();
2245 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2246 gtk_widget_set_sensitive (item, FALSE);
2247 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2250 g_list_free (codes);
2252 gtk_widget_show_all (menu);
2263 static EmpathyChatWord *
2264 chat_word_new (EmpathyChat *chat,
2268 EmpathyChatWord *chat_word;
2270 chat_word = g_slice_new0 (EmpathyChatWord);
2272 chat_word->chat = g_object_ref (chat);
2273 chat_word->word = g_strdup (word);
2274 chat_word->code = g_strdup (code);
2280 chat_word_free (EmpathyChatWord *chat_word)
2282 g_object_unref (chat_word->chat);
2283 g_free (chat_word->word);
2284 g_free (chat_word->code);
2285 g_slice_free (EmpathyChatWord, chat_word);
2289 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2290 EmpathyChatWord *chat_word)
2292 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2294 empathy_spell_add_to_dictionary (chat_word->code,
2296 priv->update_misspelled_words_id =
2297 g_idle_add (update_misspelled_words, chat_word->chat);
2301 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2303 GtkWidget *menu, *item, *lang_item, *image;
2306 const gchar *code, *name;
2307 EmpathyChatWord *chat_word;
2309 codes = empathy_spell_get_enabled_language_codes ();
2310 g_assert (codes != NULL);
2311 if (g_list_length (codes) > 1) {
2312 /* translators: %s is the selected word */
2313 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2315 item = gtk_image_menu_item_new_with_mnemonic (label);
2317 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2318 GTK_ICON_SIZE_MENU);
2319 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2322 menu = gtk_menu_new ();
2324 for (l = codes; l; l = l->next) {
2326 name = empathy_spell_get_language_name (code);
2330 lang_item = gtk_image_menu_item_new_with_label (name);
2332 chat_word= chat_word_new (chat_spell->chat,
2333 chat_spell->word, code);
2334 g_object_set_data_full (G_OBJECT (lang_item),
2335 "chat-word", chat_word,
2336 (GDestroyNotify) chat_word_free);
2338 g_signal_connect (G_OBJECT (lang_item), "activate",
2339 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2341 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2343 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2346 name = empathy_spell_get_language_name (code);
2347 g_assert (name != NULL);
2348 /* translators: first %s is the selected word,
2349 * second %s is the language name of the target dictionary */
2350 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2351 chat_spell->word, name);
2352 item = gtk_image_menu_item_new_with_mnemonic (label);
2354 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2355 GTK_ICON_SIZE_MENU);
2356 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2358 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2360 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2361 (GDestroyNotify) chat_word_free);
2363 g_signal_connect (G_OBJECT (item), "activate",
2364 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2367 g_list_free (codes);
2369 gtk_widget_show_all (item);
2375 chat_text_send_cb (GtkMenuItem *menuitem,
2378 chat_input_text_view_send (chat);
2382 chat_input_button_press_event_cb (GtkTextView *view,
2383 GdkEventButton *event,
2386 EmpathyChatPriv *priv = GET_PRIV (chat);
2388 priv->most_recent_event_type = event->type;
2394 chat_input_populate_popup_cb (GtkTextView *view,
2398 EmpathyChatPriv *priv = GET_PRIV (chat);
2399 GtkTextBuffer *buffer;
2400 GtkTextTagTable *table;
2403 GtkTextIter iter, start, end;
2406 EmpathyChatSpell *chat_spell;
2407 GtkWidget *spell_menu;
2408 GtkWidget *spell_item;
2409 EmpathySmileyManager *smiley_manager;
2410 GtkWidget *smiley_menu;
2413 buffer = gtk_text_view_get_buffer (view);
2415 /* Add the emoticon menu. */
2416 item = gtk_separator_menu_item_new ();
2417 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2418 gtk_widget_show (item);
2420 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2421 image = gtk_image_new_from_icon_name ("face-smile",
2422 GTK_ICON_SIZE_MENU);
2423 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2424 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2425 gtk_widget_show (item);
2427 smiley_manager = empathy_smiley_manager_dup_singleton ();
2428 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2429 chat_insert_smiley_activate_cb,
2431 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2432 g_object_unref (smiley_manager);
2434 /* Add the Send menu item. */
2435 gtk_text_buffer_get_bounds (buffer, &start, &end);
2436 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2437 if (!EMP_STR_EMPTY (str)) {
2438 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2439 g_signal_connect (G_OBJECT (item), "activate",
2440 G_CALLBACK (chat_text_send_cb), chat);
2441 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2442 gtk_widget_show (item);
2446 /* Add the spell check menu item. */
2447 table = gtk_text_buffer_get_tag_table (buffer);
2448 tag = gtk_text_tag_table_lookup (table, "misspelled");
2450 switch (priv->most_recent_event_type) {
2451 case GDK_BUTTON_PRESS:
2452 /* get the location from the pointer */
2453 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2454 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2455 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2457 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2458 GTK_TEXT_WINDOW_WIDGET,
2461 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2466 g_warn_if_reached ();
2467 /* assume the KEY_PRESS case */
2470 /* get the location from the cursor */
2471 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2472 gtk_text_buffer_get_insert (buffer));
2478 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2479 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2481 str = gtk_text_buffer_get_text (buffer,
2482 &start, &end, FALSE);
2484 if (!EMP_STR_EMPTY (str)) {
2485 chat_spell = chat_spell_new (chat, str, start, end);
2486 g_object_set_data_full (G_OBJECT (menu),
2487 "chat-spell", chat_spell,
2488 (GDestroyNotify) chat_spell_free);
2490 item = gtk_separator_menu_item_new ();
2491 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2492 gtk_widget_show (item);
2494 /* Spelling suggestions */
2495 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2496 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2497 GTK_ICON_SIZE_MENU);
2498 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2500 spell_menu = chat_spelling_build_menu (chat_spell);
2501 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2504 spell_item = gtk_separator_menu_item_new ();
2505 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2506 gtk_widget_show (spell_item);
2508 /* Add to dictionary */
2509 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2511 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2512 gtk_widget_show (spell_item);
2514 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2515 gtk_widget_show (item);
2521 chat_log_filter (TplEvent *event,
2524 EmpathyChat *chat = user_data;
2525 EmpathyMessage *message;
2526 EmpathyChatPriv *priv = GET_PRIV (chat);
2527 const GList *pending;
2529 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2530 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2532 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2533 message = empathy_message_from_tpl_log_event (event);
2535 for (; pending; pending = g_list_next (pending)) {
2536 if (empathy_message_equal (message, pending->data)) {
2537 g_object_unref (message);
2542 g_object_unref (message);
2548 show_pending_messages (EmpathyChat *chat) {
2549 EmpathyChatPriv *priv = GET_PRIV (chat);
2550 const GList *messages, *l;
2552 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2554 if (chat->view == NULL || priv->tp_chat == NULL)
2557 if (!priv->can_show_pending)
2560 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2562 for (l = messages; l != NULL ; l = g_list_next (l)) {
2563 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2564 chat_message_received (chat, message, TRUE);
2570 got_filtered_messages_cb (GObject *manager,
2571 GAsyncResult *result,
2576 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2577 EmpathyChatPriv *priv = GET_PRIV (chat);
2578 GError *error = NULL;
2580 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2581 result, &messages, &error)) {
2582 DEBUG ("%s. Aborting.", error->message);
2583 empathy_chat_view_append_event (chat->view,
2584 _("Failed to retrieve recent logs"));
2585 g_error_free (error);
2589 for (l = messages; l; l = g_list_next (l)) {
2590 EmpathyMessage *message;
2592 g_assert (TPL_IS_EVENT (l->data));
2594 message = empathy_message_from_tpl_log_event (l->data);
2595 g_object_unref (l->data);
2597 if (empathy_message_is_edit (message)) {
2598 /* this is an edited message, create a synthetic event
2599 * using the supersedes token and
2600 * original-message-sent timestamp, that we can then
2602 EmpathyMessage *syn_msg = g_object_new (
2603 EMPATHY_TYPE_MESSAGE,
2605 "token", empathy_message_get_supersedes (message),
2606 "type", empathy_message_get_tptype (message),
2607 "timestamp", empathy_message_get_original_timestamp (message),
2608 "incoming", empathy_message_is_incoming (message),
2610 "receiver", empathy_message_get_receiver (message),
2611 "sender", empathy_message_get_sender (message),
2614 empathy_chat_view_append_message (chat->view, syn_msg,
2615 chat_should_highlight (chat, syn_msg));
2616 empathy_chat_view_edit_message (chat->view, message);
2618 g_object_unref (syn_msg);
2620 /* append the latest message */
2621 empathy_chat_view_append_message (chat->view, message,
2622 chat_should_highlight (chat, message));
2625 g_object_unref (message);
2627 g_list_free (messages);
2630 /* in case of TPL error, skip backlog and show pending messages */
2631 priv->can_show_pending = TRUE;
2632 show_pending_messages (chat);
2634 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2635 * about it in EmpathyChatPriv definition */
2636 priv->retrieving_backlogs = FALSE;
2637 empathy_chat_messages_read (chat);
2639 /* Turn back on scrolling */
2640 empathy_chat_view_scroll (chat->view, TRUE);
2644 chat_add_logs (EmpathyChat *chat)
2646 EmpathyChatPriv *priv = GET_PRIV (chat);
2653 /* Turn off scrolling temporarily */
2654 empathy_chat_view_scroll (chat->view, FALSE);
2656 /* Add messages from last conversation */
2657 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2658 target = tpl_entity_new_from_room_id (priv->id);
2660 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2662 priv->retrieving_backlogs = TRUE;
2663 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2666 TPL_EVENT_MASK_TEXT,
2670 got_filtered_messages_cb,
2673 g_object_unref (target);
2677 chat_contacts_completion_func (const gchar *s1,
2681 gchar *tmp, *nick1, *nick2;
2688 return s1 ? -1 : +1;
2691 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2692 nick1 = g_utf8_casefold (tmp, -1);
2695 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2696 nick2 = g_utf8_casefold (tmp, -1);
2699 ret = strncmp (nick1, nick2, n);
2708 build_part_message (guint reason,
2710 EmpathyContact *actor,
2711 const gchar *message)
2713 GString *s = g_string_new ("");
2714 const gchar *actor_name = NULL;
2716 if (actor != NULL) {
2717 actor_name = empathy_contact_get_alias (actor);
2720 /* Having an actor only really makes sense for a few actions... */
2722 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2723 g_string_append_printf (s, _("%s has disconnected"), name);
2725 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2726 if (actor_name != NULL) {
2727 /* translators: reverse the order of these arguments
2728 * if the kicked should come before the kicker in your locale.
2730 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2733 g_string_append_printf (s, _("%s was kicked"), name);
2736 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2737 if (actor_name != NULL) {
2738 /* translators: reverse the order of these arguments
2739 * if the banned should come before the banner in your locale.
2741 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2744 g_string_append_printf (s, _("%s was banned"), name);
2748 g_string_append_printf (s, _("%s has left the room"), name);
2751 if (!EMP_STR_EMPTY (message)) {
2752 /* Note to translators: this string is appended to
2753 * notifications like "foo has left the room", with the message
2754 * given by the user living the room. If this poses a problem,
2755 * please let us know. :-)
2757 g_string_append_printf (s, _(" (%s)"), message);
2760 return g_string_free (s, FALSE);
2764 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2765 EmpathyContact *contact,
2766 EmpathyContact *actor,
2772 EmpathyChatPriv *priv = GET_PRIV (chat);
2773 const gchar *name = empathy_contact_get_alias (contact);
2776 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2778 if (priv->block_events_timeout_id != 0)
2782 str = g_strdup_printf (_("%s has joined the room"),
2785 str = build_part_message (reason, name, actor, message);
2788 empathy_chat_view_append_event (chat->view, str);
2793 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2794 EmpathyContact *old_contact,
2795 EmpathyContact *new_contact,
2800 EmpathyChatPriv *priv = GET_PRIV (chat);
2802 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2804 if (priv->block_events_timeout_id == 0) {
2807 str = g_strdup_printf (_("%s is now known as %s"),
2808 empathy_contact_get_alias (old_contact),
2809 empathy_contact_get_alias (new_contact));
2810 empathy_chat_view_append_event (chat->view, str);
2817 chat_contacts_visible_timeout_cb (gpointer chat)
2819 EmpathyChatPriv *priv = GET_PRIV (chat);
2821 /* Relax the size request */
2822 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2824 /* Set the position of the slider. This must be done here because
2825 * GtkPaned need to know its size allocation and it will be settled only
2826 * after the gtk_window_resize () tough effect. */
2827 if (priv->contacts_width > 0) {
2828 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2829 priv->contacts_width);
2832 priv->contacts_visible_id = 0;
2838 chat_update_contacts_visibility (EmpathyChat *chat,
2841 EmpathyChatPriv *priv = GET_PRIV (chat);
2843 if (!priv->scrolled_window_contacts) {
2847 if (priv->remote_contact != NULL) {
2851 if (show && priv->contact_list_view == NULL) {
2852 EmpathyIndividualStore *store;
2854 GtkAllocation allocation;
2856 /* We are adding the contact list to the chat, we don't want the
2857 * chat view to become too small. If the chat view is already
2858 * smaller than 250 make sure that size won't change. If the
2859 * chat view is bigger the contact list will take some space on
2860 * it but we make sure the chat view don't become smaller than
2861 * 250. Relax the size request once the resize is done */
2862 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2863 min_width = MIN (allocation.width, 250);
2864 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2866 /* There is no way to know when the window resize will happen
2867 * since it is WM's decision. Let's hope it won't be longer. */
2868 if (priv->contacts_visible_id != 0)
2869 g_source_remove (priv->contacts_visible_id);
2870 priv->contacts_visible_id = g_timeout_add (500,
2871 chat_contacts_visible_timeout_cb, chat);
2873 store = EMPATHY_INDIVIDUAL_STORE (
2874 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2876 empathy_individual_store_set_show_groups (store, FALSE);
2878 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2879 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2880 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2881 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2882 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2883 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2884 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2886 empathy_individual_view_set_show_offline (
2887 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2888 empathy_individual_view_set_show_uninteresting (
2889 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2891 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2892 priv->contact_list_view);
2894 gtk_widget_show (priv->contact_list_view);
2895 gtk_widget_show (priv->scrolled_window_contacts);
2896 g_object_unref (store);
2898 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2899 gtk_widget_hide (priv->scrolled_window_contacts);
2900 if (priv->contact_list_view != NULL) {
2901 gtk_widget_destroy (priv->contact_list_view);
2902 priv->contact_list_view = NULL;
2908 empathy_chat_set_show_contacts (EmpathyChat *chat,
2911 EmpathyChatPriv *priv = GET_PRIV (chat);
2913 priv->show_contacts = show;
2915 chat_update_contacts_visibility (chat, show);
2917 g_object_notify (G_OBJECT (chat), "show-contacts");
2921 chat_self_contact_changed_cb (EmpathyChat *chat)
2923 EmpathyChatPriv *priv = GET_PRIV (chat);
2925 if (priv->self_contact != NULL) {
2926 g_signal_handlers_disconnect_by_func (priv->self_contact,
2927 chat_self_contact_alias_changed_cb,
2930 g_clear_object (&priv->self_contact);
2932 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2933 if (priv->self_contact != NULL) {
2934 g_object_ref (priv->self_contact);
2936 if (empathy_chat_is_room (chat)) {
2937 g_signal_connect_swapped (priv->self_contact, "notify::alias",
2938 G_CALLBACK (chat_self_contact_alias_changed_cb),
2943 chat_self_contact_alias_changed_cb (chat);
2947 chat_remote_contact_changed_cb (EmpathyChat *chat)
2949 EmpathyChatPriv *priv = GET_PRIV (chat);
2951 if (priv->remote_contact != NULL) {
2952 g_object_unref (priv->remote_contact);
2953 priv->remote_contact = NULL;
2958 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2959 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2960 if (priv->remote_contact != NULL) {
2961 g_object_ref (priv->remote_contact);
2962 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2964 else if (priv->tp_chat != NULL) {
2965 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2968 chat_update_contacts_visibility (chat, priv->show_contacts);
2970 g_object_notify (G_OBJECT (chat), "remote-contact");
2971 g_object_notify (G_OBJECT (chat), "id");
2975 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2981 EmpathyChatPriv *priv;
2983 priv = GET_PRIV (chat);
2985 if (!priv->tp_chat) {
2989 chat_composing_remove_timeout (chat);
2990 g_object_unref (priv->tp_chat);
2991 priv->tp_chat = NULL;
2992 g_object_notify (G_OBJECT (chat), "tp-chat");
2994 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2995 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2997 chat_update_contacts_visibility (chat, FALSE);
2999 priv->unread_messages_when_offline = priv->unread_messages;
3003 update_misspelled_words (gpointer data)
3005 EmpathyChat *chat = EMPATHY_CHAT (data);
3006 EmpathyChatPriv *priv = GET_PRIV (chat);
3007 GtkTextBuffer *buffer;
3011 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3013 gtk_text_buffer_get_end_iter (buffer, &iter);
3014 length = gtk_text_iter_get_offset (&iter);
3015 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3016 NULL, length, chat);
3018 priv->update_misspelled_words_id = 0;
3024 conf_spell_checking_cb (GSettings *gsettings_chat,
3028 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3029 EmpathyChatPriv *priv = GET_PRIV (chat);
3030 gboolean spell_checker;
3031 GtkTextBuffer *buffer;
3033 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3036 spell_checker = g_settings_get_boolean (gsettings_chat,
3037 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3039 if (!empathy_spell_supported ()) {
3040 spell_checker = FALSE;
3043 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3045 if (spell_checker == priv->spell_checking_enabled) {
3046 if (spell_checker) {
3047 /* Possibly changed dictionaries,
3048 * update misspelled words. Need to do so in idle
3049 * so the spell checker is updated. */
3050 priv->update_misspelled_words_id =
3051 g_idle_add (update_misspelled_words, chat);
3057 if (spell_checker) {
3060 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3061 buffer, "notify::cursor-position",
3062 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3064 priv->insert_text_id = tp_g_signal_connect_object (
3065 buffer, "insert-text",
3066 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3067 chat, G_CONNECT_AFTER);
3068 priv->delete_range_id = tp_g_signal_connect_object (
3069 buffer, "delete-range",
3070 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3071 chat, G_CONNECT_AFTER);
3073 gtk_text_buffer_create_tag (buffer, "misspelled",
3074 "underline", PANGO_UNDERLINE_ERROR,
3077 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3078 gtk_text_buffer_get_insert (buffer));
3079 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3082 /* Mark misspelled words in the existing buffer.
3083 * Need to do so in idle so the spell checker is updated. */
3084 priv->update_misspelled_words_id =
3085 g_idle_add (update_misspelled_words, chat);
3087 GtkTextTagTable *table;
3090 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3091 priv->notify_cursor_position_id = 0;
3092 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3093 priv->insert_text_id = 0;
3094 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3095 priv->delete_range_id = 0;
3097 table = gtk_text_buffer_get_tag_table (buffer);
3098 tag = gtk_text_tag_table_lookup (table, "misspelled");
3099 gtk_text_tag_table_remove (table, tag);
3101 gtk_text_buffer_delete_mark_by_name (buffer,
3102 "previous-cursor-position");
3105 priv->spell_checking_enabled = spell_checker;
3109 save_paned_pos_timeout (gpointer data)
3111 EmpathyChat *self = data;
3114 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3116 g_settings_set_int (self->priv->gsettings_ui,
3117 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3124 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3128 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3130 if (chat->priv->save_paned_pos_id != 0)
3131 g_source_remove (chat->priv->save_paned_pos_id);
3133 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3134 save_paned_pos_timeout, chat);
3140 chat_create_ui (EmpathyChat *chat)
3142 EmpathyChatPriv *priv = GET_PRIV (chat);
3146 GtkTextBuffer *buffer;
3147 EmpathyThemeManager *theme_mgr;
3149 filename = empathy_file_lookup ("empathy-chat.ui",
3151 gui = empathy_builder_get_file (filename,
3152 "chat_widget", &priv->widget,
3153 "hpaned", &priv->hpaned,
3154 "vbox_left", &priv->vbox_left,
3155 "scrolled_window_chat", &priv->scrolled_window_chat,
3156 "scrolled_window_input", &priv->scrolled_window_input,
3157 "hbox_topic", &priv->hbox_topic,
3158 "expander_topic", &priv->expander_topic,
3159 "label_topic", &priv->label_topic,
3160 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3161 "info_bar_vbox", &priv->info_bar_vbox,
3164 empathy_builder_connect (gui, chat,
3165 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3166 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3171 /* Add message view. */
3172 theme_mgr = empathy_theme_manager_dup_singleton ();
3173 chat->view = empathy_theme_manager_create_view (theme_mgr);
3174 g_object_unref (theme_mgr);
3175 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3176 and other types, even though it's non-editable and doesn't accept any
3177 drags. This steals drag motion for anything inside the scrollbars,
3178 making drag destinations on chat windows far less useful.
3180 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3181 g_signal_connect (chat->view, "focus_in_event",
3182 G_CALLBACK (chat_text_view_focus_in_event_cb),
3184 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3185 GTK_WIDGET (chat->view));
3186 gtk_widget_show (GTK_WIDGET (chat->view));
3188 /* Add input GtkTextView */
3189 chat->input_text_view = empathy_input_text_view_new ();
3190 g_signal_connect (chat->input_text_view, "notify::has-focus",
3191 G_CALLBACK (chat_input_has_focus_notify_cb),
3193 g_signal_connect (chat->input_text_view, "key-press-event",
3194 G_CALLBACK (chat_input_key_press_event_cb),
3196 g_signal_connect (chat->input_text_view, "realize",
3197 G_CALLBACK (chat_input_realize_cb),
3199 g_signal_connect (chat->input_text_view, "button-press-event",
3200 G_CALLBACK (chat_input_button_press_event_cb),
3202 g_signal_connect (chat->input_text_view, "populate-popup",
3203 G_CALLBACK (chat_input_populate_popup_cb),
3205 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3206 tp_g_signal_connect_object (buffer, "changed",
3207 G_CALLBACK (chat_input_text_buffer_changed_cb),
3209 tp_g_signal_connect_object (priv->gsettings_chat,
3210 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3211 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3212 conf_spell_checking_cb (priv->gsettings_chat,
3213 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3214 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3215 chat->input_text_view);
3216 gtk_widget_show (chat->input_text_view);
3218 /* Add the (invisible) search bar */
3219 priv->search_bar = empathy_search_bar_new (chat->view);
3220 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3223 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3225 /* Initialy hide the topic, will be shown if not empty */
3226 gtk_widget_hide (priv->hbox_topic);
3228 g_signal_connect (priv->hpaned, "notify::position",
3229 G_CALLBACK (chat_hpaned_pos_changed_cb),
3232 /* Set widget focus order */
3233 list = g_list_append (NULL, priv->search_bar);
3234 list = g_list_append (list, priv->scrolled_window_input);
3235 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3238 list = g_list_append (NULL, priv->vbox_left);
3239 list = g_list_append (list, priv->scrolled_window_contacts);
3240 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3243 list = g_list_append (NULL, priv->hpaned);
3244 list = g_list_append (list, priv->hbox_topic);
3245 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3248 /* Add the main widget in the chat widget */
3249 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3250 g_object_unref (gui);
3254 chat_finalize (GObject *object)
3257 EmpathyChatPriv *priv;
3259 chat = EMPATHY_CHAT (object);
3260 priv = GET_PRIV (chat);
3262 DEBUG ("Finalized: %p", object);
3264 if (priv->update_misspelled_words_id != 0)
3265 g_source_remove (priv->update_misspelled_words_id);
3267 if (priv->save_paned_pos_id != 0)
3268 g_source_remove (priv->save_paned_pos_id);
3270 if (priv->contacts_visible_id != 0)
3271 g_source_remove (priv->contacts_visible_id);
3273 g_object_unref (priv->gsettings_chat);
3274 g_object_unref (priv->gsettings_ui);
3276 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3277 g_list_free (priv->input_history);
3279 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3280 g_list_free (priv->compositors);
3282 chat_composing_remove_timeout (chat);
3284 g_object_unref (priv->account_manager);
3285 g_object_unref (priv->log_manager);
3287 if (priv->tp_chat) {
3288 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3289 chat_invalidated_cb, chat);
3290 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3291 chat_message_received_cb, chat);
3292 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3293 chat_message_acknowledged_cb, chat);
3294 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3295 chat_send_error_cb, chat);
3296 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3297 chat_state_changed_cb, chat);
3298 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3299 chat_members_changed_cb, chat);
3300 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3301 chat_self_contact_changed_cb, chat);
3302 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3303 chat_remote_contact_changed_cb, chat);
3304 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3305 chat_title_changed_cb, chat);
3306 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3307 chat_subject_changed_cb, chat);
3308 empathy_tp_chat_leave (priv->tp_chat, "");
3309 g_object_unref (priv->tp_chat);
3311 if (priv->account) {
3312 g_object_unref (priv->account);
3314 if (priv->self_contact) {
3315 g_signal_handlers_disconnect_by_func (priv->self_contact,
3316 chat_self_contact_alias_changed_cb,
3318 g_object_unref (priv->self_contact);
3320 if (priv->remote_contact) {
3321 g_object_unref (priv->remote_contact);
3324 if (priv->block_events_timeout_id) {
3325 g_source_remove (priv->block_events_timeout_id);
3329 g_free (priv->name);
3330 g_free (priv->subject);
3331 g_completion_free (priv->completion);
3333 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3335 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3339 chat_constructed (GObject *object)
3341 EmpathyChat *chat = EMPATHY_CHAT (object);
3342 EmpathyChatPriv *priv = GET_PRIV (chat);
3344 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3345 /* First display logs from the logger and then display pending messages */
3346 chat_add_logs (chat);
3349 /* Just display pending messages for rooms */
3350 priv->can_show_pending = TRUE;
3351 show_pending_messages (chat);
3356 empathy_chat_class_init (EmpathyChatClass *klass)
3358 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3360 object_class->finalize = chat_finalize;
3361 object_class->get_property = chat_get_property;
3362 object_class->set_property = chat_set_property;
3363 object_class->constructed = chat_constructed;
3365 g_object_class_install_property (object_class,
3367 g_param_spec_object ("tp-chat",
3369 "The tp chat object",
3370 EMPATHY_TYPE_TP_CHAT,
3373 G_PARAM_STATIC_STRINGS));
3374 g_object_class_install_property (object_class,
3376 g_param_spec_object ("account",
3377 "Account of the chat",
3378 "The account of the chat",
3381 G_PARAM_STATIC_STRINGS));
3382 g_object_class_install_property (object_class,
3384 g_param_spec_string ("id",
3386 "The id of the chat",
3389 G_PARAM_STATIC_STRINGS));
3390 g_object_class_install_property (object_class,
3392 g_param_spec_string ("name",
3394 "The name of the chat",
3397 G_PARAM_STATIC_STRINGS));
3398 g_object_class_install_property (object_class,
3400 g_param_spec_string ("subject",
3402 "The subject or topic of the chat",
3405 G_PARAM_STATIC_STRINGS));
3406 g_object_class_install_property (object_class,
3407 PROP_REMOTE_CONTACT,
3408 g_param_spec_object ("remote-contact",
3409 "The remote contact",
3410 "The remote contact is any",
3411 EMPATHY_TYPE_CONTACT,
3413 G_PARAM_STATIC_STRINGS));
3414 g_object_class_install_property (object_class,
3416 g_param_spec_boolean ("show-contacts",
3417 "Contacts' visibility",
3418 "The visibility of the contacts' list",
3421 G_PARAM_STATIC_STRINGS));
3423 g_object_class_install_property (object_class,
3425 g_param_spec_boolean ("sms-channel",
3427 "TRUE if this channel is for sending SMSes",
3429 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3431 g_object_class_install_property (object_class,
3432 PROP_N_MESSAGES_SENDING,
3433 g_param_spec_uint ("n-messages-sending",
3434 "Num Messages Sending",
3435 "The number of messages being sent",
3437 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3439 g_object_class_install_property (object_class,
3440 PROP_NB_UNREAD_MESSAGES,
3441 g_param_spec_uint ("nb-unread-messages",
3442 "Num Unread Messages",
3443 "The number of unread messages",
3445 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3447 signals[COMPOSING] =
3448 g_signal_new ("composing",
3449 G_OBJECT_CLASS_TYPE (object_class),
3453 g_cclosure_marshal_generic,
3458 * EmpathyChat::new-message:
3459 * @self: the #EmpathyChat
3460 * @message: the new message
3461 * @pending: whether the message was in the pending queue when @self
3463 * @should_highlight: %TRUE if the message mentions the local user
3465 signals[NEW_MESSAGE] =
3466 g_signal_new ("new-message",
3467 G_OBJECT_CLASS_TYPE (object_class),
3471 g_cclosure_marshal_generic,
3473 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3475 signals[PART_COMMAND_ENTERED] =
3476 g_signal_new ("part-command-entered",
3477 G_OBJECT_CLASS_TYPE (object_class),
3481 g_cclosure_marshal_generic,
3485 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3489 chat_block_events_timeout_cb (gpointer data)
3491 EmpathyChatPriv *priv = GET_PRIV (data);
3493 priv->block_events_timeout_id = 0;
3499 account_manager_prepared_cb (GObject *source_object,
3500 GAsyncResult *result,
3503 GList *accounts, *l;
3504 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3505 EmpathyChat *chat = user_data;
3506 GError *error = NULL;
3508 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3509 DEBUG ("Failed to prepare the account manager: %s", error->message);
3510 g_error_free (error);
3514 accounts = tp_account_manager_get_valid_accounts (account_manager);
3516 for (l = accounts; l != NULL; l = l->next) {
3517 TpAccount *account = l->data;
3518 tp_g_signal_connect_object (account, "status-changed",
3519 G_CALLBACK (chat_new_connection_cb),
3523 g_list_free (accounts);
3527 empathy_chat_init (EmpathyChat *chat)
3529 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3530 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3533 priv->log_manager = tpl_log_manager_dup_singleton ();
3534 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3535 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3537 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3538 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3539 priv->input_history = NULL;
3540 priv->input_history_current = NULL;
3541 priv->account_manager = tp_account_manager_dup ();
3543 tp_proxy_prepare_async (priv->account_manager, NULL,
3544 account_manager_prepared_cb, chat);
3546 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3547 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3549 /* Block events for some time to avoid having "has come online" or
3550 * "joined" messages. */
3551 priv->block_events_timeout_id =
3552 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3554 /* Add nick name completion */
3555 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3556 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3558 chat_create_ui (chat);
3562 empathy_chat_new (EmpathyTpChat *tp_chat)
3564 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3568 empathy_chat_get_tp_chat (EmpathyChat *chat)
3570 EmpathyChatPriv *priv = GET_PRIV (chat);
3572 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3574 return priv->tp_chat;
3580 GtkWidget *info_bar;
3590 passwd_remember_button_cb (GtkButton *button,
3593 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3597 passwd_not_now_button_cb (GtkButton *button,
3600 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3604 remember_password_infobar_response_cb (GtkWidget *info_bar,
3608 EmpathyChatPriv *priv = GET_PRIV (data->self);
3610 if (response_id == GTK_RESPONSE_OK) {
3611 DEBUG ("Saving room password");
3612 empathy_keyring_set_room_password_async (priv->account,
3613 empathy_tp_chat_get_id (priv->tp_chat),
3618 gtk_widget_destroy (info_bar);
3619 g_free (data->password);
3620 g_slice_free (PasswordData, data);
3624 chat_prompt_to_save_password (EmpathyChat *self,
3627 GtkWidget *content_area;
3634 /* save the password in case it needs to be saved */
3635 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3637 /* Remove all previous widgets */
3638 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3639 gtk_container_forall (GTK_CONTAINER (content_area),
3640 (GtkCallback) gtk_widget_destroy, NULL);
3641 data->button = NULL;
3644 data->spinner = NULL;
3646 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3647 GTK_MESSAGE_QUESTION);
3649 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3650 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3653 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3654 GTK_ICON_SIZE_DIALOG);
3655 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3658 label = gtk_label_new (_("Would you like to store this password?"));
3659 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3661 /* Add 'Remember' button */
3662 alig = gtk_alignment_new (0, 0.5, 1, 0);
3664 button = gtk_button_new_with_label (_("Remember"));
3665 gtk_container_add (GTK_CONTAINER (alig), button);
3666 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3668 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3671 /* Add 'Not now' button */
3672 alig = gtk_alignment_new (0, 0.5, 1, 0);
3674 button = gtk_button_new_with_label (_("Not now"));
3675 gtk_container_add (GTK_CONTAINER (alig), button);
3676 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3678 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3682 g_signal_handler_disconnect (data->info_bar, data->response_id);
3683 g_signal_connect (data->info_bar, "response",
3684 G_CALLBACK (remember_password_infobar_response_cb), data);
3686 gtk_widget_show_all (data->info_bar);
3690 provide_password_cb (GObject *tp_chat,
3694 PasswordData *data = user_data;
3695 EmpathyChat *self = data->self;
3696 EmpathyChatPriv *priv = GET_PRIV (self);
3697 GError *error = NULL;
3699 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3701 DEBUG ("error: %s", error->message);
3702 /* FIXME: what should we do if that's another error? Close the channel?
3703 * Display the raw D-Bus error to the user isn't very useful */
3704 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3706 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3707 gtk_widget_set_sensitive (data->entry, TRUE);
3708 gtk_widget_grab_focus (data->entry);
3711 gtk_info_bar_set_message_type (
3712 GTK_INFO_BAR (data->info_bar),
3716 gtk_widget_set_sensitive (data->button, TRUE);
3717 gtk_button_set_label (GTK_BUTTON (data->button),
3721 gtk_label_set_text (GTK_LABEL (data->label),
3722 _("Wrong password; please try again:"));
3725 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3726 gtk_widget_hide (data->spinner);
3728 g_error_free (error);
3732 if (empathy_keyring_is_available ()) {
3733 /* ask whether they want to save the password */
3734 chat_prompt_to_save_password (self, data);
3736 /* Get rid of the password info bar finally */
3737 gtk_widget_destroy (data->info_bar);
3738 g_slice_free (PasswordData, data);
3742 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3743 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3744 gtk_widget_grab_focus (self->input_text_view);
3748 password_infobar_response_cb (GtkWidget *info_bar,
3752 EmpathyChatPriv *priv = GET_PRIV (data->self);
3753 const gchar *password;
3755 if (response_id != GTK_RESPONSE_OK) {
3756 gtk_widget_destroy (info_bar);
3757 g_slice_free (PasswordData, data);
3761 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3763 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3764 provide_password_cb, data);
3766 gtk_widget_set_sensitive (data->button, FALSE);
3767 gtk_widget_set_sensitive (data->entry, FALSE);
3769 gtk_spinner_start (GTK_SPINNER (data->spinner));
3770 gtk_widget_show (data->spinner);
3774 password_entry_activate_cb (GtkWidget *entry,
3777 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3781 passwd_join_button_cb (GtkButton *button,
3784 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3788 clear_icon_released_cb (GtkEntry *entry,
3789 GtkEntryIconPosition icon_pos,
3793 gtk_entry_set_text (entry, "");
3797 password_entry_changed_cb (GtkEditable *entry,
3802 str = gtk_entry_get_text (GTK_ENTRY (entry));
3804 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3805 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3809 infobar_chat_invalidated_cb (TpProxy *proxy,
3813 gpointer password_infobar)
3815 /* Destroy the password infobar whenever a channel is invalidated
3816 * so we don't have multiple infobars when the MUC is rejoined */
3817 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3821 display_password_info_bar (EmpathyChat *self)
3823 EmpathyChatPriv *priv = GET_PRIV (self);
3824 GtkWidget *info_bar;
3825 GtkWidget *content_area;
3835 data = g_slice_new0 (PasswordData);
3837 info_bar = gtk_info_bar_new ();
3838 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3839 GTK_MESSAGE_QUESTION);
3841 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3843 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3844 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3847 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3848 GTK_ICON_SIZE_DIALOG);
3849 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3852 label = gtk_label_new (_("This room is protected by a password:"));
3853 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3855 /* Add password entry */
3856 entry = gtk_entry_new ();
3857 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3858 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3860 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3861 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3862 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3863 GTK_ENTRY_ICON_SECONDARY, FALSE);
3865 g_signal_connect (entry, "icon-release",
3866 G_CALLBACK (clear_icon_released_cb), data);
3867 g_signal_connect (entry, "changed",
3868 G_CALLBACK (password_entry_changed_cb), data);
3870 g_signal_connect (entry, "activate",
3871 G_CALLBACK (password_entry_activate_cb), data);
3873 /* Focus the password entry once it's realized */
3874 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3876 /* Add 'Join' button */
3877 alig = gtk_alignment_new (0, 0.5, 1, 0);
3879 button = gtk_button_new_with_label (_("Join"));
3880 gtk_container_add (GTK_CONTAINER (alig), button);
3881 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3883 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3887 spinner = gtk_spinner_new ();
3888 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3890 /* Save some data for messing around with later */
3892 data->info_bar = info_bar;
3893 data->button = button;
3894 data->label = label;
3895 data->entry = entry;
3896 data->spinner = spinner;
3898 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3900 gtk_widget_show_all (hbox);
3902 tp_g_signal_connect_object (priv->tp_chat,
3903 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3906 data->response_id = g_signal_connect (info_bar, "response",
3907 G_CALLBACK (password_infobar_response_cb), data);
3909 gtk_widget_show_all (info_bar);
3910 /* ... but hide the spinner */
3911 gtk_widget_hide (spinner);
3913 /* prevent the user from typing anything */
3914 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3918 provide_saved_password_cb (GObject *tp_chat,
3922 EmpathyChat *self = user_data;
3923 EmpathyChatPriv *priv = GET_PRIV (self);
3924 GError *error = NULL;
3926 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3928 DEBUG ("error: %s", error->message);
3929 /* FIXME: what should we do if that's another error? Close the channel?
3930 * Display the raw D-Bus error to the user isn't very useful */
3931 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3932 display_password_info_bar (self);
3933 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3935 g_error_free (error);
3940 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3941 gtk_widget_grab_focus (self->input_text_view);
3945 chat_room_got_password_cb (GObject *source,
3946 GAsyncResult *result,
3949 EmpathyChat *self = user_data;
3950 EmpathyChatPriv *priv = GET_PRIV (self);
3951 const gchar *password;
3952 GError *error = NULL;
3954 password = empathy_keyring_get_room_password_finish (priv->account,
3957 if (error != NULL) {
3958 DEBUG ("Couldn't get room password: %s\n", error->message);
3959 g_clear_error (&error);
3961 display_password_info_bar (self);
3962 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3966 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3967 provide_saved_password_cb, self);
3971 chat_password_needed_changed_cb (EmpathyChat *self)
3973 EmpathyChatPriv *priv = GET_PRIV (self);
3975 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3976 empathy_keyring_get_room_password_async (priv->account,
3977 empathy_tp_chat_get_id (priv->tp_chat),
3978 chat_room_got_password_cb, self);
3983 chat_sms_channel_changed_cb (EmpathyChat *self)
3985 EmpathyChatPriv *priv = GET_PRIV (self);
3987 priv->sms_channel = tp_text_channel_is_sms_channel (
3988 (TpTextChannel *) priv->tp_chat);
3989 g_object_notify (G_OBJECT (self), "sms-channel");
3993 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3995 g_object_notify (G_OBJECT (self), "n-messages-sending");
3999 empathy_chat_set_tp_chat (EmpathyChat *chat,
4000 EmpathyTpChat *tp_chat)
4002 EmpathyChatPriv *priv = GET_PRIV (chat);
4004 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4005 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4007 if (priv->tp_chat) {
4011 if (priv->account) {
4012 g_object_unref (priv->account);
4015 priv->tp_chat = g_object_ref (tp_chat);
4016 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4018 g_signal_connect (tp_chat, "invalidated",
4019 G_CALLBACK (chat_invalidated_cb),
4021 g_signal_connect (tp_chat, "message-received-empathy",
4022 G_CALLBACK (chat_message_received_cb),
4024 g_signal_connect (tp_chat, "message_acknowledged",
4025 G_CALLBACK (chat_message_acknowledged_cb),
4027 g_signal_connect (tp_chat, "send-error",
4028 G_CALLBACK (chat_send_error_cb),
4030 g_signal_connect (tp_chat, "chat-state-changed-empathy",
4031 G_CALLBACK (chat_state_changed_cb),
4033 g_signal_connect (tp_chat, "members-changed",
4034 G_CALLBACK (chat_members_changed_cb),
4036 g_signal_connect (tp_chat, "member-renamed",
4037 G_CALLBACK (chat_member_renamed_cb),
4039 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4040 G_CALLBACK (chat_self_contact_changed_cb),
4042 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4043 G_CALLBACK (chat_remote_contact_changed_cb),
4045 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4046 G_CALLBACK (chat_password_needed_changed_cb),
4048 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4049 G_CALLBACK (chat_sms_channel_changed_cb),
4051 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4052 G_CALLBACK (chat_n_messages_sending_changed_cb),
4054 g_signal_connect_swapped (tp_chat, "notify::title",
4055 G_CALLBACK (chat_title_changed_cb),
4057 g_signal_connect_swapped (tp_chat, "notify::subject",
4058 G_CALLBACK (chat_subject_changed_cb),
4061 /* Get initial value of properties */
4062 chat_sms_channel_changed_cb (chat);
4063 chat_self_contact_changed_cb (chat);
4064 chat_remote_contact_changed_cb (chat);
4065 chat_title_changed_cb (chat);
4066 chat_subject_changed_cb (chat);
4068 if (chat->input_text_view) {
4069 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4070 if (priv->block_events_timeout_id == 0) {
4071 empathy_chat_view_append_event (chat->view, _("Connected"));
4075 g_object_notify (G_OBJECT (chat), "tp-chat");
4076 g_object_notify (G_OBJECT (chat), "id");
4077 g_object_notify (G_OBJECT (chat), "account");
4079 /* This is a noop when tp-chat is set at object construction time and causes
4080 * the pending messages to be show when it's set on the object after it has
4082 show_pending_messages (chat);
4084 /* check if a password is needed */
4085 chat_password_needed_changed_cb (chat);
4089 empathy_chat_get_account (EmpathyChat *chat)
4091 EmpathyChatPriv *priv = GET_PRIV (chat);
4093 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4095 return priv->account;
4099 empathy_chat_get_id (EmpathyChat *chat)
4101 EmpathyChatPriv *priv = GET_PRIV (chat);
4103 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4109 empathy_chat_dup_name (EmpathyChat *chat)
4111 EmpathyChatPriv *priv = GET_PRIV (chat);
4114 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4118 if (!ret && priv->remote_contact) {
4119 ret = empathy_contact_get_alias (priv->remote_contact);
4126 ret = _("Conversation");
4128 if (priv->sms_channel)
4129 /* Translators: this string is a something like
4130 * "Escher Cat (SMS)" */
4131 return g_strdup_printf (_("%s (SMS)"), ret);
4133 return g_strdup (ret);
4137 empathy_chat_get_subject (EmpathyChat *chat)
4139 EmpathyChatPriv *priv = GET_PRIV (chat);
4141 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4143 return priv->subject;
4147 empathy_chat_get_remote_contact (EmpathyChat *chat)
4149 EmpathyChatPriv *priv = GET_PRIV (chat);
4151 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4153 return priv->remote_contact;
4157 empathy_chat_get_contact_menu (EmpathyChat *chat)
4159 EmpathyChatPriv *priv = GET_PRIV (chat);
4160 GtkWidget *menu = NULL;
4161 FolksIndividual *individual;
4164 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4166 if (priv->remote_contact == NULL)
4169 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4170 if (contact == NULL)
4173 individual = empathy_ensure_individual_from_tp_contact (contact);
4175 if (individual == NULL)
4178 menu = empathy_individual_menu_new (individual,
4179 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4180 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4181 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4182 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4184 g_object_unref (individual);
4190 empathy_chat_clear (EmpathyChat *chat)
4192 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4194 empathy_chat_view_clear (chat->view);
4198 empathy_chat_scroll_down (EmpathyChat *chat)
4200 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4202 empathy_chat_view_scroll_down (chat->view);
4206 empathy_chat_cut (EmpathyChat *chat)
4208 GtkTextBuffer *buffer;
4210 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4212 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4213 if (gtk_text_buffer_get_has_selection (buffer)) {
4214 GtkClipboard *clipboard;
4216 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4218 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4223 empathy_chat_copy (EmpathyChat *chat)
4225 GtkTextBuffer *buffer;
4227 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4229 if (empathy_chat_view_get_has_selection (chat->view)) {
4230 empathy_chat_view_copy_clipboard (chat->view);
4234 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4235 if (gtk_text_buffer_get_has_selection (buffer)) {
4236 GtkClipboard *clipboard;
4238 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4240 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4245 EmpathyChatPriv *priv = GET_PRIV (chat);
4247 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4254 GtkClipboard *clipboard;
4256 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4257 start = g_utf8_offset_to_pointer (topic, start_offset);
4258 end = g_utf8_offset_to_pointer (topic, end_offset);
4259 selection = g_strndup (start, end - start);
4261 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4262 gtk_clipboard_set_text (clipboard, selection, -1);
4270 empathy_chat_paste (EmpathyChat *chat)
4272 GtkTextBuffer *buffer;
4273 GtkClipboard *clipboard;
4274 EmpathyChatPriv *priv;
4276 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4278 priv = GET_PRIV (chat);
4280 if (gtk_widget_get_visible (priv->search_bar)) {
4281 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4285 if (priv->tp_chat == NULL ||
4286 !gtk_widget_is_sensitive (chat->input_text_view))
4289 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4290 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4292 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4296 empathy_chat_find (EmpathyChat *chat)
4298 EmpathyChatPriv *priv;
4300 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4302 priv = GET_PRIV (chat);
4304 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4308 empathy_chat_correct_word (EmpathyChat *chat,
4311 const gchar *new_word)
4313 GtkTextBuffer *buffer;
4315 g_return_if_fail (chat != NULL);
4316 g_return_if_fail (new_word != NULL);
4318 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4320 gtk_text_buffer_delete (buffer, start, end);
4321 gtk_text_buffer_insert (buffer, start,
4327 empathy_chat_is_room (EmpathyChat *chat)
4329 EmpathyChatPriv *priv = GET_PRIV (chat);
4331 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4333 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4337 empathy_chat_is_highlighted (EmpathyChat *chat)
4339 EmpathyChatPriv *priv = GET_PRIV (chat);
4341 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4343 return priv->highlighted;
4347 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4349 EmpathyChatPriv *priv = GET_PRIV (self);
4351 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4353 return priv->unread_messages;
4356 /* called when the messages have been read by user */
4358 empathy_chat_messages_read (EmpathyChat *self)
4360 EmpathyChatPriv *priv = GET_PRIV (self);
4362 g_return_if_fail (EMPATHY_IS_CHAT (self));
4364 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4365 * definition. If we are still retrieving the backlogs, do not ACK */
4366 if (priv->retrieving_backlogs)
4369 if (priv->tp_chat != NULL) {
4370 tp_text_channel_ack_all_pending_messages_async (
4371 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4374 priv->highlighted = FALSE;
4376 if (priv->unread_messages_when_offline > 0) {
4377 /* We can't ack those as the connection has gone away so just consider
4379 priv->unread_messages -= priv->unread_messages_when_offline;
4380 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4381 priv->unread_messages_when_offline = 0;
4385 /* Return TRUE if on of the contacts in this chat is composing */
4387 empathy_chat_is_composing (EmpathyChat *chat)
4389 return chat->priv->compositors != NULL;
4393 empathy_chat_is_sms_channel (EmpathyChat *self)
4395 EmpathyChatPriv *priv = GET_PRIV (self);
4397 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4399 return priv->sms_channel;
4403 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4405 EmpathyChatPriv *priv;
4407 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4409 priv = GET_PRIV (self);
4411 if (priv->tp_chat == NULL) {
4416 g_object_get (priv->tp_chat,
4417 "n-messages-sending", &n_messages,
4425 empathy_chat_dup_text (EmpathyChat *self)
4427 GtkTextBuffer *buffer;
4428 GtkTextIter start, end;
4430 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4432 gtk_text_buffer_get_bounds (buffer, &start, &end);
4433 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4437 empathy_chat_set_text (EmpathyChat *self,
4440 GtkTextBuffer *buffer;
4442 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4444 gtk_text_buffer_set_text (buffer, text, -1);