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;
152 gchar *text; /* Original message that was specified
153 * upon entry creation. */
154 gchar *modified_text; /* Message that was modified by user.
155 * When no modifications were made, it is NULL */
175 static guint signals[LAST_SIGNAL] = { 0 };
177 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
179 static gboolean update_misspelled_words (gpointer data);
182 chat_get_property (GObject *object,
187 EmpathyChat *chat = EMPATHY_CHAT (object);
188 EmpathyChatPriv *priv = GET_PRIV (object);
192 g_value_set_object (value, priv->tp_chat);
195 g_value_set_object (value, priv->account);
198 g_value_set_string (value, empathy_chat_get_name (chat));
201 g_value_set_string (value, priv->id);
204 g_value_set_string (value, priv->subject);
206 case PROP_REMOTE_CONTACT:
207 g_value_set_object (value, priv->remote_contact);
209 case PROP_SHOW_CONTACTS:
210 g_value_set_boolean (value, priv->show_contacts);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
219 chat_set_property (GObject *object,
224 EmpathyChat *chat = EMPATHY_CHAT (object);
228 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
230 case PROP_SHOW_CONTACTS:
231 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
240 account_reconnected (EmpathyChat *chat,
243 EmpathyChatPriv *priv = GET_PRIV (chat);
245 DEBUG ("Account reconnected, request a new Text channel");
247 /* FIXME: Ideally we should ask to handle ourself the channel so we can
248 * report the error if any but this is blocked by
249 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
250 switch (priv->handle_type) {
251 case TP_HANDLE_TYPE_CONTACT:
252 empathy_chat_with_contact_id (
253 account, priv->id, TP_USER_ACTION_TIME_NOT_USER_ACTION);
255 case TP_HANDLE_TYPE_ROOM:
256 empathy_join_muc (account, priv->id,
257 TP_USER_ACTION_TIME_NOT_USER_ACTION);
259 case TP_HANDLE_TYPE_NONE:
260 case TP_HANDLE_TYPE_LIST:
261 case TP_HANDLE_TYPE_GROUP:
263 g_assert_not_reached ();
267 g_object_unref (chat);
271 chat_new_connection_cb (TpAccount *account,
275 gchar *dbus_error_name,
279 EmpathyChatPriv *priv = GET_PRIV (chat);
281 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
284 if (priv->tp_chat != NULL || account != priv->account ||
285 priv->handle_type == TP_HANDLE_TYPE_NONE ||
286 EMP_STR_EMPTY (priv->id))
291 account_reconnected (chat, account);
295 chat_composing_remove_timeout (EmpathyChat *chat)
297 EmpathyChatPriv *priv;
299 priv = GET_PRIV (chat);
301 if (priv->composing_stop_timeout_id) {
302 g_source_remove (priv->composing_stop_timeout_id);
303 priv->composing_stop_timeout_id = 0;
308 chat_composing_stop_timeout_cb (EmpathyChat *chat)
310 EmpathyChatPriv *priv;
312 priv = GET_PRIV (chat);
314 priv->composing_stop_timeout_id = 0;
315 empathy_tp_chat_set_state (priv->tp_chat,
316 TP_CHANNEL_CHAT_STATE_PAUSED);
322 chat_composing_start (EmpathyChat *chat)
324 EmpathyChatPriv *priv;
326 priv = GET_PRIV (chat);
328 if (priv->composing_stop_timeout_id) {
329 /* Just restart the timeout */
330 chat_composing_remove_timeout (chat);
332 empathy_tp_chat_set_state (priv->tp_chat,
333 TP_CHANNEL_CHAT_STATE_COMPOSING);
336 priv->composing_stop_timeout_id = g_timeout_add_seconds (
337 COMPOSING_STOP_TIMEOUT,
338 (GSourceFunc) chat_composing_stop_timeout_cb,
343 chat_composing_stop (EmpathyChat *chat)
345 EmpathyChatPriv *priv;
347 priv = GET_PRIV (chat);
349 chat_composing_remove_timeout (chat);
350 empathy_tp_chat_set_state (priv->tp_chat,
351 TP_CHANNEL_CHAT_STATE_ACTIVE);
355 chat_input_history_entry_cmp (InputHistoryEntry *entry,
358 if (!tp_strdiff (entry->text, text)) {
359 if (entry->modified_text != NULL) {
360 /* Modified entry and single string cannot be equal. */
368 static InputHistoryEntry *
369 chat_input_history_entry_new_with_text (const gchar *text)
371 InputHistoryEntry *entry;
372 entry = g_slice_new0 (InputHistoryEntry);
373 entry->text = g_strdup (text);
379 chat_input_history_entry_free (InputHistoryEntry *entry)
381 g_free (entry->text);
382 g_free (entry->modified_text);
383 g_slice_free (InputHistoryEntry, entry);
387 chat_input_history_entry_revert (InputHistoryEntry *entry)
389 g_free (entry->modified_text);
390 entry->modified_text = NULL;
394 chat_input_history_entry_update_text (InputHistoryEntry *entry,
399 if (!tp_strdiff (text, entry->text)) {
400 g_free (entry->modified_text);
401 entry->modified_text = NULL;
405 old = entry->modified_text;
406 entry->modified_text = g_strdup (text);
411 chat_input_history_entry_get_text (InputHistoryEntry *entry)
417 if (entry->modified_text != NULL) {
418 return entry->modified_text;
424 chat_input_history_remove_item (GList *list,
427 list = g_list_remove_link (list, item);
428 chat_input_history_entry_free (item->data);
429 g_list_free_1 (item);
434 chat_input_history_revert (EmpathyChat *chat)
436 EmpathyChatPriv *priv;
440 InputHistoryEntry *entry;
442 priv = GET_PRIV (chat);
443 list = priv->input_history;
446 DEBUG ("No input history");
450 /* Delete temporary entry */
451 if (priv->input_history_current != NULL) {
453 list = chat_input_history_remove_item (list, item1);
454 if (priv->input_history_current == item1) {
455 /* Removed temporary entry was current entry */
456 priv->input_history = list;
457 priv->input_history_current = NULL;
462 /* There is no entry to revert */
466 /* Restore the current history entry to original value */
467 item1 = priv->input_history_current;
469 chat_input_history_entry_revert (entry);
471 /* Remove restored entry if there is other occurance before this entry */
472 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
473 (GCompareFunc) chat_input_history_entry_cmp);
474 if (item2 != item1) {
475 list = chat_input_history_remove_item (list, item1);
478 /* Remove other occurance of the restored entry */
479 item2 = g_list_find_custom (item1->next,
480 chat_input_history_entry_get_text (entry),
481 (GCompareFunc) chat_input_history_entry_cmp);
483 list = chat_input_history_remove_item (list, item2);
487 priv->input_history_current = NULL;
488 priv->input_history = list;
492 chat_input_history_add (EmpathyChat *chat,
496 EmpathyChatPriv *priv;
499 InputHistoryEntry *entry;
501 priv = GET_PRIV (chat);
503 list = priv->input_history;
505 /* Remove any other occurances of this entry, if not temporary */
507 while ((item = g_list_find_custom (list, str,
508 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
509 list = chat_input_history_remove_item (list, item);
512 /* Trim the list to the last 10 items */
513 while (g_list_length (list) > 10) {
514 item = g_list_last (list);
516 list = chat_input_history_remove_item (list, item);
524 entry = chat_input_history_entry_new_with_text (str);
525 list = g_list_prepend (list, entry);
527 /* Set the list and the current item pointer */
528 priv->input_history = list;
530 priv->input_history_current = list;
533 priv->input_history_current = NULL;
538 chat_input_history_get_next (EmpathyChat *chat)
540 EmpathyChatPriv *priv;
544 priv = GET_PRIV (chat);
546 if (priv->input_history == NULL) {
547 DEBUG ("No input history, next entry is NULL");
550 g_assert (priv->input_history_current != NULL);
552 if ((item = g_list_next (priv->input_history_current)) == NULL)
554 item = priv->input_history_current;
557 msg = chat_input_history_entry_get_text (item->data);
559 DEBUG ("Returning next entry: '%s'", msg);
561 priv->input_history_current = item;
567 chat_input_history_get_prev (EmpathyChat *chat)
569 EmpathyChatPriv *priv;
573 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
575 priv = GET_PRIV (chat);
577 if (priv->input_history == NULL) {
578 DEBUG ("No input history, previous entry is NULL");
582 if (priv->input_history_current == NULL)
586 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
588 item = priv->input_history_current;
591 msg = chat_input_history_entry_get_text (item->data);
593 DEBUG ("Returning previous entry: '%s'", msg);
595 priv->input_history_current = item;
601 chat_input_history_update (EmpathyChat *chat,
602 GtkTextBuffer *buffer)
604 EmpathyChatPriv *priv;
605 GtkTextIter start, end;
607 InputHistoryEntry *entry;
609 priv = GET_PRIV (chat);
611 gtk_text_buffer_get_bounds (buffer, &start, &end);
612 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
614 if (priv->input_history_current == NULL) {
615 /* Add the current text temporarily to the history */
616 chat_input_history_add (chat, text, TRUE);
621 /* Save the changes in the history */
622 entry = priv->input_history_current->data;
623 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
624 chat_input_history_entry_update_text (entry, text);
633 } ChatCommandMsgData;
636 chat_command_msg_cb (GObject *source,
637 GAsyncResult *result,
640 ChatCommandMsgData *data = user_data;
641 GError *error = NULL;
644 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
645 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
647 if (channel == NULL) {
648 DEBUG ("Failed to get channel: %s", error->message);
649 g_error_free (error);
651 empathy_chat_view_append_event (data->chat->view,
652 _("Failed to open private chat"));
656 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
657 TpTextChannel *text = (TpTextChannel *) channel;
660 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
663 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
665 g_object_unref (msg);
668 g_object_unref (channel);
671 g_free (data->message);
672 g_slice_free (ChatCommandMsgData, data);
676 nick_command_supported (EmpathyChat *chat)
678 EmpathyChatPriv * priv = GET_PRIV (chat);
679 TpConnection *connection;
681 connection = empathy_tp_chat_get_connection (priv->tp_chat);
682 return tp_proxy_has_interface_by_id (connection,
683 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
688 part_command_supported (EmpathyChat *chat)
690 EmpathyChatPriv * priv = GET_PRIV (chat);
693 channel = empathy_tp_chat_get_channel (priv->tp_chat);
694 return tp_proxy_has_interface_by_id (channel,
695 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
700 chat_command_clear (EmpathyChat *chat,
703 empathy_chat_view_clear (chat->view);
707 chat_command_topic (EmpathyChat *chat,
710 EmpathyChatPriv *priv = GET_PRIV (chat);
711 EmpathyTpChatProperty *property;
712 GValue value = {0, };
714 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
715 if (property == NULL) {
716 empathy_chat_view_append_event (chat->view,
717 _("Topic not supported on this conversation"));
721 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
722 empathy_chat_view_append_event (chat->view,
723 _("You are not allowed to change the topic"));
727 g_value_init (&value, G_TYPE_STRING);
728 g_value_set_string (&value, strv[1]);
729 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
730 g_value_unset (&value);
734 empathy_chat_join_muc (EmpathyChat *chat,
737 EmpathyChatPriv *priv = GET_PRIV (chat);
739 empathy_join_muc (priv->account, room,
740 gtk_get_current_event_time ());
744 chat_command_join (EmpathyChat *chat,
749 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
751 /* FIXME: Ideally we should ask to handle ourself the channel so we can
752 * report the error if any but this is blocked by
753 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
754 while (rooms[i] != NULL) {
755 /* ignore empty strings */
756 if (!EMP_STR_EMPTY (rooms[i])) {
757 empathy_chat_join_muc (chat, rooms[i]);
765 empathy_chat_leave_chat (EmpathyChat *chat)
767 EmpathyChatPriv *priv = GET_PRIV (chat);
769 empathy_tp_chat_leave (priv->tp_chat, "");
774 chat_command_part (EmpathyChat *chat,
777 EmpathyChatPriv *priv = GET_PRIV (chat);
778 EmpathyChat *chat_to_be_parted;
780 if (strv[1] == NULL) {
781 empathy_tp_chat_leave (priv->tp_chat, "");
784 chat_to_be_parted = empathy_chat_window_find_chat (priv->account, strv[1]);
786 if (chat_to_be_parted != NULL) {
787 empathy_tp_chat_leave (empathy_chat_get_tp_chat (chat_to_be_parted), strv[2]);
789 empathy_tp_chat_leave (priv->tp_chat, strv[1]);
795 chat_command_msg_internal (EmpathyChat *chat,
796 const gchar *contact_id,
797 const gchar *message)
799 EmpathyChatPriv *priv = GET_PRIV (chat);
800 ChatCommandMsgData *data;
801 TpAccountChannelRequest *req;
804 request = tp_asv_new (
805 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
806 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
807 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
810 req = tp_account_channel_request_new (priv->account, request,
811 tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
813 /* FIXME: We should probably search in members alias. But this
814 * is enough for IRC */
815 data = g_slice_new (ChatCommandMsgData);
817 data->message = g_strdup (message);
819 tp_account_channel_request_ensure_and_observe_channel_async (req,
820 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
822 g_object_unref (req);
823 g_hash_table_unref (request);
827 chat_command_query (EmpathyChat *chat,
830 /* If <message> part is not defined,
831 * strv[2] will be the terminal NULL */
832 chat_command_msg_internal (chat, strv[1], strv[2]);
836 chat_command_msg (EmpathyChat *chat,
839 chat_command_msg_internal (chat, strv[1], strv[2]);
843 callback_for_request_rename (TpProxy *proxy,
846 GObject *weak_object)
849 DEBUG ("Call to RequestRename method failed: %s",error->message);
854 chat_command_nick (EmpathyChat *chat,
857 EmpathyChatPriv *priv = GET_PRIV (chat);
860 proxy = TP_PROXY (tp_account_get_connection (priv->account));
862 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
863 strv[1], callback_for_request_rename, NULL, NULL, NULL);
867 chat_command_me (EmpathyChat *chat,
870 EmpathyChatPriv *priv = GET_PRIV (chat);
874 channel = empathy_tp_chat_get_channel (priv->tp_chat);
876 /* Strictly speaking we don't depend yet on Messages so best to check that
877 * the channel is actually a TpTextChannel before casting it. */
878 if (TP_IS_TEXT_CHANNEL (channel) &&
879 !tp_text_channel_supports_message_type (TP_TEXT_CHANNEL (channel),
880 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
881 /* Action message are not supported, 'simulate' the action */
882 EmpathyContact *self_contact;
885 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
886 /* The TpChat can't be ready if it doesn't have the self contact */
887 g_assert (self_contact != NULL);
889 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
891 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
896 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
900 empathy_tp_chat_send (priv->tp_chat, message);
901 g_object_unref (message);
905 chat_command_say (EmpathyChat *chat,
908 EmpathyChatPriv *priv = GET_PRIV (chat);
911 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
913 empathy_tp_chat_send (priv->tp_chat, message);
914 g_object_unref (message);
917 static void chat_command_help (EmpathyChat *chat, GStrv strv);
919 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
925 ChatCommandFunc func;
926 gboolean (*is_supported)(EmpathyChat *chat);
930 static ChatCommandItem commands[] = {
931 {"clear", 1, 1, chat_command_clear, NULL,
932 N_("/clear: clear all messages from the current conversation")},
934 {"topic", 2, 2, chat_command_topic, NULL,
935 N_("/topic <topic>: set the topic of the current conversation")},
937 {"join", 2, 2, chat_command_join, NULL,
938 N_("/join <chat room ID>: join a new chat room")},
940 {"j", 2, 2, chat_command_join, NULL,
941 N_("/j <chat room ID>: join a new chat room")},
944 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
945 {"part", 1, 3, chat_command_part, part_command_supported,
946 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
947 "by default the current one")},
950 {"query", 2, 3, chat_command_query, NULL,
951 N_("/query <contact ID> [<message>]: open a private chat")},
953 {"msg", 3, 3, chat_command_msg, NULL,
954 N_("/msg <contact ID> <message>: open a private chat")},
956 {"nick", 2, 2, chat_command_nick, nick_command_supported,
957 N_("/nick <nickname>: change your nickname on the current server")},
959 {"me", 2, 2, chat_command_me, NULL,
960 N_("/me <message>: send an ACTION message to the current conversation")},
962 {"say", 2, 2, chat_command_say, NULL,
963 N_("/say <message>: send <message> to the current conversation. "
964 "This is used to send a message starting with a '/'. For example: "
965 "\"/say /join is used to join a new chat room\"")},
967 {"help", 1, 2, chat_command_help, NULL,
968 N_("/help [<command>]: show all supported commands. "
969 "If <command> is defined, show its usage.")},
973 chat_command_show_help (EmpathyChat *chat,
974 ChatCommandItem *item)
978 str = g_strdup_printf (_("Usage: %s"), _(item->help));
979 empathy_chat_view_append_event (chat->view, str);
984 chat_command_help (EmpathyChat *chat,
989 /* If <command> part is not defined,
990 * strv[1] will be the terminal NULL */
991 if (strv[1] == NULL) {
992 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
993 if (commands[i].is_supported != NULL) {
994 if (!commands[i].is_supported (chat)) {
998 empathy_chat_view_append_event (chat->view,
999 _(commands[i].help));
1004 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1005 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1006 if (commands[i].is_supported != NULL) {
1007 if (!commands[i].is_supported (chat)) {
1011 chat_command_show_help (chat, &commands[i]);
1016 empathy_chat_view_append_event (chat->view,
1017 _("Unknown command"));
1021 chat_command_parse (const gchar *text, guint max_parts)
1026 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1028 array = g_ptr_array_sized_new (max_parts + 1);
1029 while (max_parts > 1) {
1032 /* Skip white spaces */
1033 while (g_ascii_isspace (*text)) {
1037 /* Search the end of this part, until first space. */
1038 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1044 item = g_strndup (text, end - text);
1045 g_ptr_array_add (array, item);
1046 DEBUG ("\tITEM: \"%s\"", item);
1052 /* Append last part if not empty */
1053 item = g_strstrip (g_strdup (text));
1054 if (!EMP_STR_EMPTY (item)) {
1055 g_ptr_array_add (array, item);
1056 DEBUG ("\tITEM: \"%s\"", item);
1061 /* Make the array NULL-terminated */
1062 g_ptr_array_add (array, NULL);
1064 return (GStrv) g_ptr_array_free (array, FALSE);
1068 has_prefix_case (const gchar *s,
1069 const gchar *prefix)
1071 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1075 chat_send (EmpathyChat *chat,
1078 EmpathyChatPriv *priv;
1082 if (EMP_STR_EMPTY (msg)) {
1086 priv = GET_PRIV (chat);
1088 chat_input_history_add (chat, msg, FALSE);
1090 if (msg[0] == '/') {
1091 gboolean second_slash = FALSE;
1092 const gchar *iter = msg + 1;
1094 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1099 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1102 c = *(msg + 1 + strlen (commands[i].prefix));
1103 if (c != '\0' && !g_ascii_isspace (c)) {
1106 if (commands[i].is_supported != NULL) {
1107 if (!commands[i].is_supported (chat)) {
1112 /* We can't use g_strsplit here because it does
1113 * not deal correctly if we have more than one space
1115 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1117 strv_len = g_strv_length (strv);
1118 if (strv_len < commands[i].min_parts ||
1119 strv_len > commands[i].max_parts) {
1120 chat_command_show_help (chat, &commands[i]);
1125 commands[i].func (chat, strv);
1130 /* Also allow messages with two slashes before the
1131 * first space, so it is possible to send a /unix/path.
1132 * This heuristic is kind of crap. */
1133 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1135 second_slash = TRUE;
1141 if (!second_slash) {
1142 empathy_chat_view_append_event (chat->view,
1143 _("Unknown command; see /help for the available"
1149 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1151 empathy_tp_chat_send (priv->tp_chat, message);
1152 g_object_unref (message);
1156 chat_input_text_view_send (EmpathyChat *chat)
1158 GtkTextBuffer *buffer;
1159 GtkTextIter start, end;
1162 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1164 gtk_text_buffer_get_bounds (buffer, &start, &end);
1165 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1167 /* clear the input field */
1168 gtk_text_buffer_set_text (buffer, "", -1);
1169 /* delete input history modifications */
1170 chat_input_history_revert (chat);
1172 chat_send (chat, msg);
1177 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1178 EmpathyContact *contact,
1179 TpChannelChatState state,
1182 EmpathyChatPriv *priv;
1184 gboolean was_composing;
1186 priv = GET_PRIV (chat);
1188 if (empathy_contact_is_user (contact)) {
1189 /* We don't care about our own chat state */
1193 was_composing = (priv->compositors != NULL);
1195 /* Find the contact in the list. After that l is the list elem or NULL */
1196 for (l = priv->compositors; l; l = l->next) {
1197 if (contact == l->data) {
1203 case TP_CHANNEL_CHAT_STATE_GONE:
1204 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1205 case TP_CHANNEL_CHAT_STATE_PAUSED:
1206 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1207 /* Contact is not composing */
1209 priv->compositors = g_list_remove_link (priv->compositors, l);
1210 g_object_unref (l->data);
1214 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1215 /* Contact is composing */
1217 priv->compositors = g_list_prepend (priv->compositors,
1218 g_object_ref (contact));
1222 g_assert_not_reached ();
1225 DEBUG ("Was composing: %s now composing: %s",
1226 was_composing ? "yes" : "no",
1227 priv->compositors ? "yes" : "no");
1229 if ((was_composing && !priv->compositors) ||
1230 (!was_composing && priv->compositors)) {
1231 /* Composing state changed */
1232 g_signal_emit (chat, signals[COMPOSING], 0,
1233 priv->compositors != NULL);
1238 chat_message_received (EmpathyChat *chat,
1239 EmpathyMessage *message,
1242 EmpathyChatPriv *priv = GET_PRIV (chat);
1243 EmpathyContact *sender;
1245 sender = empathy_message_get_sender (message);
1247 DEBUG ("Appending new message from %s (%d)",
1248 empathy_contact_get_alias (sender),
1249 empathy_contact_get_handle (sender));
1251 empathy_chat_view_append_message (chat->view, message);
1253 /* We received a message so the contact is no longer composing */
1254 chat_state_changed_cb (priv->tp_chat, sender,
1255 TP_CHANNEL_CHAT_STATE_ACTIVE,
1258 priv->unread_messages++;
1259 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1263 chat_message_received_cb (EmpathyTpChat *tp_chat,
1264 EmpathyMessage *message,
1267 chat_message_received (chat, message, FALSE);
1271 chat_send_error_cb (EmpathyTpChat *tp_chat,
1272 const gchar *message_body,
1273 TpChannelTextSendError error_code,
1279 switch (error_code) {
1280 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1281 error = _("offline");
1283 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1284 error = _("invalid contact");
1286 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1287 error = _("permission denied");
1289 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1290 error = _("too long message");
1292 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1293 error = _("not implemented");
1295 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1297 error = _("unknown");
1301 if (message_body != NULL) {
1302 str = g_strdup_printf (_("Error sending message '%s': %s"),
1303 message_body, error);
1306 str = g_strdup_printf (_("Error sending message: %s"), error);
1309 empathy_chat_view_append_event (chat->view, str);
1314 chat_topic_label_size_allocate_cb (GtkLabel *label,
1315 GtkAllocation *allocation,
1318 EmpathyChatPriv *priv = GET_PRIV (chat);
1320 if (!gtk_label_get_line_wrap (label)) {
1321 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1322 gtk_widget_show (priv->expander_topic);
1324 gtk_widget_hide (priv->expander_topic);
1331 chat_topic_expander_activate_cb (GtkExpander *expander,
1332 GParamSpec *param_spec,
1335 EmpathyChatPriv *priv = GET_PRIV (chat);
1337 if (gtk_expander_get_expanded (expander)) {
1338 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1339 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1341 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1342 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1347 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1352 EmpathyChatPriv *priv = GET_PRIV (chat);
1354 if (!tp_strdiff (name, "subject")) {
1355 g_free (priv->subject);
1356 priv->subject = g_value_dup_string (value);
1357 g_object_notify (G_OBJECT (chat), "subject");
1359 if (EMP_STR_EMPTY (priv->subject)) {
1360 gtk_widget_hide (priv->hbox_topic);
1362 gchar *markup_topic;
1365 markup_topic = empathy_add_link_markup (priv->subject);
1366 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1367 _("Topic:"), markup_topic);
1369 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1370 g_free (markup_text);
1371 g_free (markup_topic);
1373 gtk_widget_show (priv->hbox_topic);
1375 if (priv->block_events_timeout_id == 0) {
1378 if (!EMP_STR_EMPTY (priv->subject)) {
1379 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1381 str = g_strdup (_("No topic defined"));
1383 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1387 else if (!tp_strdiff (name, "name")) {
1388 g_free (priv->name);
1389 priv->name = g_value_dup_string (value);
1390 g_object_notify (G_OBJECT (chat), "name");
1395 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1399 GtkTextIter word_start = *iter;
1400 GtkTextIter word_end = *iter;
1403 if (gtk_text_iter_inside_word (&word_end) &&
1404 !gtk_text_iter_ends_word (&word_end)) {
1405 gtk_text_iter_forward_word_end (&word_end);
1410 if (gtk_text_iter_get_char (&tmp) == '\'') {
1411 gtk_text_iter_forward_char (&tmp);
1413 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1414 gtk_text_iter_forward_word_end (&word_end);
1419 if (gtk_text_iter_inside_word (&word_start) ||
1420 gtk_text_iter_ends_word (&word_start)) {
1421 if (!gtk_text_iter_starts_word (&word_start) ||
1422 gtk_text_iter_equal (&word_start, &word_end)) {
1423 gtk_text_iter_backward_word_start (&word_start);
1427 gtk_text_iter_backward_char (&tmp);
1429 if (gtk_text_iter_get_char (&tmp) == '\'') {
1430 gtk_text_iter_backward_char (&tmp);
1432 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1433 gtk_text_iter_backward_word_start (&word_start);
1438 *start = word_start;
1444 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1445 GtkTextIter *location,
1450 GtkTextIter iter, pos;
1452 /* Remove all misspelled tags in the inserted text.
1453 * This happens when text is inserted within a misspelled word. */
1454 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1455 gtk_text_iter_get_offset (location) - len);
1456 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1459 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1462 GtkTextIter start, end;
1465 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1468 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1470 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1471 gtk_text_iter_equal (&pos, &end) ||
1472 empathy_spell_check (str)) {
1473 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1475 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1480 } while (gtk_text_iter_forward_word_end (&iter) &&
1481 gtk_text_iter_compare (&iter, location) <= 0);
1485 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1490 GtkTextIter word_start, word_end;
1492 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1493 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1494 &word_start, &word_end);
1499 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1502 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1503 chat_composing_stop (chat);
1505 chat_composing_start (chat);
1510 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1515 GtkTextIter prev_pos;
1516 GtkTextIter word_start;
1517 GtkTextIter word_end;
1521 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1523 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1524 gtk_text_buffer_get_insert (buffer));
1525 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1527 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1530 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1531 !gtk_text_iter_equal (&pos, &word_end)) {
1532 str = gtk_text_buffer_get_text (buffer,
1533 &word_start, &word_end, FALSE);
1535 if (!empathy_spell_check (str)) {
1536 gtk_text_buffer_apply_tag_by_name (buffer,
1537 "misspelled", &word_start, &word_end);
1539 gtk_text_buffer_remove_tag_by_name (buffer,
1540 "misspelled", &word_start, &word_end);
1547 gtk_text_buffer_move_mark (buffer, mark, &pos);
1551 empathy_isspace_cb (gunichar c,
1554 return g_unichar_isspace (c);
1558 chat_input_key_press_event_cb (GtkWidget *widget,
1562 EmpathyChatPriv *priv;
1565 GtkWidget *text_view_sw;
1567 priv = GET_PRIV (chat);
1569 /* Catch ctrl+up/down so we can traverse messages we sent */
1570 if ((event->state & GDK_CONTROL_MASK) &&
1571 (event->keyval == GDK_KEY_Up ||
1572 event->keyval == GDK_KEY_Down)) {
1573 GtkTextBuffer *buffer;
1576 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1577 chat_input_history_update (chat, buffer);
1579 if (event->keyval == GDK_KEY_Up) {
1580 str = chat_input_history_get_next (chat);
1582 str = chat_input_history_get_prev (chat);
1585 g_signal_handlers_block_by_func (buffer,
1586 chat_input_text_buffer_changed_cb,
1588 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1589 g_signal_handlers_unblock_by_func (buffer,
1590 chat_input_text_buffer_changed_cb,
1596 /* Catch enter but not ctrl/shift-enter */
1597 if (IS_ENTER (event->keyval) &&
1598 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1601 /* This is to make sure that kinput2 gets the enter. And if
1602 * it's handled there we shouldn't send on it. This is because
1603 * kinput2 uses Enter to commit letters. See:
1604 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1607 view = GTK_TEXT_VIEW (chat->input_text_view);
1608 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1609 gtk_text_view_reset_im_context (view);
1613 chat_input_text_view_send (chat);
1617 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1619 if (IS_ENTER (event->keyval) &&
1620 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1621 /* Newline for shift/control-enter. */
1624 if (!(event->state & GDK_CONTROL_MASK) &&
1625 event->keyval == GDK_KEY_Page_Up) {
1626 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1627 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1630 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1631 event->keyval == GDK_KEY_Page_Down) {
1632 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1633 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1634 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1635 gtk_adjustment_set_value (adj, val);
1638 if (event->keyval == GDK_KEY_Escape) {
1639 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1641 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1642 event->keyval == GDK_KEY_Tab) {
1643 GtkTextBuffer *buffer;
1644 GtkTextIter start, current;
1645 gchar *nick, *completed;
1646 GList *list, *completed_list;
1647 gboolean is_start_of_buffer;
1649 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1650 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1652 /* Get the start of the nick to complete. */
1653 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1654 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1655 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1657 is_start_of_buffer = gtk_text_iter_is_start (&start);
1659 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1660 g_completion_add_items (priv->completion, list);
1662 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1663 completed_list = g_completion_complete (priv->completion,
1672 GString *message = NULL;
1675 gtk_text_buffer_delete (buffer, &start, ¤t);
1677 len = g_list_length (completed_list);
1680 /* If we only have one hit, use that text
1681 * instead of the text in completed since the
1682 * completed text will use the typed string
1683 * which might be cased all wrong.
1686 text = empathy_contact_get_alias (completed_list->data);
1690 /* Print all hits to the scrollback view, so the
1691 * user knows what possibilities he has.
1694 message = g_string_new ("");
1695 for (l = completed_list; l != NULL; l = l->next) {
1696 g_string_append (message, empathy_contact_get_alias (l->data));
1697 g_string_append (message, " - ");
1699 empathy_chat_view_append_event (chat->view, message->str);
1700 g_string_free (message, TRUE);
1703 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1705 if (len == 1 && is_start_of_buffer) {
1706 gchar *complete_char;
1708 complete_char = g_settings_get_string (
1709 priv->gsettings_chat,
1710 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1712 if (complete_char != NULL) {
1713 gtk_text_buffer_insert_at_cursor (buffer,
1715 strlen (complete_char));
1716 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1717 g_free (complete_char);
1724 g_completion_clear_items (priv->completion);
1726 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1736 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1740 gtk_widget_grab_focus (chat->input_text_view);
1746 chat_input_realize_cb (GtkWidget *widget,
1749 DEBUG ("Setting focus to the input text view");
1750 if (gtk_widget_is_sensitive (widget)) {
1751 gtk_widget_grab_focus (widget);
1756 chat_input_has_focus_notify_cb (GtkWidget *widget,
1760 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1764 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1765 EmpathySmiley *smiley,
1768 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1769 GtkTextBuffer *buffer;
1772 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1774 gtk_text_buffer_get_end_iter (buffer, &iter);
1775 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1777 gtk_text_buffer_get_end_iter (buffer, &iter);
1778 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1789 static EmpathyChatSpell *
1790 chat_spell_new (EmpathyChat *chat,
1795 EmpathyChatSpell *chat_spell;
1797 chat_spell = g_slice_new0 (EmpathyChatSpell);
1799 chat_spell->chat = g_object_ref (chat);
1800 chat_spell->word = g_strdup (word);
1801 chat_spell->start = start;
1802 chat_spell->end = end;
1808 chat_spell_free (EmpathyChatSpell *chat_spell)
1810 g_object_unref (chat_spell->chat);
1811 g_free (chat_spell->word);
1812 g_slice_free (EmpathyChatSpell, chat_spell);
1816 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1817 EmpathyChatSpell *chat_spell)
1819 empathy_chat_correct_word (chat_spell->chat,
1820 &(chat_spell->start),
1822 gtk_menu_item_get_label (menu_item));
1827 chat_spelling_build_suggestions_menu (const gchar *code,
1828 EmpathyChatSpell *chat_spell)
1830 GList *suggestions, *l;
1831 GtkWidget *menu, *menu_item;
1833 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1834 if (suggestions == NULL)
1837 menu = gtk_menu_new ();
1838 for (l = suggestions; l; l = l->next) {
1839 menu_item = gtk_menu_item_new_with_label (l->data);
1840 g_signal_connect (G_OBJECT (menu_item), "activate",
1841 G_CALLBACK (chat_spelling_menu_activate_cb),
1843 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1845 empathy_spell_free_suggestions (suggestions);
1847 gtk_widget_show_all (menu);
1853 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1855 GtkWidget *menu, *submenu, *item;
1858 codes = empathy_spell_get_enabled_language_codes ();
1859 g_assert (codes != NULL);
1861 if (g_list_length (codes) > 1) {
1862 menu = gtk_menu_new ();
1864 for (l = codes; l; l = l->next) {
1865 const gchar *code, *name;
1868 name = empathy_spell_get_language_name (code);
1872 item = gtk_image_menu_item_new_with_label (name);
1874 submenu = chat_spelling_build_suggestions_menu (
1876 if (submenu == NULL)
1877 gtk_widget_set_sensitive (item, FALSE);
1879 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1881 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1884 menu = chat_spelling_build_suggestions_menu (codes->data,
1887 menu = gtk_menu_new ();
1888 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1889 gtk_widget_set_sensitive (item, FALSE);
1890 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1893 g_list_free (codes);
1895 gtk_widget_show_all (menu);
1906 static EmpathyChatWord *
1907 chat_word_new (EmpathyChat *chat,
1911 EmpathyChatWord *chat_word;
1913 chat_word = g_slice_new0 (EmpathyChatWord);
1915 chat_word->chat = g_object_ref (chat);
1916 chat_word->word = g_strdup (word);
1917 chat_word->code = g_strdup (code);
1923 chat_word_free (EmpathyChatWord *chat_word)
1925 g_object_unref (chat_word->chat);
1926 g_free (chat_word->word);
1927 g_free (chat_word->code);
1928 g_slice_free (EmpathyChatWord, chat_word);
1932 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1933 EmpathyChatWord *chat_word)
1935 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1937 empathy_spell_add_to_dictionary (chat_word->code,
1939 priv->update_misspelled_words_id =
1940 g_idle_add (update_misspelled_words, chat_word->chat);
1944 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1946 GtkWidget *menu, *item, *lang_item, *image;
1949 const gchar *code, *name;
1950 EmpathyChatWord *chat_word;
1952 codes = empathy_spell_get_enabled_language_codes ();
1953 g_assert (codes != NULL);
1954 if (g_list_length (codes) > 1) {
1955 /* translators: %s is the selected word */
1956 label = g_strdup_printf (_("Add '%s' to Dictionary"),
1958 item = gtk_image_menu_item_new_with_mnemonic (label);
1960 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1961 GTK_ICON_SIZE_MENU);
1962 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1965 menu = gtk_menu_new ();
1967 for (l = codes; l; l = l->next) {
1969 name = empathy_spell_get_language_name (code);
1973 lang_item = gtk_image_menu_item_new_with_label (name);
1975 chat_word= chat_word_new (chat_spell->chat,
1976 chat_spell->word, code);
1977 g_object_set_data_full (G_OBJECT (lang_item),
1978 "chat-word", chat_word,
1979 (GDestroyNotify) chat_word_free);
1981 g_signal_connect (G_OBJECT (lang_item), "activate",
1982 G_CALLBACK (chat_add_to_dictionary_activate_cb),
1984 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
1986 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
1989 name = empathy_spell_get_language_name (code);
1990 g_assert (name != NULL);
1991 /* translators: first %s is the selected word,
1992 * second %s is the language name of the target dictionary */
1993 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
1994 chat_spell->word, name);
1995 item = gtk_image_menu_item_new_with_mnemonic (label);
1997 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1998 GTK_ICON_SIZE_MENU);
1999 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2001 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2003 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2004 (GDestroyNotify) chat_word_free);
2006 g_signal_connect (G_OBJECT (item), "activate",
2007 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2010 g_list_free (codes);
2012 gtk_widget_show_all (item);
2018 chat_text_send_cb (GtkMenuItem *menuitem,
2021 chat_input_text_view_send (chat);
2025 chat_input_populate_popup_cb (GtkTextView *view,
2029 GtkTextBuffer *buffer;
2030 GtkTextTagTable *table;
2033 GtkTextIter iter, start, end;
2036 EmpathyChatSpell *chat_spell;
2037 GtkWidget *spell_menu;
2038 GtkWidget *spell_item;
2039 EmpathySmileyManager *smiley_manager;
2040 GtkWidget *smiley_menu;
2043 buffer = gtk_text_view_get_buffer (view);
2045 /* Add the emoticon menu. */
2046 item = gtk_separator_menu_item_new ();
2047 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2048 gtk_widget_show (item);
2050 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2051 image = gtk_image_new_from_icon_name ("face-smile",
2052 GTK_ICON_SIZE_MENU);
2053 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2054 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2055 gtk_widget_show (item);
2057 smiley_manager = empathy_smiley_manager_dup_singleton ();
2058 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2059 chat_insert_smiley_activate_cb,
2061 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2062 g_object_unref (smiley_manager);
2064 /* Add the Send menu item. */
2065 gtk_text_buffer_get_bounds (buffer, &start, &end);
2066 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2067 if (!EMP_STR_EMPTY (str)) {
2068 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2069 g_signal_connect (G_OBJECT (item), "activate",
2070 G_CALLBACK (chat_text_send_cb), chat);
2071 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2072 gtk_widget_show (item);
2076 /* Add the spell check menu item. */
2077 table = gtk_text_buffer_get_tag_table (buffer);
2078 tag = gtk_text_tag_table_lookup (table, "misspelled");
2079 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2080 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2081 GTK_TEXT_WINDOW_WIDGET,
2084 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2086 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2087 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2089 str = gtk_text_buffer_get_text (buffer,
2090 &start, &end, FALSE);
2092 if (!EMP_STR_EMPTY (str)) {
2093 chat_spell = chat_spell_new (chat, str, start, end);
2094 g_object_set_data_full (G_OBJECT (menu),
2095 "chat-spell", chat_spell,
2096 (GDestroyNotify) chat_spell_free);
2098 item = gtk_separator_menu_item_new ();
2099 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2100 gtk_widget_show (item);
2102 /* Spelling suggestions */
2103 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2104 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2105 GTK_ICON_SIZE_MENU);
2106 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2108 spell_menu = chat_spelling_build_menu (chat_spell);
2109 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2112 spell_item = gtk_separator_menu_item_new ();
2113 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2114 gtk_widget_show (spell_item);
2116 /* Add to dictionary */
2117 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2119 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2120 gtk_widget_show (spell_item);
2122 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2123 gtk_widget_show (item);
2129 chat_log_filter (TplEvent *log,
2132 EmpathyChat *chat = user_data;
2133 EmpathyMessage *message;
2134 EmpathyChatPriv *priv = GET_PRIV (chat);
2135 const GList *pending;
2137 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2138 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2140 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2141 message = empathy_message_from_tpl_log_event (log);
2143 for (; pending; pending = g_list_next (pending)) {
2144 if (empathy_message_equal (message, pending->data)) {
2145 g_object_unref (message);
2150 g_object_unref (message);
2156 show_pending_messages (EmpathyChat *chat) {
2157 EmpathyChatPriv *priv = GET_PRIV (chat);
2158 const GList *messages, *l;
2160 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2162 if (chat->view == NULL || priv->tp_chat == NULL)
2165 if (!priv->can_show_pending)
2168 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2170 for (l = messages; l != NULL ; l = g_list_next (l)) {
2171 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2172 chat_message_received (chat, message, TRUE);
2178 got_filtered_messages_cb (GObject *manager,
2179 GAsyncResult *result,
2184 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2185 EmpathyChatPriv *priv = GET_PRIV (chat);
2186 GError *error = NULL;
2188 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2189 result, &messages, &error)) {
2190 DEBUG ("%s. Aborting.", error->message);
2191 empathy_chat_view_append_event (chat->view,
2192 _("Failed to retrieve recent logs"));
2193 g_error_free (error);
2197 for (l = messages; l; l = g_list_next (l)) {
2198 EmpathyMessage *message;
2199 g_assert (TPL_IS_EVENT (l->data));
2201 message = empathy_message_from_tpl_log_event (l->data);
2202 g_object_unref (l->data);
2204 empathy_chat_view_append_message (chat->view, message);
2205 g_object_unref (message);
2207 g_list_free (messages);
2210 /* in case of TPL error, skip backlog and show pending messages */
2211 priv->can_show_pending = TRUE;
2212 show_pending_messages (chat);
2214 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2215 * about it in EmpathyChatPriv definition */
2216 priv->retrieving_backlogs = FALSE;
2217 empathy_chat_messages_read (chat);
2219 /* Turn back on scrolling */
2220 empathy_chat_view_scroll (chat->view, TRUE);
2224 chat_add_logs (EmpathyChat *chat)
2226 EmpathyChatPriv *priv = GET_PRIV (chat);
2233 /* Turn off scrolling temporarily */
2234 empathy_chat_view_scroll (chat->view, FALSE);
2236 /* Add messages from last conversation */
2237 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2238 target = tpl_entity_new_from_room_id (priv->id);
2240 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2242 priv->retrieving_backlogs = TRUE;
2243 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2246 TPL_EVENT_MASK_TEXT,
2250 got_filtered_messages_cb,
2253 g_object_unref (target);
2257 chat_contacts_completion_func (const gchar *s1,
2261 gchar *tmp, *nick1, *nick2;
2268 return s1 ? -1 : +1;
2271 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2272 nick1 = g_utf8_casefold (tmp, -1);
2275 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2276 nick2 = g_utf8_casefold (tmp, -1);
2279 ret = strncmp (nick1, nick2, n);
2288 build_part_message (guint reason,
2290 EmpathyContact *actor,
2291 const gchar *message)
2293 GString *s = g_string_new ("");
2294 const gchar *actor_name = NULL;
2296 if (actor != NULL) {
2297 actor_name = empathy_contact_get_alias (actor);
2300 /* Having an actor only really makes sense for a few actions... */
2302 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2303 g_string_append_printf (s, _("%s has disconnected"), name);
2305 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2306 if (actor_name != NULL) {
2307 /* translators: reverse the order of these arguments
2308 * if the kicked should come before the kicker in your locale.
2310 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2313 g_string_append_printf (s, _("%s was kicked"), name);
2316 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2317 if (actor_name != NULL) {
2318 /* translators: reverse the order of these arguments
2319 * if the banned should come before the banner in your locale.
2321 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2324 g_string_append_printf (s, _("%s was banned"), name);
2328 g_string_append_printf (s, _("%s has left the room"), name);
2331 if (!EMP_STR_EMPTY (message)) {
2332 /* Note to translators: this string is appended to
2333 * notifications like "foo has left the room", with the message
2334 * given by the user living the room. If this poses a problem,
2335 * please let us know. :-)
2337 g_string_append_printf (s, _(" (%s)"), message);
2340 return g_string_free (s, FALSE);
2344 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2345 EmpathyContact *contact,
2346 EmpathyContact *actor,
2352 EmpathyChatPriv *priv = GET_PRIV (chat);
2353 const gchar *name = empathy_contact_get_alias (contact);
2356 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2358 if (priv->block_events_timeout_id != 0)
2362 str = g_strdup_printf (_("%s has joined the room"),
2365 str = build_part_message (reason, name, actor, message);
2368 empathy_chat_view_append_event (chat->view, str);
2373 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2374 EmpathyContact *old_contact,
2375 EmpathyContact *new_contact,
2380 EmpathyChatPriv *priv = GET_PRIV (chat);
2382 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2384 if (priv->block_events_timeout_id == 0) {
2387 str = g_strdup_printf (_("%s is now known as %s"),
2388 empathy_contact_get_alias (old_contact),
2389 empathy_contact_get_alias (new_contact));
2390 empathy_chat_view_append_event (chat->view, str);
2397 chat_reset_size_request (gpointer widget)
2399 gtk_widget_set_size_request (widget, -1, -1);
2405 chat_update_contacts_visibility (EmpathyChat *chat,
2408 EmpathyChatPriv *priv = GET_PRIV (chat);
2409 GtkAllocation allocation;
2411 if (!priv->scrolled_window_contacts) {
2415 if (priv->remote_contact != NULL) {
2419 if (show && priv->contact_list_view == NULL) {
2420 EmpathyContactListStore *store;
2423 /* We are adding the contact list to the chat, we don't want the
2424 * chat view to become too small. If the chat view is already
2425 * smaller than 250 make sure that size won't change. If the
2426 * chat view is bigger the contact list will take some space on
2427 * it but we make sure the chat view don't become smaller than
2428 * 250. Relax the size request once the resize is done */
2429 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2430 min_width = MIN (allocation.width, 250);
2431 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2432 g_idle_add (chat_reset_size_request, priv->vbox_left);
2434 if (priv->contacts_width > 0) {
2435 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2436 priv->contacts_width);
2439 store = empathy_contact_list_store_new (
2440 EMPATHY_CONTACT_LIST (priv->tp_chat));
2441 empathy_contact_list_store_set_show_groups (
2442 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2444 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2445 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2446 EMPATHY_CONTACT_FEATURE_CHAT |
2447 EMPATHY_CONTACT_FEATURE_CALL |
2448 EMPATHY_CONTACT_FEATURE_LOG |
2449 EMPATHY_CONTACT_FEATURE_INFO));
2450 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2451 priv->contact_list_view);
2452 gtk_widget_show (priv->contact_list_view);
2453 gtk_widget_show (priv->scrolled_window_contacts);
2454 g_object_unref (store);
2456 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2457 gtk_widget_hide (priv->scrolled_window_contacts);
2458 if (priv->contact_list_view != NULL) {
2459 gtk_widget_destroy (priv->contact_list_view);
2460 priv->contact_list_view = NULL;
2466 empathy_chat_set_show_contacts (EmpathyChat *chat,
2469 EmpathyChatPriv *priv = GET_PRIV (chat);
2471 priv->show_contacts = show;
2473 chat_update_contacts_visibility (chat, show);
2475 g_object_notify (G_OBJECT (chat), "show-contacts");
2479 chat_remote_contact_changed_cb (EmpathyChat *chat)
2481 EmpathyChatPriv *priv = GET_PRIV (chat);
2483 if (priv->remote_contact != NULL) {
2484 g_object_unref (priv->remote_contact);
2485 priv->remote_contact = NULL;
2490 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2491 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2492 if (priv->remote_contact != NULL) {
2493 g_object_ref (priv->remote_contact);
2494 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2496 else if (priv->tp_chat != NULL) {
2499 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2500 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2503 chat_update_contacts_visibility (chat, priv->show_contacts);
2505 g_object_notify (G_OBJECT (chat), "remote-contact");
2506 g_object_notify (G_OBJECT (chat), "id");
2510 chat_destroy_cb (EmpathyTpChat *tp_chat,
2513 EmpathyChatPriv *priv;
2515 priv = GET_PRIV (chat);
2517 if (!priv->tp_chat) {
2521 chat_composing_remove_timeout (chat);
2522 g_object_unref (priv->tp_chat);
2523 priv->tp_chat = NULL;
2524 g_object_notify (G_OBJECT (chat), "tp-chat");
2526 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2527 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2529 chat_update_contacts_visibility (chat, FALSE);
2533 update_misspelled_words (gpointer data)
2535 EmpathyChat *chat = EMPATHY_CHAT (data);
2536 EmpathyChatPriv *priv = GET_PRIV (chat);
2537 GtkTextBuffer *buffer;
2541 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2543 gtk_text_buffer_get_end_iter (buffer, &iter);
2544 length = gtk_text_iter_get_offset (&iter);
2545 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2546 NULL, length, chat);
2548 priv->update_misspelled_words_id = 0;
2554 conf_spell_checking_cb (GSettings *gsettings_chat,
2558 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2559 EmpathyChatPriv *priv = GET_PRIV (chat);
2560 gboolean spell_checker;
2561 GtkTextBuffer *buffer;
2563 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2566 spell_checker = g_settings_get_boolean (gsettings_chat,
2567 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2569 if (!empathy_spell_supported ()) {
2570 spell_checker = FALSE;
2573 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2575 if (spell_checker == priv->spell_checking_enabled) {
2576 if (spell_checker) {
2577 /* Possibly changed dictionaries,
2578 * update misspelled words. Need to do so in idle
2579 * so the spell checker is updated. */
2580 priv->update_misspelled_words_id =
2581 g_idle_add (update_misspelled_words, chat);
2587 if (spell_checker) {
2590 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2591 buffer, "notify::cursor-position",
2592 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2594 priv->insert_text_id = tp_g_signal_connect_object (
2595 buffer, "insert-text",
2596 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2597 chat, G_CONNECT_AFTER);
2598 priv->delete_range_id = tp_g_signal_connect_object (
2599 buffer, "delete-range",
2600 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2601 chat, G_CONNECT_AFTER);
2603 gtk_text_buffer_create_tag (buffer, "misspelled",
2604 "underline", PANGO_UNDERLINE_ERROR,
2607 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2608 gtk_text_buffer_get_insert (buffer));
2609 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2612 /* Mark misspelled words in the existing buffer.
2613 * Need to do so in idle so the spell checker is updated. */
2614 priv->update_misspelled_words_id =
2615 g_idle_add (update_misspelled_words, chat);
2617 GtkTextTagTable *table;
2620 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2621 priv->notify_cursor_position_id = 0;
2622 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2623 priv->insert_text_id = 0;
2624 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2625 priv->delete_range_id = 0;
2627 table = gtk_text_buffer_get_tag_table (buffer);
2628 tag = gtk_text_tag_table_lookup (table, "misspelled");
2629 gtk_text_tag_table_remove (table, tag);
2631 gtk_text_buffer_delete_mark_by_name (buffer,
2632 "previous-cursor-position");
2635 priv->spell_checking_enabled = spell_checker;
2639 save_paned_pos_timeout (gpointer data)
2641 EmpathyChat *self = data;
2644 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2646 g_settings_set_int (self->priv->gsettings_ui,
2647 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2654 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2658 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2660 if (chat->priv->save_paned_pos_id != 0)
2661 g_source_remove (chat->priv->save_paned_pos_id);
2663 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2664 save_paned_pos_timeout, chat);
2670 chat_create_ui (EmpathyChat *chat)
2672 EmpathyChatPriv *priv = GET_PRIV (chat);
2676 GtkTextBuffer *buffer;
2678 EmpathyThemeManager *theme_mgr;
2680 filename = empathy_file_lookup ("empathy-chat.ui",
2682 gui = empathy_builder_get_file (filename,
2683 "chat_widget", &priv->widget,
2684 "hpaned", &priv->hpaned,
2685 "vbox_left", &priv->vbox_left,
2686 "scrolled_window_chat", &priv->scrolled_window_chat,
2687 "scrolled_window_input", &priv->scrolled_window_input,
2688 "hbox_topic", &priv->hbox_topic,
2689 "expander_topic", &priv->expander_topic,
2690 "label_topic", &priv->label_topic,
2691 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2692 "info_bar_vbox", &priv->info_bar_vbox,
2695 empathy_builder_connect (gui, chat,
2696 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2697 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2702 /* Add message view. */
2703 theme_mgr = empathy_theme_manager_dup_singleton ();
2704 chat->view = empathy_theme_manager_create_view (theme_mgr);
2705 g_object_unref (theme_mgr);
2706 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2707 and other types, even though it's non-editable and doesn't accept any
2708 drags. This steals drag motion for anything inside the scrollbars,
2709 making drag destinations on chat windows far less useful.
2711 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2712 g_signal_connect (chat->view, "focus_in_event",
2713 G_CALLBACK (chat_text_view_focus_in_event_cb),
2715 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2716 GTK_WIDGET (chat->view));
2717 gtk_widget_show (GTK_WIDGET (chat->view));
2719 /* Add input GtkTextView */
2720 chat->input_text_view = empathy_input_text_view_new ();
2721 g_signal_connect (chat->input_text_view, "notify::has-focus",
2722 G_CALLBACK (chat_input_has_focus_notify_cb),
2724 g_signal_connect (chat->input_text_view, "key-press-event",
2725 G_CALLBACK (chat_input_key_press_event_cb),
2727 g_signal_connect (chat->input_text_view, "realize",
2728 G_CALLBACK (chat_input_realize_cb),
2730 g_signal_connect (chat->input_text_view, "populate-popup",
2731 G_CALLBACK (chat_input_populate_popup_cb),
2733 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2734 tp_g_signal_connect_object (buffer, "changed",
2735 G_CALLBACK (chat_input_text_buffer_changed_cb),
2737 tp_g_signal_connect_object (priv->gsettings_chat,
2738 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2739 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2740 conf_spell_checking_cb (priv->gsettings_chat,
2741 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2742 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2743 chat->input_text_view);
2744 gtk_widget_show (chat->input_text_view);
2746 /* Add the (invisible) search bar */
2747 priv->search_bar = empathy_search_bar_new (chat->view);
2748 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2751 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2753 /* Initialy hide the topic, will be shown if not empty */
2754 gtk_widget_hide (priv->hbox_topic);
2756 g_signal_connect (priv->hpaned, "notify::position",
2757 G_CALLBACK (chat_hpaned_pos_changed_cb),
2760 /* Load the paned position */
2761 paned_pos = g_settings_get_int (priv->gsettings_ui,
2762 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2764 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2766 /* Set widget focus order */
2767 list = g_list_append (NULL, priv->search_bar);
2768 list = g_list_append (list, priv->scrolled_window_input);
2769 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2772 list = g_list_append (NULL, priv->vbox_left);
2773 list = g_list_append (list, priv->scrolled_window_contacts);
2774 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2777 list = g_list_append (NULL, priv->hpaned);
2778 list = g_list_append (list, priv->hbox_topic);
2779 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2782 /* Add the main widget in the chat widget */
2783 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2784 g_object_unref (gui);
2788 chat_size_allocate (GtkWidget *widget,
2789 GtkAllocation *allocation)
2791 GtkBin *bin = GTK_BIN (widget);
2792 GtkAllocation child_allocation;
2795 gtk_widget_set_allocation (widget, allocation);
2797 child = gtk_bin_get_child (bin);
2799 if (child && gtk_widget_get_visible (child))
2801 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2802 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2803 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2804 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2806 gtk_widget_size_allocate (child, &child_allocation);
2811 chat_finalize (GObject *object)
2814 EmpathyChatPriv *priv;
2816 chat = EMPATHY_CHAT (object);
2817 priv = GET_PRIV (chat);
2819 DEBUG ("Finalized: %p", object);
2821 if (priv->update_misspelled_words_id != 0)
2822 g_source_remove (priv->update_misspelled_words_id);
2824 if (priv->save_paned_pos_id != 0)
2825 g_source_remove (priv->save_paned_pos_id);
2827 g_object_unref (priv->gsettings_chat);
2828 g_object_unref (priv->gsettings_ui);
2830 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2831 g_list_free (priv->input_history);
2833 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2834 g_list_free (priv->compositors);
2836 chat_composing_remove_timeout (chat);
2838 g_object_unref (priv->account_manager);
2839 g_object_unref (priv->log_manager);
2841 if (priv->tp_chat) {
2842 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2843 chat_destroy_cb, chat);
2844 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2845 chat_message_received_cb, chat);
2846 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2847 chat_send_error_cb, chat);
2848 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2849 chat_state_changed_cb, chat);
2850 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2851 chat_property_changed_cb, chat);
2852 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2853 chat_members_changed_cb, chat);
2854 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2855 chat_remote_contact_changed_cb, chat);
2856 empathy_tp_chat_leave (priv->tp_chat, "");
2857 g_object_unref (priv->tp_chat);
2859 if (priv->account) {
2860 g_object_unref (priv->account);
2862 if (priv->remote_contact) {
2863 g_object_unref (priv->remote_contact);
2866 if (priv->block_events_timeout_id) {
2867 g_source_remove (priv->block_events_timeout_id);
2871 g_free (priv->name);
2872 g_free (priv->subject);
2873 g_completion_free (priv->completion);
2875 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2879 chat_constructed (GObject *object)
2881 EmpathyChat *chat = EMPATHY_CHAT (object);
2882 EmpathyChatPriv *priv = GET_PRIV (chat);
2884 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2885 /* First display logs from the logger and then display pending messages */
2886 chat_add_logs (chat);
2889 /* Just display pending messages for rooms */
2890 priv->can_show_pending = TRUE;
2891 show_pending_messages (chat);
2896 empathy_chat_class_init (EmpathyChatClass *klass)
2898 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2899 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2901 object_class->finalize = chat_finalize;
2902 object_class->get_property = chat_get_property;
2903 object_class->set_property = chat_set_property;
2904 object_class->constructed = chat_constructed;
2906 widget_class->size_allocate = chat_size_allocate;
2908 g_object_class_install_property (object_class,
2910 g_param_spec_object ("tp-chat",
2912 "The tp chat object",
2913 EMPATHY_TYPE_TP_CHAT,
2916 G_PARAM_STATIC_STRINGS));
2917 g_object_class_install_property (object_class,
2919 g_param_spec_object ("account",
2920 "Account of the chat",
2921 "The account of the chat",
2924 G_PARAM_STATIC_STRINGS));
2925 g_object_class_install_property (object_class,
2927 g_param_spec_string ("id",
2929 "The id of the chat",
2932 G_PARAM_STATIC_STRINGS));
2933 g_object_class_install_property (object_class,
2935 g_param_spec_string ("name",
2937 "The name of the chat",
2940 G_PARAM_STATIC_STRINGS));
2941 g_object_class_install_property (object_class,
2943 g_param_spec_string ("subject",
2945 "The subject or topic of the chat",
2948 G_PARAM_STATIC_STRINGS));
2949 g_object_class_install_property (object_class,
2950 PROP_REMOTE_CONTACT,
2951 g_param_spec_object ("remote-contact",
2952 "The remote contact",
2953 "The remote contact is any",
2954 EMPATHY_TYPE_CONTACT,
2956 G_PARAM_STATIC_STRINGS));
2957 g_object_class_install_property (object_class,
2959 g_param_spec_boolean ("show-contacts",
2960 "Contacts' visibility",
2961 "The visibility of the contacts' list",
2964 G_PARAM_STATIC_STRINGS));
2966 signals[COMPOSING] =
2967 g_signal_new ("composing",
2968 G_OBJECT_CLASS_TYPE (object_class),
2972 g_cclosure_marshal_VOID__BOOLEAN,
2976 signals[NEW_MESSAGE] =
2977 g_signal_new ("new-message",
2978 G_OBJECT_CLASS_TYPE (object_class),
2982 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
2984 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
2986 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2990 chat_block_events_timeout_cb (gpointer data)
2992 EmpathyChatPriv *priv = GET_PRIV (data);
2994 priv->block_events_timeout_id = 0;
3000 account_manager_prepared_cb (GObject *source_object,
3001 GAsyncResult *result,
3004 GList *accounts, *l;
3005 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3006 EmpathyChat *chat = user_data;
3007 GError *error = NULL;
3009 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3010 DEBUG ("Failed to prepare the account manager: %s", error->message);
3011 g_error_free (error);
3015 accounts = tp_account_manager_get_valid_accounts (account_manager);
3017 for (l = accounts; l != NULL; l = l->next) {
3018 TpAccount *account = l->data;
3019 tp_g_signal_connect_object (account, "status-changed",
3020 G_CALLBACK (chat_new_connection_cb),
3024 g_list_free (accounts);
3028 empathy_chat_init (EmpathyChat *chat)
3030 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3031 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3034 priv->log_manager = tpl_log_manager_dup_singleton ();
3035 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3036 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3038 priv->contacts_width = -1;
3039 priv->input_history = NULL;
3040 priv->input_history_current = NULL;
3041 priv->account_manager = tp_account_manager_dup ();
3043 tp_account_manager_prepare_async (priv->account_manager, NULL,
3044 account_manager_prepared_cb, chat);
3046 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3047 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3049 /* Block events for some time to avoid having "has come online" or
3050 * "joined" messages. */
3051 priv->block_events_timeout_id =
3052 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3054 /* Add nick name completion */
3055 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3056 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3058 chat_create_ui (chat);
3062 empathy_chat_new (EmpathyTpChat *tp_chat)
3064 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3068 empathy_chat_get_tp_chat (EmpathyChat *chat)
3070 EmpathyChatPriv *priv = GET_PRIV (chat);
3072 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3074 return priv->tp_chat;
3080 GtkWidget *info_bar;
3090 passwd_remember_button_cb (GtkButton *button,
3093 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3097 passwd_not_now_button_cb (GtkButton *button,
3100 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3104 remember_password_infobar_response_cb (GtkWidget *info_bar,
3108 EmpathyChatPriv *priv = GET_PRIV (data->self);
3110 if (response_id == GTK_RESPONSE_OK) {
3111 DEBUG ("Saving room password");
3112 empathy_keyring_set_room_password_async (priv->account,
3113 empathy_tp_chat_get_id (priv->tp_chat),
3118 gtk_widget_destroy (info_bar);
3119 g_free (data->password);
3120 g_slice_free (PasswordData, data);
3124 chat_prompt_to_save_password (EmpathyChat *self,
3127 GtkWidget *content_area;
3134 /* save the password in case it needs to be saved */
3135 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3137 /* Remove all previous widgets */
3138 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3139 gtk_container_forall (GTK_CONTAINER (content_area),
3140 (GtkCallback) gtk_widget_destroy, NULL);
3141 data->button = NULL;
3144 data->spinner = NULL;
3146 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3147 GTK_MESSAGE_QUESTION);
3149 hbox = gtk_hbox_new (FALSE, 5);
3150 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3153 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3154 GTK_ICON_SIZE_DIALOG);
3155 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3158 label = gtk_label_new (_("Would you like to store this password?"));
3159 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3161 /* Add 'Remember' button */
3162 alig = gtk_alignment_new (0, 0.5, 1, 0);
3164 button = gtk_button_new_with_label (_("Remember"));
3165 gtk_container_add (GTK_CONTAINER (alig), button);
3166 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3168 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3171 /* Add 'Not now' button */
3172 alig = gtk_alignment_new (0, 0.5, 1, 0);
3174 button = gtk_button_new_with_label (_("Not now"));
3175 gtk_container_add (GTK_CONTAINER (alig), button);
3176 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3178 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3182 g_signal_handler_disconnect (data->info_bar, data->response_id);
3183 g_signal_connect (data->info_bar, "response",
3184 G_CALLBACK (remember_password_infobar_response_cb), data);
3186 gtk_widget_show_all (data->info_bar);
3190 provide_password_cb (GObject *tp_chat,
3194 PasswordData *data = user_data;
3195 EmpathyChat *self = data->self;
3196 EmpathyChatPriv *priv = GET_PRIV (self);
3197 GError *error = NULL;
3199 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3201 DEBUG ("error: %s", error->message);
3202 /* FIXME: what should we do if that's another error? Close the channel?
3203 * Display the raw D-Bus error to the user isn't very useful */
3204 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3206 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3207 gtk_widget_set_sensitive (data->entry, TRUE);
3208 gtk_widget_grab_focus (data->entry);
3211 gtk_info_bar_set_message_type (
3212 GTK_INFO_BAR (data->info_bar),
3216 gtk_widget_set_sensitive (data->button, TRUE);
3217 gtk_button_set_label (GTK_BUTTON (data->button),
3221 gtk_label_set_text (GTK_LABEL (data->label),
3222 _("Wrong password; please try again:"));
3225 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3226 gtk_widget_hide (data->spinner);
3228 g_error_free (error);
3232 if (empathy_keyring_is_available ()) {
3233 /* ask whether they want to save the password */
3234 chat_prompt_to_save_password (self, data);
3236 /* Get rid of the password info bar finally */
3237 gtk_widget_destroy (data->info_bar);
3238 g_slice_free (PasswordData, data);
3242 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3243 gtk_widget_grab_focus (self->input_text_view);
3247 password_infobar_response_cb (GtkWidget *info_bar,
3251 EmpathyChatPriv *priv = GET_PRIV (data->self);
3252 const gchar *password;
3254 if (response_id != GTK_RESPONSE_OK) {
3255 gtk_widget_destroy (info_bar);
3256 g_slice_free (PasswordData, data);
3260 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3262 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3263 provide_password_cb, data);
3265 gtk_widget_set_sensitive (data->button, FALSE);
3266 gtk_widget_set_sensitive (data->entry, FALSE);
3268 gtk_spinner_start (GTK_SPINNER (data->spinner));
3269 gtk_widget_show (data->spinner);
3273 password_entry_activate_cb (GtkWidget *entry,
3276 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3280 passwd_join_button_cb (GtkButton *button,
3283 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3287 clear_icon_released_cb (GtkEntry *entry,
3288 GtkEntryIconPosition icon_pos,
3292 gtk_entry_set_text (entry, "");
3296 password_entry_changed_cb (GtkEditable *entry,
3301 str = gtk_entry_get_text (GTK_ENTRY (entry));
3303 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3304 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3308 display_password_info_bar (EmpathyChat *self)
3310 EmpathyChatPriv *priv = GET_PRIV (self);
3311 GtkWidget *info_bar;
3312 GtkWidget *content_area;
3322 data = g_slice_new0 (PasswordData);
3324 info_bar = gtk_info_bar_new ();
3325 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3326 GTK_MESSAGE_QUESTION);
3328 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3330 hbox = gtk_hbox_new (FALSE, 5);
3331 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3334 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3335 GTK_ICON_SIZE_DIALOG);
3336 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3339 label = gtk_label_new (_("This room is protected by a password:"));
3340 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3342 /* Add password entry */
3343 entry = gtk_entry_new ();
3344 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3345 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3347 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3348 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3349 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3350 GTK_ENTRY_ICON_SECONDARY, FALSE);
3352 g_signal_connect (entry, "icon-release",
3353 G_CALLBACK (clear_icon_released_cb), data);
3354 g_signal_connect (entry, "changed",
3355 G_CALLBACK (password_entry_changed_cb), data);
3357 g_signal_connect (entry, "activate",
3358 G_CALLBACK (password_entry_activate_cb), data);
3360 /* Focus the password entry once it's realized */
3361 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3363 /* Add 'Join' button */
3364 alig = gtk_alignment_new (0, 0.5, 1, 0);
3366 button = gtk_button_new_with_label (_("Join"));
3367 gtk_container_add (GTK_CONTAINER (alig), button);
3368 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3370 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3374 spinner = gtk_spinner_new ();
3375 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3377 /* Save some data for messing around with later */
3379 data->info_bar = info_bar;
3380 data->button = button;
3381 data->label = label;
3382 data->entry = entry;
3383 data->spinner = spinner;
3385 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3387 gtk_widget_show_all (hbox);
3389 data->response_id = g_signal_connect (info_bar, "response",
3390 G_CALLBACK (password_infobar_response_cb), data);
3392 gtk_widget_show_all (info_bar);
3393 /* ... but hide the spinner */
3394 gtk_widget_hide (spinner);
3398 provide_saved_password_cb (GObject *tp_chat,
3402 EmpathyChat *self = user_data;
3403 EmpathyChatPriv *priv = GET_PRIV (self);
3404 GError *error = NULL;
3406 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3408 DEBUG ("error: %s", error->message);
3409 /* FIXME: what should we do if that's another error? Close the channel?
3410 * Display the raw D-Bus error to the user isn't very useful */
3411 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3412 display_password_info_bar (self);
3413 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3415 g_error_free (error);
3420 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3421 gtk_widget_grab_focus (self->input_text_view);
3425 chat_room_got_password_cb (GObject *source,
3426 GAsyncResult *result,
3429 EmpathyChat *self = user_data;
3430 EmpathyChatPriv *priv = GET_PRIV (self);
3431 const gchar *password;
3432 GError *error = NULL;
3434 password = empathy_keyring_get_room_password_finish (priv->account,
3437 if (error != NULL) {
3438 DEBUG ("Couldn't get room password: %s\n", error->message);
3439 g_clear_error (&error);
3441 display_password_info_bar (self);
3442 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3446 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3447 provide_saved_password_cb, self);
3451 chat_password_needed_changed_cb (EmpathyChat *self)
3453 EmpathyChatPriv *priv = GET_PRIV (self);
3455 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3456 empathy_keyring_get_room_password_async (priv->account,
3457 empathy_tp_chat_get_id (priv->tp_chat),
3458 chat_room_got_password_cb, self);
3463 empathy_chat_set_tp_chat (EmpathyChat *chat,
3464 EmpathyTpChat *tp_chat)
3466 EmpathyChatPriv *priv = GET_PRIV (chat);
3467 GPtrArray *properties;
3469 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3470 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3471 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3473 if (priv->tp_chat) {
3477 if (priv->account) {
3478 g_object_unref (priv->account);
3481 priv->tp_chat = g_object_ref (tp_chat);
3482 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3484 g_signal_connect (tp_chat, "destroy",
3485 G_CALLBACK (chat_destroy_cb),
3487 g_signal_connect (tp_chat, "message-received",
3488 G_CALLBACK (chat_message_received_cb),
3490 g_signal_connect (tp_chat, "send-error",
3491 G_CALLBACK (chat_send_error_cb),
3493 g_signal_connect (tp_chat, "chat-state-changed",
3494 G_CALLBACK (chat_state_changed_cb),
3496 g_signal_connect (tp_chat, "property-changed",
3497 G_CALLBACK (chat_property_changed_cb),
3499 g_signal_connect (tp_chat, "members-changed",
3500 G_CALLBACK (chat_members_changed_cb),
3502 g_signal_connect (tp_chat, "member-renamed",
3503 G_CALLBACK (chat_member_renamed_cb),
3505 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3506 G_CALLBACK (chat_remote_contact_changed_cb),
3508 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3509 G_CALLBACK (chat_password_needed_changed_cb),
3512 /* Get initial value of properties */
3513 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3514 if (properties != NULL) {
3517 for (i = 0; i < properties->len; i++) {
3518 EmpathyTpChatProperty *property;
3520 property = g_ptr_array_index (properties, i);
3521 if (property->value == NULL)
3524 chat_property_changed_cb (priv->tp_chat,
3531 chat_remote_contact_changed_cb (chat);
3533 if (chat->input_text_view) {
3534 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3535 if (priv->block_events_timeout_id == 0) {
3536 empathy_chat_view_append_event (chat->view, _("Connected"));
3540 g_object_notify (G_OBJECT (chat), "tp-chat");
3541 g_object_notify (G_OBJECT (chat), "id");
3542 g_object_notify (G_OBJECT (chat), "account");
3544 /* This is a noop when tp-chat is set at object construction time and causes
3545 * the pending messages to be show when it's set on the object after it has
3547 show_pending_messages (chat);
3549 /* check if a password is needed */
3550 chat_password_needed_changed_cb (chat);
3554 empathy_chat_get_account (EmpathyChat *chat)
3556 EmpathyChatPriv *priv = GET_PRIV (chat);
3558 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3560 return priv->account;
3564 empathy_chat_get_id (EmpathyChat *chat)
3566 EmpathyChatPriv *priv = GET_PRIV (chat);
3568 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3574 empathy_chat_get_name (EmpathyChat *chat)
3576 EmpathyChatPriv *priv = GET_PRIV (chat);
3579 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3582 if (!ret && priv->remote_contact) {
3583 ret = empathy_contact_get_alias (priv->remote_contact);
3589 return ret ? ret : _("Conversation");
3593 empathy_chat_get_subject (EmpathyChat *chat)
3595 EmpathyChatPriv *priv = GET_PRIV (chat);
3597 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3599 return priv->subject;
3603 empathy_chat_get_remote_contact (EmpathyChat *chat)
3605 EmpathyChatPriv *priv = GET_PRIV (chat);
3607 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3609 return priv->remote_contact;
3613 empathy_chat_get_contact_menu (EmpathyChat *chat)
3615 EmpathyChatPriv *priv = GET_PRIV (chat);
3616 GtkWidget *menu = NULL;
3618 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3620 if (priv->remote_contact) {
3621 menu = empathy_contact_menu_new (priv->remote_contact,
3622 EMPATHY_CONTACT_FEATURE_CALL |
3623 EMPATHY_CONTACT_FEATURE_LOG |
3624 EMPATHY_CONTACT_FEATURE_INFO |
3625 EMPATHY_CONTACT_FEATURE_BLOCK);
3632 empathy_chat_clear (EmpathyChat *chat)
3634 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3636 empathy_chat_view_clear (chat->view);
3640 empathy_chat_scroll_down (EmpathyChat *chat)
3642 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3644 empathy_chat_view_scroll_down (chat->view);
3648 empathy_chat_cut (EmpathyChat *chat)
3650 GtkTextBuffer *buffer;
3652 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3654 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3655 if (gtk_text_buffer_get_has_selection (buffer)) {
3656 GtkClipboard *clipboard;
3658 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3660 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3665 empathy_chat_copy (EmpathyChat *chat)
3667 GtkTextBuffer *buffer;
3669 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3671 if (empathy_chat_view_get_has_selection (chat->view)) {
3672 empathy_chat_view_copy_clipboard (chat->view);
3676 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3677 if (gtk_text_buffer_get_has_selection (buffer)) {
3678 GtkClipboard *clipboard;
3680 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3682 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3687 EmpathyChatPriv *priv = GET_PRIV (chat);
3689 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3696 GtkClipboard *clipboard;
3698 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3699 start = g_utf8_offset_to_pointer (topic, start_offset);
3700 end = g_utf8_offset_to_pointer (topic, end_offset);
3701 selection = g_strndup (start, end - start);
3703 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3704 gtk_clipboard_set_text (clipboard, selection, -1);
3712 empathy_chat_paste (EmpathyChat *chat)
3714 GtkTextBuffer *buffer;
3715 GtkClipboard *clipboard;
3716 EmpathyChatPriv *priv;
3718 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3720 priv = GET_PRIV (chat);
3722 if (gtk_widget_get_visible (priv->search_bar)) {
3723 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3727 if (priv->tp_chat == NULL ||
3728 !gtk_widget_is_sensitive (chat->input_text_view))
3731 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3732 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3734 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3738 empathy_chat_find (EmpathyChat *chat)
3740 EmpathyChatPriv *priv;
3742 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3744 priv = GET_PRIV (chat);
3746 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3750 empathy_chat_correct_word (EmpathyChat *chat,
3753 const gchar *new_word)
3755 GtkTextBuffer *buffer;
3757 g_return_if_fail (chat != NULL);
3758 g_return_if_fail (new_word != NULL);
3760 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3762 gtk_text_buffer_delete (buffer, start, end);
3763 gtk_text_buffer_insert (buffer, start,
3769 empathy_chat_is_room (EmpathyChat *chat)
3771 EmpathyChatPriv *priv = GET_PRIV (chat);
3773 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3775 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3779 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3781 EmpathyChatPriv *priv = GET_PRIV (self);
3783 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3785 return priv->unread_messages;
3788 /* called when the messages have been read by user */
3790 empathy_chat_messages_read (EmpathyChat *self)
3792 EmpathyChatPriv *priv = GET_PRIV (self);
3794 g_return_if_fail (EMPATHY_IS_CHAT (self));
3796 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3797 * definition. If we are still retrieving the backlogs, do not ACK */
3798 if (priv->retrieving_backlogs)
3801 if (priv->tp_chat != NULL ) {
3802 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3804 priv->unread_messages = 0;
3806 empathy_chat_view_focus_toggled (self->view, TRUE);
3809 /* Return TRUE if on of the contacts in this chat is composing */
3811 empathy_chat_is_composing (EmpathyChat *chat)
3813 return chat->priv->compositors != NULL;