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/telepathy-glib.h>
40 #include <telepathy-logger/telepathy-logger.h>
41 #include <libempathy/empathy-gsettings.h>
42 #include <libempathy/empathy-keyring.h>
43 #include <libempathy/empathy-utils.h>
44 #include <libempathy/empathy-request-util.h>
45 #include <libempathy/empathy-chatroom-manager.h>
46 #include <libempathy/empathy-client-factory.h>
48 #include "empathy-chat.h"
49 #include "empathy-spell.h"
50 #include "empathy-individual-information-dialog.h"
51 #include "empathy-individual-store-channel.h"
52 #include "empathy-individual-view.h"
53 #include "empathy-input-text-view.h"
54 #include "empathy-search-bar.h"
55 #include "empathy-theme-manager.h"
56 #include "empathy-theme-adium.h"
57 #include "empathy-smiley-manager.h"
58 #include "empathy-ui-utils.h"
59 #include "empathy-string-parser.h"
60 #include "extensions/extensions.h"
62 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
63 #include <libempathy/empathy-debug.h>
65 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
66 #define COMPOSING_STOP_TIMEOUT 5
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
69 struct _EmpathyChatPriv {
70 EmpathyTpChat *tp_chat;
75 EmpathyContact *self_contact;
76 EmpathyContact *remote_contact;
77 gboolean show_contacts;
79 GSettings *gsettings_chat;
80 GSettings *gsettings_ui;
82 TplLogManager *log_manager;
83 TpAccountManager *account_manager;
85 GList *input_history_current;
87 GCompletion *completion;
88 guint composing_stop_timeout_id;
89 guint block_events_timeout_id;
90 TpHandleType handle_type;
92 gboolean has_input_vscroll;
94 /* TRUE if spell checking is enabled, FALSE otherwise.
95 * This is to keep track of the last state of spell checking
97 gboolean spell_checking_enabled;
99 /* These store the signal handler ids for the enclosed text entry. */
100 gulong insert_text_id;
101 gulong delete_range_id;
102 gulong notify_cursor_position_id;
104 /* Source func ID for update_misspelled_words () */
105 guint update_misspelled_words_id;
106 /* Source func ID for save_paned_pos_timeout () */
107 guint save_paned_pos_id;
108 /* Source func ID for chat_contacts_visible_timeout_cb () */
109 guint contacts_visible_id;
113 GtkWidget *vbox_left;
114 GtkWidget *scrolled_window_chat;
115 GtkWidget *scrolled_window_input;
116 GtkWidget *scrolled_window_contacts;
117 GtkWidget *hbox_topic;
118 GtkWidget *expander_topic;
119 GtkWidget *label_topic;
120 GtkWidget *contact_list_view;
121 GtkWidget *info_bar_vbox;
122 GtkWidget *search_bar;
124 guint unread_messages;
125 guint unread_messages_when_offline;
126 /* TRUE if the pending messages can be displayed. This is to avoid to show
127 * pending messages *before* messages from logs. (#603980) */
128 gboolean can_show_pending;
130 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
131 * be differently handled since it introduces another race condition, which
132 * is really hard to occur, but still possible.
134 * With the current workaround (which has the race above), we need to be
135 * sure to ACK any pending messages only when the retrieval of backlogs is
136 * finished, that's why using retrieving_backlogs flag.
137 * empathy_chat_messages_read () will check this variable and not ACK
138 * anything when TRUE. It will be set TRUE at chat_constructed () and set
139 * back to FALSE when the backlog has been retrieved and the pending
140 * messages actually showed to the user.
142 * Race condition introduced with this workaround:
143 * Scenario: a message is pending, the user is notified and selects the tab.
144 * the tab with a pending message is focused before the messages are properly
145 * shown (since the preparation of the window is slower AND async WRT the
146 * tab showing), which means the user won't see any new messages (rare but
147 * possible), if he/she will change tab focus before the messages are
148 * properly shown, the tab will be set as 'seen' and the user won't be
149 * notified again about the already notified pending messages when the
150 * messages in tab will be properly shown */
151 gboolean retrieving_backlogs;
152 gboolean sms_channel;
154 /* we need to know whether populate-popup happened in response to
155 * the keyboard or the mouse. We can't ask GTK for the most recent
156 * event, because it will be a notify event. Instead we track it here */
157 GdkEventType most_recent_event_type;
159 /* A regex matching our own current nickname in the room, or %NULL if
160 * !empathy_chat_is_room (). */
161 GRegex *highlight_regex;
163 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
164 * Cleared by empathy_chat_messages_read (). */
165 gboolean highlighted;
169 gchar *text; /* Original message that was specified
170 * upon entry creation. */
171 gchar *modified_text; /* Message that was modified by user.
172 * When no modifications were made, it is NULL */
178 PART_COMMAND_ENTERED,
192 PROP_N_MESSAGES_SENDING,
193 PROP_NB_UNREAD_MESSAGES,
196 static guint signals[LAST_SIGNAL] = { 0 };
198 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
200 static gboolean update_misspelled_words (gpointer data);
203 chat_get_property (GObject *object,
208 EmpathyChat *chat = EMPATHY_CHAT (object);
209 EmpathyChatPriv *priv = GET_PRIV (object);
213 g_value_set_object (value, priv->tp_chat);
216 g_value_set_object (value, priv->account);
219 g_value_take_string (value, empathy_chat_dup_name (chat));
222 g_value_set_string (value, priv->id);
225 g_value_set_string (value, priv->subject);
227 case PROP_REMOTE_CONTACT:
228 g_value_set_object (value, priv->remote_contact);
230 case PROP_SHOW_CONTACTS:
231 g_value_set_boolean (value, priv->show_contacts);
233 case PROP_SMS_CHANNEL:
234 g_value_set_boolean (value, priv->sms_channel);
236 case PROP_N_MESSAGES_SENDING:
237 g_value_set_uint (value,
238 empathy_chat_get_n_messages_sending (chat));
240 case PROP_NB_UNREAD_MESSAGES:
241 g_value_set_uint (value,
242 empathy_chat_get_nb_unread_messages (chat));
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
251 chat_set_property (GObject *object,
256 EmpathyChat *chat = EMPATHY_CHAT (object);
260 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
262 case PROP_SHOW_CONTACTS:
263 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
272 account_reconnected (EmpathyChat *chat,
275 EmpathyChatPriv *priv = GET_PRIV (chat);
277 DEBUG ("Account reconnected, request a new Text channel");
279 /* FIXME: Ideally we should ask to handle ourself the channel so we can
280 * report the error if any but this is blocked by
281 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
282 switch (priv->handle_type) {
283 case TP_HANDLE_TYPE_CONTACT:
284 if (priv->sms_channel)
285 empathy_sms_contact_id (
287 TP_USER_ACTION_TIME_NOT_USER_ACTION,
290 empathy_chat_with_contact_id (
292 TP_USER_ACTION_TIME_NOT_USER_ACTION,
295 case TP_HANDLE_TYPE_ROOM:
296 empathy_join_muc (account, priv->id,
297 TP_USER_ACTION_TIME_NOT_USER_ACTION);
299 case TP_HANDLE_TYPE_NONE:
300 case TP_HANDLE_TYPE_LIST:
301 case TP_HANDLE_TYPE_GROUP:
303 g_assert_not_reached ();
307 g_object_unref (chat);
311 chat_new_connection_cb (TpAccount *account,
315 gchar *dbus_error_name,
319 EmpathyChatPriv *priv = GET_PRIV (chat);
321 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
324 if (priv->tp_chat != NULL || account != priv->account ||
325 priv->handle_type == TP_HANDLE_TYPE_NONE ||
326 EMP_STR_EMPTY (priv->id))
331 account_reconnected (chat, account);
335 chat_composing_remove_timeout (EmpathyChat *chat)
337 EmpathyChatPriv *priv;
339 priv = GET_PRIV (chat);
341 if (priv->composing_stop_timeout_id) {
342 g_source_remove (priv->composing_stop_timeout_id);
343 priv->composing_stop_timeout_id = 0;
348 set_chate_state_cb (GObject *source,
349 GAsyncResult *result,
352 GError *error = NULL;
354 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
356 DEBUG ("Failed to set chat state: %s", error->message);
357 g_error_free (error);
362 set_chat_state (EmpathyChat *self,
363 TpChannelChatState state)
365 EmpathyChatPriv *priv = GET_PRIV (self);
367 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
368 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
371 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
372 set_chate_state_cb, self);
376 chat_composing_stop_timeout_cb (EmpathyChat *chat)
378 EmpathyChatPriv *priv;
379 gboolean send_chat_states;
381 priv = GET_PRIV (chat);
383 priv->composing_stop_timeout_id = 0;
384 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
385 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
386 if (!send_chat_states) {
387 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
389 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
396 chat_composing_start (EmpathyChat *chat)
398 EmpathyChatPriv *priv;
399 gboolean send_chat_states;
401 priv = GET_PRIV (chat);
403 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
404 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
405 if (!send_chat_states) {
409 if (priv->composing_stop_timeout_id) {
410 /* Just restart the timeout */
411 chat_composing_remove_timeout (chat);
413 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
416 priv->composing_stop_timeout_id = g_timeout_add_seconds (
417 COMPOSING_STOP_TIMEOUT,
418 (GSourceFunc) chat_composing_stop_timeout_cb,
423 chat_composing_stop (EmpathyChat *chat)
425 chat_composing_remove_timeout (chat);
426 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
430 chat_input_history_entry_cmp (InputHistoryEntry *entry,
433 if (!tp_strdiff (entry->text, text)) {
434 if (entry->modified_text != NULL) {
435 /* Modified entry and single string cannot be equal. */
443 static InputHistoryEntry *
444 chat_input_history_entry_new_with_text (const gchar *text)
446 InputHistoryEntry *entry;
447 entry = g_slice_new0 (InputHistoryEntry);
448 entry->text = g_strdup (text);
454 chat_input_history_entry_free (InputHistoryEntry *entry)
456 g_free (entry->text);
457 g_free (entry->modified_text);
458 g_slice_free (InputHistoryEntry, entry);
462 chat_input_history_entry_revert (InputHistoryEntry *entry)
464 g_free (entry->modified_text);
465 entry->modified_text = NULL;
469 chat_input_history_entry_update_text (InputHistoryEntry *entry,
474 if (!tp_strdiff (text, entry->text)) {
475 g_free (entry->modified_text);
476 entry->modified_text = NULL;
480 old = entry->modified_text;
481 entry->modified_text = g_strdup (text);
486 chat_input_history_entry_get_text (InputHistoryEntry *entry)
492 if (entry->modified_text != NULL) {
493 return entry->modified_text;
499 chat_input_history_remove_item (GList *list,
502 list = g_list_remove_link (list, item);
503 chat_input_history_entry_free (item->data);
504 g_list_free_1 (item);
509 chat_input_history_revert (EmpathyChat *chat)
511 EmpathyChatPriv *priv;
515 InputHistoryEntry *entry;
517 priv = GET_PRIV (chat);
518 list = priv->input_history;
521 DEBUG ("No input history");
525 /* Delete temporary entry */
526 if (priv->input_history_current != NULL) {
528 list = chat_input_history_remove_item (list, item1);
529 if (priv->input_history_current == item1) {
530 /* Removed temporary entry was current entry */
531 priv->input_history = list;
532 priv->input_history_current = NULL;
537 /* There is no entry to revert */
541 /* Restore the current history entry to original value */
542 item1 = priv->input_history_current;
544 chat_input_history_entry_revert (entry);
546 /* Remove restored entry if there is other occurance before this entry */
547 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
548 (GCompareFunc) chat_input_history_entry_cmp);
549 if (item2 != item1) {
550 list = chat_input_history_remove_item (list, item1);
553 /* Remove other occurance of the restored entry */
554 item2 = g_list_find_custom (item1->next,
555 chat_input_history_entry_get_text (entry),
556 (GCompareFunc) chat_input_history_entry_cmp);
558 list = chat_input_history_remove_item (list, item2);
562 priv->input_history_current = NULL;
563 priv->input_history = list;
567 chat_input_history_add (EmpathyChat *chat,
571 EmpathyChatPriv *priv;
574 InputHistoryEntry *entry;
576 priv = GET_PRIV (chat);
578 list = priv->input_history;
580 /* Remove any other occurances of this entry, if not temporary */
582 while ((item = g_list_find_custom (list, str,
583 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
584 list = chat_input_history_remove_item (list, item);
587 /* Trim the list to the last 10 items */
588 while (g_list_length (list) > 10) {
589 item = g_list_last (list);
591 list = chat_input_history_remove_item (list, item);
599 entry = chat_input_history_entry_new_with_text (str);
600 list = g_list_prepend (list, entry);
602 /* Set the list and the current item pointer */
603 priv->input_history = list;
605 priv->input_history_current = list;
608 priv->input_history_current = NULL;
613 chat_input_history_get_next (EmpathyChat *chat)
615 EmpathyChatPriv *priv;
619 priv = GET_PRIV (chat);
621 if (priv->input_history == NULL) {
622 DEBUG ("No input history, next entry is NULL");
625 g_assert (priv->input_history_current != NULL);
627 if ((item = g_list_next (priv->input_history_current)) == NULL)
629 item = priv->input_history_current;
632 msg = chat_input_history_entry_get_text (item->data);
634 DEBUG ("Returning next entry: '%s'", msg);
636 priv->input_history_current = item;
642 chat_input_history_get_prev (EmpathyChat *chat)
644 EmpathyChatPriv *priv;
648 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
650 priv = GET_PRIV (chat);
652 if (priv->input_history == NULL) {
653 DEBUG ("No input history, previous entry is NULL");
657 if (priv->input_history_current == NULL)
661 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
663 item = priv->input_history_current;
666 msg = chat_input_history_entry_get_text (item->data);
668 DEBUG ("Returning previous entry: '%s'", msg);
670 priv->input_history_current = item;
676 chat_input_history_update (EmpathyChat *chat,
677 GtkTextBuffer *buffer)
679 EmpathyChatPriv *priv;
680 GtkTextIter start, end;
682 InputHistoryEntry *entry;
684 priv = GET_PRIV (chat);
686 gtk_text_buffer_get_bounds (buffer, &start, &end);
687 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
689 if (priv->input_history_current == NULL) {
690 /* Add the current text temporarily to the history */
691 chat_input_history_add (chat, text, TRUE);
696 /* Save the changes in the history */
697 entry = priv->input_history_current->data;
698 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
699 chat_input_history_entry_update_text (entry, text);
708 } ChatCommandMsgData;
711 chat_command_msg_cb (GObject *source,
712 GAsyncResult *result,
715 ChatCommandMsgData *data = user_data;
716 GError *error = NULL;
719 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
720 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
722 if (channel == NULL) {
723 DEBUG ("Failed to get channel: %s", error->message);
724 g_error_free (error);
726 empathy_theme_adium_append_event (data->chat->view,
727 _("Failed to open private chat"));
731 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
732 TpTextChannel *text = (TpTextChannel *) channel;
735 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
738 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
740 g_object_unref (msg);
743 g_object_unref (channel);
746 g_free (data->message);
747 g_slice_free (ChatCommandMsgData, data);
751 nick_command_supported (EmpathyChat *chat)
753 EmpathyChatPriv * priv = GET_PRIV (chat);
754 TpConnection *connection;
756 connection = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
757 return tp_proxy_has_interface_by_id (connection,
758 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
762 part_command_supported (EmpathyChat *chat)
764 EmpathyChatPriv * priv = GET_PRIV (chat);
766 return tp_proxy_has_interface_by_id (priv->tp_chat,
767 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
771 chat_command_clear (EmpathyChat *chat,
774 empathy_theme_adium_clear (chat->view);
778 chat_command_topic (EmpathyChat *chat,
781 EmpathyChatPriv *priv = GET_PRIV (chat);
783 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
784 empathy_theme_adium_append_event (chat->view,
785 _("Topic not supported on this conversation"));
789 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
790 empathy_theme_adium_append_event (chat->view,
791 _("You are not allowed to change the topic"));
795 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
799 empathy_chat_join_muc (EmpathyChat *chat,
802 EmpathyChatPriv *priv = GET_PRIV (chat);
804 empathy_join_muc (priv->account, room,
805 empathy_get_current_action_time ());
809 chat_command_join (EmpathyChat *chat,
814 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
816 /* FIXME: Ideally we should ask to handle ourself the channel so we can
817 * report the error if any but this is blocked by
818 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
819 while (rooms[i] != NULL) {
820 /* ignore empty strings */
821 if (!EMP_STR_EMPTY (rooms[i])) {
822 empathy_chat_join_muc (chat, rooms[i]);
830 chat_command_part (EmpathyChat *chat,
833 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
837 chat_command_msg_internal (EmpathyChat *chat,
838 const gchar *contact_id,
839 const gchar *message)
841 EmpathyChatPriv *priv = GET_PRIV (chat);
842 ChatCommandMsgData *data;
843 TpAccountChannelRequest *req;
846 request = tp_asv_new (
847 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
848 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
849 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
852 req = tp_account_channel_request_new (priv->account, request,
853 empathy_get_current_action_time ());
855 /* FIXME: We should probably search in members alias. But this
856 * is enough for IRC */
857 data = g_slice_new (ChatCommandMsgData);
859 data->message = g_strdup (message);
861 tp_account_channel_request_ensure_and_observe_channel_async (req,
862 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
864 g_object_unref (req);
865 g_hash_table_unref (request);
869 chat_command_query (EmpathyChat *chat,
872 /* If <message> part is not defined,
873 * strv[2] will be the terminal NULL */
874 chat_command_msg_internal (chat, strv[1], strv[2]);
878 chat_command_msg (EmpathyChat *chat,
881 chat_command_msg_internal (chat, strv[1], strv[2]);
885 callback_for_request_rename (TpProxy *proxy,
888 GObject *weak_object)
891 DEBUG ("Call to RequestRename method failed: %s",error->message);
896 chat_command_nick (EmpathyChat *chat,
899 EmpathyChatPriv *priv = GET_PRIV (chat);
902 proxy = TP_PROXY (tp_account_get_connection (priv->account));
904 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
905 strv[1], callback_for_request_rename, NULL, NULL, NULL);
909 chat_command_me (EmpathyChat *chat,
912 EmpathyChatPriv *priv = GET_PRIV (chat);
914 TpTextChannel *channel;
916 channel = (TpTextChannel *) (priv->tp_chat);
918 if (!tp_text_channel_supports_message_type (channel,
919 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
920 /* Action message are not supported, 'simulate' the action */
923 /* The TpChat can't be ready if it doesn't have the self contact */
924 g_assert (priv->self_contact != NULL);
926 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
928 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
933 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
937 empathy_tp_chat_send (priv->tp_chat, message);
938 g_object_unref (message);
942 chat_command_say (EmpathyChat *chat,
945 EmpathyChatPriv *priv = GET_PRIV (chat);
948 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
950 empathy_tp_chat_send (priv->tp_chat, message);
951 g_object_unref (message);
955 whois_got_contact_cb (GObject *source,
956 GAsyncResult *result,
959 EmpathyChat *chat = user_data;
960 EmpathyContact *contact;
961 FolksIndividual *individual;
963 contact = empathy_client_factory_dup_contact_by_id_finish (
964 EMPATHY_CLIENT_FACTORY (source), result, NULL);
966 if (contact == NULL) {
967 empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
971 individual = empathy_ensure_individual_from_tp_contact (
972 empathy_contact_get_tp_contact (contact));
973 empathy_display_individual_info (individual);
975 g_object_unref (individual);
976 g_object_unref (contact);
979 g_object_unref (chat);
983 chat_command_whois (EmpathyChat *chat,
986 EmpathyChatPriv *priv = GET_PRIV (chat);
988 EmpathyClientFactory *factory;
990 conn = tp_channel_get_connection ((TpChannel *) priv->tp_chat);
991 factory = empathy_client_factory_dup ();
993 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
994 * entered by the user (including spaces, if any). */
995 empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
996 whois_got_contact_cb, g_object_ref (chat));
998 g_object_unref (factory);
1002 chat_command_whale (EmpathyChat *chat,
1005 EmpathyChatPriv *priv = GET_PRIV (chat);
1008 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1010 "•_______________•");
1011 empathy_tp_chat_send (priv->tp_chat, message);
1012 g_object_unref (message);
1016 chat_command_babywhale (EmpathyChat *chat,
1019 EmpathyChatPriv *priv = GET_PRIV (chat);
1022 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1025 empathy_tp_chat_send (priv->tp_chat, message);
1026 g_object_unref (message);
1030 chat_command_inspector (EmpathyChat *chat,
1033 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1034 empathy_theme_adium_show_inspector (
1035 EMPATHY_THEME_ADIUM (chat->view));
1039 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1041 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1044 const gchar *prefix;
1047 ChatCommandFunc func;
1048 gboolean (*is_supported)(EmpathyChat *chat);
1052 static ChatCommandItem commands[] = {
1053 {"clear", 1, 1, chat_command_clear, NULL,
1054 N_("/clear: clear all messages from the current conversation")},
1056 {"topic", 2, 2, chat_command_topic, NULL,
1057 N_("/topic <topic>: set the topic of the current conversation")},
1059 {"join", 2, 2, chat_command_join, NULL,
1060 N_("/join <chat room ID>: join a new chat room")},
1062 {"j", 2, 2, chat_command_join, NULL,
1063 N_("/j <chat room ID>: join a new chat room")},
1065 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1066 {"part", 1, 3, chat_command_part, part_command_supported,
1067 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1068 "by default the current one")},
1070 {"query", 2, 3, chat_command_query, NULL,
1071 N_("/query <contact ID> [<message>]: open a private chat")},
1073 {"msg", 3, 3, chat_command_msg, NULL,
1074 N_("/msg <contact ID> <message>: open a private chat")},
1076 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1077 N_("/nick <nickname>: change your nickname on the current server")},
1079 {"me", 2, 2, chat_command_me, NULL,
1080 N_("/me <message>: send an ACTION message to the current conversation")},
1082 {"say", 2, 2, chat_command_say, NULL,
1083 N_("/say <message>: send <message> to the current conversation. "
1084 "This is used to send a message starting with a '/'. For example: "
1085 "\"/say /join is used to join a new chat room\"")},
1087 {"whois", 2, 2, chat_command_whois, NULL,
1088 N_("/whois <contact ID>: display information about a contact")},
1090 {"help", 1, 2, chat_command_help, NULL,
1091 N_("/help [<command>]: show all supported commands. "
1092 "If <command> is defined, show its usage.")},
1094 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1096 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1097 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1101 chat_command_show_help (EmpathyChat *chat,
1102 ChatCommandItem *item)
1106 if (item->help == NULL) {
1110 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1111 empathy_theme_adium_append_event (chat->view, str);
1116 chat_command_help (EmpathyChat *chat,
1121 /* If <command> part is not defined,
1122 * strv[1] will be the terminal NULL */
1123 if (strv[1] == NULL) {
1124 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1125 if (commands[i].is_supported != NULL) {
1126 if (!commands[i].is_supported (chat)) {
1130 if (commands[i].help == NULL) {
1133 empathy_theme_adium_append_event (chat->view,
1134 _(commands[i].help));
1139 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1140 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1141 if (commands[i].is_supported != NULL) {
1142 if (!commands[i].is_supported (chat)) {
1146 if (commands[i].help == NULL) {
1149 chat_command_show_help (chat, &commands[i]);
1154 empathy_theme_adium_append_event (chat->view,
1155 _("Unknown command"));
1159 chat_command_parse (const gchar *text, guint max_parts)
1164 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1166 array = g_ptr_array_sized_new (max_parts + 1);
1167 while (max_parts > 1) {
1170 /* Skip white spaces */
1171 while (g_ascii_isspace (*text)) {
1175 /* Search the end of this part, until first space. */
1176 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1182 item = g_strndup (text, end - text);
1183 g_ptr_array_add (array, item);
1184 DEBUG ("\tITEM: \"%s\"", item);
1190 /* Append last part if not empty */
1191 item = g_strstrip (g_strdup (text));
1192 if (!EMP_STR_EMPTY (item)) {
1193 g_ptr_array_add (array, item);
1194 DEBUG ("\tITEM: \"%s\"", item);
1199 /* Make the array NULL-terminated */
1200 g_ptr_array_add (array, NULL);
1202 return (GStrv) g_ptr_array_free (array, FALSE);
1206 has_prefix_case (const gchar *s,
1207 const gchar *prefix)
1209 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1213 chat_send (EmpathyChat *chat,
1216 EmpathyChatPriv *priv;
1220 if (EMP_STR_EMPTY (msg)) {
1224 priv = GET_PRIV (chat);
1226 chat_input_history_add (chat, msg, FALSE);
1228 if (msg[0] == '/') {
1229 gboolean second_slash = FALSE;
1230 const gchar *iter = msg + 1;
1232 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1237 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1240 c = *(msg + 1 + strlen (commands[i].prefix));
1241 if (c != '\0' && !g_ascii_isspace (c)) {
1244 if (commands[i].is_supported != NULL) {
1245 if (!commands[i].is_supported (chat)) {
1250 /* We can't use g_strsplit here because it does
1251 * not deal correctly if we have more than one space
1253 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1255 strv_len = g_strv_length (strv);
1256 if (strv_len < commands[i].min_parts ||
1257 strv_len > commands[i].max_parts) {
1258 chat_command_show_help (chat, &commands[i]);
1263 commands[i].func (chat, strv);
1268 /* Also allow messages with two slashes before the
1269 * first space, so it is possible to send a /unix/path.
1270 * This heuristic is kind of crap. */
1271 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1273 second_slash = TRUE;
1279 if (!second_slash) {
1280 empathy_theme_adium_append_event (chat->view,
1281 _("Unknown command; see /help for the available"
1287 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1289 empathy_tp_chat_send (priv->tp_chat, message);
1290 g_object_unref (message);
1294 chat_input_text_view_send (EmpathyChat *chat)
1296 GtkTextBuffer *buffer;
1297 GtkTextIter start, end;
1300 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1302 gtk_text_buffer_get_bounds (buffer, &start, &end);
1303 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1305 /* clear the input field */
1306 gtk_text_buffer_set_text (buffer, "", -1);
1307 /* delete input history modifications */
1308 chat_input_history_revert (chat);
1310 chat_send (chat, msg);
1315 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1316 TpContact *tp_contact,
1317 TpChannelChatState state,
1320 EmpathyChatPriv *priv;
1322 gboolean was_composing;
1323 EmpathyContact *contact;
1325 priv = GET_PRIV (chat);
1327 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1329 if (empathy_contact_is_user (contact)) {
1330 /* We don't care about our own chat state */
1334 was_composing = (priv->compositors != NULL);
1336 /* Find the contact in the list. After that l is the list elem or NULL */
1337 for (l = priv->compositors; l; l = l->next) {
1338 if (contact == l->data) {
1344 case TP_CHANNEL_CHAT_STATE_GONE:
1345 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1346 case TP_CHANNEL_CHAT_STATE_PAUSED:
1347 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1348 /* Contact is not composing */
1350 priv->compositors = g_list_remove_link (priv->compositors, l);
1351 g_object_unref (l->data);
1355 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1356 /* Contact is composing */
1358 priv->compositors = g_list_prepend (priv->compositors,
1359 g_object_ref (contact));
1363 g_assert_not_reached ();
1366 DEBUG ("Was composing: %s now composing: %s",
1367 was_composing ? "yes" : "no",
1368 priv->compositors ? "yes" : "no");
1370 if ((was_composing && !priv->compositors) ||
1371 (!was_composing && priv->compositors)) {
1372 /* Composing state changed */
1373 g_signal_emit (chat, signals[COMPOSING], 0,
1374 priv->compositors != NULL);
1378 g_object_unref (contact);
1382 get_highlight_regex_for (const gchar *name)
1385 gchar *name_esc, *pattern;
1386 GError *error = NULL;
1388 name_esc = g_regex_escape_string (name, -1);
1389 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1390 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1393 if (regex == NULL) {
1394 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1397 g_error_free (error);
1406 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1407 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1410 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1412 EmpathyChatPriv *priv = GET_PRIV (chat);
1414 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1416 if (priv->self_contact != NULL) {
1417 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1419 g_return_if_fail (alias != NULL);
1420 priv->highlight_regex = get_highlight_regex_for (alias);
1425 chat_should_highlight (EmpathyChat *chat,
1426 EmpathyMessage *message)
1428 EmpathyChatPriv *priv = GET_PRIV (chat);
1430 TpChannelTextMessageFlags flags;
1432 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1434 if (!empathy_chat_is_room (chat)) {
1438 if (!empathy_message_is_incoming (message)) {
1442 msg = empathy_message_get_body (message);
1447 flags = empathy_message_get_flags (message);
1448 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
1449 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1450 * have already been received by the user before (and so are in the logs) */
1454 if (priv->highlight_regex == NULL) {
1458 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1462 chat_message_received (EmpathyChat *chat,
1463 EmpathyMessage *message,
1466 EmpathyChatPriv *priv = GET_PRIV (chat);
1467 EmpathyContact *sender;
1469 sender = empathy_message_get_sender (message);
1471 if (empathy_message_is_edit (message)) {
1472 DEBUG ("Editing message '%s' to '%s'",
1473 empathy_message_get_supersedes (message),
1474 empathy_message_get_body (message));
1476 empathy_theme_adium_edit_message (chat->view, message);
1478 gboolean should_highlight = chat_should_highlight (chat, message);
1480 if (should_highlight) {
1481 priv->highlighted = TRUE;
1484 DEBUG ("Appending new message '%s' from %s (%d)",
1485 empathy_message_get_token (message),
1486 empathy_contact_get_alias (sender),
1487 empathy_contact_get_handle (sender));
1489 empathy_theme_adium_append_message (chat->view, message, should_highlight);
1491 if (empathy_message_is_incoming (message)) {
1492 priv->unread_messages++;
1493 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1496 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1500 /* We received a message so the contact is no longer
1502 chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
1503 TP_CHANNEL_CHAT_STATE_ACTIVE,
1508 chat_message_received_cb (EmpathyTpChat *tp_chat,
1509 EmpathyMessage *message,
1512 chat_message_received (chat, message, FALSE);
1516 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1517 EmpathyMessage *message,
1520 EmpathyChatPriv *priv = GET_PRIV (chat);
1522 empathy_theme_adium_message_acknowledged (chat->view,
1525 if (!empathy_message_is_edit (message)) {
1526 priv->unread_messages--;
1527 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1532 append_balance_error (EmpathyChat *chat,
1533 const gchar *message_body)
1535 EmpathyChatPriv *priv = GET_PRIV (chat);
1536 TpConnection *conn = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
1537 const gchar *uri = tp_connection_get_balance_uri (conn);
1538 const gchar *error = _("insufficient balance to send message");
1539 gchar *str, *str_markup = NULL;
1541 if (message_body != NULL) {
1542 str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
1544 str = g_strdup_printf (_("Error sending message: %s"), error);
1547 if (!tp_str_empty (uri)) {
1548 /* translators: error used when user doesn't have enough credit on his
1549 * account to send the message. */
1550 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1551 " <a href='%s'>Top up</a>."), uri);
1553 if (message_body != NULL) {
1554 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1556 str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
1557 escaped_body, markup_error);
1559 g_free (escaped_body);
1561 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1564 g_free (markup_error);
1567 if (str_markup != NULL)
1568 empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
1570 empathy_theme_adium_append_event (chat->view, str);
1573 g_free (str_markup);
1577 chat_send_error_cb (EmpathyTpChat *tp_chat,
1578 const gchar *message_body,
1579 TpChannelTextSendError error_code,
1580 const gchar *dbus_error,
1583 const gchar *error = NULL;
1586 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1587 append_balance_error (chat, message_body);
1589 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1590 error = _("not capable");
1593 if (error == NULL) {
1594 /* if we didn't find a dbus-error, try the old error */
1595 switch (error_code) {
1596 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1597 error = _("offline");
1599 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1600 error = _("invalid contact");
1602 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1603 error = _("permission denied");
1605 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1606 error = _("too long message");
1608 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1609 error = _("not implemented");
1611 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1613 error = _("unknown");
1618 if (message_body != NULL) {
1619 str = g_strdup_printf (_("Error sending message '%s': %s"),
1620 message_body, error);
1623 str = g_strdup_printf (_("Error sending message: %s"), error);
1626 empathy_theme_adium_append_event (chat->view, str);
1631 chat_topic_label_size_allocate_cb (GtkLabel *label,
1632 GtkAllocation *allocation,
1635 EmpathyChatPriv *priv = GET_PRIV (chat);
1637 if (!gtk_label_get_line_wrap (label)) {
1638 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1639 gtk_widget_show (priv->expander_topic);
1641 gtk_widget_hide (priv->expander_topic);
1648 chat_topic_expander_activate_cb (GtkExpander *expander,
1649 GParamSpec *param_spec,
1652 EmpathyChatPriv *priv = GET_PRIV (chat);
1654 if (gtk_expander_get_expanded (expander)) {
1655 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1656 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1658 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1659 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1664 chat_subject_changed_cb (EmpathyChat *chat)
1666 EmpathyChatPriv *priv = GET_PRIV (chat);
1668 g_free (priv->subject);
1669 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1670 g_object_notify (G_OBJECT (chat), "subject");
1672 if (EMP_STR_EMPTY (priv->subject)) {
1673 gtk_widget_hide (priv->hbox_topic);
1675 gchar *markup_topic;
1678 markup_topic = empathy_add_link_markup (priv->subject);
1679 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1680 _("Topic:"), markup_topic);
1682 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1683 g_free (markup_text);
1684 g_free (markup_topic);
1686 gtk_widget_show (priv->hbox_topic);
1688 if (priv->block_events_timeout_id == 0) {
1691 if (!EMP_STR_EMPTY (priv->subject)) {
1692 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1694 if (tp_str_empty (actor)) {
1695 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1697 str = g_strdup_printf (_("Topic set by %s to: %s"),
1698 actor, priv->subject);
1700 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1701 /* No need to display this 'event' is no topic can be defined anyway */
1702 str = g_strdup (_("No topic defined"));
1706 empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
1713 chat_title_changed_cb (EmpathyChat *chat)
1715 EmpathyChatPriv *priv = GET_PRIV (chat);
1717 g_free (priv->name);
1718 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1719 g_object_notify (G_OBJECT (chat), "name");
1723 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1727 GtkTextIter word_start = *iter;
1728 GtkTextIter word_end = *iter;
1731 if (gtk_text_iter_inside_word (&word_end) &&
1732 !gtk_text_iter_ends_word (&word_end)) {
1733 gtk_text_iter_forward_word_end (&word_end);
1738 if (gtk_text_iter_get_char (&tmp) == '\'') {
1739 gtk_text_iter_forward_char (&tmp);
1741 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1742 gtk_text_iter_forward_word_end (&word_end);
1747 if (gtk_text_iter_inside_word (&word_start) ||
1748 gtk_text_iter_ends_word (&word_start)) {
1749 if (!gtk_text_iter_starts_word (&word_start) ||
1750 gtk_text_iter_equal (&word_start, &word_end)) {
1751 gtk_text_iter_backward_word_start (&word_start);
1755 gtk_text_iter_backward_char (&tmp);
1757 if (gtk_text_iter_get_char (&tmp) == '\'') {
1758 gtk_text_iter_backward_char (&tmp);
1760 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1761 gtk_text_iter_backward_word_start (&word_start);
1766 *start = word_start;
1772 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1773 GtkTextIter *location,
1778 GtkTextIter iter, pos;
1780 /* Remove all misspelled tags in the inserted text.
1781 * This happens when text is inserted within a misspelled word. */
1782 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1783 gtk_text_iter_get_offset (location) - len);
1784 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1787 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1790 GtkTextIter start, end;
1793 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1796 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1798 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1799 gtk_text_iter_equal (&pos, &end) ||
1800 empathy_spell_check (str)) {
1801 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1803 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1808 } while (gtk_text_iter_forward_word_end (&iter) &&
1809 gtk_text_iter_compare (&iter, location) <= 0);
1813 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1818 GtkTextIter word_start, word_end;
1820 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1821 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1822 &word_start, &word_end);
1827 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1830 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1831 chat_composing_stop (chat);
1833 chat_composing_start (chat);
1838 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1843 GtkTextIter prev_pos;
1844 GtkTextIter word_start;
1845 GtkTextIter word_end;
1849 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1851 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1852 gtk_text_buffer_get_insert (buffer));
1853 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1855 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1858 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1859 !gtk_text_iter_equal (&pos, &word_end)) {
1860 str = gtk_text_buffer_get_text (buffer,
1861 &word_start, &word_end, FALSE);
1863 if (!empathy_spell_check (str)) {
1864 gtk_text_buffer_apply_tag_by_name (buffer,
1865 "misspelled", &word_start, &word_end);
1867 gtk_text_buffer_remove_tag_by_name (buffer,
1868 "misspelled", &word_start, &word_end);
1875 gtk_text_buffer_move_mark (buffer, mark, &pos);
1879 empathy_isspace_cb (gunichar c,
1882 return g_unichar_isspace (c);
1886 chat_input_key_press_event_cb (GtkWidget *widget,
1890 EmpathyChatPriv *priv;
1893 GtkWidget *text_view_sw;
1895 priv = GET_PRIV (chat);
1897 priv->most_recent_event_type = event->type;
1899 /* Catch ctrl+up/down so we can traverse messages we sent */
1900 if ((event->state & GDK_CONTROL_MASK) &&
1901 (event->keyval == GDK_KEY_Up ||
1902 event->keyval == GDK_KEY_Down)) {
1903 GtkTextBuffer *buffer;
1906 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1907 chat_input_history_update (chat, buffer);
1909 if (event->keyval == GDK_KEY_Up) {
1910 str = chat_input_history_get_next (chat);
1912 str = chat_input_history_get_prev (chat);
1915 g_signal_handlers_block_by_func (buffer,
1916 chat_input_text_buffer_changed_cb,
1918 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1919 g_signal_handlers_unblock_by_func (buffer,
1920 chat_input_text_buffer_changed_cb,
1926 /* Catch enter but not ctrl/shift-enter */
1927 if (IS_ENTER (event->keyval) &&
1928 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1931 /* This is to make sure that kinput2 gets the enter. And if
1932 * it's handled there we shouldn't send on it. This is because
1933 * kinput2 uses Enter to commit letters. See:
1934 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1937 view = GTK_TEXT_VIEW (chat->input_text_view);
1938 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1939 gtk_text_view_reset_im_context (view);
1943 chat_input_text_view_send (chat);
1947 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1949 if (IS_ENTER (event->keyval) &&
1950 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1951 /* Newline for shift/control-enter. */
1954 if (!(event->state & GDK_CONTROL_MASK) &&
1955 event->keyval == GDK_KEY_Page_Up) {
1956 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1957 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1960 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1961 event->keyval == GDK_KEY_Page_Down) {
1962 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1963 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1964 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1965 gtk_adjustment_set_value (adj, val);
1968 if (event->keyval == GDK_KEY_Escape) {
1969 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1971 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1972 event->keyval == GDK_KEY_Tab) {
1973 GtkTextBuffer *buffer;
1974 GtkTextIter start, current;
1975 gchar *nick, *completed;
1976 GList *list, *completed_list;
1977 gboolean is_start_of_buffer;
1979 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1980 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1982 /* Get the start of the nick to complete. */
1983 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1984 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1985 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1987 is_start_of_buffer = gtk_text_iter_is_start (&start);
1989 list = empathy_tp_chat_get_members (priv->tp_chat);
1990 g_completion_add_items (priv->completion, list);
1992 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1993 completed_list = g_completion_complete (priv->completion,
2002 GString *message = NULL;
2005 gtk_text_buffer_delete (buffer, &start, ¤t);
2007 len = g_list_length (completed_list);
2010 /* If we only have one hit, use that text
2011 * instead of the text in completed since the
2012 * completed text will use the typed string
2013 * which might be cased all wrong.
2016 text = empathy_contact_get_alias (completed_list->data);
2020 /* Print all hits to the scrollback view, so the
2021 * user knows what possibilities he has.
2024 message = g_string_new ("");
2025 for (l = completed_list; l != NULL; l = l->next) {
2026 g_string_append (message, empathy_contact_get_alias (l->data));
2027 g_string_append (message, " - ");
2029 empathy_theme_adium_append_event (chat->view, message->str);
2030 g_string_free (message, TRUE);
2033 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2035 if (len == 1 && is_start_of_buffer) {
2036 gchar *complete_char;
2038 complete_char = g_settings_get_string (
2039 priv->gsettings_chat,
2040 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2042 if (complete_char != NULL) {
2043 gtk_text_buffer_insert_at_cursor (buffer,
2045 strlen (complete_char));
2046 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2047 g_free (complete_char);
2054 g_completion_clear_items (priv->completion);
2056 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2066 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2070 gtk_widget_grab_focus (chat->input_text_view);
2076 chat_input_realize_cb (GtkWidget *widget,
2079 DEBUG ("Setting focus to the input text view");
2080 if (gtk_widget_is_sensitive (widget)) {
2081 gtk_widget_grab_focus (widget);
2086 chat_input_has_focus_notify_cb (GtkWidget *widget,
2090 empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2094 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2095 EmpathySmiley *smiley,
2098 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2099 GtkTextBuffer *buffer;
2102 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2104 gtk_text_buffer_get_end_iter (buffer, &iter);
2105 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
2107 gtk_text_buffer_get_end_iter (buffer, &iter);
2108 gtk_text_buffer_insert (buffer, &iter, " ", -1);
2119 static EmpathyChatSpell *
2120 chat_spell_new (EmpathyChat *chat,
2125 EmpathyChatSpell *chat_spell;
2127 chat_spell = g_slice_new0 (EmpathyChatSpell);
2129 chat_spell->chat = g_object_ref (chat);
2130 chat_spell->word = g_strdup (word);
2131 chat_spell->start = start;
2132 chat_spell->end = end;
2138 chat_spell_free (EmpathyChatSpell *chat_spell)
2140 g_object_unref (chat_spell->chat);
2141 g_free (chat_spell->word);
2142 g_slice_free (EmpathyChatSpell, chat_spell);
2146 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2147 EmpathyChatSpell *chat_spell)
2149 empathy_chat_correct_word (chat_spell->chat,
2150 &(chat_spell->start),
2152 gtk_menu_item_get_label (menu_item));
2157 chat_spelling_build_suggestions_menu (const gchar *code,
2158 EmpathyChatSpell *chat_spell)
2160 GList *suggestions, *l;
2161 GtkWidget *menu, *menu_item;
2163 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2164 if (suggestions == NULL)
2167 menu = gtk_menu_new ();
2168 for (l = suggestions; l; l = l->next) {
2169 menu_item = gtk_menu_item_new_with_label (l->data);
2170 g_signal_connect (G_OBJECT (menu_item), "activate",
2171 G_CALLBACK (chat_spelling_menu_activate_cb),
2173 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2175 empathy_spell_free_suggestions (suggestions);
2177 gtk_widget_show_all (menu);
2183 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2185 GtkWidget *menu, *submenu, *item;
2188 codes = empathy_spell_get_enabled_language_codes ();
2189 g_assert (codes != NULL);
2191 if (g_list_length (codes) > 1) {
2192 menu = gtk_menu_new ();
2194 for (l = codes; l; l = l->next) {
2195 const gchar *code, *name;
2198 name = empathy_spell_get_language_name (code);
2202 item = gtk_image_menu_item_new_with_label (name);
2204 submenu = chat_spelling_build_suggestions_menu (
2206 if (submenu == NULL)
2207 gtk_widget_set_sensitive (item, FALSE);
2209 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2211 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2214 menu = chat_spelling_build_suggestions_menu (codes->data,
2217 menu = gtk_menu_new ();
2218 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2219 gtk_widget_set_sensitive (item, FALSE);
2220 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2223 g_list_free (codes);
2225 gtk_widget_show_all (menu);
2236 static EmpathyChatWord *
2237 chat_word_new (EmpathyChat *chat,
2241 EmpathyChatWord *chat_word;
2243 chat_word = g_slice_new0 (EmpathyChatWord);
2245 chat_word->chat = g_object_ref (chat);
2246 chat_word->word = g_strdup (word);
2247 chat_word->code = g_strdup (code);
2253 chat_word_free (EmpathyChatWord *chat_word)
2255 g_object_unref (chat_word->chat);
2256 g_free (chat_word->word);
2257 g_free (chat_word->code);
2258 g_slice_free (EmpathyChatWord, chat_word);
2262 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2263 EmpathyChatWord *chat_word)
2265 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2267 empathy_spell_add_to_dictionary (chat_word->code,
2269 priv->update_misspelled_words_id =
2270 g_idle_add (update_misspelled_words, chat_word->chat);
2274 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2276 GtkWidget *menu, *item, *lang_item, *image;
2279 const gchar *code, *name;
2280 EmpathyChatWord *chat_word;
2282 codes = empathy_spell_get_enabled_language_codes ();
2283 g_assert (codes != NULL);
2284 if (g_list_length (codes) > 1) {
2285 /* translators: %s is the selected word */
2286 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2288 item = gtk_image_menu_item_new_with_mnemonic (label);
2290 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2291 GTK_ICON_SIZE_MENU);
2292 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2295 menu = gtk_menu_new ();
2297 for (l = codes; l; l = l->next) {
2299 name = empathy_spell_get_language_name (code);
2303 lang_item = gtk_image_menu_item_new_with_label (name);
2305 chat_word= chat_word_new (chat_spell->chat,
2306 chat_spell->word, code);
2307 g_object_set_data_full (G_OBJECT (lang_item),
2308 "chat-word", chat_word,
2309 (GDestroyNotify) chat_word_free);
2311 g_signal_connect (G_OBJECT (lang_item), "activate",
2312 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2314 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2316 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2319 name = empathy_spell_get_language_name (code);
2320 g_assert (name != NULL);
2321 /* translators: first %s is the selected word,
2322 * second %s is the language name of the target dictionary */
2323 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2324 chat_spell->word, name);
2325 item = gtk_image_menu_item_new_with_mnemonic (label);
2327 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2328 GTK_ICON_SIZE_MENU);
2329 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2331 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2333 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2334 (GDestroyNotify) chat_word_free);
2336 g_signal_connect (G_OBJECT (item), "activate",
2337 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2340 g_list_free (codes);
2342 gtk_widget_show_all (item);
2348 chat_text_send_cb (GtkMenuItem *menuitem,
2351 chat_input_text_view_send (chat);
2355 chat_input_button_press_event_cb (GtkTextView *view,
2356 GdkEventButton *event,
2359 EmpathyChatPriv *priv = GET_PRIV (chat);
2361 priv->most_recent_event_type = event->type;
2367 chat_input_populate_popup_cb (GtkTextView *view,
2371 EmpathyChatPriv *priv = GET_PRIV (chat);
2372 GtkTextBuffer *buffer;
2373 GtkTextTagTable *table;
2376 GtkTextIter iter, start, end;
2379 EmpathyChatSpell *chat_spell;
2380 GtkWidget *spell_menu;
2381 GtkWidget *spell_item;
2382 EmpathySmileyManager *smiley_manager;
2383 GtkWidget *smiley_menu;
2386 buffer = gtk_text_view_get_buffer (view);
2388 /* Add the emoticon menu. */
2389 item = gtk_separator_menu_item_new ();
2390 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2391 gtk_widget_show (item);
2393 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2394 image = gtk_image_new_from_icon_name ("face-smile",
2395 GTK_ICON_SIZE_MENU);
2396 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2397 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2398 gtk_widget_show (item);
2400 smiley_manager = empathy_smiley_manager_dup_singleton ();
2401 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2402 chat_insert_smiley_activate_cb,
2404 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2405 g_object_unref (smiley_manager);
2407 /* Add the Send menu item. */
2408 gtk_text_buffer_get_bounds (buffer, &start, &end);
2409 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2410 if (!EMP_STR_EMPTY (str)) {
2411 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2412 g_signal_connect (G_OBJECT (item), "activate",
2413 G_CALLBACK (chat_text_send_cb), chat);
2414 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2415 gtk_widget_show (item);
2419 /* Add the spell check menu item. */
2420 table = gtk_text_buffer_get_tag_table (buffer);
2421 tag = gtk_text_tag_table_lookup (table, "misspelled");
2423 switch (priv->most_recent_event_type) {
2424 case GDK_BUTTON_PRESS:
2425 /* get the location from the pointer */
2426 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2427 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2428 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2430 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2431 GTK_TEXT_WINDOW_WIDGET,
2434 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2439 g_warn_if_reached ();
2440 /* assume the KEY_PRESS case */
2443 /* get the location from the cursor */
2444 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2445 gtk_text_buffer_get_insert (buffer));
2451 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2452 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2454 str = gtk_text_buffer_get_text (buffer,
2455 &start, &end, FALSE);
2457 if (!EMP_STR_EMPTY (str)) {
2458 chat_spell = chat_spell_new (chat, str, start, end);
2459 g_object_set_data_full (G_OBJECT (menu),
2460 "chat-spell", chat_spell,
2461 (GDestroyNotify) chat_spell_free);
2463 item = gtk_separator_menu_item_new ();
2464 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2465 gtk_widget_show (item);
2467 /* Spelling suggestions */
2468 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2469 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2470 GTK_ICON_SIZE_MENU);
2471 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2473 spell_menu = chat_spelling_build_menu (chat_spell);
2474 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2477 spell_item = gtk_separator_menu_item_new ();
2478 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2479 gtk_widget_show (spell_item);
2481 /* Add to dictionary */
2482 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2484 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2485 gtk_widget_show (spell_item);
2487 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2488 gtk_widget_show (item);
2494 chat_log_filter (TplEvent *event,
2497 TpWeakRef *wr = user_data;
2498 EmpathyChat *chat = tp_weak_ref_dup_object (wr);
2499 EmpathyMessage *message;
2500 EmpathyChatPriv *priv;
2501 const GList *pending;
2502 bool retval = FALSE;
2507 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2508 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2510 priv = GET_PRIV (chat);
2512 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2513 message = empathy_message_from_tpl_log_event (event);
2515 for (; pending; pending = g_list_next (pending)) {
2516 if (empathy_message_equal (message, pending->data))
2523 g_object_unref (message);
2524 g_object_unref (chat);
2530 show_pending_messages (EmpathyChat *chat) {
2531 EmpathyChatPriv *priv = GET_PRIV (chat);
2532 const GList *messages, *l;
2534 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2536 if (chat->view == NULL || priv->tp_chat == NULL)
2539 if (!priv->can_show_pending)
2542 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2544 for (l = messages; l != NULL ; l = g_list_next (l)) {
2545 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2546 chat_message_received (chat, message, TRUE);
2552 got_filtered_messages_cb (GObject *manager,
2553 GAsyncResult *result,
2558 TpWeakRef *wr = user_data;
2559 EmpathyChat *chat = tp_weak_ref_dup_object (wr);
2560 EmpathyChatPriv *priv;
2561 GError *error = NULL;
2564 tp_weak_ref_destroy (wr);
2568 priv = GET_PRIV (chat);
2570 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2571 result, &messages, &error)) {
2572 DEBUG ("%s. Aborting.", error->message);
2573 empathy_theme_adium_append_event (chat->view,
2574 _("Failed to retrieve recent logs"));
2575 g_error_free (error);
2579 for (l = messages; l; l = g_list_next (l)) {
2580 EmpathyMessage *message;
2582 g_assert (TPL_IS_EVENT (l->data));
2584 message = empathy_message_from_tpl_log_event (l->data);
2585 g_object_unref (l->data);
2587 if (empathy_message_is_edit (message)) {
2588 /* this is an edited message, create a synthetic event
2589 * using the supersedes token and
2590 * original-message-sent timestamp, that we can then
2592 EmpathyMessage *syn_msg = g_object_new (
2593 EMPATHY_TYPE_MESSAGE,
2595 "token", empathy_message_get_supersedes (message),
2596 "type", empathy_message_get_tptype (message),
2597 "timestamp", empathy_message_get_original_timestamp (message),
2598 "incoming", empathy_message_is_incoming (message),
2600 "receiver", empathy_message_get_receiver (message),
2601 "sender", empathy_message_get_sender (message),
2604 empathy_theme_adium_append_message (chat->view, syn_msg,
2605 chat_should_highlight (chat, syn_msg));
2606 empathy_theme_adium_edit_message (chat->view, message);
2608 g_object_unref (syn_msg);
2610 /* append the latest message */
2611 empathy_theme_adium_append_message (chat->view, message,
2612 chat_should_highlight (chat, message));
2615 g_object_unref (message);
2617 g_list_free (messages);
2620 /* in case of TPL error, skip backlog and show pending messages */
2621 priv->can_show_pending = TRUE;
2622 show_pending_messages (chat);
2624 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2625 * about it in EmpathyChatPriv definition */
2626 priv->retrieving_backlogs = FALSE;
2627 empathy_chat_messages_read (chat);
2629 /* Turn back on scrolling */
2630 empathy_theme_adium_scroll (chat->view, TRUE);
2632 g_object_unref (chat);
2633 tp_weak_ref_destroy (wr);
2637 chat_add_logs (EmpathyChat *chat)
2639 EmpathyChatPriv *priv = GET_PRIV (chat);
2647 /* Turn off scrolling temporarily */
2648 empathy_theme_adium_scroll (chat->view, FALSE);
2650 /* Add messages from last conversation */
2651 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2652 target = tpl_entity_new_from_room_id (priv->id);
2654 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2656 priv->retrieving_backlogs = TRUE;
2657 wr = tp_weak_ref_new (chat, NULL, NULL);
2658 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2661 TPL_EVENT_MASK_TEXT,
2665 got_filtered_messages_cb,
2668 g_object_unref (target);
2672 chat_contacts_completion_func (const gchar *s1,
2676 gchar *tmp, *nick1, *nick2;
2683 return s1 ? -1 : +1;
2686 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2687 nick1 = g_utf8_casefold (tmp, -1);
2690 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2691 nick2 = g_utf8_casefold (tmp, -1);
2694 ret = strncmp (nick1, nick2, n);
2703 build_part_message (guint reason,
2705 EmpathyContact *actor,
2706 const gchar *message)
2708 GString *s = g_string_new ("");
2709 const gchar *actor_name = NULL;
2711 if (actor != NULL) {
2712 actor_name = empathy_contact_get_alias (actor);
2715 /* Having an actor only really makes sense for a few actions... */
2717 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2718 g_string_append_printf (s, _("%s has disconnected"), name);
2720 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2721 if (actor_name != NULL) {
2722 /* translators: reverse the order of these arguments
2723 * if the kicked should come before the kicker in your locale.
2725 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2728 g_string_append_printf (s, _("%s was kicked"), name);
2731 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2732 if (actor_name != NULL) {
2733 /* translators: reverse the order of these arguments
2734 * if the banned should come before the banner in your locale.
2736 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2739 g_string_append_printf (s, _("%s was banned"), name);
2743 g_string_append_printf (s, _("%s has left the room"), name);
2746 if (!EMP_STR_EMPTY (message)) {
2747 /* Note to translators: this string is appended to
2748 * notifications like "foo has left the room", with the message
2749 * given by the user living the room. If this poses a problem,
2750 * please let us know. :-)
2752 g_string_append_printf (s, _(" (%s)"), message);
2755 return g_string_free (s, FALSE);
2759 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2760 EmpathyContact *contact,
2761 EmpathyContact *actor,
2767 EmpathyChatPriv *priv = GET_PRIV (chat);
2768 const gchar *name = empathy_contact_get_alias (contact);
2771 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2773 if (priv->block_events_timeout_id != 0)
2777 str = g_strdup_printf (_("%s has joined the room"),
2780 str = build_part_message (reason, name, actor, message);
2783 empathy_theme_adium_append_event (chat->view, str);
2788 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2789 EmpathyContact *old_contact,
2790 EmpathyContact *new_contact,
2795 EmpathyChatPriv *priv = GET_PRIV (chat);
2797 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2799 if (priv->block_events_timeout_id == 0) {
2802 str = g_strdup_printf (_("%s is now known as %s"),
2803 empathy_contact_get_alias (old_contact),
2804 empathy_contact_get_alias (new_contact));
2805 empathy_theme_adium_append_event (chat->view, str);
2812 chat_contacts_visible_timeout_cb (gpointer chat)
2814 EmpathyChatPriv *priv = GET_PRIV (chat);
2816 /* Relax the size request */
2817 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2819 /* Set the position of the slider. This must be done here because
2820 * GtkPaned need to know its size allocation and it will be settled only
2821 * after the gtk_window_resize () tough effect. */
2822 if (priv->contacts_width > 0) {
2823 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2824 priv->contacts_width);
2827 priv->contacts_visible_id = 0;
2833 chat_update_contacts_visibility (EmpathyChat *chat,
2836 EmpathyChatPriv *priv = GET_PRIV (chat);
2838 if (!priv->scrolled_window_contacts) {
2842 if (priv->remote_contact != NULL) {
2846 if (show && priv->contact_list_view == NULL) {
2847 EmpathyIndividualStore *store;
2849 GtkAllocation allocation;
2851 /* We are adding the contact list to the chat, we don't want the
2852 * chat view to become too small. If the chat view is already
2853 * smaller than 250 make sure that size won't change. If the
2854 * chat view is bigger the contact list will take some space on
2855 * it but we make sure the chat view don't become smaller than
2856 * 250. Relax the size request once the resize is done */
2857 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2858 min_width = MIN (allocation.width, 250);
2859 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2861 /* There is no way to know when the window resize will happen
2862 * since it is WM's decision. Let's hope it won't be longer. */
2863 if (priv->contacts_visible_id != 0)
2864 g_source_remove (priv->contacts_visible_id);
2865 priv->contacts_visible_id = g_timeout_add (500,
2866 chat_contacts_visible_timeout_cb, chat);
2868 store = EMPATHY_INDIVIDUAL_STORE (
2869 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2871 empathy_individual_store_set_show_groups (store, FALSE);
2873 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2874 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2875 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2876 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2877 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2878 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2879 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2881 empathy_individual_view_set_show_offline (
2882 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2883 empathy_individual_view_set_show_uninteresting (
2884 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2886 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2887 priv->contact_list_view);
2889 gtk_widget_show (priv->contact_list_view);
2890 gtk_widget_show (priv->scrolled_window_contacts);
2891 g_object_unref (store);
2893 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2894 gtk_widget_hide (priv->scrolled_window_contacts);
2895 if (priv->contact_list_view != NULL) {
2896 gtk_widget_destroy (priv->contact_list_view);
2897 priv->contact_list_view = NULL;
2903 empathy_chat_set_show_contacts (EmpathyChat *chat,
2906 EmpathyChatPriv *priv = GET_PRIV (chat);
2908 priv->show_contacts = show;
2910 chat_update_contacts_visibility (chat, show);
2912 g_object_notify (G_OBJECT (chat), "show-contacts");
2916 chat_self_contact_changed_cb (EmpathyChat *chat)
2918 EmpathyChatPriv *priv = GET_PRIV (chat);
2920 if (priv->self_contact != NULL) {
2921 g_signal_handlers_disconnect_by_func (priv->self_contact,
2922 chat_self_contact_alias_changed_cb,
2925 g_clear_object (&priv->self_contact);
2927 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
2928 if (priv->self_contact != NULL) {
2929 g_object_ref (priv->self_contact);
2931 if (empathy_chat_is_room (chat)) {
2932 g_signal_connect_swapped (priv->self_contact, "notify::alias",
2933 G_CALLBACK (chat_self_contact_alias_changed_cb),
2938 chat_self_contact_alias_changed_cb (chat);
2942 chat_remote_contact_changed_cb (EmpathyChat *chat)
2944 EmpathyChatPriv *priv = GET_PRIV (chat);
2946 if (priv->remote_contact != NULL) {
2947 g_object_unref (priv->remote_contact);
2948 priv->remote_contact = NULL;
2953 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2954 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2955 if (priv->remote_contact != NULL) {
2956 g_object_ref (priv->remote_contact);
2957 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2959 else if (priv->tp_chat != NULL) {
2960 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
2963 chat_update_contacts_visibility (chat, priv->show_contacts);
2965 g_object_notify (G_OBJECT (chat), "remote-contact");
2966 g_object_notify (G_OBJECT (chat), "id");
2970 chat_invalidated_cb (EmpathyTpChat *tp_chat,
2976 EmpathyChatPriv *priv;
2978 priv = GET_PRIV (chat);
2980 if (!priv->tp_chat) {
2984 chat_composing_remove_timeout (chat);
2985 g_object_unref (priv->tp_chat);
2986 priv->tp_chat = NULL;
2987 g_object_notify (G_OBJECT (chat), "tp-chat");
2989 empathy_theme_adium_append_event (chat->view, _("Disconnected"));
2990 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2992 chat_update_contacts_visibility (chat, FALSE);
2994 priv->unread_messages_when_offline = priv->unread_messages;
2998 update_misspelled_words (gpointer data)
3000 EmpathyChat *chat = EMPATHY_CHAT (data);
3001 EmpathyChatPriv *priv = GET_PRIV (chat);
3002 GtkTextBuffer *buffer;
3006 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3008 gtk_text_buffer_get_end_iter (buffer, &iter);
3009 length = gtk_text_iter_get_offset (&iter);
3010 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3011 NULL, length, chat);
3013 priv->update_misspelled_words_id = 0;
3019 conf_spell_checking_cb (GSettings *gsettings_chat,
3023 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3024 EmpathyChatPriv *priv = GET_PRIV (chat);
3025 gboolean spell_checker;
3026 GtkTextBuffer *buffer;
3028 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3031 spell_checker = g_settings_get_boolean (gsettings_chat,
3032 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3034 if (!empathy_spell_supported ()) {
3035 spell_checker = FALSE;
3038 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3040 if (spell_checker == priv->spell_checking_enabled) {
3041 if (spell_checker) {
3042 /* Possibly changed dictionaries,
3043 * update misspelled words. Need to do so in idle
3044 * so the spell checker is updated. */
3045 priv->update_misspelled_words_id =
3046 g_idle_add (update_misspelled_words, chat);
3052 if (spell_checker) {
3055 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3056 buffer, "notify::cursor-position",
3057 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3059 priv->insert_text_id = tp_g_signal_connect_object (
3060 buffer, "insert-text",
3061 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3062 chat, G_CONNECT_AFTER);
3063 priv->delete_range_id = tp_g_signal_connect_object (
3064 buffer, "delete-range",
3065 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3066 chat, G_CONNECT_AFTER);
3068 gtk_text_buffer_create_tag (buffer, "misspelled",
3069 "underline", PANGO_UNDERLINE_ERROR,
3072 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3073 gtk_text_buffer_get_insert (buffer));
3074 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3077 /* Mark misspelled words in the existing buffer.
3078 * Need to do so in idle so the spell checker is updated. */
3079 priv->update_misspelled_words_id =
3080 g_idle_add (update_misspelled_words, chat);
3082 GtkTextTagTable *table;
3085 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3086 priv->notify_cursor_position_id = 0;
3087 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3088 priv->insert_text_id = 0;
3089 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3090 priv->delete_range_id = 0;
3092 table = gtk_text_buffer_get_tag_table (buffer);
3093 tag = gtk_text_tag_table_lookup (table, "misspelled");
3094 gtk_text_tag_table_remove (table, tag);
3096 gtk_text_buffer_delete_mark_by_name (buffer,
3097 "previous-cursor-position");
3100 priv->spell_checking_enabled = spell_checker;
3104 save_paned_pos_timeout (gpointer data)
3106 EmpathyChat *self = data;
3109 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3111 g_settings_set_int (self->priv->gsettings_ui,
3112 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3119 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3123 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3125 if (chat->priv->save_paned_pos_id != 0)
3126 g_source_remove (chat->priv->save_paned_pos_id);
3128 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3129 save_paned_pos_timeout, chat);
3135 chat_create_ui (EmpathyChat *chat)
3137 EmpathyChatPriv *priv = GET_PRIV (chat);
3141 GtkTextBuffer *buffer;
3142 EmpathyThemeManager *theme_mgr;
3144 filename = empathy_file_lookup ("empathy-chat.ui",
3146 gui = empathy_builder_get_file (filename,
3147 "chat_widget", &priv->widget,
3148 "hpaned", &priv->hpaned,
3149 "vbox_left", &priv->vbox_left,
3150 "scrolled_window_chat", &priv->scrolled_window_chat,
3151 "scrolled_window_input", &priv->scrolled_window_input,
3152 "hbox_topic", &priv->hbox_topic,
3153 "expander_topic", &priv->expander_topic,
3154 "label_topic", &priv->label_topic,
3155 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3156 "info_bar_vbox", &priv->info_bar_vbox,
3159 empathy_builder_connect (gui, chat,
3160 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3161 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3166 /* Add message view. */
3167 theme_mgr = empathy_theme_manager_dup_singleton ();
3168 chat->view = empathy_theme_manager_create_view (theme_mgr);
3169 g_object_unref (theme_mgr);
3170 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3171 and other types, even though it's non-editable and doesn't accept any
3172 drags. This steals drag motion for anything inside the scrollbars,
3173 making drag destinations on chat windows far less useful.
3175 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3176 g_signal_connect (chat->view, "focus_in_event",
3177 G_CALLBACK (chat_text_view_focus_in_event_cb),
3179 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3180 GTK_WIDGET (chat->view));
3181 gtk_widget_show (GTK_WIDGET (chat->view));
3183 /* Add input GtkTextView */
3184 chat->input_text_view = empathy_input_text_view_new ();
3185 g_signal_connect (chat->input_text_view, "notify::has-focus",
3186 G_CALLBACK (chat_input_has_focus_notify_cb),
3188 g_signal_connect (chat->input_text_view, "key-press-event",
3189 G_CALLBACK (chat_input_key_press_event_cb),
3191 g_signal_connect (chat->input_text_view, "realize",
3192 G_CALLBACK (chat_input_realize_cb),
3194 g_signal_connect (chat->input_text_view, "button-press-event",
3195 G_CALLBACK (chat_input_button_press_event_cb),
3197 g_signal_connect (chat->input_text_view, "populate-popup",
3198 G_CALLBACK (chat_input_populate_popup_cb),
3200 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3201 tp_g_signal_connect_object (buffer, "changed",
3202 G_CALLBACK (chat_input_text_buffer_changed_cb),
3204 tp_g_signal_connect_object (priv->gsettings_chat,
3205 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3206 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3207 conf_spell_checking_cb (priv->gsettings_chat,
3208 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3209 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3210 chat->input_text_view);
3211 gtk_widget_show (chat->input_text_view);
3213 /* Add the (invisible) search bar */
3214 priv->search_bar = empathy_search_bar_new (chat->view);
3215 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3218 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3220 /* Initialy hide the topic, will be shown if not empty */
3221 gtk_widget_hide (priv->hbox_topic);
3223 g_signal_connect (priv->hpaned, "notify::position",
3224 G_CALLBACK (chat_hpaned_pos_changed_cb),
3227 /* Set widget focus order */
3228 list = g_list_append (NULL, priv->search_bar);
3229 list = g_list_append (list, priv->scrolled_window_input);
3230 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3233 list = g_list_append (NULL, priv->vbox_left);
3234 list = g_list_append (list, priv->scrolled_window_contacts);
3235 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3238 list = g_list_append (NULL, priv->hpaned);
3239 list = g_list_append (list, priv->hbox_topic);
3240 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3243 /* Add the main widget in the chat widget */
3244 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3245 g_object_unref (gui);
3249 chat_finalize (GObject *object)
3252 EmpathyChatPriv *priv;
3254 chat = EMPATHY_CHAT (object);
3255 priv = GET_PRIV (chat);
3257 DEBUG ("Finalized: %p", object);
3259 if (priv->update_misspelled_words_id != 0)
3260 g_source_remove (priv->update_misspelled_words_id);
3262 if (priv->save_paned_pos_id != 0)
3263 g_source_remove (priv->save_paned_pos_id);
3265 if (priv->contacts_visible_id != 0)
3266 g_source_remove (priv->contacts_visible_id);
3268 g_object_unref (priv->gsettings_chat);
3269 g_object_unref (priv->gsettings_ui);
3271 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3272 g_list_free (priv->input_history);
3274 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3275 g_list_free (priv->compositors);
3277 chat_composing_remove_timeout (chat);
3279 g_object_unref (priv->account_manager);
3280 g_object_unref (priv->log_manager);
3282 if (priv->tp_chat) {
3283 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3284 chat_invalidated_cb, chat);
3285 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3286 chat_message_received_cb, chat);
3287 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3288 chat_message_acknowledged_cb, chat);
3289 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3290 chat_send_error_cb, chat);
3291 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3292 chat_state_changed_cb, chat);
3293 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3294 chat_members_changed_cb, chat);
3295 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3296 chat_self_contact_changed_cb, chat);
3297 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3298 chat_remote_contact_changed_cb, chat);
3299 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3300 chat_title_changed_cb, chat);
3301 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3302 chat_subject_changed_cb, chat);
3303 empathy_tp_chat_leave (priv->tp_chat, "");
3304 g_object_unref (priv->tp_chat);
3306 if (priv->account) {
3307 g_object_unref (priv->account);
3309 if (priv->self_contact) {
3310 g_signal_handlers_disconnect_by_func (priv->self_contact,
3311 chat_self_contact_alias_changed_cb,
3313 g_object_unref (priv->self_contact);
3315 if (priv->remote_contact) {
3316 g_object_unref (priv->remote_contact);
3319 if (priv->block_events_timeout_id) {
3320 g_source_remove (priv->block_events_timeout_id);
3324 g_free (priv->name);
3325 g_free (priv->subject);
3326 g_completion_free (priv->completion);
3328 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3330 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3334 chat_constructed (GObject *object)
3336 EmpathyChat *chat = EMPATHY_CHAT (object);
3337 EmpathyChatPriv *priv = GET_PRIV (chat);
3339 if (priv->tp_chat != NULL) {
3340 TpChannel *channel = TP_CHANNEL (priv->tp_chat);
3341 TpConnection *conn = tp_channel_get_connection (channel);
3342 gboolean supports_avatars =
3343 tp_proxy_has_interface_by_id (conn,
3344 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS);
3346 empathy_theme_adium_set_show_avatars (chat->view,
3350 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3351 /* First display logs from the logger and then display pending messages */
3352 chat_add_logs (chat);
3355 /* Just display pending messages for rooms */
3356 priv->can_show_pending = TRUE;
3357 show_pending_messages (chat);
3362 empathy_chat_class_init (EmpathyChatClass *klass)
3364 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3366 object_class->finalize = chat_finalize;
3367 object_class->get_property = chat_get_property;
3368 object_class->set_property = chat_set_property;
3369 object_class->constructed = chat_constructed;
3371 g_object_class_install_property (object_class,
3373 g_param_spec_object ("tp-chat",
3375 "The tp chat object",
3376 EMPATHY_TYPE_TP_CHAT,
3379 G_PARAM_STATIC_STRINGS));
3380 g_object_class_install_property (object_class,
3382 g_param_spec_object ("account",
3383 "Account of the chat",
3384 "The account of the chat",
3387 G_PARAM_STATIC_STRINGS));
3388 g_object_class_install_property (object_class,
3390 g_param_spec_string ("id",
3392 "The id of the chat",
3395 G_PARAM_STATIC_STRINGS));
3396 g_object_class_install_property (object_class,
3398 g_param_spec_string ("name",
3400 "The name of the chat",
3403 G_PARAM_STATIC_STRINGS));
3404 g_object_class_install_property (object_class,
3406 g_param_spec_string ("subject",
3408 "The subject or topic of the chat",
3411 G_PARAM_STATIC_STRINGS));
3412 g_object_class_install_property (object_class,
3413 PROP_REMOTE_CONTACT,
3414 g_param_spec_object ("remote-contact",
3415 "The remote contact",
3416 "The remote contact is any",
3417 EMPATHY_TYPE_CONTACT,
3419 G_PARAM_STATIC_STRINGS));
3420 g_object_class_install_property (object_class,
3422 g_param_spec_boolean ("show-contacts",
3423 "Contacts' visibility",
3424 "The visibility of the contacts' list",
3427 G_PARAM_STATIC_STRINGS));
3429 g_object_class_install_property (object_class,
3431 g_param_spec_boolean ("sms-channel",
3433 "TRUE if this channel is for sending SMSes",
3435 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3437 g_object_class_install_property (object_class,
3438 PROP_N_MESSAGES_SENDING,
3439 g_param_spec_uint ("n-messages-sending",
3440 "Num Messages Sending",
3441 "The number of messages being sent",
3443 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3445 g_object_class_install_property (object_class,
3446 PROP_NB_UNREAD_MESSAGES,
3447 g_param_spec_uint ("nb-unread-messages",
3448 "Num Unread Messages",
3449 "The number of unread messages",
3451 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3453 signals[COMPOSING] =
3454 g_signal_new ("composing",
3455 G_OBJECT_CLASS_TYPE (object_class),
3459 g_cclosure_marshal_generic,
3464 * EmpathyChat::new-message:
3465 * @self: the #EmpathyChat
3466 * @message: the new message
3467 * @pending: whether the message was in the pending queue when @self
3469 * @should_highlight: %TRUE if the message mentions the local user
3471 signals[NEW_MESSAGE] =
3472 g_signal_new ("new-message",
3473 G_OBJECT_CLASS_TYPE (object_class),
3477 g_cclosure_marshal_generic,
3479 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3481 signals[PART_COMMAND_ENTERED] =
3482 g_signal_new ("part-command-entered",
3483 G_OBJECT_CLASS_TYPE (object_class),
3487 g_cclosure_marshal_generic,
3491 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3495 chat_block_events_timeout_cb (gpointer data)
3497 EmpathyChatPriv *priv = GET_PRIV (data);
3499 priv->block_events_timeout_id = 0;
3505 account_manager_prepared_cb (GObject *source_object,
3506 GAsyncResult *result,
3509 GList *accounts, *l;
3510 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3511 EmpathyChat *chat = user_data;
3512 GError *error = NULL;
3514 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3515 DEBUG ("Failed to prepare the account manager: %s", error->message);
3516 g_error_free (error);
3520 accounts = tp_account_manager_dup_valid_accounts (account_manager);
3522 for (l = accounts; l != NULL; l = l->next) {
3523 TpAccount *account = l->data;
3524 tp_g_signal_connect_object (account, "status-changed",
3525 G_CALLBACK (chat_new_connection_cb),
3529 g_list_free_full (accounts, g_object_unref);
3533 empathy_chat_init (EmpathyChat *chat)
3535 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3536 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3539 priv->log_manager = tpl_log_manager_dup_singleton ();
3540 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3541 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3543 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3544 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3545 priv->input_history = NULL;
3546 priv->input_history_current = NULL;
3547 priv->account_manager = tp_account_manager_dup ();
3549 tp_proxy_prepare_async (priv->account_manager, NULL,
3550 account_manager_prepared_cb, chat);
3552 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3553 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3555 /* Block events for some time to avoid having "has come online" or
3556 * "joined" messages. */
3557 priv->block_events_timeout_id =
3558 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3560 /* Add nick name completion */
3561 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3562 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3564 chat_create_ui (chat);
3568 empathy_chat_new (EmpathyTpChat *tp_chat)
3570 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3574 empathy_chat_get_tp_chat (EmpathyChat *chat)
3576 EmpathyChatPriv *priv = GET_PRIV (chat);
3578 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3580 return priv->tp_chat;
3586 GtkWidget *info_bar;
3596 passwd_remember_button_cb (GtkButton *button,
3599 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3603 passwd_not_now_button_cb (GtkButton *button,
3606 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3610 remember_password_infobar_response_cb (GtkWidget *info_bar,
3614 EmpathyChatPriv *priv = GET_PRIV (data->self);
3616 if (response_id == GTK_RESPONSE_OK) {
3617 DEBUG ("Saving room password");
3618 empathy_keyring_set_room_password_async (priv->account,
3619 empathy_tp_chat_get_id (priv->tp_chat),
3624 gtk_widget_destroy (info_bar);
3625 g_free (data->password);
3626 g_slice_free (PasswordData, data);
3630 chat_prompt_to_save_password (EmpathyChat *self,
3633 GtkWidget *content_area;
3640 /* save the password in case it needs to be saved */
3641 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3643 /* Remove all previous widgets */
3644 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3645 gtk_container_forall (GTK_CONTAINER (content_area),
3646 (GtkCallback) gtk_widget_destroy, NULL);
3647 data->button = NULL;
3650 data->spinner = NULL;
3652 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3653 GTK_MESSAGE_QUESTION);
3655 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3656 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3659 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3660 GTK_ICON_SIZE_DIALOG);
3661 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3664 label = gtk_label_new (_("Would you like to store this password?"));
3665 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3667 /* Add 'Remember' button */
3668 alig = gtk_alignment_new (0, 0.5, 1, 0);
3670 button = gtk_button_new_with_label (_("Remember"));
3671 gtk_container_add (GTK_CONTAINER (alig), button);
3672 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3674 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3677 /* Add 'Not now' button */
3678 alig = gtk_alignment_new (0, 0.5, 1, 0);
3680 button = gtk_button_new_with_label (_("Not now"));
3681 gtk_container_add (GTK_CONTAINER (alig), button);
3682 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3684 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3688 g_signal_handler_disconnect (data->info_bar, data->response_id);
3689 g_signal_connect (data->info_bar, "response",
3690 G_CALLBACK (remember_password_infobar_response_cb), data);
3692 gtk_widget_show_all (data->info_bar);
3696 provide_password_cb (GObject *tp_chat,
3700 PasswordData *data = user_data;
3701 EmpathyChat *self = data->self;
3702 EmpathyChatPriv *priv = GET_PRIV (self);
3703 GError *error = NULL;
3705 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3707 DEBUG ("error: %s", error->message);
3708 /* FIXME: what should we do if that's another error? Close the channel?
3709 * Display the raw D-Bus error to the user isn't very useful */
3710 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3712 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3713 gtk_widget_set_sensitive (data->entry, TRUE);
3714 gtk_widget_grab_focus (data->entry);
3717 gtk_info_bar_set_message_type (
3718 GTK_INFO_BAR (data->info_bar),
3722 gtk_widget_set_sensitive (data->button, TRUE);
3723 gtk_button_set_label (GTK_BUTTON (data->button),
3727 gtk_label_set_text (GTK_LABEL (data->label),
3728 _("Wrong password; please try again:"));
3731 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3732 gtk_widget_hide (data->spinner);
3734 g_error_free (error);
3738 if (empathy_keyring_is_available ()) {
3739 /* ask whether they want to save the password */
3740 chat_prompt_to_save_password (self, data);
3742 /* Get rid of the password info bar finally */
3743 gtk_widget_destroy (data->info_bar);
3744 g_slice_free (PasswordData, data);
3748 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3749 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3750 gtk_widget_grab_focus (self->input_text_view);
3754 password_infobar_response_cb (GtkWidget *info_bar,
3758 EmpathyChatPriv *priv = GET_PRIV (data->self);
3759 const gchar *password;
3761 if (response_id != GTK_RESPONSE_OK) {
3762 gtk_widget_destroy (info_bar);
3763 g_slice_free (PasswordData, data);
3767 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3769 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3770 provide_password_cb, data);
3772 gtk_widget_set_sensitive (data->button, FALSE);
3773 gtk_widget_set_sensitive (data->entry, FALSE);
3775 gtk_spinner_start (GTK_SPINNER (data->spinner));
3776 gtk_widget_show (data->spinner);
3780 password_entry_activate_cb (GtkWidget *entry,
3783 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3787 passwd_join_button_cb (GtkButton *button,
3790 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3794 clear_icon_released_cb (GtkEntry *entry,
3795 GtkEntryIconPosition icon_pos,
3799 gtk_entry_set_text (entry, "");
3803 password_entry_changed_cb (GtkEditable *entry,
3808 str = gtk_entry_get_text (GTK_ENTRY (entry));
3810 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3811 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3815 infobar_chat_invalidated_cb (TpProxy *proxy,
3819 gpointer password_infobar)
3821 /* Destroy the password infobar whenever a channel is invalidated
3822 * so we don't have multiple infobars when the MUC is rejoined */
3823 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3827 display_password_info_bar (EmpathyChat *self)
3829 EmpathyChatPriv *priv = GET_PRIV (self);
3830 GtkWidget *info_bar;
3831 GtkWidget *content_area;
3841 data = g_slice_new0 (PasswordData);
3843 info_bar = gtk_info_bar_new ();
3844 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3845 GTK_MESSAGE_QUESTION);
3847 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3849 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3850 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3853 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3854 GTK_ICON_SIZE_DIALOG);
3855 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3858 label = gtk_label_new (_("This room is protected by a password:"));
3859 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3861 /* Add password entry */
3862 entry = gtk_entry_new ();
3863 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3864 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3866 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3867 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3868 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3869 GTK_ENTRY_ICON_SECONDARY, FALSE);
3871 g_signal_connect (entry, "icon-release",
3872 G_CALLBACK (clear_icon_released_cb), data);
3873 g_signal_connect (entry, "changed",
3874 G_CALLBACK (password_entry_changed_cb), data);
3876 g_signal_connect (entry, "activate",
3877 G_CALLBACK (password_entry_activate_cb), data);
3879 /* Focus the password entry once it's realized */
3880 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3882 /* Add 'Join' button */
3883 alig = gtk_alignment_new (0, 0.5, 1, 0);
3885 button = gtk_button_new_with_label (_("Join"));
3886 gtk_container_add (GTK_CONTAINER (alig), button);
3887 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3889 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3893 spinner = gtk_spinner_new ();
3894 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3896 /* Save some data for messing around with later */
3898 data->info_bar = info_bar;
3899 data->button = button;
3900 data->label = label;
3901 data->entry = entry;
3902 data->spinner = spinner;
3904 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3906 gtk_widget_show_all (hbox);
3908 tp_g_signal_connect_object (priv->tp_chat,
3909 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
3912 data->response_id = g_signal_connect (info_bar, "response",
3913 G_CALLBACK (password_infobar_response_cb), data);
3915 gtk_widget_show_all (info_bar);
3916 /* ... but hide the spinner */
3917 gtk_widget_hide (spinner);
3919 /* prevent the user from typing anything */
3920 gtk_widget_set_sensitive (self->input_text_view, FALSE);
3924 provide_saved_password_cb (GObject *tp_chat,
3928 EmpathyChat *self = user_data;
3929 EmpathyChatPriv *priv = GET_PRIV (self);
3930 GError *error = NULL;
3932 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3934 DEBUG ("error: %s", error->message);
3935 /* FIXME: what should we do if that's another error? Close the channel?
3936 * Display the raw D-Bus error to the user isn't very useful */
3937 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3938 display_password_info_bar (self);
3939 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3941 g_error_free (error);
3946 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3947 gtk_widget_grab_focus (self->input_text_view);
3951 chat_room_got_password_cb (GObject *source,
3952 GAsyncResult *result,
3955 EmpathyChat *self = user_data;
3956 EmpathyChatPriv *priv = GET_PRIV (self);
3957 const gchar *password;
3958 GError *error = NULL;
3960 password = empathy_keyring_get_room_password_finish (priv->account,
3963 if (error != NULL) {
3964 DEBUG ("Couldn't get room password: %s\n", error->message);
3965 g_clear_error (&error);
3967 display_password_info_bar (self);
3968 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3972 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3973 provide_saved_password_cb, self);
3977 chat_password_needed_changed_cb (EmpathyChat *self)
3979 EmpathyChatPriv *priv = GET_PRIV (self);
3981 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
3982 empathy_keyring_get_room_password_async (priv->account,
3983 empathy_tp_chat_get_id (priv->tp_chat),
3984 chat_room_got_password_cb, self);
3989 chat_sms_channel_changed_cb (EmpathyChat *self)
3991 EmpathyChatPriv *priv = GET_PRIV (self);
3993 priv->sms_channel = tp_text_channel_is_sms_channel (
3994 (TpTextChannel *) priv->tp_chat);
3995 g_object_notify (G_OBJECT (self), "sms-channel");
3999 chat_n_messages_sending_changed_cb (EmpathyChat *self)
4001 g_object_notify (G_OBJECT (self), "n-messages-sending");
4005 empathy_chat_set_tp_chat (EmpathyChat *chat,
4006 EmpathyTpChat *tp_chat)
4008 EmpathyChatPriv *priv = GET_PRIV (chat);
4010 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4011 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4013 if (priv->tp_chat) {
4017 if (priv->account) {
4018 g_object_unref (priv->account);
4021 priv->tp_chat = g_object_ref (tp_chat);
4022 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4024 g_signal_connect (tp_chat, "invalidated",
4025 G_CALLBACK (chat_invalidated_cb),
4027 g_signal_connect (tp_chat, "message-received-empathy",
4028 G_CALLBACK (chat_message_received_cb),
4030 g_signal_connect (tp_chat, "message_acknowledged",
4031 G_CALLBACK (chat_message_acknowledged_cb),
4033 g_signal_connect (tp_chat, "send-error",
4034 G_CALLBACK (chat_send_error_cb),
4036 g_signal_connect (tp_chat, "contact-chat-state-changed",
4037 G_CALLBACK (chat_state_changed_cb),
4039 g_signal_connect (tp_chat, "members-changed",
4040 G_CALLBACK (chat_members_changed_cb),
4042 g_signal_connect (tp_chat, "member-renamed",
4043 G_CALLBACK (chat_member_renamed_cb),
4045 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4046 G_CALLBACK (chat_self_contact_changed_cb),
4048 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4049 G_CALLBACK (chat_remote_contact_changed_cb),
4051 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4052 G_CALLBACK (chat_password_needed_changed_cb),
4054 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4055 G_CALLBACK (chat_sms_channel_changed_cb),
4057 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4058 G_CALLBACK (chat_n_messages_sending_changed_cb),
4060 g_signal_connect_swapped (tp_chat, "notify::title",
4061 G_CALLBACK (chat_title_changed_cb),
4063 g_signal_connect_swapped (tp_chat, "notify::subject",
4064 G_CALLBACK (chat_subject_changed_cb),
4067 /* Get initial value of properties */
4068 chat_sms_channel_changed_cb (chat);
4069 chat_self_contact_changed_cb (chat);
4070 chat_remote_contact_changed_cb (chat);
4071 chat_title_changed_cb (chat);
4072 chat_subject_changed_cb (chat);
4074 if (chat->input_text_view) {
4075 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4076 if (priv->block_events_timeout_id == 0) {
4077 empathy_theme_adium_append_event (chat->view, _("Connected"));
4081 g_object_notify (G_OBJECT (chat), "tp-chat");
4082 g_object_notify (G_OBJECT (chat), "id");
4083 g_object_notify (G_OBJECT (chat), "account");
4085 /* This is a noop when tp-chat is set at object construction time and causes
4086 * the pending messages to be show when it's set on the object after it has
4088 show_pending_messages (chat);
4090 /* check if a password is needed */
4091 chat_password_needed_changed_cb (chat);
4095 empathy_chat_get_account (EmpathyChat *chat)
4097 EmpathyChatPriv *priv = GET_PRIV (chat);
4099 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4101 return priv->account;
4105 empathy_chat_get_id (EmpathyChat *chat)
4107 EmpathyChatPriv *priv = GET_PRIV (chat);
4109 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4115 empathy_chat_dup_name (EmpathyChat *chat)
4117 EmpathyChatPriv *priv = GET_PRIV (chat);
4120 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4124 if (!ret && priv->remote_contact) {
4125 ret = empathy_contact_get_alias (priv->remote_contact);
4132 ret = _("Conversation");
4134 if (priv->sms_channel)
4135 /* Translators: this string is a something like
4136 * "Escher Cat (SMS)" */
4137 return g_strdup_printf (_("%s (SMS)"), ret);
4139 return g_strdup (ret);
4143 empathy_chat_get_subject (EmpathyChat *chat)
4145 EmpathyChatPriv *priv = GET_PRIV (chat);
4147 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4149 return priv->subject;
4153 empathy_chat_get_remote_contact (EmpathyChat *chat)
4155 EmpathyChatPriv *priv = GET_PRIV (chat);
4157 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4159 return priv->remote_contact;
4163 empathy_chat_get_contact_menu (EmpathyChat *chat)
4165 EmpathyChatPriv *priv = GET_PRIV (chat);
4166 GtkWidget *menu = NULL;
4167 FolksIndividual *individual;
4170 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4172 if (priv->remote_contact == NULL)
4175 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4176 if (contact == NULL)
4179 individual = empathy_ensure_individual_from_tp_contact (contact);
4181 if (individual == NULL)
4184 menu = empathy_individual_menu_new (individual,
4185 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4186 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4187 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4188 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4190 g_object_unref (individual);
4196 empathy_chat_clear (EmpathyChat *chat)
4198 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4200 empathy_theme_adium_clear (chat->view);
4204 empathy_chat_scroll_down (EmpathyChat *chat)
4206 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4208 empathy_theme_adium_scroll_down (chat->view);
4212 empathy_chat_cut (EmpathyChat *chat)
4214 GtkTextBuffer *buffer;
4216 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4218 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4219 if (gtk_text_buffer_get_has_selection (buffer)) {
4220 GtkClipboard *clipboard;
4222 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4224 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4229 copy_from_chat_view (EmpathyChat *chat)
4231 if (!empathy_theme_adium_get_has_selection (chat->view))
4234 empathy_theme_adium_copy_clipboard (chat->view);
4239 copy_from_input (EmpathyChat *chat)
4241 GtkTextBuffer *buffer;
4242 GtkClipboard *clipboard;
4244 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4245 if (!gtk_text_buffer_get_has_selection (buffer))
4248 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4249 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4254 copy_from_topic (EmpathyChat *chat)
4256 EmpathyChatPriv *priv = GET_PRIV (chat);
4263 GtkClipboard *clipboard;
4265 if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4270 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4271 start = g_utf8_offset_to_pointer (topic, start_offset);
4272 end = g_utf8_offset_to_pointer (topic, end_offset);
4273 selection = g_strndup (start, end - start);
4275 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4276 gtk_clipboard_set_text (clipboard, selection, -1);
4283 empathy_chat_copy (EmpathyChat *chat)
4285 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4287 if (copy_from_chat_view (chat))
4290 if (copy_from_input (chat))
4293 copy_from_topic (chat);
4297 empathy_chat_paste (EmpathyChat *chat)
4299 GtkTextBuffer *buffer;
4300 GtkClipboard *clipboard;
4301 EmpathyChatPriv *priv;
4303 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4305 priv = GET_PRIV (chat);
4307 if (gtk_widget_get_visible (priv->search_bar)) {
4308 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4312 if (priv->tp_chat == NULL ||
4313 !gtk_widget_is_sensitive (chat->input_text_view))
4316 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4317 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4319 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4323 empathy_chat_find (EmpathyChat *chat)
4325 EmpathyChatPriv *priv;
4327 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4329 priv = GET_PRIV (chat);
4331 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4335 empathy_chat_correct_word (EmpathyChat *chat,
4338 const gchar *new_word)
4340 GtkTextBuffer *buffer;
4342 g_return_if_fail (chat != NULL);
4343 g_return_if_fail (new_word != NULL);
4345 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4347 gtk_text_buffer_delete (buffer, start, end);
4348 gtk_text_buffer_insert (buffer, start,
4354 empathy_chat_is_room (EmpathyChat *chat)
4356 EmpathyChatPriv *priv = GET_PRIV (chat);
4358 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4360 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4364 empathy_chat_is_highlighted (EmpathyChat *chat)
4366 EmpathyChatPriv *priv = GET_PRIV (chat);
4368 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4370 return priv->highlighted;
4374 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4376 EmpathyChatPriv *priv = GET_PRIV (self);
4378 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4380 return priv->unread_messages;
4383 /* called when the messages have been read by user */
4385 empathy_chat_messages_read (EmpathyChat *self)
4387 EmpathyChatPriv *priv = GET_PRIV (self);
4389 g_return_if_fail (EMPATHY_IS_CHAT (self));
4391 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4392 * definition. If we are still retrieving the backlogs, do not ACK */
4393 if (priv->retrieving_backlogs)
4396 if (priv->tp_chat != NULL) {
4397 tp_text_channel_ack_all_pending_messages_async (
4398 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4401 priv->highlighted = FALSE;
4403 if (priv->unread_messages_when_offline > 0) {
4404 /* We can't ack those as the connection has gone away so just consider
4406 priv->unread_messages -= priv->unread_messages_when_offline;
4407 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4408 priv->unread_messages_when_offline = 0;
4412 /* Return TRUE if on of the contacts in this chat is composing */
4414 empathy_chat_is_composing (EmpathyChat *chat)
4416 return chat->priv->compositors != NULL;
4420 empathy_chat_is_sms_channel (EmpathyChat *self)
4422 EmpathyChatPriv *priv = GET_PRIV (self);
4424 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4426 return priv->sms_channel;
4430 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4432 EmpathyChatPriv *priv;
4434 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4436 priv = GET_PRIV (self);
4438 if (priv->tp_chat == NULL) {
4443 g_object_get (priv->tp_chat,
4444 "n-messages-sending", &n_messages,
4452 empathy_chat_dup_text (EmpathyChat *self)
4454 GtkTextBuffer *buffer;
4455 GtkTextIter start, end;
4457 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4459 gtk_text_buffer_get_bounds (buffer, &start, &end);
4460 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4464 empathy_chat_set_text (EmpathyChat *self,
4467 GtkTextBuffer *buffer;
4469 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4471 gtk_text_buffer_set_text (buffer, text, -1);