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>
33 #undef G_DISABLE_DEPRECATED /* for GCompletion */
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n-lib.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-gsettings.h>
43 #include <libempathy/empathy-keyring.h>
44 #include <libempathy/empathy-utils.h>
45 #include <libempathy/empathy-request-util.h>
46 #include <libempathy/empathy-chatroom-manager.h>
47 #include <src/empathy-chat-window.h>
49 #include "empathy-chat.h"
50 #include "empathy-spell.h"
51 #include "empathy-contact-list-store.h"
52 #include "empathy-contact-list-view.h"
53 #include "empathy-contact-menu.h"
54 #include "empathy-gtk-marshal.h"
55 #include "empathy-input-text-view.h"
56 #include "empathy-search-bar.h"
57 #include "empathy-theme-manager.h"
58 #include "empathy-smiley-manager.h"
59 #include "empathy-ui-utils.h"
60 #include "empathy-string-parser.h"
61 #include "extensions/extensions.h"
63 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
64 #include <libempathy/empathy-debug.h>
66 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
67 #define COMPOSING_STOP_TIMEOUT 5
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
70 struct _EmpathyChatPriv {
71 EmpathyTpChat *tp_chat;
76 EmpathyContact *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;
111 GtkWidget *vbox_left;
112 GtkWidget *scrolled_window_chat;
113 GtkWidget *scrolled_window_input;
114 GtkWidget *scrolled_window_contacts;
115 GtkWidget *hbox_topic;
116 GtkWidget *expander_topic;
117 GtkWidget *label_topic;
118 GtkWidget *contact_list_view;
119 GtkWidget *info_bar_vbox;
120 GtkWidget *search_bar;
122 guint unread_messages;
123 /* TRUE if the pending messages can be displayed. This is to avoid to show
124 * pending messages *before* messages from logs. (#603980) */
125 gboolean can_show_pending;
127 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
128 * be differently handled since it introduces another race condition, which
129 * is really hard to occur, but still possible.
131 * With the current workaround (which has the race above), we need to be
132 * sure to ACK any pending messages only when the retrieval of backlogs is
133 * finished, that's why using retrieving_backlogs flag.
134 * empathy_chat_messages_read () will check this variable and not ACK
135 * anything when TRUE. It will be set TRUE at chat_constructed () and set
136 * back to FALSE when the backlog has been retrieved and the pending
137 * messages actually showed to the user.
139 * Race condition introduced with this workaround:
140 * Scenario: a message is pending, the user is notified and selects the tab.
141 * the tab with a pending message is focused before the messages are properly
142 * shown (since the preparation of the window is slower AND async WRT the
143 * tab showing), which means the user won't see any new messages (rare but
144 * possible), if he/she will change tab focus before the messages are
145 * properly shown, the tab will be set as 'seen' and the user won't be
146 * notified again about the already notified pending messages when the
147 * messages in tab will be properly shown */
148 gboolean retrieving_backlogs;
149 gboolean sms_channel;
153 gchar *text; /* Original message that was specified
154 * upon entry creation. */
155 gchar *modified_text; /* Message that was modified by user.
156 * When no modifications were made, it is NULL */
175 PROP_N_MESSAGES_SENDING,
176 PROP_NB_UNREAD_MESSAGES,
179 static guint signals[LAST_SIGNAL] = { 0 };
181 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
183 static gboolean update_misspelled_words (gpointer data);
186 chat_get_property (GObject *object,
191 EmpathyChat *chat = EMPATHY_CHAT (object);
192 EmpathyChatPriv *priv = GET_PRIV (object);
196 g_value_set_object (value, priv->tp_chat);
199 g_value_set_object (value, priv->account);
202 g_value_take_string (value, empathy_chat_dup_name (chat));
205 g_value_set_string (value, priv->id);
208 g_value_set_string (value, priv->subject);
210 case PROP_REMOTE_CONTACT:
211 g_value_set_object (value, priv->remote_contact);
213 case PROP_SHOW_CONTACTS:
214 g_value_set_boolean (value, priv->show_contacts);
216 case PROP_SMS_CHANNEL:
217 g_value_set_boolean (value, priv->sms_channel);
219 case PROP_N_MESSAGES_SENDING:
220 g_value_set_uint (value,
221 empathy_chat_get_n_messages_sending (chat));
223 case PROP_NB_UNREAD_MESSAGES:
224 g_value_set_uint (value,
225 empathy_chat_get_nb_unread_messages (chat));
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
234 chat_set_property (GObject *object,
239 EmpathyChat *chat = EMPATHY_CHAT (object);
243 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
245 case PROP_SHOW_CONTACTS:
246 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
255 account_reconnected (EmpathyChat *chat,
258 EmpathyChatPriv *priv = GET_PRIV (chat);
260 DEBUG ("Account reconnected, request a new Text channel");
262 /* FIXME: Ideally we should ask to handle ourself the channel so we can
263 * report the error if any but this is blocked by
264 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
265 switch (priv->handle_type) {
266 case TP_HANDLE_TYPE_CONTACT:
267 if (priv->sms_channel)
268 empathy_sms_contact_id (
270 TP_USER_ACTION_TIME_NOT_USER_ACTION);
272 empathy_chat_with_contact_id (
274 TP_USER_ACTION_TIME_NOT_USER_ACTION);
276 case TP_HANDLE_TYPE_ROOM:
277 empathy_join_muc (account, priv->id,
278 TP_USER_ACTION_TIME_NOT_USER_ACTION);
280 case TP_HANDLE_TYPE_NONE:
281 case TP_HANDLE_TYPE_LIST:
282 case TP_HANDLE_TYPE_GROUP:
284 g_assert_not_reached ();
288 g_object_unref (chat);
292 chat_new_connection_cb (TpAccount *account,
296 gchar *dbus_error_name,
300 EmpathyChatPriv *priv = GET_PRIV (chat);
302 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
305 if (priv->tp_chat != NULL || account != priv->account ||
306 priv->handle_type == TP_HANDLE_TYPE_NONE ||
307 EMP_STR_EMPTY (priv->id))
312 account_reconnected (chat, account);
316 chat_composing_remove_timeout (EmpathyChat *chat)
318 EmpathyChatPriv *priv;
320 priv = GET_PRIV (chat);
322 if (priv->composing_stop_timeout_id) {
323 g_source_remove (priv->composing_stop_timeout_id);
324 priv->composing_stop_timeout_id = 0;
329 chat_composing_stop_timeout_cb (EmpathyChat *chat)
331 EmpathyChatPriv *priv;
333 priv = GET_PRIV (chat);
335 priv->composing_stop_timeout_id = 0;
336 empathy_tp_chat_set_state (priv->tp_chat,
337 TP_CHANNEL_CHAT_STATE_PAUSED);
343 chat_composing_start (EmpathyChat *chat)
345 EmpathyChatPriv *priv;
347 priv = GET_PRIV (chat);
349 if (priv->composing_stop_timeout_id) {
350 /* Just restart the timeout */
351 chat_composing_remove_timeout (chat);
353 empathy_tp_chat_set_state (priv->tp_chat,
354 TP_CHANNEL_CHAT_STATE_COMPOSING);
357 priv->composing_stop_timeout_id = g_timeout_add_seconds (
358 COMPOSING_STOP_TIMEOUT,
359 (GSourceFunc) chat_composing_stop_timeout_cb,
364 chat_composing_stop (EmpathyChat *chat)
366 EmpathyChatPriv *priv;
368 priv = GET_PRIV (chat);
370 chat_composing_remove_timeout (chat);
371 empathy_tp_chat_set_state (priv->tp_chat,
372 TP_CHANNEL_CHAT_STATE_ACTIVE);
376 chat_input_history_entry_cmp (InputHistoryEntry *entry,
379 if (!tp_strdiff (entry->text, text)) {
380 if (entry->modified_text != NULL) {
381 /* Modified entry and single string cannot be equal. */
389 static InputHistoryEntry *
390 chat_input_history_entry_new_with_text (const gchar *text)
392 InputHistoryEntry *entry;
393 entry = g_slice_new0 (InputHistoryEntry);
394 entry->text = g_strdup (text);
400 chat_input_history_entry_free (InputHistoryEntry *entry)
402 g_free (entry->text);
403 g_free (entry->modified_text);
404 g_slice_free (InputHistoryEntry, entry);
408 chat_input_history_entry_revert (InputHistoryEntry *entry)
410 g_free (entry->modified_text);
411 entry->modified_text = NULL;
415 chat_input_history_entry_update_text (InputHistoryEntry *entry,
420 if (!tp_strdiff (text, entry->text)) {
421 g_free (entry->modified_text);
422 entry->modified_text = NULL;
426 old = entry->modified_text;
427 entry->modified_text = g_strdup (text);
432 chat_input_history_entry_get_text (InputHistoryEntry *entry)
438 if (entry->modified_text != NULL) {
439 return entry->modified_text;
445 chat_input_history_remove_item (GList *list,
448 list = g_list_remove_link (list, item);
449 chat_input_history_entry_free (item->data);
450 g_list_free_1 (item);
455 chat_input_history_revert (EmpathyChat *chat)
457 EmpathyChatPriv *priv;
461 InputHistoryEntry *entry;
463 priv = GET_PRIV (chat);
464 list = priv->input_history;
467 DEBUG ("No input history");
471 /* Delete temporary entry */
472 if (priv->input_history_current != NULL) {
474 list = chat_input_history_remove_item (list, item1);
475 if (priv->input_history_current == item1) {
476 /* Removed temporary entry was current entry */
477 priv->input_history = list;
478 priv->input_history_current = NULL;
483 /* There is no entry to revert */
487 /* Restore the current history entry to original value */
488 item1 = priv->input_history_current;
490 chat_input_history_entry_revert (entry);
492 /* Remove restored entry if there is other occurance before this entry */
493 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
494 (GCompareFunc) chat_input_history_entry_cmp);
495 if (item2 != item1) {
496 list = chat_input_history_remove_item (list, item1);
499 /* Remove other occurance of the restored entry */
500 item2 = g_list_find_custom (item1->next,
501 chat_input_history_entry_get_text (entry),
502 (GCompareFunc) chat_input_history_entry_cmp);
504 list = chat_input_history_remove_item (list, item2);
508 priv->input_history_current = NULL;
509 priv->input_history = list;
513 chat_input_history_add (EmpathyChat *chat,
517 EmpathyChatPriv *priv;
520 InputHistoryEntry *entry;
522 priv = GET_PRIV (chat);
524 list = priv->input_history;
526 /* Remove any other occurances of this entry, if not temporary */
528 while ((item = g_list_find_custom (list, str,
529 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
530 list = chat_input_history_remove_item (list, item);
533 /* Trim the list to the last 10 items */
534 while (g_list_length (list) > 10) {
535 item = g_list_last (list);
537 list = chat_input_history_remove_item (list, item);
545 entry = chat_input_history_entry_new_with_text (str);
546 list = g_list_prepend (list, entry);
548 /* Set the list and the current item pointer */
549 priv->input_history = list;
551 priv->input_history_current = list;
554 priv->input_history_current = NULL;
559 chat_input_history_get_next (EmpathyChat *chat)
561 EmpathyChatPriv *priv;
565 priv = GET_PRIV (chat);
567 if (priv->input_history == NULL) {
568 DEBUG ("No input history, next entry is NULL");
571 g_assert (priv->input_history_current != NULL);
573 if ((item = g_list_next (priv->input_history_current)) == NULL)
575 item = priv->input_history_current;
578 msg = chat_input_history_entry_get_text (item->data);
580 DEBUG ("Returning next entry: '%s'", msg);
582 priv->input_history_current = item;
588 chat_input_history_get_prev (EmpathyChat *chat)
590 EmpathyChatPriv *priv;
594 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
596 priv = GET_PRIV (chat);
598 if (priv->input_history == NULL) {
599 DEBUG ("No input history, previous entry is NULL");
603 if (priv->input_history_current == NULL)
607 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
609 item = priv->input_history_current;
612 msg = chat_input_history_entry_get_text (item->data);
614 DEBUG ("Returning previous entry: '%s'", msg);
616 priv->input_history_current = item;
622 chat_input_history_update (EmpathyChat *chat,
623 GtkTextBuffer *buffer)
625 EmpathyChatPriv *priv;
626 GtkTextIter start, end;
628 InputHistoryEntry *entry;
630 priv = GET_PRIV (chat);
632 gtk_text_buffer_get_bounds (buffer, &start, &end);
633 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
635 if (priv->input_history_current == NULL) {
636 /* Add the current text temporarily to the history */
637 chat_input_history_add (chat, text, TRUE);
642 /* Save the changes in the history */
643 entry = priv->input_history_current->data;
644 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
645 chat_input_history_entry_update_text (entry, text);
654 } ChatCommandMsgData;
657 chat_command_msg_cb (GObject *source,
658 GAsyncResult *result,
661 ChatCommandMsgData *data = user_data;
662 GError *error = NULL;
665 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
666 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
668 if (channel == NULL) {
669 DEBUG ("Failed to get channel: %s", error->message);
670 g_error_free (error);
672 empathy_chat_view_append_event (data->chat->view,
673 _("Failed to open private chat"));
677 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
678 TpTextChannel *text = (TpTextChannel *) channel;
681 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
684 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
686 g_object_unref (msg);
689 g_object_unref (channel);
692 g_free (data->message);
693 g_slice_free (ChatCommandMsgData, data);
697 nick_command_supported (EmpathyChat *chat)
699 EmpathyChatPriv * priv = GET_PRIV (chat);
700 TpConnection *connection;
702 connection = empathy_tp_chat_get_connection (priv->tp_chat);
703 return tp_proxy_has_interface_by_id (connection,
704 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
709 part_command_supported (EmpathyChat *chat)
711 EmpathyChatPriv * priv = GET_PRIV (chat);
714 channel = empathy_tp_chat_get_channel (priv->tp_chat);
715 return tp_proxy_has_interface_by_id (channel,
716 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
721 chat_command_clear (EmpathyChat *chat,
724 empathy_chat_view_clear (chat->view);
728 chat_command_topic (EmpathyChat *chat,
731 EmpathyChatPriv *priv = GET_PRIV (chat);
732 EmpathyTpChatProperty *property;
733 GValue value = {0, };
735 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
736 if (property == NULL) {
737 empathy_chat_view_append_event (chat->view,
738 _("Topic not supported on this conversation"));
742 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
743 empathy_chat_view_append_event (chat->view,
744 _("You are not allowed to change the topic"));
748 g_value_init (&value, G_TYPE_STRING);
749 g_value_set_string (&value, strv[1]);
750 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
751 g_value_unset (&value);
755 empathy_chat_join_muc (EmpathyChat *chat,
758 EmpathyChatPriv *priv = GET_PRIV (chat);
760 empathy_join_muc (priv->account, room,
761 gtk_get_current_event_time ());
765 chat_command_join (EmpathyChat *chat,
770 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
772 /* FIXME: Ideally we should ask to handle ourself the channel so we can
773 * report the error if any but this is blocked by
774 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
775 while (rooms[i] != NULL) {
776 /* ignore empty strings */
777 if (!EMP_STR_EMPTY (rooms[i])) {
778 empathy_chat_join_muc (chat, rooms[i]);
786 empathy_chat_leave_chat (EmpathyChat *chat)
788 EmpathyChatPriv *priv = GET_PRIV (chat);
790 empathy_tp_chat_leave (priv->tp_chat, "");
795 chat_command_part (EmpathyChat *chat,
798 EmpathyChatPriv *priv = GET_PRIV (chat);
799 EmpathyChat *chat_to_be_parted;
801 if (strv[1] == NULL) {
802 empathy_tp_chat_leave (priv->tp_chat, "");
805 chat_to_be_parted = empathy_chat_window_find_chat (priv->account, strv[1]);
807 if (chat_to_be_parted != NULL) {
808 empathy_tp_chat_leave (empathy_chat_get_tp_chat (chat_to_be_parted), strv[2]);
810 empathy_tp_chat_leave (priv->tp_chat, strv[1]);
816 chat_command_msg_internal (EmpathyChat *chat,
817 const gchar *contact_id,
818 const gchar *message)
820 EmpathyChatPriv *priv = GET_PRIV (chat);
821 ChatCommandMsgData *data;
822 TpAccountChannelRequest *req;
825 request = tp_asv_new (
826 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
827 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
828 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
831 req = tp_account_channel_request_new (priv->account, request,
832 tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
834 /* FIXME: We should probably search in members alias. But this
835 * is enough for IRC */
836 data = g_slice_new (ChatCommandMsgData);
838 data->message = g_strdup (message);
840 tp_account_channel_request_ensure_and_observe_channel_async (req,
841 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
843 g_object_unref (req);
844 g_hash_table_unref (request);
848 chat_command_query (EmpathyChat *chat,
851 /* If <message> part is not defined,
852 * strv[2] will be the terminal NULL */
853 chat_command_msg_internal (chat, strv[1], strv[2]);
857 chat_command_msg (EmpathyChat *chat,
860 chat_command_msg_internal (chat, strv[1], strv[2]);
864 callback_for_request_rename (TpProxy *proxy,
867 GObject *weak_object)
870 DEBUG ("Call to RequestRename method failed: %s",error->message);
875 chat_command_nick (EmpathyChat *chat,
878 EmpathyChatPriv *priv = GET_PRIV (chat);
881 proxy = TP_PROXY (tp_account_get_connection (priv->account));
883 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
884 strv[1], callback_for_request_rename, NULL, NULL, NULL);
888 chat_command_me (EmpathyChat *chat,
891 EmpathyChatPriv *priv = GET_PRIV (chat);
895 channel = empathy_tp_chat_get_channel (priv->tp_chat);
897 /* Strictly speaking we don't depend yet on Messages so best to check that
898 * the channel is actually a TpTextChannel before casting it. */
899 if (TP_IS_TEXT_CHANNEL (channel) &&
900 !tp_text_channel_supports_message_type (TP_TEXT_CHANNEL (channel),
901 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
902 /* Action message are not supported, 'simulate' the action */
903 EmpathyContact *self_contact;
906 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
907 /* The TpChat can't be ready if it doesn't have the self contact */
908 g_assert (self_contact != NULL);
910 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
912 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
917 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
921 empathy_tp_chat_send (priv->tp_chat, message);
922 g_object_unref (message);
926 chat_command_say (EmpathyChat *chat,
929 EmpathyChatPriv *priv = GET_PRIV (chat);
932 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
934 empathy_tp_chat_send (priv->tp_chat, message);
935 g_object_unref (message);
938 static void chat_command_help (EmpathyChat *chat, GStrv strv);
940 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
946 ChatCommandFunc func;
947 gboolean (*is_supported)(EmpathyChat *chat);
951 static ChatCommandItem commands[] = {
952 {"clear", 1, 1, chat_command_clear, NULL,
953 N_("/clear: clear all messages from the current conversation")},
955 {"topic", 2, 2, chat_command_topic, NULL,
956 N_("/topic <topic>: set the topic of the current conversation")},
958 {"join", 2, 2, chat_command_join, NULL,
959 N_("/join <chat room ID>: join a new chat room")},
961 {"j", 2, 2, chat_command_join, NULL,
962 N_("/j <chat room ID>: join a new chat room")},
965 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
966 {"part", 1, 3, chat_command_part, part_command_supported,
967 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
968 "by default the current one")},
971 {"query", 2, 3, chat_command_query, NULL,
972 N_("/query <contact ID> [<message>]: open a private chat")},
974 {"msg", 3, 3, chat_command_msg, NULL,
975 N_("/msg <contact ID> <message>: open a private chat")},
977 {"nick", 2, 2, chat_command_nick, nick_command_supported,
978 N_("/nick <nickname>: change your nickname on the current server")},
980 {"me", 2, 2, chat_command_me, NULL,
981 N_("/me <message>: send an ACTION message to the current conversation")},
983 {"say", 2, 2, chat_command_say, NULL,
984 N_("/say <message>: send <message> to the current conversation. "
985 "This is used to send a message starting with a '/'. For example: "
986 "\"/say /join is used to join a new chat room\"")},
988 {"help", 1, 2, chat_command_help, NULL,
989 N_("/help [<command>]: show all supported commands. "
990 "If <command> is defined, show its usage.")},
994 chat_command_show_help (EmpathyChat *chat,
995 ChatCommandItem *item)
999 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1000 empathy_chat_view_append_event (chat->view, str);
1005 chat_command_help (EmpathyChat *chat,
1010 /* If <command> part is not defined,
1011 * strv[1] will be the terminal NULL */
1012 if (strv[1] == NULL) {
1013 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1014 if (commands[i].is_supported != NULL) {
1015 if (!commands[i].is_supported (chat)) {
1019 empathy_chat_view_append_event (chat->view,
1020 _(commands[i].help));
1025 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1026 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1027 if (commands[i].is_supported != NULL) {
1028 if (!commands[i].is_supported (chat)) {
1032 chat_command_show_help (chat, &commands[i]);
1037 empathy_chat_view_append_event (chat->view,
1038 _("Unknown command"));
1042 chat_command_parse (const gchar *text, guint max_parts)
1047 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1049 array = g_ptr_array_sized_new (max_parts + 1);
1050 while (max_parts > 1) {
1053 /* Skip white spaces */
1054 while (g_ascii_isspace (*text)) {
1058 /* Search the end of this part, until first space. */
1059 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1065 item = g_strndup (text, end - text);
1066 g_ptr_array_add (array, item);
1067 DEBUG ("\tITEM: \"%s\"", item);
1073 /* Append last part if not empty */
1074 item = g_strstrip (g_strdup (text));
1075 if (!EMP_STR_EMPTY (item)) {
1076 g_ptr_array_add (array, item);
1077 DEBUG ("\tITEM: \"%s\"", item);
1082 /* Make the array NULL-terminated */
1083 g_ptr_array_add (array, NULL);
1085 return (GStrv) g_ptr_array_free (array, FALSE);
1089 has_prefix_case (const gchar *s,
1090 const gchar *prefix)
1092 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1096 chat_send (EmpathyChat *chat,
1099 EmpathyChatPriv *priv;
1103 if (EMP_STR_EMPTY (msg)) {
1107 priv = GET_PRIV (chat);
1109 chat_input_history_add (chat, msg, FALSE);
1111 if (msg[0] == '/') {
1112 gboolean second_slash = FALSE;
1113 const gchar *iter = msg + 1;
1115 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1120 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1123 c = *(msg + 1 + strlen (commands[i].prefix));
1124 if (c != '\0' && !g_ascii_isspace (c)) {
1127 if (commands[i].is_supported != NULL) {
1128 if (!commands[i].is_supported (chat)) {
1133 /* We can't use g_strsplit here because it does
1134 * not deal correctly if we have more than one space
1136 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1138 strv_len = g_strv_length (strv);
1139 if (strv_len < commands[i].min_parts ||
1140 strv_len > commands[i].max_parts) {
1141 chat_command_show_help (chat, &commands[i]);
1146 commands[i].func (chat, strv);
1151 /* Also allow messages with two slashes before the
1152 * first space, so it is possible to send a /unix/path.
1153 * This heuristic is kind of crap. */
1154 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1156 second_slash = TRUE;
1162 if (!second_slash) {
1163 empathy_chat_view_append_event (chat->view,
1164 _("Unknown command; see /help for the available"
1170 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1172 empathy_tp_chat_send (priv->tp_chat, message);
1173 g_object_unref (message);
1177 chat_input_text_view_send (EmpathyChat *chat)
1179 GtkTextBuffer *buffer;
1180 GtkTextIter start, end;
1183 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1185 gtk_text_buffer_get_bounds (buffer, &start, &end);
1186 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1188 /* clear the input field */
1189 gtk_text_buffer_set_text (buffer, "", -1);
1190 /* delete input history modifications */
1191 chat_input_history_revert (chat);
1193 chat_send (chat, msg);
1198 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1199 EmpathyContact *contact,
1200 TpChannelChatState state,
1203 EmpathyChatPriv *priv;
1205 gboolean was_composing;
1207 priv = GET_PRIV (chat);
1209 if (empathy_contact_is_user (contact)) {
1210 /* We don't care about our own chat state */
1214 was_composing = (priv->compositors != NULL);
1216 /* Find the contact in the list. After that l is the list elem or NULL */
1217 for (l = priv->compositors; l; l = l->next) {
1218 if (contact == l->data) {
1224 case TP_CHANNEL_CHAT_STATE_GONE:
1225 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1226 case TP_CHANNEL_CHAT_STATE_PAUSED:
1227 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1228 /* Contact is not composing */
1230 priv->compositors = g_list_remove_link (priv->compositors, l);
1231 g_object_unref (l->data);
1235 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1236 /* Contact is composing */
1238 priv->compositors = g_list_prepend (priv->compositors,
1239 g_object_ref (contact));
1243 g_assert_not_reached ();
1246 DEBUG ("Was composing: %s now composing: %s",
1247 was_composing ? "yes" : "no",
1248 priv->compositors ? "yes" : "no");
1250 if ((was_composing && !priv->compositors) ||
1251 (!was_composing && priv->compositors)) {
1252 /* Composing state changed */
1253 g_signal_emit (chat, signals[COMPOSING], 0,
1254 priv->compositors != NULL);
1259 chat_message_received (EmpathyChat *chat,
1260 EmpathyMessage *message,
1263 EmpathyChatPriv *priv = GET_PRIV (chat);
1264 EmpathyContact *sender;
1266 sender = empathy_message_get_sender (message);
1268 DEBUG ("Appending new message from %s (%d)",
1269 empathy_contact_get_alias (sender),
1270 empathy_contact_get_handle (sender));
1272 empathy_chat_view_append_message (chat->view, message);
1274 /* We received a message so the contact is no longer composing */
1275 chat_state_changed_cb (priv->tp_chat, sender,
1276 TP_CHANNEL_CHAT_STATE_ACTIVE,
1279 if (empathy_message_is_incoming (message)) {
1280 priv->unread_messages++;
1283 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1287 chat_message_received_cb (EmpathyTpChat *tp_chat,
1288 EmpathyMessage *message,
1291 chat_message_received (chat, message, FALSE);
1295 chat_pending_message_removed_cb (EmpathyTpChat *tp_chat,
1298 EmpathyChatPriv *priv = GET_PRIV (chat);
1300 priv->unread_messages--;
1301 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1305 chat_send_error_cb (EmpathyTpChat *tp_chat,
1306 const gchar *message_body,
1307 TpChannelTextSendError error_code,
1308 const gchar *dbus_error,
1311 const gchar *error = NULL;
1314 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1315 /* translators: error used when user doesn't have enough credit on his
1316 * account to send the message. */
1317 error = _("insufficient balance to send message");
1318 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1319 error = _("not capable");
1322 if (error == NULL) {
1323 /* if we didn't find a dbus-error, try the old error */
1324 switch (error_code) {
1325 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1326 error = _("offline");
1328 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1329 error = _("invalid contact");
1331 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1332 error = _("permission denied");
1334 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1335 error = _("too long message");
1337 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1338 error = _("not implemented");
1340 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1342 error = _("unknown");
1347 if (message_body != NULL) {
1348 str = g_strdup_printf (_("Error sending message '%s': %s"),
1349 message_body, error);
1352 str = g_strdup_printf (_("Error sending message: %s"), error);
1355 empathy_chat_view_append_event (chat->view, str);
1360 chat_topic_label_size_allocate_cb (GtkLabel *label,
1361 GtkAllocation *allocation,
1364 EmpathyChatPriv *priv = GET_PRIV (chat);
1366 if (!gtk_label_get_line_wrap (label)) {
1367 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1368 gtk_widget_show (priv->expander_topic);
1370 gtk_widget_hide (priv->expander_topic);
1377 chat_topic_expander_activate_cb (GtkExpander *expander,
1378 GParamSpec *param_spec,
1381 EmpathyChatPriv *priv = GET_PRIV (chat);
1383 if (gtk_expander_get_expanded (expander)) {
1384 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1385 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1387 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1388 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1393 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1398 EmpathyChatPriv *priv = GET_PRIV (chat);
1400 if (!tp_strdiff (name, "subject")) {
1401 g_free (priv->subject);
1402 priv->subject = g_value_dup_string (value);
1403 g_object_notify (G_OBJECT (chat), "subject");
1405 if (EMP_STR_EMPTY (priv->subject)) {
1406 gtk_widget_hide (priv->hbox_topic);
1408 gchar *markup_topic;
1411 markup_topic = empathy_add_link_markup (priv->subject);
1412 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1413 _("Topic:"), markup_topic);
1415 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1416 g_free (markup_text);
1417 g_free (markup_topic);
1419 gtk_widget_show (priv->hbox_topic);
1421 if (priv->block_events_timeout_id == 0) {
1424 if (!EMP_STR_EMPTY (priv->subject)) {
1425 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1427 str = g_strdup (_("No topic defined"));
1429 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1433 else if (!tp_strdiff (name, "name")) {
1434 g_free (priv->name);
1435 priv->name = g_value_dup_string (value);
1436 g_object_notify (G_OBJECT (chat), "name");
1441 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1445 GtkTextIter word_start = *iter;
1446 GtkTextIter word_end = *iter;
1449 if (gtk_text_iter_inside_word (&word_end) &&
1450 !gtk_text_iter_ends_word (&word_end)) {
1451 gtk_text_iter_forward_word_end (&word_end);
1456 if (gtk_text_iter_get_char (&tmp) == '\'') {
1457 gtk_text_iter_forward_char (&tmp);
1459 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1460 gtk_text_iter_forward_word_end (&word_end);
1465 if (gtk_text_iter_inside_word (&word_start) ||
1466 gtk_text_iter_ends_word (&word_start)) {
1467 if (!gtk_text_iter_starts_word (&word_start) ||
1468 gtk_text_iter_equal (&word_start, &word_end)) {
1469 gtk_text_iter_backward_word_start (&word_start);
1473 gtk_text_iter_backward_char (&tmp);
1475 if (gtk_text_iter_get_char (&tmp) == '\'') {
1476 gtk_text_iter_backward_char (&tmp);
1478 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1479 gtk_text_iter_backward_word_start (&word_start);
1484 *start = word_start;
1490 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1491 GtkTextIter *location,
1496 GtkTextIter iter, pos;
1498 /* Remove all misspelled tags in the inserted text.
1499 * This happens when text is inserted within a misspelled word. */
1500 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1501 gtk_text_iter_get_offset (location) - len);
1502 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1505 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1508 GtkTextIter start, end;
1511 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1514 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1516 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1517 gtk_text_iter_equal (&pos, &end) ||
1518 empathy_spell_check (str)) {
1519 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1521 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1526 } while (gtk_text_iter_forward_word_end (&iter) &&
1527 gtk_text_iter_compare (&iter, location) <= 0);
1531 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1536 GtkTextIter word_start, word_end;
1538 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1539 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1540 &word_start, &word_end);
1545 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1548 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1549 chat_composing_stop (chat);
1551 chat_composing_start (chat);
1556 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1561 GtkTextIter prev_pos;
1562 GtkTextIter word_start;
1563 GtkTextIter word_end;
1567 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1569 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1570 gtk_text_buffer_get_insert (buffer));
1571 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1573 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1576 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1577 !gtk_text_iter_equal (&pos, &word_end)) {
1578 str = gtk_text_buffer_get_text (buffer,
1579 &word_start, &word_end, FALSE);
1581 if (!empathy_spell_check (str)) {
1582 gtk_text_buffer_apply_tag_by_name (buffer,
1583 "misspelled", &word_start, &word_end);
1585 gtk_text_buffer_remove_tag_by_name (buffer,
1586 "misspelled", &word_start, &word_end);
1593 gtk_text_buffer_move_mark (buffer, mark, &pos);
1597 empathy_isspace_cb (gunichar c,
1600 return g_unichar_isspace (c);
1604 chat_input_key_press_event_cb (GtkWidget *widget,
1608 EmpathyChatPriv *priv;
1611 GtkWidget *text_view_sw;
1613 priv = GET_PRIV (chat);
1615 /* Catch ctrl+up/down so we can traverse messages we sent */
1616 if ((event->state & GDK_CONTROL_MASK) &&
1617 (event->keyval == GDK_KEY_Up ||
1618 event->keyval == GDK_KEY_Down)) {
1619 GtkTextBuffer *buffer;
1622 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1623 chat_input_history_update (chat, buffer);
1625 if (event->keyval == GDK_KEY_Up) {
1626 str = chat_input_history_get_next (chat);
1628 str = chat_input_history_get_prev (chat);
1631 g_signal_handlers_block_by_func (buffer,
1632 chat_input_text_buffer_changed_cb,
1634 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1635 g_signal_handlers_unblock_by_func (buffer,
1636 chat_input_text_buffer_changed_cb,
1642 /* Catch enter but not ctrl/shift-enter */
1643 if (IS_ENTER (event->keyval) &&
1644 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1647 /* This is to make sure that kinput2 gets the enter. And if
1648 * it's handled there we shouldn't send on it. This is because
1649 * kinput2 uses Enter to commit letters. See:
1650 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1653 view = GTK_TEXT_VIEW (chat->input_text_view);
1654 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1655 gtk_text_view_reset_im_context (view);
1659 chat_input_text_view_send (chat);
1663 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1665 if (IS_ENTER (event->keyval) &&
1666 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1667 /* Newline for shift/control-enter. */
1670 if (!(event->state & GDK_CONTROL_MASK) &&
1671 event->keyval == GDK_KEY_Page_Up) {
1672 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1673 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1676 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1677 event->keyval == GDK_KEY_Page_Down) {
1678 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1679 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1680 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1681 gtk_adjustment_set_value (adj, val);
1684 if (event->keyval == GDK_KEY_Escape) {
1685 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1687 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1688 event->keyval == GDK_KEY_Tab) {
1689 GtkTextBuffer *buffer;
1690 GtkTextIter start, current;
1691 gchar *nick, *completed;
1692 GList *list, *completed_list;
1693 gboolean is_start_of_buffer;
1695 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1696 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1698 /* Get the start of the nick to complete. */
1699 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1700 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1701 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1703 is_start_of_buffer = gtk_text_iter_is_start (&start);
1705 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1706 g_completion_add_items (priv->completion, list);
1708 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1709 completed_list = g_completion_complete (priv->completion,
1718 GString *message = NULL;
1721 gtk_text_buffer_delete (buffer, &start, ¤t);
1723 len = g_list_length (completed_list);
1726 /* If we only have one hit, use that text
1727 * instead of the text in completed since the
1728 * completed text will use the typed string
1729 * which might be cased all wrong.
1732 text = empathy_contact_get_alias (completed_list->data);
1736 /* Print all hits to the scrollback view, so the
1737 * user knows what possibilities he has.
1740 message = g_string_new ("");
1741 for (l = completed_list; l != NULL; l = l->next) {
1742 g_string_append (message, empathy_contact_get_alias (l->data));
1743 g_string_append (message, " - ");
1745 empathy_chat_view_append_event (chat->view, message->str);
1746 g_string_free (message, TRUE);
1749 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1751 if (len == 1 && is_start_of_buffer) {
1752 gchar *complete_char;
1754 complete_char = g_settings_get_string (
1755 priv->gsettings_chat,
1756 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1758 if (complete_char != NULL) {
1759 gtk_text_buffer_insert_at_cursor (buffer,
1761 strlen (complete_char));
1762 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1763 g_free (complete_char);
1770 g_completion_clear_items (priv->completion);
1772 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1782 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1786 gtk_widget_grab_focus (chat->input_text_view);
1792 chat_input_realize_cb (GtkWidget *widget,
1795 DEBUG ("Setting focus to the input text view");
1796 if (gtk_widget_is_sensitive (widget)) {
1797 gtk_widget_grab_focus (widget);
1802 chat_input_has_focus_notify_cb (GtkWidget *widget,
1806 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1810 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1811 EmpathySmiley *smiley,
1814 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1815 GtkTextBuffer *buffer;
1818 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1820 gtk_text_buffer_get_end_iter (buffer, &iter);
1821 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1823 gtk_text_buffer_get_end_iter (buffer, &iter);
1824 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1835 static EmpathyChatSpell *
1836 chat_spell_new (EmpathyChat *chat,
1841 EmpathyChatSpell *chat_spell;
1843 chat_spell = g_slice_new0 (EmpathyChatSpell);
1845 chat_spell->chat = g_object_ref (chat);
1846 chat_spell->word = g_strdup (word);
1847 chat_spell->start = start;
1848 chat_spell->end = end;
1854 chat_spell_free (EmpathyChatSpell *chat_spell)
1856 g_object_unref (chat_spell->chat);
1857 g_free (chat_spell->word);
1858 g_slice_free (EmpathyChatSpell, chat_spell);
1862 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1863 EmpathyChatSpell *chat_spell)
1865 empathy_chat_correct_word (chat_spell->chat,
1866 &(chat_spell->start),
1868 gtk_menu_item_get_label (menu_item));
1873 chat_spelling_build_suggestions_menu (const gchar *code,
1874 EmpathyChatSpell *chat_spell)
1876 GList *suggestions, *l;
1877 GtkWidget *menu, *menu_item;
1879 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1880 if (suggestions == NULL)
1883 menu = gtk_menu_new ();
1884 for (l = suggestions; l; l = l->next) {
1885 menu_item = gtk_menu_item_new_with_label (l->data);
1886 g_signal_connect (G_OBJECT (menu_item), "activate",
1887 G_CALLBACK (chat_spelling_menu_activate_cb),
1889 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1891 empathy_spell_free_suggestions (suggestions);
1893 gtk_widget_show_all (menu);
1899 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1901 GtkWidget *menu, *submenu, *item;
1904 codes = empathy_spell_get_enabled_language_codes ();
1905 g_assert (codes != NULL);
1907 if (g_list_length (codes) > 1) {
1908 menu = gtk_menu_new ();
1910 for (l = codes; l; l = l->next) {
1911 const gchar *code, *name;
1914 name = empathy_spell_get_language_name (code);
1918 item = gtk_image_menu_item_new_with_label (name);
1920 submenu = chat_spelling_build_suggestions_menu (
1922 if (submenu == NULL)
1923 gtk_widget_set_sensitive (item, FALSE);
1925 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1927 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1930 menu = chat_spelling_build_suggestions_menu (codes->data,
1933 menu = gtk_menu_new ();
1934 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1935 gtk_widget_set_sensitive (item, FALSE);
1936 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1939 g_list_free (codes);
1941 gtk_widget_show_all (menu);
1952 static EmpathyChatWord *
1953 chat_word_new (EmpathyChat *chat,
1957 EmpathyChatWord *chat_word;
1959 chat_word = g_slice_new0 (EmpathyChatWord);
1961 chat_word->chat = g_object_ref (chat);
1962 chat_word->word = g_strdup (word);
1963 chat_word->code = g_strdup (code);
1969 chat_word_free (EmpathyChatWord *chat_word)
1971 g_object_unref (chat_word->chat);
1972 g_free (chat_word->word);
1973 g_free (chat_word->code);
1974 g_slice_free (EmpathyChatWord, chat_word);
1978 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1979 EmpathyChatWord *chat_word)
1981 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1983 empathy_spell_add_to_dictionary (chat_word->code,
1985 priv->update_misspelled_words_id =
1986 g_idle_add (update_misspelled_words, chat_word->chat);
1990 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1992 GtkWidget *menu, *item, *lang_item, *image;
1995 const gchar *code, *name;
1996 EmpathyChatWord *chat_word;
1998 codes = empathy_spell_get_enabled_language_codes ();
1999 g_assert (codes != NULL);
2000 if (g_list_length (codes) > 1) {
2001 /* translators: %s is the selected word */
2002 label = g_strdup_printf (_("Add '%s' to Dictionary"),
2004 item = gtk_image_menu_item_new_with_mnemonic (label);
2006 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2007 GTK_ICON_SIZE_MENU);
2008 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2011 menu = gtk_menu_new ();
2013 for (l = codes; l; l = l->next) {
2015 name = empathy_spell_get_language_name (code);
2019 lang_item = gtk_image_menu_item_new_with_label (name);
2021 chat_word= chat_word_new (chat_spell->chat,
2022 chat_spell->word, code);
2023 g_object_set_data_full (G_OBJECT (lang_item),
2024 "chat-word", chat_word,
2025 (GDestroyNotify) chat_word_free);
2027 g_signal_connect (G_OBJECT (lang_item), "activate",
2028 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2030 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2032 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2035 name = empathy_spell_get_language_name (code);
2036 g_assert (name != NULL);
2037 /* translators: first %s is the selected word,
2038 * second %s is the language name of the target dictionary */
2039 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2040 chat_spell->word, name);
2041 item = gtk_image_menu_item_new_with_mnemonic (label);
2043 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2044 GTK_ICON_SIZE_MENU);
2045 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2047 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2049 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2050 (GDestroyNotify) chat_word_free);
2052 g_signal_connect (G_OBJECT (item), "activate",
2053 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2056 g_list_free (codes);
2058 gtk_widget_show_all (item);
2064 chat_text_send_cb (GtkMenuItem *menuitem,
2067 chat_input_text_view_send (chat);
2071 chat_input_populate_popup_cb (GtkTextView *view,
2075 GtkTextBuffer *buffer;
2076 GtkTextTagTable *table;
2079 GtkTextIter iter, start, end;
2082 EmpathyChatSpell *chat_spell;
2083 GtkWidget *spell_menu;
2084 GtkWidget *spell_item;
2085 EmpathySmileyManager *smiley_manager;
2086 GtkWidget *smiley_menu;
2089 buffer = gtk_text_view_get_buffer (view);
2091 /* Add the emoticon menu. */
2092 item = gtk_separator_menu_item_new ();
2093 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2094 gtk_widget_show (item);
2096 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2097 image = gtk_image_new_from_icon_name ("face-smile",
2098 GTK_ICON_SIZE_MENU);
2099 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2100 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2101 gtk_widget_show (item);
2103 smiley_manager = empathy_smiley_manager_dup_singleton ();
2104 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2105 chat_insert_smiley_activate_cb,
2107 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2108 g_object_unref (smiley_manager);
2110 /* Add the Send menu item. */
2111 gtk_text_buffer_get_bounds (buffer, &start, &end);
2112 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2113 if (!EMP_STR_EMPTY (str)) {
2114 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2115 g_signal_connect (G_OBJECT (item), "activate",
2116 G_CALLBACK (chat_text_send_cb), chat);
2117 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2118 gtk_widget_show (item);
2122 /* Add the spell check menu item. */
2123 table = gtk_text_buffer_get_tag_table (buffer);
2124 tag = gtk_text_tag_table_lookup (table, "misspelled");
2125 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2126 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2127 GTK_TEXT_WINDOW_WIDGET,
2130 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2132 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2133 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2135 str = gtk_text_buffer_get_text (buffer,
2136 &start, &end, FALSE);
2138 if (!EMP_STR_EMPTY (str)) {
2139 chat_spell = chat_spell_new (chat, str, start, end);
2140 g_object_set_data_full (G_OBJECT (menu),
2141 "chat-spell", chat_spell,
2142 (GDestroyNotify) chat_spell_free);
2144 item = gtk_separator_menu_item_new ();
2145 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2146 gtk_widget_show (item);
2148 /* Spelling suggestions */
2149 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2150 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2151 GTK_ICON_SIZE_MENU);
2152 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2154 spell_menu = chat_spelling_build_menu (chat_spell);
2155 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2158 spell_item = gtk_separator_menu_item_new ();
2159 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2160 gtk_widget_show (spell_item);
2162 /* Add to dictionary */
2163 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2165 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2166 gtk_widget_show (spell_item);
2168 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2169 gtk_widget_show (item);
2175 chat_log_filter (TplEvent *log,
2178 EmpathyChat *chat = user_data;
2179 EmpathyMessage *message;
2180 EmpathyChatPriv *priv = GET_PRIV (chat);
2181 const GList *pending;
2183 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2184 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2186 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2187 message = empathy_message_from_tpl_log_event (log);
2189 for (; pending; pending = g_list_next (pending)) {
2190 if (empathy_message_equal (message, pending->data)) {
2191 g_object_unref (message);
2196 g_object_unref (message);
2202 show_pending_messages (EmpathyChat *chat) {
2203 EmpathyChatPriv *priv = GET_PRIV (chat);
2204 const GList *messages, *l;
2206 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2208 if (chat->view == NULL || priv->tp_chat == NULL)
2211 if (!priv->can_show_pending)
2214 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2216 for (l = messages; l != NULL ; l = g_list_next (l)) {
2217 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2218 chat_message_received (chat, message, TRUE);
2224 got_filtered_messages_cb (GObject *manager,
2225 GAsyncResult *result,
2230 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2231 EmpathyChatPriv *priv = GET_PRIV (chat);
2232 GError *error = NULL;
2234 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2235 result, &messages, &error)) {
2236 DEBUG ("%s. Aborting.", error->message);
2237 empathy_chat_view_append_event (chat->view,
2238 _("Failed to retrieve recent logs"));
2239 g_error_free (error);
2243 for (l = messages; l; l = g_list_next (l)) {
2244 EmpathyMessage *message;
2245 g_assert (TPL_IS_EVENT (l->data));
2247 message = empathy_message_from_tpl_log_event (l->data);
2248 g_object_unref (l->data);
2250 empathy_chat_view_append_message (chat->view, message);
2251 g_object_unref (message);
2253 g_list_free (messages);
2256 /* in case of TPL error, skip backlog and show pending messages */
2257 priv->can_show_pending = TRUE;
2258 show_pending_messages (chat);
2260 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2261 * about it in EmpathyChatPriv definition */
2262 priv->retrieving_backlogs = FALSE;
2263 empathy_chat_messages_read (chat);
2265 /* Turn back on scrolling */
2266 empathy_chat_view_scroll (chat->view, TRUE);
2270 chat_add_logs (EmpathyChat *chat)
2272 EmpathyChatPriv *priv = GET_PRIV (chat);
2279 /* Turn off scrolling temporarily */
2280 empathy_chat_view_scroll (chat->view, FALSE);
2282 /* Add messages from last conversation */
2283 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2284 target = tpl_entity_new_from_room_id (priv->id);
2286 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2288 priv->retrieving_backlogs = TRUE;
2289 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2292 TPL_EVENT_MASK_TEXT,
2296 got_filtered_messages_cb,
2299 g_object_unref (target);
2303 chat_contacts_completion_func (const gchar *s1,
2307 gchar *tmp, *nick1, *nick2;
2314 return s1 ? -1 : +1;
2317 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2318 nick1 = g_utf8_casefold (tmp, -1);
2321 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2322 nick2 = g_utf8_casefold (tmp, -1);
2325 ret = strncmp (nick1, nick2, n);
2334 build_part_message (guint reason,
2336 EmpathyContact *actor,
2337 const gchar *message)
2339 GString *s = g_string_new ("");
2340 const gchar *actor_name = NULL;
2342 if (actor != NULL) {
2343 actor_name = empathy_contact_get_alias (actor);
2346 /* Having an actor only really makes sense for a few actions... */
2348 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2349 g_string_append_printf (s, _("%s has disconnected"), name);
2351 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2352 if (actor_name != NULL) {
2353 /* translators: reverse the order of these arguments
2354 * if the kicked should come before the kicker in your locale.
2356 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2359 g_string_append_printf (s, _("%s was kicked"), name);
2362 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2363 if (actor_name != NULL) {
2364 /* translators: reverse the order of these arguments
2365 * if the banned should come before the banner in your locale.
2367 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2370 g_string_append_printf (s, _("%s was banned"), name);
2374 g_string_append_printf (s, _("%s has left the room"), name);
2377 if (!EMP_STR_EMPTY (message)) {
2378 /* Note to translators: this string is appended to
2379 * notifications like "foo has left the room", with the message
2380 * given by the user living the room. If this poses a problem,
2381 * please let us know. :-)
2383 g_string_append_printf (s, _(" (%s)"), message);
2386 return g_string_free (s, FALSE);
2390 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2391 EmpathyContact *contact,
2392 EmpathyContact *actor,
2398 EmpathyChatPriv *priv = GET_PRIV (chat);
2399 const gchar *name = empathy_contact_get_alias (contact);
2402 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2404 if (priv->block_events_timeout_id != 0)
2408 str = g_strdup_printf (_("%s has joined the room"),
2411 str = build_part_message (reason, name, actor, message);
2414 empathy_chat_view_append_event (chat->view, str);
2419 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2420 EmpathyContact *old_contact,
2421 EmpathyContact *new_contact,
2426 EmpathyChatPriv *priv = GET_PRIV (chat);
2428 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2430 if (priv->block_events_timeout_id == 0) {
2433 str = g_strdup_printf (_("%s is now known as %s"),
2434 empathy_contact_get_alias (old_contact),
2435 empathy_contact_get_alias (new_contact));
2436 empathy_chat_view_append_event (chat->view, str);
2443 chat_reset_size_request (gpointer widget)
2445 gtk_widget_set_size_request (widget, -1, -1);
2451 chat_update_contacts_visibility (EmpathyChat *chat,
2454 EmpathyChatPriv *priv = GET_PRIV (chat);
2455 GtkAllocation allocation;
2457 if (!priv->scrolled_window_contacts) {
2461 if (priv->remote_contact != NULL) {
2465 if (show && priv->contact_list_view == NULL) {
2466 EmpathyContactListStore *store;
2469 /* We are adding the contact list to the chat, we don't want the
2470 * chat view to become too small. If the chat view is already
2471 * smaller than 250 make sure that size won't change. If the
2472 * chat view is bigger the contact list will take some space on
2473 * it but we make sure the chat view don't become smaller than
2474 * 250. Relax the size request once the resize is done */
2475 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2476 min_width = MIN (allocation.width, 250);
2477 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2478 g_idle_add (chat_reset_size_request, priv->vbox_left);
2480 if (priv->contacts_width > 0) {
2481 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2482 priv->contacts_width);
2485 store = empathy_contact_list_store_new (
2486 EMPATHY_CONTACT_LIST (priv->tp_chat));
2487 empathy_contact_list_store_set_show_groups (
2488 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2490 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2491 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2492 EMPATHY_CONTACT_FEATURE_CHAT |
2493 EMPATHY_CONTACT_FEATURE_CALL |
2494 EMPATHY_CONTACT_FEATURE_LOG |
2495 EMPATHY_CONTACT_FEATURE_INFO));
2496 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2497 priv->contact_list_view);
2498 gtk_widget_show (priv->contact_list_view);
2499 gtk_widget_show (priv->scrolled_window_contacts);
2500 g_object_unref (store);
2502 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2503 gtk_widget_hide (priv->scrolled_window_contacts);
2504 if (priv->contact_list_view != NULL) {
2505 gtk_widget_destroy (priv->contact_list_view);
2506 priv->contact_list_view = NULL;
2512 empathy_chat_set_show_contacts (EmpathyChat *chat,
2515 EmpathyChatPriv *priv = GET_PRIV (chat);
2517 priv->show_contacts = show;
2519 chat_update_contacts_visibility (chat, show);
2521 g_object_notify (G_OBJECT (chat), "show-contacts");
2525 chat_remote_contact_changed_cb (EmpathyChat *chat)
2527 EmpathyChatPriv *priv = GET_PRIV (chat);
2529 if (priv->remote_contact != NULL) {
2530 g_object_unref (priv->remote_contact);
2531 priv->remote_contact = NULL;
2536 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2537 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2538 if (priv->remote_contact != NULL) {
2539 g_object_ref (priv->remote_contact);
2540 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2542 else if (priv->tp_chat != NULL) {
2545 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2546 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2549 chat_update_contacts_visibility (chat, priv->show_contacts);
2551 g_object_notify (G_OBJECT (chat), "remote-contact");
2552 g_object_notify (G_OBJECT (chat), "id");
2556 chat_destroy_cb (EmpathyTpChat *tp_chat,
2559 EmpathyChatPriv *priv;
2561 priv = GET_PRIV (chat);
2563 if (!priv->tp_chat) {
2567 chat_composing_remove_timeout (chat);
2568 g_object_unref (priv->tp_chat);
2569 priv->tp_chat = NULL;
2570 g_object_notify (G_OBJECT (chat), "tp-chat");
2572 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2573 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2575 chat_update_contacts_visibility (chat, FALSE);
2579 update_misspelled_words (gpointer data)
2581 EmpathyChat *chat = EMPATHY_CHAT (data);
2582 EmpathyChatPriv *priv = GET_PRIV (chat);
2583 GtkTextBuffer *buffer;
2587 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2589 gtk_text_buffer_get_end_iter (buffer, &iter);
2590 length = gtk_text_iter_get_offset (&iter);
2591 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2592 NULL, length, chat);
2594 priv->update_misspelled_words_id = 0;
2600 conf_spell_checking_cb (GSettings *gsettings_chat,
2604 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2605 EmpathyChatPriv *priv = GET_PRIV (chat);
2606 gboolean spell_checker;
2607 GtkTextBuffer *buffer;
2609 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2612 spell_checker = g_settings_get_boolean (gsettings_chat,
2613 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2615 if (!empathy_spell_supported ()) {
2616 spell_checker = FALSE;
2619 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2621 if (spell_checker == priv->spell_checking_enabled) {
2622 if (spell_checker) {
2623 /* Possibly changed dictionaries,
2624 * update misspelled words. Need to do so in idle
2625 * so the spell checker is updated. */
2626 priv->update_misspelled_words_id =
2627 g_idle_add (update_misspelled_words, chat);
2633 if (spell_checker) {
2636 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2637 buffer, "notify::cursor-position",
2638 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2640 priv->insert_text_id = tp_g_signal_connect_object (
2641 buffer, "insert-text",
2642 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2643 chat, G_CONNECT_AFTER);
2644 priv->delete_range_id = tp_g_signal_connect_object (
2645 buffer, "delete-range",
2646 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2647 chat, G_CONNECT_AFTER);
2649 gtk_text_buffer_create_tag (buffer, "misspelled",
2650 "underline", PANGO_UNDERLINE_ERROR,
2653 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2654 gtk_text_buffer_get_insert (buffer));
2655 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2658 /* Mark misspelled words in the existing buffer.
2659 * Need to do so in idle so the spell checker is updated. */
2660 priv->update_misspelled_words_id =
2661 g_idle_add (update_misspelled_words, chat);
2663 GtkTextTagTable *table;
2666 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2667 priv->notify_cursor_position_id = 0;
2668 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2669 priv->insert_text_id = 0;
2670 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2671 priv->delete_range_id = 0;
2673 table = gtk_text_buffer_get_tag_table (buffer);
2674 tag = gtk_text_tag_table_lookup (table, "misspelled");
2675 gtk_text_tag_table_remove (table, tag);
2677 gtk_text_buffer_delete_mark_by_name (buffer,
2678 "previous-cursor-position");
2681 priv->spell_checking_enabled = spell_checker;
2685 save_paned_pos_timeout (gpointer data)
2687 EmpathyChat *self = data;
2690 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2692 g_settings_set_int (self->priv->gsettings_ui,
2693 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2700 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2704 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2706 if (chat->priv->save_paned_pos_id != 0)
2707 g_source_remove (chat->priv->save_paned_pos_id);
2709 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2710 save_paned_pos_timeout, chat);
2716 chat_create_ui (EmpathyChat *chat)
2718 EmpathyChatPriv *priv = GET_PRIV (chat);
2722 GtkTextBuffer *buffer;
2724 EmpathyThemeManager *theme_mgr;
2726 filename = empathy_file_lookup ("empathy-chat.ui",
2728 gui = empathy_builder_get_file (filename,
2729 "chat_widget", &priv->widget,
2730 "hpaned", &priv->hpaned,
2731 "vbox_left", &priv->vbox_left,
2732 "scrolled_window_chat", &priv->scrolled_window_chat,
2733 "scrolled_window_input", &priv->scrolled_window_input,
2734 "hbox_topic", &priv->hbox_topic,
2735 "expander_topic", &priv->expander_topic,
2736 "label_topic", &priv->label_topic,
2737 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2738 "info_bar_vbox", &priv->info_bar_vbox,
2741 empathy_builder_connect (gui, chat,
2742 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2743 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2748 /* Add message view. */
2749 theme_mgr = empathy_theme_manager_dup_singleton ();
2750 chat->view = empathy_theme_manager_create_view (theme_mgr);
2751 g_object_unref (theme_mgr);
2752 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2753 and other types, even though it's non-editable and doesn't accept any
2754 drags. This steals drag motion for anything inside the scrollbars,
2755 making drag destinations on chat windows far less useful.
2757 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2758 g_signal_connect (chat->view, "focus_in_event",
2759 G_CALLBACK (chat_text_view_focus_in_event_cb),
2761 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2762 GTK_WIDGET (chat->view));
2763 gtk_widget_show (GTK_WIDGET (chat->view));
2765 /* Add input GtkTextView */
2766 chat->input_text_view = empathy_input_text_view_new ();
2767 g_signal_connect (chat->input_text_view, "notify::has-focus",
2768 G_CALLBACK (chat_input_has_focus_notify_cb),
2770 g_signal_connect (chat->input_text_view, "key-press-event",
2771 G_CALLBACK (chat_input_key_press_event_cb),
2773 g_signal_connect (chat->input_text_view, "realize",
2774 G_CALLBACK (chat_input_realize_cb),
2776 g_signal_connect (chat->input_text_view, "populate-popup",
2777 G_CALLBACK (chat_input_populate_popup_cb),
2779 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2780 tp_g_signal_connect_object (buffer, "changed",
2781 G_CALLBACK (chat_input_text_buffer_changed_cb),
2783 tp_g_signal_connect_object (priv->gsettings_chat,
2784 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2785 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2786 conf_spell_checking_cb (priv->gsettings_chat,
2787 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2788 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2789 chat->input_text_view);
2790 gtk_widget_show (chat->input_text_view);
2792 /* Add the (invisible) search bar */
2793 priv->search_bar = empathy_search_bar_new (chat->view);
2794 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2797 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2799 /* Initialy hide the topic, will be shown if not empty */
2800 gtk_widget_hide (priv->hbox_topic);
2802 g_signal_connect (priv->hpaned, "notify::position",
2803 G_CALLBACK (chat_hpaned_pos_changed_cb),
2806 /* Load the paned position */
2807 paned_pos = g_settings_get_int (priv->gsettings_ui,
2808 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2810 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2812 /* Set widget focus order */
2813 list = g_list_append (NULL, priv->search_bar);
2814 list = g_list_append (list, priv->scrolled_window_input);
2815 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2818 list = g_list_append (NULL, priv->vbox_left);
2819 list = g_list_append (list, priv->scrolled_window_contacts);
2820 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2823 list = g_list_append (NULL, priv->hpaned);
2824 list = g_list_append (list, priv->hbox_topic);
2825 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2828 /* Add the main widget in the chat widget */
2829 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2830 g_object_unref (gui);
2834 chat_size_allocate (GtkWidget *widget,
2835 GtkAllocation *allocation)
2837 GtkBin *bin = GTK_BIN (widget);
2838 GtkAllocation child_allocation;
2841 gtk_widget_set_allocation (widget, allocation);
2843 child = gtk_bin_get_child (bin);
2845 if (child && gtk_widget_get_visible (child))
2847 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2848 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2849 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2850 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2852 gtk_widget_size_allocate (child, &child_allocation);
2857 chat_finalize (GObject *object)
2860 EmpathyChatPriv *priv;
2862 chat = EMPATHY_CHAT (object);
2863 priv = GET_PRIV (chat);
2865 DEBUG ("Finalized: %p", object);
2867 if (priv->update_misspelled_words_id != 0)
2868 g_source_remove (priv->update_misspelled_words_id);
2870 if (priv->save_paned_pos_id != 0)
2871 g_source_remove (priv->save_paned_pos_id);
2873 g_object_unref (priv->gsettings_chat);
2874 g_object_unref (priv->gsettings_ui);
2876 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2877 g_list_free (priv->input_history);
2879 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2880 g_list_free (priv->compositors);
2882 chat_composing_remove_timeout (chat);
2884 g_object_unref (priv->account_manager);
2885 g_object_unref (priv->log_manager);
2887 if (priv->tp_chat) {
2888 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2889 chat_destroy_cb, chat);
2890 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2891 chat_message_received_cb, chat);
2892 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2893 chat_pending_message_removed_cb, chat);
2894 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2895 chat_send_error_cb, chat);
2896 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2897 chat_state_changed_cb, chat);
2898 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2899 chat_property_changed_cb, chat);
2900 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2901 chat_members_changed_cb, chat);
2902 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2903 chat_remote_contact_changed_cb, chat);
2904 empathy_tp_chat_leave (priv->tp_chat, "");
2905 g_object_unref (priv->tp_chat);
2907 if (priv->account) {
2908 g_object_unref (priv->account);
2910 if (priv->remote_contact) {
2911 g_object_unref (priv->remote_contact);
2914 if (priv->block_events_timeout_id) {
2915 g_source_remove (priv->block_events_timeout_id);
2919 g_free (priv->name);
2920 g_free (priv->subject);
2921 g_completion_free (priv->completion);
2923 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2927 chat_constructed (GObject *object)
2929 EmpathyChat *chat = EMPATHY_CHAT (object);
2930 EmpathyChatPriv *priv = GET_PRIV (chat);
2932 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2933 /* First display logs from the logger and then display pending messages */
2934 chat_add_logs (chat);
2937 /* Just display pending messages for rooms */
2938 priv->can_show_pending = TRUE;
2939 show_pending_messages (chat);
2944 empathy_chat_class_init (EmpathyChatClass *klass)
2946 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2947 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2949 object_class->finalize = chat_finalize;
2950 object_class->get_property = chat_get_property;
2951 object_class->set_property = chat_set_property;
2952 object_class->constructed = chat_constructed;
2954 widget_class->size_allocate = chat_size_allocate;
2956 g_object_class_install_property (object_class,
2958 g_param_spec_object ("tp-chat",
2960 "The tp chat object",
2961 EMPATHY_TYPE_TP_CHAT,
2964 G_PARAM_STATIC_STRINGS));
2965 g_object_class_install_property (object_class,
2967 g_param_spec_object ("account",
2968 "Account of the chat",
2969 "The account of the chat",
2972 G_PARAM_STATIC_STRINGS));
2973 g_object_class_install_property (object_class,
2975 g_param_spec_string ("id",
2977 "The id of the chat",
2980 G_PARAM_STATIC_STRINGS));
2981 g_object_class_install_property (object_class,
2983 g_param_spec_string ("name",
2985 "The name of the chat",
2988 G_PARAM_STATIC_STRINGS));
2989 g_object_class_install_property (object_class,
2991 g_param_spec_string ("subject",
2993 "The subject or topic of the chat",
2996 G_PARAM_STATIC_STRINGS));
2997 g_object_class_install_property (object_class,
2998 PROP_REMOTE_CONTACT,
2999 g_param_spec_object ("remote-contact",
3000 "The remote contact",
3001 "The remote contact is any",
3002 EMPATHY_TYPE_CONTACT,
3004 G_PARAM_STATIC_STRINGS));
3005 g_object_class_install_property (object_class,
3007 g_param_spec_boolean ("show-contacts",
3008 "Contacts' visibility",
3009 "The visibility of the contacts' list",
3012 G_PARAM_STATIC_STRINGS));
3014 g_object_class_install_property (object_class,
3016 g_param_spec_boolean ("sms-channel",
3018 "TRUE if this channel is for sending SMSes",
3020 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3022 g_object_class_install_property (object_class,
3023 PROP_N_MESSAGES_SENDING,
3024 g_param_spec_uint ("n-messages-sending",
3025 "Num Messages Sending",
3026 "The number of messages being sent",
3028 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3030 g_object_class_install_property (object_class,
3031 PROP_NB_UNREAD_MESSAGES,
3032 g_param_spec_uint ("nb-unread-messages",
3033 "Num Unread Messages",
3034 "The number of unread messages",
3036 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3038 signals[COMPOSING] =
3039 g_signal_new ("composing",
3040 G_OBJECT_CLASS_TYPE (object_class),
3044 g_cclosure_marshal_VOID__BOOLEAN,
3048 signals[NEW_MESSAGE] =
3049 g_signal_new ("new-message",
3050 G_OBJECT_CLASS_TYPE (object_class),
3054 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3056 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3058 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3062 chat_block_events_timeout_cb (gpointer data)
3064 EmpathyChatPriv *priv = GET_PRIV (data);
3066 priv->block_events_timeout_id = 0;
3072 account_manager_prepared_cb (GObject *source_object,
3073 GAsyncResult *result,
3076 GList *accounts, *l;
3077 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3078 EmpathyChat *chat = user_data;
3079 GError *error = NULL;
3081 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3082 DEBUG ("Failed to prepare the account manager: %s", error->message);
3083 g_error_free (error);
3087 accounts = tp_account_manager_get_valid_accounts (account_manager);
3089 for (l = accounts; l != NULL; l = l->next) {
3090 TpAccount *account = l->data;
3091 tp_g_signal_connect_object (account, "status-changed",
3092 G_CALLBACK (chat_new_connection_cb),
3096 g_list_free (accounts);
3100 empathy_chat_init (EmpathyChat *chat)
3102 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3103 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3106 priv->log_manager = tpl_log_manager_dup_singleton ();
3107 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3108 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3110 priv->contacts_width = -1;
3111 priv->input_history = NULL;
3112 priv->input_history_current = NULL;
3113 priv->account_manager = tp_account_manager_dup ();
3115 tp_account_manager_prepare_async (priv->account_manager, NULL,
3116 account_manager_prepared_cb, chat);
3118 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3119 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3121 /* Block events for some time to avoid having "has come online" or
3122 * "joined" messages. */
3123 priv->block_events_timeout_id =
3124 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3126 /* Add nick name completion */
3127 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3128 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3130 chat_create_ui (chat);
3134 empathy_chat_new (EmpathyTpChat *tp_chat)
3136 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3140 empathy_chat_get_tp_chat (EmpathyChat *chat)
3142 EmpathyChatPriv *priv = GET_PRIV (chat);
3144 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3146 return priv->tp_chat;
3152 GtkWidget *info_bar;
3162 passwd_remember_button_cb (GtkButton *button,
3165 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3169 passwd_not_now_button_cb (GtkButton *button,
3172 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3176 remember_password_infobar_response_cb (GtkWidget *info_bar,
3180 EmpathyChatPriv *priv = GET_PRIV (data->self);
3182 if (response_id == GTK_RESPONSE_OK) {
3183 DEBUG ("Saving room password");
3184 empathy_keyring_set_room_password_async (priv->account,
3185 empathy_tp_chat_get_id (priv->tp_chat),
3190 gtk_widget_destroy (info_bar);
3191 g_free (data->password);
3192 g_slice_free (PasswordData, data);
3196 chat_prompt_to_save_password (EmpathyChat *self,
3199 GtkWidget *content_area;
3206 /* save the password in case it needs to be saved */
3207 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3209 /* Remove all previous widgets */
3210 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3211 gtk_container_forall (GTK_CONTAINER (content_area),
3212 (GtkCallback) gtk_widget_destroy, NULL);
3213 data->button = NULL;
3216 data->spinner = NULL;
3218 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3219 GTK_MESSAGE_QUESTION);
3221 hbox = gtk_hbox_new (FALSE, 5);
3222 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3225 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3226 GTK_ICON_SIZE_DIALOG);
3227 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3230 label = gtk_label_new (_("Would you like to store this password?"));
3231 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3233 /* Add 'Remember' button */
3234 alig = gtk_alignment_new (0, 0.5, 1, 0);
3236 button = gtk_button_new_with_label (_("Remember"));
3237 gtk_container_add (GTK_CONTAINER (alig), button);
3238 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3240 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3243 /* Add 'Not now' button */
3244 alig = gtk_alignment_new (0, 0.5, 1, 0);
3246 button = gtk_button_new_with_label (_("Not now"));
3247 gtk_container_add (GTK_CONTAINER (alig), button);
3248 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3250 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3254 g_signal_handler_disconnect (data->info_bar, data->response_id);
3255 g_signal_connect (data->info_bar, "response",
3256 G_CALLBACK (remember_password_infobar_response_cb), data);
3258 gtk_widget_show_all (data->info_bar);
3262 provide_password_cb (GObject *tp_chat,
3266 PasswordData *data = user_data;
3267 EmpathyChat *self = data->self;
3268 EmpathyChatPriv *priv = GET_PRIV (self);
3269 GError *error = NULL;
3271 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3273 DEBUG ("error: %s", error->message);
3274 /* FIXME: what should we do if that's another error? Close the channel?
3275 * Display the raw D-Bus error to the user isn't very useful */
3276 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3278 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3279 gtk_widget_set_sensitive (data->entry, TRUE);
3280 gtk_widget_grab_focus (data->entry);
3283 gtk_info_bar_set_message_type (
3284 GTK_INFO_BAR (data->info_bar),
3288 gtk_widget_set_sensitive (data->button, TRUE);
3289 gtk_button_set_label (GTK_BUTTON (data->button),
3293 gtk_label_set_text (GTK_LABEL (data->label),
3294 _("Wrong password; please try again:"));
3297 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3298 gtk_widget_hide (data->spinner);
3300 g_error_free (error);
3304 if (empathy_keyring_is_available ()) {
3305 /* ask whether they want to save the password */
3306 chat_prompt_to_save_password (self, data);
3308 /* Get rid of the password info bar finally */
3309 gtk_widget_destroy (data->info_bar);
3310 g_slice_free (PasswordData, data);
3314 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3315 gtk_widget_grab_focus (self->input_text_view);
3319 password_infobar_response_cb (GtkWidget *info_bar,
3323 EmpathyChatPriv *priv = GET_PRIV (data->self);
3324 const gchar *password;
3326 if (response_id != GTK_RESPONSE_OK) {
3327 gtk_widget_destroy (info_bar);
3328 g_slice_free (PasswordData, data);
3332 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3334 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3335 provide_password_cb, data);
3337 gtk_widget_set_sensitive (data->button, FALSE);
3338 gtk_widget_set_sensitive (data->entry, FALSE);
3340 gtk_spinner_start (GTK_SPINNER (data->spinner));
3341 gtk_widget_show (data->spinner);
3345 password_entry_activate_cb (GtkWidget *entry,
3348 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3352 passwd_join_button_cb (GtkButton *button,
3355 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3359 clear_icon_released_cb (GtkEntry *entry,
3360 GtkEntryIconPosition icon_pos,
3364 gtk_entry_set_text (entry, "");
3368 password_entry_changed_cb (GtkEditable *entry,
3373 str = gtk_entry_get_text (GTK_ENTRY (entry));
3375 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3376 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3380 display_password_info_bar (EmpathyChat *self)
3382 EmpathyChatPriv *priv = GET_PRIV (self);
3383 GtkWidget *info_bar;
3384 GtkWidget *content_area;
3394 data = g_slice_new0 (PasswordData);
3396 info_bar = gtk_info_bar_new ();
3397 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3398 GTK_MESSAGE_QUESTION);
3400 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3402 hbox = gtk_hbox_new (FALSE, 5);
3403 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3406 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3407 GTK_ICON_SIZE_DIALOG);
3408 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3411 label = gtk_label_new (_("This room is protected by a password:"));
3412 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3414 /* Add password entry */
3415 entry = gtk_entry_new ();
3416 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3417 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3419 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3420 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3421 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3422 GTK_ENTRY_ICON_SECONDARY, FALSE);
3424 g_signal_connect (entry, "icon-release",
3425 G_CALLBACK (clear_icon_released_cb), data);
3426 g_signal_connect (entry, "changed",
3427 G_CALLBACK (password_entry_changed_cb), data);
3429 g_signal_connect (entry, "activate",
3430 G_CALLBACK (password_entry_activate_cb), data);
3432 /* Focus the password entry once it's realized */
3433 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3435 /* Add 'Join' button */
3436 alig = gtk_alignment_new (0, 0.5, 1, 0);
3438 button = gtk_button_new_with_label (_("Join"));
3439 gtk_container_add (GTK_CONTAINER (alig), button);
3440 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3442 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3446 spinner = gtk_spinner_new ();
3447 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3449 /* Save some data for messing around with later */
3451 data->info_bar = info_bar;
3452 data->button = button;
3453 data->label = label;
3454 data->entry = entry;
3455 data->spinner = spinner;
3457 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3459 gtk_widget_show_all (hbox);
3461 data->response_id = g_signal_connect (info_bar, "response",
3462 G_CALLBACK (password_infobar_response_cb), data);
3464 gtk_widget_show_all (info_bar);
3465 /* ... but hide the spinner */
3466 gtk_widget_hide (spinner);
3470 provide_saved_password_cb (GObject *tp_chat,
3474 EmpathyChat *self = user_data;
3475 EmpathyChatPriv *priv = GET_PRIV (self);
3476 GError *error = NULL;
3478 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3480 DEBUG ("error: %s", error->message);
3481 /* FIXME: what should we do if that's another error? Close the channel?
3482 * Display the raw D-Bus error to the user isn't very useful */
3483 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3484 display_password_info_bar (self);
3485 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3487 g_error_free (error);
3492 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3493 gtk_widget_grab_focus (self->input_text_view);
3497 chat_room_got_password_cb (GObject *source,
3498 GAsyncResult *result,
3501 EmpathyChat *self = user_data;
3502 EmpathyChatPriv *priv = GET_PRIV (self);
3503 const gchar *password;
3504 GError *error = NULL;
3506 password = empathy_keyring_get_room_password_finish (priv->account,
3509 if (error != NULL) {
3510 DEBUG ("Couldn't get room password: %s\n", error->message);
3511 g_clear_error (&error);
3513 display_password_info_bar (self);
3514 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3518 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3519 provide_saved_password_cb, self);
3523 chat_password_needed_changed_cb (EmpathyChat *self)
3525 EmpathyChatPriv *priv = GET_PRIV (self);
3527 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3528 empathy_keyring_get_room_password_async (priv->account,
3529 empathy_tp_chat_get_id (priv->tp_chat),
3530 chat_room_got_password_cb, self);
3535 chat_sms_channel_changed_cb (EmpathyChat *self)
3537 EmpathyChatPriv *priv = GET_PRIV (self);
3539 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3540 g_object_notify (G_OBJECT (self), "sms-channel");
3544 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3546 g_object_notify (G_OBJECT (self), "n-messages-sending");
3550 empathy_chat_set_tp_chat (EmpathyChat *chat,
3551 EmpathyTpChat *tp_chat)
3553 EmpathyChatPriv *priv = GET_PRIV (chat);
3554 GPtrArray *properties;
3556 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3557 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3558 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3560 if (priv->tp_chat) {
3564 if (priv->account) {
3565 g_object_unref (priv->account);
3568 priv->tp_chat = g_object_ref (tp_chat);
3569 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3571 g_signal_connect (tp_chat, "destroy",
3572 G_CALLBACK (chat_destroy_cb),
3574 g_signal_connect (tp_chat, "message-received",
3575 G_CALLBACK (chat_message_received_cb),
3577 g_signal_connect (tp_chat, "pending-message-removed",
3578 G_CALLBACK (chat_pending_message_removed_cb),
3580 g_signal_connect (tp_chat, "send-error",
3581 G_CALLBACK (chat_send_error_cb),
3583 g_signal_connect (tp_chat, "chat-state-changed",
3584 G_CALLBACK (chat_state_changed_cb),
3586 g_signal_connect (tp_chat, "property-changed",
3587 G_CALLBACK (chat_property_changed_cb),
3589 g_signal_connect (tp_chat, "members-changed",
3590 G_CALLBACK (chat_members_changed_cb),
3592 g_signal_connect (tp_chat, "member-renamed",
3593 G_CALLBACK (chat_member_renamed_cb),
3595 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3596 G_CALLBACK (chat_remote_contact_changed_cb),
3598 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3599 G_CALLBACK (chat_password_needed_changed_cb),
3601 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3602 G_CALLBACK (chat_sms_channel_changed_cb),
3604 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3605 G_CALLBACK (chat_n_messages_sending_changed_cb),
3608 /* Get initial value of properties */
3609 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3610 if (properties != NULL) {
3613 for (i = 0; i < properties->len; i++) {
3614 EmpathyTpChatProperty *property;
3616 property = g_ptr_array_index (properties, i);
3617 if (property->value == NULL)
3620 chat_property_changed_cb (priv->tp_chat,
3627 chat_sms_channel_changed_cb (chat);
3628 chat_remote_contact_changed_cb (chat);
3630 if (chat->input_text_view) {
3631 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3632 if (priv->block_events_timeout_id == 0) {
3633 empathy_chat_view_append_event (chat->view, _("Connected"));
3637 g_object_notify (G_OBJECT (chat), "tp-chat");
3638 g_object_notify (G_OBJECT (chat), "id");
3639 g_object_notify (G_OBJECT (chat), "account");
3641 /* This is a noop when tp-chat is set at object construction time and causes
3642 * the pending messages to be show when it's set on the object after it has
3644 show_pending_messages (chat);
3646 /* check if a password is needed */
3647 chat_password_needed_changed_cb (chat);
3651 empathy_chat_get_account (EmpathyChat *chat)
3653 EmpathyChatPriv *priv = GET_PRIV (chat);
3655 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3657 return priv->account;
3661 empathy_chat_get_id (EmpathyChat *chat)
3663 EmpathyChatPriv *priv = GET_PRIV (chat);
3665 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3671 empathy_chat_dup_name (EmpathyChat *chat)
3673 EmpathyChatPriv *priv = GET_PRIV (chat);
3676 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3680 if (!ret && priv->remote_contact) {
3681 ret = empathy_contact_get_alias (priv->remote_contact);
3688 ret = _("Conversation");
3690 if (priv->sms_channel)
3691 /* Translators: this string is a something like
3692 * "Escher Cat (SMS)" */
3693 return g_strdup_printf (_("%s (SMS)"), ret);
3695 return g_strdup (ret);
3699 empathy_chat_get_subject (EmpathyChat *chat)
3701 EmpathyChatPriv *priv = GET_PRIV (chat);
3703 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3705 return priv->subject;
3709 empathy_chat_get_remote_contact (EmpathyChat *chat)
3711 EmpathyChatPriv *priv = GET_PRIV (chat);
3713 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3715 return priv->remote_contact;
3719 empathy_chat_get_contact_menu (EmpathyChat *chat)
3721 EmpathyChatPriv *priv = GET_PRIV (chat);
3722 GtkWidget *menu = NULL;
3724 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3726 if (priv->remote_contact) {
3727 menu = empathy_contact_menu_new (priv->remote_contact,
3728 EMPATHY_CONTACT_FEATURE_CALL |
3729 EMPATHY_CONTACT_FEATURE_LOG |
3730 EMPATHY_CONTACT_FEATURE_INFO |
3731 EMPATHY_CONTACT_FEATURE_BLOCK);
3738 empathy_chat_clear (EmpathyChat *chat)
3740 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3742 empathy_chat_view_clear (chat->view);
3746 empathy_chat_scroll_down (EmpathyChat *chat)
3748 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3750 empathy_chat_view_scroll_down (chat->view);
3754 empathy_chat_cut (EmpathyChat *chat)
3756 GtkTextBuffer *buffer;
3758 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3760 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3761 if (gtk_text_buffer_get_has_selection (buffer)) {
3762 GtkClipboard *clipboard;
3764 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3766 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3771 empathy_chat_copy (EmpathyChat *chat)
3773 GtkTextBuffer *buffer;
3775 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3777 if (empathy_chat_view_get_has_selection (chat->view)) {
3778 empathy_chat_view_copy_clipboard (chat->view);
3782 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3783 if (gtk_text_buffer_get_has_selection (buffer)) {
3784 GtkClipboard *clipboard;
3786 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3788 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3793 EmpathyChatPriv *priv = GET_PRIV (chat);
3795 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3802 GtkClipboard *clipboard;
3804 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3805 start = g_utf8_offset_to_pointer (topic, start_offset);
3806 end = g_utf8_offset_to_pointer (topic, end_offset);
3807 selection = g_strndup (start, end - start);
3809 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3810 gtk_clipboard_set_text (clipboard, selection, -1);
3818 empathy_chat_paste (EmpathyChat *chat)
3820 GtkTextBuffer *buffer;
3821 GtkClipboard *clipboard;
3822 EmpathyChatPriv *priv;
3824 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3826 priv = GET_PRIV (chat);
3828 if (gtk_widget_get_visible (priv->search_bar)) {
3829 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3833 if (priv->tp_chat == NULL ||
3834 !gtk_widget_is_sensitive (chat->input_text_view))
3837 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3838 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3840 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3844 empathy_chat_find (EmpathyChat *chat)
3846 EmpathyChatPriv *priv;
3848 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3850 priv = GET_PRIV (chat);
3852 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3856 empathy_chat_correct_word (EmpathyChat *chat,
3859 const gchar *new_word)
3861 GtkTextBuffer *buffer;
3863 g_return_if_fail (chat != NULL);
3864 g_return_if_fail (new_word != NULL);
3866 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3868 gtk_text_buffer_delete (buffer, start, end);
3869 gtk_text_buffer_insert (buffer, start,
3875 empathy_chat_is_room (EmpathyChat *chat)
3877 EmpathyChatPriv *priv = GET_PRIV (chat);
3879 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3881 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3885 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3887 EmpathyChatPriv *priv = GET_PRIV (self);
3889 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3891 return priv->unread_messages;
3894 /* called when the messages have been read by user */
3896 empathy_chat_messages_read (EmpathyChat *self)
3898 EmpathyChatPriv *priv = GET_PRIV (self);
3900 g_return_if_fail (EMPATHY_IS_CHAT (self));
3902 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3903 * definition. If we are still retrieving the backlogs, do not ACK */
3904 if (priv->retrieving_backlogs)
3907 if (priv->tp_chat != NULL ) {
3908 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3911 empathy_chat_view_focus_toggled (self->view, TRUE);
3914 /* Return TRUE if on of the contacts in this chat is composing */
3916 empathy_chat_is_composing (EmpathyChat *chat)
3918 return chat->priv->compositors != NULL;
3922 empathy_chat_is_sms_channel (EmpathyChat *self)
3924 EmpathyChatPriv *priv = GET_PRIV (self);
3926 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3928 return priv->sms_channel;
3932 empathy_chat_get_n_messages_sending (EmpathyChat *self)
3934 EmpathyChatPriv *priv;
3936 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3938 priv = GET_PRIV (self);
3940 if (priv->tp_chat == NULL) {
3945 g_object_get (priv->tp_chat,
3946 "n-messages-sending", &n_messages,