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,
178 static guint signals[LAST_SIGNAL] = { 0 };
180 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
182 static gboolean update_misspelled_words (gpointer data);
185 chat_get_property (GObject *object,
190 EmpathyChat *chat = EMPATHY_CHAT (object);
191 EmpathyChatPriv *priv = GET_PRIV (object);
195 g_value_set_object (value, priv->tp_chat);
198 g_value_set_object (value, priv->account);
201 g_value_take_string (value, empathy_chat_dup_name (chat));
204 g_value_set_string (value, priv->id);
207 g_value_set_string (value, priv->subject);
209 case PROP_REMOTE_CONTACT:
210 g_value_set_object (value, priv->remote_contact);
212 case PROP_SHOW_CONTACTS:
213 g_value_set_boolean (value, priv->show_contacts);
215 case PROP_SMS_CHANNEL:
216 g_value_set_boolean (value, priv->sms_channel);
218 case PROP_N_MESSAGES_SENDING:
219 g_value_set_uint (value,
220 empathy_chat_get_n_messages_sending (chat));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
229 chat_set_property (GObject *object,
234 EmpathyChat *chat = EMPATHY_CHAT (object);
238 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
240 case PROP_SHOW_CONTACTS:
241 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
250 account_reconnected (EmpathyChat *chat,
253 EmpathyChatPriv *priv = GET_PRIV (chat);
255 DEBUG ("Account reconnected, request a new Text channel");
257 /* FIXME: Ideally we should ask to handle ourself the channel so we can
258 * report the error if any but this is blocked by
259 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
260 switch (priv->handle_type) {
261 case TP_HANDLE_TYPE_CONTACT:
262 if (priv->sms_channel)
263 empathy_sms_contact_id (
265 TP_USER_ACTION_TIME_NOT_USER_ACTION);
267 empathy_chat_with_contact_id (
269 TP_USER_ACTION_TIME_NOT_USER_ACTION);
271 case TP_HANDLE_TYPE_ROOM:
272 empathy_join_muc (account, priv->id,
273 TP_USER_ACTION_TIME_NOT_USER_ACTION);
275 case TP_HANDLE_TYPE_NONE:
276 case TP_HANDLE_TYPE_LIST:
277 case TP_HANDLE_TYPE_GROUP:
279 g_assert_not_reached ();
283 g_object_unref (chat);
287 chat_new_connection_cb (TpAccount *account,
291 gchar *dbus_error_name,
295 EmpathyChatPriv *priv = GET_PRIV (chat);
297 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
300 if (priv->tp_chat != NULL || account != priv->account ||
301 priv->handle_type == TP_HANDLE_TYPE_NONE ||
302 EMP_STR_EMPTY (priv->id))
307 account_reconnected (chat, account);
311 chat_composing_remove_timeout (EmpathyChat *chat)
313 EmpathyChatPriv *priv;
315 priv = GET_PRIV (chat);
317 if (priv->composing_stop_timeout_id) {
318 g_source_remove (priv->composing_stop_timeout_id);
319 priv->composing_stop_timeout_id = 0;
324 chat_composing_stop_timeout_cb (EmpathyChat *chat)
326 EmpathyChatPriv *priv;
328 priv = GET_PRIV (chat);
330 priv->composing_stop_timeout_id = 0;
331 empathy_tp_chat_set_state (priv->tp_chat,
332 TP_CHANNEL_CHAT_STATE_PAUSED);
338 chat_composing_start (EmpathyChat *chat)
340 EmpathyChatPriv *priv;
342 priv = GET_PRIV (chat);
344 if (priv->composing_stop_timeout_id) {
345 /* Just restart the timeout */
346 chat_composing_remove_timeout (chat);
348 empathy_tp_chat_set_state (priv->tp_chat,
349 TP_CHANNEL_CHAT_STATE_COMPOSING);
352 priv->composing_stop_timeout_id = g_timeout_add_seconds (
353 COMPOSING_STOP_TIMEOUT,
354 (GSourceFunc) chat_composing_stop_timeout_cb,
359 chat_composing_stop (EmpathyChat *chat)
361 EmpathyChatPriv *priv;
363 priv = GET_PRIV (chat);
365 chat_composing_remove_timeout (chat);
366 empathy_tp_chat_set_state (priv->tp_chat,
367 TP_CHANNEL_CHAT_STATE_ACTIVE);
371 chat_input_history_entry_cmp (InputHistoryEntry *entry,
374 if (!tp_strdiff (entry->text, text)) {
375 if (entry->modified_text != NULL) {
376 /* Modified entry and single string cannot be equal. */
384 static InputHistoryEntry *
385 chat_input_history_entry_new_with_text (const gchar *text)
387 InputHistoryEntry *entry;
388 entry = g_slice_new0 (InputHistoryEntry);
389 entry->text = g_strdup (text);
395 chat_input_history_entry_free (InputHistoryEntry *entry)
397 g_free (entry->text);
398 g_free (entry->modified_text);
399 g_slice_free (InputHistoryEntry, entry);
403 chat_input_history_entry_revert (InputHistoryEntry *entry)
405 g_free (entry->modified_text);
406 entry->modified_text = NULL;
410 chat_input_history_entry_update_text (InputHistoryEntry *entry,
415 if (!tp_strdiff (text, entry->text)) {
416 g_free (entry->modified_text);
417 entry->modified_text = NULL;
421 old = entry->modified_text;
422 entry->modified_text = g_strdup (text);
427 chat_input_history_entry_get_text (InputHistoryEntry *entry)
433 if (entry->modified_text != NULL) {
434 return entry->modified_text;
440 chat_input_history_remove_item (GList *list,
443 list = g_list_remove_link (list, item);
444 chat_input_history_entry_free (item->data);
445 g_list_free_1 (item);
450 chat_input_history_revert (EmpathyChat *chat)
452 EmpathyChatPriv *priv;
456 InputHistoryEntry *entry;
458 priv = GET_PRIV (chat);
459 list = priv->input_history;
462 DEBUG ("No input history");
466 /* Delete temporary entry */
467 if (priv->input_history_current != NULL) {
469 list = chat_input_history_remove_item (list, item1);
470 if (priv->input_history_current == item1) {
471 /* Removed temporary entry was current entry */
472 priv->input_history = list;
473 priv->input_history_current = NULL;
478 /* There is no entry to revert */
482 /* Restore the current history entry to original value */
483 item1 = priv->input_history_current;
485 chat_input_history_entry_revert (entry);
487 /* Remove restored entry if there is other occurance before this entry */
488 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
489 (GCompareFunc) chat_input_history_entry_cmp);
490 if (item2 != item1) {
491 list = chat_input_history_remove_item (list, item1);
494 /* Remove other occurance of the restored entry */
495 item2 = g_list_find_custom (item1->next,
496 chat_input_history_entry_get_text (entry),
497 (GCompareFunc) chat_input_history_entry_cmp);
499 list = chat_input_history_remove_item (list, item2);
503 priv->input_history_current = NULL;
504 priv->input_history = list;
508 chat_input_history_add (EmpathyChat *chat,
512 EmpathyChatPriv *priv;
515 InputHistoryEntry *entry;
517 priv = GET_PRIV (chat);
519 list = priv->input_history;
521 /* Remove any other occurances of this entry, if not temporary */
523 while ((item = g_list_find_custom (list, str,
524 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
525 list = chat_input_history_remove_item (list, item);
528 /* Trim the list to the last 10 items */
529 while (g_list_length (list) > 10) {
530 item = g_list_last (list);
532 list = chat_input_history_remove_item (list, item);
540 entry = chat_input_history_entry_new_with_text (str);
541 list = g_list_prepend (list, entry);
543 /* Set the list and the current item pointer */
544 priv->input_history = list;
546 priv->input_history_current = list;
549 priv->input_history_current = NULL;
554 chat_input_history_get_next (EmpathyChat *chat)
556 EmpathyChatPriv *priv;
560 priv = GET_PRIV (chat);
562 if (priv->input_history == NULL) {
563 DEBUG ("No input history, next entry is NULL");
566 g_assert (priv->input_history_current != NULL);
568 if ((item = g_list_next (priv->input_history_current)) == NULL)
570 item = priv->input_history_current;
573 msg = chat_input_history_entry_get_text (item->data);
575 DEBUG ("Returning next entry: '%s'", msg);
577 priv->input_history_current = item;
583 chat_input_history_get_prev (EmpathyChat *chat)
585 EmpathyChatPriv *priv;
589 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
591 priv = GET_PRIV (chat);
593 if (priv->input_history == NULL) {
594 DEBUG ("No input history, previous entry is NULL");
598 if (priv->input_history_current == NULL)
602 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
604 item = priv->input_history_current;
607 msg = chat_input_history_entry_get_text (item->data);
609 DEBUG ("Returning previous entry: '%s'", msg);
611 priv->input_history_current = item;
617 chat_input_history_update (EmpathyChat *chat,
618 GtkTextBuffer *buffer)
620 EmpathyChatPriv *priv;
621 GtkTextIter start, end;
623 InputHistoryEntry *entry;
625 priv = GET_PRIV (chat);
627 gtk_text_buffer_get_bounds (buffer, &start, &end);
628 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
630 if (priv->input_history_current == NULL) {
631 /* Add the current text temporarily to the history */
632 chat_input_history_add (chat, text, TRUE);
637 /* Save the changes in the history */
638 entry = priv->input_history_current->data;
639 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
640 chat_input_history_entry_update_text (entry, text);
649 } ChatCommandMsgData;
652 chat_command_msg_cb (GObject *source,
653 GAsyncResult *result,
656 ChatCommandMsgData *data = user_data;
657 GError *error = NULL;
660 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
661 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
663 if (channel == NULL) {
664 DEBUG ("Failed to get channel: %s", error->message);
665 g_error_free (error);
667 empathy_chat_view_append_event (data->chat->view,
668 _("Failed to open private chat"));
672 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
673 TpTextChannel *text = (TpTextChannel *) channel;
676 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
679 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
681 g_object_unref (msg);
684 g_object_unref (channel);
687 g_free (data->message);
688 g_slice_free (ChatCommandMsgData, data);
692 nick_command_supported (EmpathyChat *chat)
694 EmpathyChatPriv * priv = GET_PRIV (chat);
695 TpConnection *connection;
697 connection = empathy_tp_chat_get_connection (priv->tp_chat);
698 return tp_proxy_has_interface_by_id (connection,
699 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
704 part_command_supported (EmpathyChat *chat)
706 EmpathyChatPriv * priv = GET_PRIV (chat);
709 channel = empathy_tp_chat_get_channel (priv->tp_chat);
710 return tp_proxy_has_interface_by_id (channel,
711 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
716 chat_command_clear (EmpathyChat *chat,
719 empathy_chat_view_clear (chat->view);
723 chat_command_topic (EmpathyChat *chat,
726 EmpathyChatPriv *priv = GET_PRIV (chat);
727 EmpathyTpChatProperty *property;
728 GValue value = {0, };
730 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
731 if (property == NULL) {
732 empathy_chat_view_append_event (chat->view,
733 _("Topic not supported on this conversation"));
737 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
738 empathy_chat_view_append_event (chat->view,
739 _("You are not allowed to change the topic"));
743 g_value_init (&value, G_TYPE_STRING);
744 g_value_set_string (&value, strv[1]);
745 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
746 g_value_unset (&value);
750 empathy_chat_join_muc (EmpathyChat *chat,
753 EmpathyChatPriv *priv = GET_PRIV (chat);
755 empathy_join_muc (priv->account, room,
756 gtk_get_current_event_time ());
760 chat_command_join (EmpathyChat *chat,
765 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
767 /* FIXME: Ideally we should ask to handle ourself the channel so we can
768 * report the error if any but this is blocked by
769 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
770 while (rooms[i] != NULL) {
771 /* ignore empty strings */
772 if (!EMP_STR_EMPTY (rooms[i])) {
773 empathy_chat_join_muc (chat, rooms[i]);
781 empathy_chat_leave_chat (EmpathyChat *chat)
783 EmpathyChatPriv *priv = GET_PRIV (chat);
785 empathy_tp_chat_leave (priv->tp_chat, "");
790 chat_command_part (EmpathyChat *chat,
793 EmpathyChatPriv *priv = GET_PRIV (chat);
794 EmpathyChat *chat_to_be_parted;
796 if (strv[1] == NULL) {
797 empathy_tp_chat_leave (priv->tp_chat, "");
800 chat_to_be_parted = empathy_chat_window_find_chat (priv->account, strv[1]);
802 if (chat_to_be_parted != NULL) {
803 empathy_tp_chat_leave (empathy_chat_get_tp_chat (chat_to_be_parted), strv[2]);
805 empathy_tp_chat_leave (priv->tp_chat, strv[1]);
811 chat_command_msg_internal (EmpathyChat *chat,
812 const gchar *contact_id,
813 const gchar *message)
815 EmpathyChatPriv *priv = GET_PRIV (chat);
816 ChatCommandMsgData *data;
817 TpAccountChannelRequest *req;
820 request = tp_asv_new (
821 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
822 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
823 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
826 req = tp_account_channel_request_new (priv->account, request,
827 tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
829 /* FIXME: We should probably search in members alias. But this
830 * is enough for IRC */
831 data = g_slice_new (ChatCommandMsgData);
833 data->message = g_strdup (message);
835 tp_account_channel_request_ensure_and_observe_channel_async (req,
836 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
838 g_object_unref (req);
839 g_hash_table_unref (request);
843 chat_command_query (EmpathyChat *chat,
846 /* If <message> part is not defined,
847 * strv[2] will be the terminal NULL */
848 chat_command_msg_internal (chat, strv[1], strv[2]);
852 chat_command_msg (EmpathyChat *chat,
855 chat_command_msg_internal (chat, strv[1], strv[2]);
859 callback_for_request_rename (TpProxy *proxy,
862 GObject *weak_object)
865 DEBUG ("Call to RequestRename method failed: %s",error->message);
870 chat_command_nick (EmpathyChat *chat,
873 EmpathyChatPriv *priv = GET_PRIV (chat);
876 proxy = TP_PROXY (tp_account_get_connection (priv->account));
878 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
879 strv[1], callback_for_request_rename, NULL, NULL, NULL);
883 chat_command_me (EmpathyChat *chat,
886 EmpathyChatPriv *priv = GET_PRIV (chat);
890 channel = empathy_tp_chat_get_channel (priv->tp_chat);
892 /* Strictly speaking we don't depend yet on Messages so best to check that
893 * the channel is actually a TpTextChannel before casting it. */
894 if (TP_IS_TEXT_CHANNEL (channel) &&
895 !tp_text_channel_supports_message_type (TP_TEXT_CHANNEL (channel),
896 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
897 /* Action message are not supported, 'simulate' the action */
898 EmpathyContact *self_contact;
901 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
902 /* The TpChat can't be ready if it doesn't have the self contact */
903 g_assert (self_contact != NULL);
905 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
907 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
912 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
916 empathy_tp_chat_send (priv->tp_chat, message);
917 g_object_unref (message);
921 chat_command_say (EmpathyChat *chat,
924 EmpathyChatPriv *priv = GET_PRIV (chat);
927 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
929 empathy_tp_chat_send (priv->tp_chat, message);
930 g_object_unref (message);
933 static void chat_command_help (EmpathyChat *chat, GStrv strv);
935 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
941 ChatCommandFunc func;
942 gboolean (*is_supported)(EmpathyChat *chat);
946 static ChatCommandItem commands[] = {
947 {"clear", 1, 1, chat_command_clear, NULL,
948 N_("/clear: clear all messages from the current conversation")},
950 {"topic", 2, 2, chat_command_topic, NULL,
951 N_("/topic <topic>: set the topic of the current conversation")},
953 {"join", 2, 2, chat_command_join, NULL,
954 N_("/join <chat room ID>: join a new chat room")},
956 {"j", 2, 2, chat_command_join, NULL,
957 N_("/j <chat room ID>: join a new chat room")},
960 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
961 {"part", 1, 3, chat_command_part, part_command_supported,
962 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
963 "by default the current one")},
966 {"query", 2, 3, chat_command_query, NULL,
967 N_("/query <contact ID> [<message>]: open a private chat")},
969 {"msg", 3, 3, chat_command_msg, NULL,
970 N_("/msg <contact ID> <message>: open a private chat")},
972 {"nick", 2, 2, chat_command_nick, nick_command_supported,
973 N_("/nick <nickname>: change your nickname on the current server")},
975 {"me", 2, 2, chat_command_me, NULL,
976 N_("/me <message>: send an ACTION message to the current conversation")},
978 {"say", 2, 2, chat_command_say, NULL,
979 N_("/say <message>: send <message> to the current conversation. "
980 "This is used to send a message starting with a '/'. For example: "
981 "\"/say /join is used to join a new chat room\"")},
983 {"help", 1, 2, chat_command_help, NULL,
984 N_("/help [<command>]: show all supported commands. "
985 "If <command> is defined, show its usage.")},
989 chat_command_show_help (EmpathyChat *chat,
990 ChatCommandItem *item)
994 str = g_strdup_printf (_("Usage: %s"), _(item->help));
995 empathy_chat_view_append_event (chat->view, str);
1000 chat_command_help (EmpathyChat *chat,
1005 /* If <command> part is not defined,
1006 * strv[1] will be the terminal NULL */
1007 if (strv[1] == NULL) {
1008 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1009 if (commands[i].is_supported != NULL) {
1010 if (!commands[i].is_supported (chat)) {
1014 empathy_chat_view_append_event (chat->view,
1015 _(commands[i].help));
1020 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1021 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1022 if (commands[i].is_supported != NULL) {
1023 if (!commands[i].is_supported (chat)) {
1027 chat_command_show_help (chat, &commands[i]);
1032 empathy_chat_view_append_event (chat->view,
1033 _("Unknown command"));
1037 chat_command_parse (const gchar *text, guint max_parts)
1042 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1044 array = g_ptr_array_sized_new (max_parts + 1);
1045 while (max_parts > 1) {
1048 /* Skip white spaces */
1049 while (g_ascii_isspace (*text)) {
1053 /* Search the end of this part, until first space. */
1054 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1060 item = g_strndup (text, end - text);
1061 g_ptr_array_add (array, item);
1062 DEBUG ("\tITEM: \"%s\"", item);
1068 /* Append last part if not empty */
1069 item = g_strstrip (g_strdup (text));
1070 if (!EMP_STR_EMPTY (item)) {
1071 g_ptr_array_add (array, item);
1072 DEBUG ("\tITEM: \"%s\"", item);
1077 /* Make the array NULL-terminated */
1078 g_ptr_array_add (array, NULL);
1080 return (GStrv) g_ptr_array_free (array, FALSE);
1084 has_prefix_case (const gchar *s,
1085 const gchar *prefix)
1087 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1091 chat_send (EmpathyChat *chat,
1094 EmpathyChatPriv *priv;
1098 if (EMP_STR_EMPTY (msg)) {
1102 priv = GET_PRIV (chat);
1104 chat_input_history_add (chat, msg, FALSE);
1106 if (msg[0] == '/') {
1107 gboolean second_slash = FALSE;
1108 const gchar *iter = msg + 1;
1110 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1115 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1118 c = *(msg + 1 + strlen (commands[i].prefix));
1119 if (c != '\0' && !g_ascii_isspace (c)) {
1122 if (commands[i].is_supported != NULL) {
1123 if (!commands[i].is_supported (chat)) {
1128 /* We can't use g_strsplit here because it does
1129 * not deal correctly if we have more than one space
1131 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1133 strv_len = g_strv_length (strv);
1134 if (strv_len < commands[i].min_parts ||
1135 strv_len > commands[i].max_parts) {
1136 chat_command_show_help (chat, &commands[i]);
1141 commands[i].func (chat, strv);
1146 /* Also allow messages with two slashes before the
1147 * first space, so it is possible to send a /unix/path.
1148 * This heuristic is kind of crap. */
1149 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1151 second_slash = TRUE;
1157 if (!second_slash) {
1158 empathy_chat_view_append_event (chat->view,
1159 _("Unknown command; see /help for the available"
1165 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1167 empathy_tp_chat_send (priv->tp_chat, message);
1168 g_object_unref (message);
1172 chat_input_text_view_send (EmpathyChat *chat)
1174 GtkTextBuffer *buffer;
1175 GtkTextIter start, end;
1178 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1180 gtk_text_buffer_get_bounds (buffer, &start, &end);
1181 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1183 /* clear the input field */
1184 gtk_text_buffer_set_text (buffer, "", -1);
1185 /* delete input history modifications */
1186 chat_input_history_revert (chat);
1188 chat_send (chat, msg);
1193 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1194 EmpathyContact *contact,
1195 TpChannelChatState state,
1198 EmpathyChatPriv *priv;
1200 gboolean was_composing;
1202 priv = GET_PRIV (chat);
1204 if (empathy_contact_is_user (contact)) {
1205 /* We don't care about our own chat state */
1209 was_composing = (priv->compositors != NULL);
1211 /* Find the contact in the list. After that l is the list elem or NULL */
1212 for (l = priv->compositors; l; l = l->next) {
1213 if (contact == l->data) {
1219 case TP_CHANNEL_CHAT_STATE_GONE:
1220 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1221 case TP_CHANNEL_CHAT_STATE_PAUSED:
1222 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1223 /* Contact is not composing */
1225 priv->compositors = g_list_remove_link (priv->compositors, l);
1226 g_object_unref (l->data);
1230 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1231 /* Contact is composing */
1233 priv->compositors = g_list_prepend (priv->compositors,
1234 g_object_ref (contact));
1238 g_assert_not_reached ();
1241 DEBUG ("Was composing: %s now composing: %s",
1242 was_composing ? "yes" : "no",
1243 priv->compositors ? "yes" : "no");
1245 if ((was_composing && !priv->compositors) ||
1246 (!was_composing && priv->compositors)) {
1247 /* Composing state changed */
1248 g_signal_emit (chat, signals[COMPOSING], 0,
1249 priv->compositors != NULL);
1254 chat_message_received (EmpathyChat *chat,
1255 EmpathyMessage *message,
1258 EmpathyChatPriv *priv = GET_PRIV (chat);
1259 EmpathyContact *sender;
1261 sender = empathy_message_get_sender (message);
1263 DEBUG ("Appending new message from %s (%d)",
1264 empathy_contact_get_alias (sender),
1265 empathy_contact_get_handle (sender));
1267 empathy_chat_view_append_message (chat->view, message);
1269 /* We received a message so the contact is no longer composing */
1270 chat_state_changed_cb (priv->tp_chat, sender,
1271 TP_CHANNEL_CHAT_STATE_ACTIVE,
1274 priv->unread_messages++;
1275 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1279 chat_message_received_cb (EmpathyTpChat *tp_chat,
1280 EmpathyMessage *message,
1283 chat_message_received (chat, message, FALSE);
1287 chat_send_error_cb (EmpathyTpChat *tp_chat,
1288 const gchar *message_body,
1289 TpChannelTextSendError error_code,
1290 const gchar *dbus_error,
1293 const gchar *error = NULL;
1296 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1297 error = _("insufficient balance to send message");
1298 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1299 error = _("not capable");
1302 if (error == NULL) {
1303 /* if we didn't find a dbus-error, try the old error */
1304 switch (error_code) {
1305 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1306 error = _("offline");
1308 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1309 error = _("invalid contact");
1311 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1312 error = _("permission denied");
1314 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1315 error = _("too long message");
1317 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1318 error = _("not implemented");
1320 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1322 error = _("unknown");
1327 if (message_body != NULL) {
1328 str = g_strdup_printf (_("Error sending message '%s': %s"),
1329 message_body, error);
1332 str = g_strdup_printf (_("Error sending message: %s"), error);
1335 empathy_chat_view_append_event (chat->view, str);
1340 chat_topic_label_size_allocate_cb (GtkLabel *label,
1341 GtkAllocation *allocation,
1344 EmpathyChatPriv *priv = GET_PRIV (chat);
1346 if (!gtk_label_get_line_wrap (label)) {
1347 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1348 gtk_widget_show (priv->expander_topic);
1350 gtk_widget_hide (priv->expander_topic);
1357 chat_topic_expander_activate_cb (GtkExpander *expander,
1358 GParamSpec *param_spec,
1361 EmpathyChatPriv *priv = GET_PRIV (chat);
1363 if (gtk_expander_get_expanded (expander)) {
1364 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1365 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1367 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1368 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1373 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1378 EmpathyChatPriv *priv = GET_PRIV (chat);
1380 if (!tp_strdiff (name, "subject")) {
1381 g_free (priv->subject);
1382 priv->subject = g_value_dup_string (value);
1383 g_object_notify (G_OBJECT (chat), "subject");
1385 if (EMP_STR_EMPTY (priv->subject)) {
1386 gtk_widget_hide (priv->hbox_topic);
1388 gchar *markup_topic;
1391 markup_topic = empathy_add_link_markup (priv->subject);
1392 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1393 _("Topic:"), markup_topic);
1395 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1396 g_free (markup_text);
1397 g_free (markup_topic);
1399 gtk_widget_show (priv->hbox_topic);
1401 if (priv->block_events_timeout_id == 0) {
1404 if (!EMP_STR_EMPTY (priv->subject)) {
1405 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1407 str = g_strdup (_("No topic defined"));
1409 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1413 else if (!tp_strdiff (name, "name")) {
1414 g_free (priv->name);
1415 priv->name = g_value_dup_string (value);
1416 g_object_notify (G_OBJECT (chat), "name");
1421 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1425 GtkTextIter word_start = *iter;
1426 GtkTextIter word_end = *iter;
1429 if (gtk_text_iter_inside_word (&word_end) &&
1430 !gtk_text_iter_ends_word (&word_end)) {
1431 gtk_text_iter_forward_word_end (&word_end);
1436 if (gtk_text_iter_get_char (&tmp) == '\'') {
1437 gtk_text_iter_forward_char (&tmp);
1439 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1440 gtk_text_iter_forward_word_end (&word_end);
1445 if (gtk_text_iter_inside_word (&word_start) ||
1446 gtk_text_iter_ends_word (&word_start)) {
1447 if (!gtk_text_iter_starts_word (&word_start) ||
1448 gtk_text_iter_equal (&word_start, &word_end)) {
1449 gtk_text_iter_backward_word_start (&word_start);
1453 gtk_text_iter_backward_char (&tmp);
1455 if (gtk_text_iter_get_char (&tmp) == '\'') {
1456 gtk_text_iter_backward_char (&tmp);
1458 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1459 gtk_text_iter_backward_word_start (&word_start);
1464 *start = word_start;
1470 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1471 GtkTextIter *location,
1476 GtkTextIter iter, pos;
1478 /* Remove all misspelled tags in the inserted text.
1479 * This happens when text is inserted within a misspelled word. */
1480 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1481 gtk_text_iter_get_offset (location) - len);
1482 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1485 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1488 GtkTextIter start, end;
1491 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1494 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1496 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1497 gtk_text_iter_equal (&pos, &end) ||
1498 empathy_spell_check (str)) {
1499 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1501 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1506 } while (gtk_text_iter_forward_word_end (&iter) &&
1507 gtk_text_iter_compare (&iter, location) <= 0);
1511 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1516 GtkTextIter word_start, word_end;
1518 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1519 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1520 &word_start, &word_end);
1525 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1528 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1529 chat_composing_stop (chat);
1531 chat_composing_start (chat);
1536 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1541 GtkTextIter prev_pos;
1542 GtkTextIter word_start;
1543 GtkTextIter word_end;
1547 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1549 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1550 gtk_text_buffer_get_insert (buffer));
1551 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1553 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1556 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1557 !gtk_text_iter_equal (&pos, &word_end)) {
1558 str = gtk_text_buffer_get_text (buffer,
1559 &word_start, &word_end, FALSE);
1561 if (!empathy_spell_check (str)) {
1562 gtk_text_buffer_apply_tag_by_name (buffer,
1563 "misspelled", &word_start, &word_end);
1565 gtk_text_buffer_remove_tag_by_name (buffer,
1566 "misspelled", &word_start, &word_end);
1573 gtk_text_buffer_move_mark (buffer, mark, &pos);
1577 empathy_isspace_cb (gunichar c,
1580 return g_unichar_isspace (c);
1584 chat_input_key_press_event_cb (GtkWidget *widget,
1588 EmpathyChatPriv *priv;
1591 GtkWidget *text_view_sw;
1593 priv = GET_PRIV (chat);
1595 /* Catch ctrl+up/down so we can traverse messages we sent */
1596 if ((event->state & GDK_CONTROL_MASK) &&
1597 (event->keyval == GDK_KEY_Up ||
1598 event->keyval == GDK_KEY_Down)) {
1599 GtkTextBuffer *buffer;
1602 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1603 chat_input_history_update (chat, buffer);
1605 if (event->keyval == GDK_KEY_Up) {
1606 str = chat_input_history_get_next (chat);
1608 str = chat_input_history_get_prev (chat);
1611 g_signal_handlers_block_by_func (buffer,
1612 chat_input_text_buffer_changed_cb,
1614 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1615 g_signal_handlers_unblock_by_func (buffer,
1616 chat_input_text_buffer_changed_cb,
1622 /* Catch enter but not ctrl/shift-enter */
1623 if (IS_ENTER (event->keyval) &&
1624 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1627 /* This is to make sure that kinput2 gets the enter. And if
1628 * it's handled there we shouldn't send on it. This is because
1629 * kinput2 uses Enter to commit letters. See:
1630 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1633 view = GTK_TEXT_VIEW (chat->input_text_view);
1634 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1635 gtk_text_view_reset_im_context (view);
1639 chat_input_text_view_send (chat);
1643 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1645 if (IS_ENTER (event->keyval) &&
1646 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1647 /* Newline for shift/control-enter. */
1650 if (!(event->state & GDK_CONTROL_MASK) &&
1651 event->keyval == GDK_KEY_Page_Up) {
1652 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1653 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1656 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1657 event->keyval == GDK_KEY_Page_Down) {
1658 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1659 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1660 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1661 gtk_adjustment_set_value (adj, val);
1664 if (event->keyval == GDK_KEY_Escape) {
1665 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1667 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1668 event->keyval == GDK_KEY_Tab) {
1669 GtkTextBuffer *buffer;
1670 GtkTextIter start, current;
1671 gchar *nick, *completed;
1672 GList *list, *completed_list;
1673 gboolean is_start_of_buffer;
1675 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1676 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1678 /* Get the start of the nick to complete. */
1679 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1680 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1681 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1683 is_start_of_buffer = gtk_text_iter_is_start (&start);
1685 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1686 g_completion_add_items (priv->completion, list);
1688 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1689 completed_list = g_completion_complete (priv->completion,
1698 GString *message = NULL;
1701 gtk_text_buffer_delete (buffer, &start, ¤t);
1703 len = g_list_length (completed_list);
1706 /* If we only have one hit, use that text
1707 * instead of the text in completed since the
1708 * completed text will use the typed string
1709 * which might be cased all wrong.
1712 text = empathy_contact_get_alias (completed_list->data);
1716 /* Print all hits to the scrollback view, so the
1717 * user knows what possibilities he has.
1720 message = g_string_new ("");
1721 for (l = completed_list; l != NULL; l = l->next) {
1722 g_string_append (message, empathy_contact_get_alias (l->data));
1723 g_string_append (message, " - ");
1725 empathy_chat_view_append_event (chat->view, message->str);
1726 g_string_free (message, TRUE);
1729 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1731 if (len == 1 && is_start_of_buffer) {
1732 gchar *complete_char;
1734 complete_char = g_settings_get_string (
1735 priv->gsettings_chat,
1736 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1738 if (complete_char != NULL) {
1739 gtk_text_buffer_insert_at_cursor (buffer,
1741 strlen (complete_char));
1742 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1743 g_free (complete_char);
1750 g_completion_clear_items (priv->completion);
1752 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1762 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1766 gtk_widget_grab_focus (chat->input_text_view);
1772 chat_input_realize_cb (GtkWidget *widget,
1775 DEBUG ("Setting focus to the input text view");
1776 if (gtk_widget_is_sensitive (widget)) {
1777 gtk_widget_grab_focus (widget);
1782 chat_input_has_focus_notify_cb (GtkWidget *widget,
1786 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1790 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1791 EmpathySmiley *smiley,
1794 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1795 GtkTextBuffer *buffer;
1798 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1800 gtk_text_buffer_get_end_iter (buffer, &iter);
1801 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1803 gtk_text_buffer_get_end_iter (buffer, &iter);
1804 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1815 static EmpathyChatSpell *
1816 chat_spell_new (EmpathyChat *chat,
1821 EmpathyChatSpell *chat_spell;
1823 chat_spell = g_slice_new0 (EmpathyChatSpell);
1825 chat_spell->chat = g_object_ref (chat);
1826 chat_spell->word = g_strdup (word);
1827 chat_spell->start = start;
1828 chat_spell->end = end;
1834 chat_spell_free (EmpathyChatSpell *chat_spell)
1836 g_object_unref (chat_spell->chat);
1837 g_free (chat_spell->word);
1838 g_slice_free (EmpathyChatSpell, chat_spell);
1842 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1843 EmpathyChatSpell *chat_spell)
1845 empathy_chat_correct_word (chat_spell->chat,
1846 &(chat_spell->start),
1848 gtk_menu_item_get_label (menu_item));
1853 chat_spelling_build_suggestions_menu (const gchar *code,
1854 EmpathyChatSpell *chat_spell)
1856 GList *suggestions, *l;
1857 GtkWidget *menu, *menu_item;
1859 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1860 if (suggestions == NULL)
1863 menu = gtk_menu_new ();
1864 for (l = suggestions; l; l = l->next) {
1865 menu_item = gtk_menu_item_new_with_label (l->data);
1866 g_signal_connect (G_OBJECT (menu_item), "activate",
1867 G_CALLBACK (chat_spelling_menu_activate_cb),
1869 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1871 empathy_spell_free_suggestions (suggestions);
1873 gtk_widget_show_all (menu);
1879 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1881 GtkWidget *menu, *submenu, *item;
1884 codes = empathy_spell_get_enabled_language_codes ();
1885 g_assert (codes != NULL);
1887 if (g_list_length (codes) > 1) {
1888 menu = gtk_menu_new ();
1890 for (l = codes; l; l = l->next) {
1891 const gchar *code, *name;
1894 name = empathy_spell_get_language_name (code);
1898 item = gtk_image_menu_item_new_with_label (name);
1900 submenu = chat_spelling_build_suggestions_menu (
1902 if (submenu == NULL)
1903 gtk_widget_set_sensitive (item, FALSE);
1905 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1907 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1910 menu = chat_spelling_build_suggestions_menu (codes->data,
1913 menu = gtk_menu_new ();
1914 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1915 gtk_widget_set_sensitive (item, FALSE);
1916 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1919 g_list_free (codes);
1921 gtk_widget_show_all (menu);
1932 static EmpathyChatWord *
1933 chat_word_new (EmpathyChat *chat,
1937 EmpathyChatWord *chat_word;
1939 chat_word = g_slice_new0 (EmpathyChatWord);
1941 chat_word->chat = g_object_ref (chat);
1942 chat_word->word = g_strdup (word);
1943 chat_word->code = g_strdup (code);
1949 chat_word_free (EmpathyChatWord *chat_word)
1951 g_object_unref (chat_word->chat);
1952 g_free (chat_word->word);
1953 g_free (chat_word->code);
1954 g_slice_free (EmpathyChatWord, chat_word);
1958 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1959 EmpathyChatWord *chat_word)
1961 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1963 empathy_spell_add_to_dictionary (chat_word->code,
1965 priv->update_misspelled_words_id =
1966 g_idle_add (update_misspelled_words, chat_word->chat);
1970 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1972 GtkWidget *menu, *item, *lang_item, *image;
1975 const gchar *code, *name;
1976 EmpathyChatWord *chat_word;
1978 codes = empathy_spell_get_enabled_language_codes ();
1979 g_assert (codes != NULL);
1980 if (g_list_length (codes) > 1) {
1981 /* translators: %s is the selected word */
1982 label = g_strdup_printf (_("Add '%s' to Dictionary"),
1984 item = gtk_image_menu_item_new_with_mnemonic (label);
1986 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1987 GTK_ICON_SIZE_MENU);
1988 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1991 menu = gtk_menu_new ();
1993 for (l = codes; l; l = l->next) {
1995 name = empathy_spell_get_language_name (code);
1999 lang_item = gtk_image_menu_item_new_with_label (name);
2001 chat_word= chat_word_new (chat_spell->chat,
2002 chat_spell->word, code);
2003 g_object_set_data_full (G_OBJECT (lang_item),
2004 "chat-word", chat_word,
2005 (GDestroyNotify) chat_word_free);
2007 g_signal_connect (G_OBJECT (lang_item), "activate",
2008 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2010 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2012 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2015 name = empathy_spell_get_language_name (code);
2016 g_assert (name != NULL);
2017 /* translators: first %s is the selected word,
2018 * second %s is the language name of the target dictionary */
2019 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2020 chat_spell->word, name);
2021 item = gtk_image_menu_item_new_with_mnemonic (label);
2023 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2024 GTK_ICON_SIZE_MENU);
2025 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2027 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2029 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2030 (GDestroyNotify) chat_word_free);
2032 g_signal_connect (G_OBJECT (item), "activate",
2033 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2036 g_list_free (codes);
2038 gtk_widget_show_all (item);
2044 chat_text_send_cb (GtkMenuItem *menuitem,
2047 chat_input_text_view_send (chat);
2051 chat_input_populate_popup_cb (GtkTextView *view,
2055 GtkTextBuffer *buffer;
2056 GtkTextTagTable *table;
2059 GtkTextIter iter, start, end;
2062 EmpathyChatSpell *chat_spell;
2063 GtkWidget *spell_menu;
2064 GtkWidget *spell_item;
2065 EmpathySmileyManager *smiley_manager;
2066 GtkWidget *smiley_menu;
2069 buffer = gtk_text_view_get_buffer (view);
2071 /* Add the emoticon menu. */
2072 item = gtk_separator_menu_item_new ();
2073 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2074 gtk_widget_show (item);
2076 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2077 image = gtk_image_new_from_icon_name ("face-smile",
2078 GTK_ICON_SIZE_MENU);
2079 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2080 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2081 gtk_widget_show (item);
2083 smiley_manager = empathy_smiley_manager_dup_singleton ();
2084 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2085 chat_insert_smiley_activate_cb,
2087 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2088 g_object_unref (smiley_manager);
2090 /* Add the Send menu item. */
2091 gtk_text_buffer_get_bounds (buffer, &start, &end);
2092 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2093 if (!EMP_STR_EMPTY (str)) {
2094 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2095 g_signal_connect (G_OBJECT (item), "activate",
2096 G_CALLBACK (chat_text_send_cb), chat);
2097 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2098 gtk_widget_show (item);
2102 /* Add the spell check menu item. */
2103 table = gtk_text_buffer_get_tag_table (buffer);
2104 tag = gtk_text_tag_table_lookup (table, "misspelled");
2105 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2106 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2107 GTK_TEXT_WINDOW_WIDGET,
2110 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2112 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2113 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2115 str = gtk_text_buffer_get_text (buffer,
2116 &start, &end, FALSE);
2118 if (!EMP_STR_EMPTY (str)) {
2119 chat_spell = chat_spell_new (chat, str, start, end);
2120 g_object_set_data_full (G_OBJECT (menu),
2121 "chat-spell", chat_spell,
2122 (GDestroyNotify) chat_spell_free);
2124 item = gtk_separator_menu_item_new ();
2125 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2126 gtk_widget_show (item);
2128 /* Spelling suggestions */
2129 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2130 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2131 GTK_ICON_SIZE_MENU);
2132 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2134 spell_menu = chat_spelling_build_menu (chat_spell);
2135 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2138 spell_item = gtk_separator_menu_item_new ();
2139 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2140 gtk_widget_show (spell_item);
2142 /* Add to dictionary */
2143 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2145 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2146 gtk_widget_show (spell_item);
2148 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2149 gtk_widget_show (item);
2155 chat_log_filter (TplEvent *log,
2158 EmpathyChat *chat = user_data;
2159 EmpathyMessage *message;
2160 EmpathyChatPriv *priv = GET_PRIV (chat);
2161 const GList *pending;
2163 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2164 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2166 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2167 message = empathy_message_from_tpl_log_event (log);
2169 for (; pending; pending = g_list_next (pending)) {
2170 if (empathy_message_equal (message, pending->data)) {
2171 g_object_unref (message);
2176 g_object_unref (message);
2182 show_pending_messages (EmpathyChat *chat) {
2183 EmpathyChatPriv *priv = GET_PRIV (chat);
2184 const GList *messages, *l;
2186 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2188 if (chat->view == NULL || priv->tp_chat == NULL)
2191 if (!priv->can_show_pending)
2194 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2196 for (l = messages; l != NULL ; l = g_list_next (l)) {
2197 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2198 chat_message_received (chat, message, TRUE);
2204 got_filtered_messages_cb (GObject *manager,
2205 GAsyncResult *result,
2210 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2211 EmpathyChatPriv *priv = GET_PRIV (chat);
2212 GError *error = NULL;
2214 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2215 result, &messages, &error)) {
2216 DEBUG ("%s. Aborting.", error->message);
2217 empathy_chat_view_append_event (chat->view,
2218 _("Failed to retrieve recent logs"));
2219 g_error_free (error);
2223 for (l = messages; l; l = g_list_next (l)) {
2224 EmpathyMessage *message;
2225 g_assert (TPL_IS_EVENT (l->data));
2227 message = empathy_message_from_tpl_log_event (l->data);
2228 g_object_unref (l->data);
2230 empathy_chat_view_append_message (chat->view, message);
2231 g_object_unref (message);
2233 g_list_free (messages);
2236 /* in case of TPL error, skip backlog and show pending messages */
2237 priv->can_show_pending = TRUE;
2238 show_pending_messages (chat);
2240 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2241 * about it in EmpathyChatPriv definition */
2242 priv->retrieving_backlogs = FALSE;
2243 empathy_chat_messages_read (chat);
2245 /* Turn back on scrolling */
2246 empathy_chat_view_scroll (chat->view, TRUE);
2250 chat_add_logs (EmpathyChat *chat)
2252 EmpathyChatPriv *priv = GET_PRIV (chat);
2259 /* Turn off scrolling temporarily */
2260 empathy_chat_view_scroll (chat->view, FALSE);
2262 /* Add messages from last conversation */
2263 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2264 target = tpl_entity_new_from_room_id (priv->id);
2266 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2268 priv->retrieving_backlogs = TRUE;
2269 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2272 TPL_EVENT_MASK_TEXT,
2276 got_filtered_messages_cb,
2279 g_object_unref (target);
2283 chat_contacts_completion_func (const gchar *s1,
2287 gchar *tmp, *nick1, *nick2;
2294 return s1 ? -1 : +1;
2297 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2298 nick1 = g_utf8_casefold (tmp, -1);
2301 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2302 nick2 = g_utf8_casefold (tmp, -1);
2305 ret = strncmp (nick1, nick2, n);
2314 build_part_message (guint reason,
2316 EmpathyContact *actor,
2317 const gchar *message)
2319 GString *s = g_string_new ("");
2320 const gchar *actor_name = NULL;
2322 if (actor != NULL) {
2323 actor_name = empathy_contact_get_alias (actor);
2326 /* Having an actor only really makes sense for a few actions... */
2328 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2329 g_string_append_printf (s, _("%s has disconnected"), name);
2331 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2332 if (actor_name != NULL) {
2333 /* translators: reverse the order of these arguments
2334 * if the kicked should come before the kicker in your locale.
2336 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2339 g_string_append_printf (s, _("%s was kicked"), name);
2342 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2343 if (actor_name != NULL) {
2344 /* translators: reverse the order of these arguments
2345 * if the banned should come before the banner in your locale.
2347 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2350 g_string_append_printf (s, _("%s was banned"), name);
2354 g_string_append_printf (s, _("%s has left the room"), name);
2357 if (!EMP_STR_EMPTY (message)) {
2358 /* Note to translators: this string is appended to
2359 * notifications like "foo has left the room", with the message
2360 * given by the user living the room. If this poses a problem,
2361 * please let us know. :-)
2363 g_string_append_printf (s, _(" (%s)"), message);
2366 return g_string_free (s, FALSE);
2370 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2371 EmpathyContact *contact,
2372 EmpathyContact *actor,
2378 EmpathyChatPriv *priv = GET_PRIV (chat);
2379 const gchar *name = empathy_contact_get_alias (contact);
2382 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2384 if (priv->block_events_timeout_id != 0)
2388 str = g_strdup_printf (_("%s has joined the room"),
2391 str = build_part_message (reason, name, actor, message);
2394 empathy_chat_view_append_event (chat->view, str);
2399 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2400 EmpathyContact *old_contact,
2401 EmpathyContact *new_contact,
2406 EmpathyChatPriv *priv = GET_PRIV (chat);
2408 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2410 if (priv->block_events_timeout_id == 0) {
2413 str = g_strdup_printf (_("%s is now known as %s"),
2414 empathy_contact_get_alias (old_contact),
2415 empathy_contact_get_alias (new_contact));
2416 empathy_chat_view_append_event (chat->view, str);
2423 chat_reset_size_request (gpointer widget)
2425 gtk_widget_set_size_request (widget, -1, -1);
2431 chat_update_contacts_visibility (EmpathyChat *chat,
2434 EmpathyChatPriv *priv = GET_PRIV (chat);
2435 GtkAllocation allocation;
2437 if (!priv->scrolled_window_contacts) {
2441 if (priv->remote_contact != NULL) {
2445 if (show && priv->contact_list_view == NULL) {
2446 EmpathyContactListStore *store;
2449 /* We are adding the contact list to the chat, we don't want the
2450 * chat view to become too small. If the chat view is already
2451 * smaller than 250 make sure that size won't change. If the
2452 * chat view is bigger the contact list will take some space on
2453 * it but we make sure the chat view don't become smaller than
2454 * 250. Relax the size request once the resize is done */
2455 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2456 min_width = MIN (allocation.width, 250);
2457 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2458 g_idle_add (chat_reset_size_request, priv->vbox_left);
2460 if (priv->contacts_width > 0) {
2461 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2462 priv->contacts_width);
2465 store = empathy_contact_list_store_new (
2466 EMPATHY_CONTACT_LIST (priv->tp_chat));
2467 empathy_contact_list_store_set_show_groups (
2468 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2470 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2471 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2472 EMPATHY_CONTACT_FEATURE_CHAT |
2473 EMPATHY_CONTACT_FEATURE_CALL |
2474 EMPATHY_CONTACT_FEATURE_LOG |
2475 EMPATHY_CONTACT_FEATURE_INFO));
2476 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2477 priv->contact_list_view);
2478 gtk_widget_show (priv->contact_list_view);
2479 gtk_widget_show (priv->scrolled_window_contacts);
2480 g_object_unref (store);
2482 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2483 gtk_widget_hide (priv->scrolled_window_contacts);
2484 if (priv->contact_list_view != NULL) {
2485 gtk_widget_destroy (priv->contact_list_view);
2486 priv->contact_list_view = NULL;
2492 empathy_chat_set_show_contacts (EmpathyChat *chat,
2495 EmpathyChatPriv *priv = GET_PRIV (chat);
2497 priv->show_contacts = show;
2499 chat_update_contacts_visibility (chat, show);
2501 g_object_notify (G_OBJECT (chat), "show-contacts");
2505 chat_remote_contact_changed_cb (EmpathyChat *chat)
2507 EmpathyChatPriv *priv = GET_PRIV (chat);
2509 if (priv->remote_contact != NULL) {
2510 g_object_unref (priv->remote_contact);
2511 priv->remote_contact = NULL;
2516 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2517 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2518 if (priv->remote_contact != NULL) {
2519 g_object_ref (priv->remote_contact);
2520 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2522 else if (priv->tp_chat != NULL) {
2525 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2526 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2529 chat_update_contacts_visibility (chat, priv->show_contacts);
2531 g_object_notify (G_OBJECT (chat), "remote-contact");
2532 g_object_notify (G_OBJECT (chat), "id");
2536 chat_destroy_cb (EmpathyTpChat *tp_chat,
2539 EmpathyChatPriv *priv;
2541 priv = GET_PRIV (chat);
2543 if (!priv->tp_chat) {
2547 chat_composing_remove_timeout (chat);
2548 g_object_unref (priv->tp_chat);
2549 priv->tp_chat = NULL;
2550 g_object_notify (G_OBJECT (chat), "tp-chat");
2552 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2553 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2555 chat_update_contacts_visibility (chat, FALSE);
2559 update_misspelled_words (gpointer data)
2561 EmpathyChat *chat = EMPATHY_CHAT (data);
2562 EmpathyChatPriv *priv = GET_PRIV (chat);
2563 GtkTextBuffer *buffer;
2567 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2569 gtk_text_buffer_get_end_iter (buffer, &iter);
2570 length = gtk_text_iter_get_offset (&iter);
2571 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2572 NULL, length, chat);
2574 priv->update_misspelled_words_id = 0;
2580 conf_spell_checking_cb (GSettings *gsettings_chat,
2584 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2585 EmpathyChatPriv *priv = GET_PRIV (chat);
2586 gboolean spell_checker;
2587 GtkTextBuffer *buffer;
2589 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2592 spell_checker = g_settings_get_boolean (gsettings_chat,
2593 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2595 if (!empathy_spell_supported ()) {
2596 spell_checker = FALSE;
2599 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2601 if (spell_checker == priv->spell_checking_enabled) {
2602 if (spell_checker) {
2603 /* Possibly changed dictionaries,
2604 * update misspelled words. Need to do so in idle
2605 * so the spell checker is updated. */
2606 priv->update_misspelled_words_id =
2607 g_idle_add (update_misspelled_words, chat);
2613 if (spell_checker) {
2616 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2617 buffer, "notify::cursor-position",
2618 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2620 priv->insert_text_id = tp_g_signal_connect_object (
2621 buffer, "insert-text",
2622 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2623 chat, G_CONNECT_AFTER);
2624 priv->delete_range_id = tp_g_signal_connect_object (
2625 buffer, "delete-range",
2626 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2627 chat, G_CONNECT_AFTER);
2629 gtk_text_buffer_create_tag (buffer, "misspelled",
2630 "underline", PANGO_UNDERLINE_ERROR,
2633 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2634 gtk_text_buffer_get_insert (buffer));
2635 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2638 /* Mark misspelled words in the existing buffer.
2639 * Need to do so in idle so the spell checker is updated. */
2640 priv->update_misspelled_words_id =
2641 g_idle_add (update_misspelled_words, chat);
2643 GtkTextTagTable *table;
2646 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2647 priv->notify_cursor_position_id = 0;
2648 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2649 priv->insert_text_id = 0;
2650 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2651 priv->delete_range_id = 0;
2653 table = gtk_text_buffer_get_tag_table (buffer);
2654 tag = gtk_text_tag_table_lookup (table, "misspelled");
2655 gtk_text_tag_table_remove (table, tag);
2657 gtk_text_buffer_delete_mark_by_name (buffer,
2658 "previous-cursor-position");
2661 priv->spell_checking_enabled = spell_checker;
2665 save_paned_pos_timeout (gpointer data)
2667 EmpathyChat *self = data;
2670 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2672 g_settings_set_int (self->priv->gsettings_ui,
2673 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2680 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2684 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2686 if (chat->priv->save_paned_pos_id != 0)
2687 g_source_remove (chat->priv->save_paned_pos_id);
2689 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2690 save_paned_pos_timeout, chat);
2696 chat_create_ui (EmpathyChat *chat)
2698 EmpathyChatPriv *priv = GET_PRIV (chat);
2702 GtkTextBuffer *buffer;
2704 EmpathyThemeManager *theme_mgr;
2706 filename = empathy_file_lookup ("empathy-chat.ui",
2708 gui = empathy_builder_get_file (filename,
2709 "chat_widget", &priv->widget,
2710 "hpaned", &priv->hpaned,
2711 "vbox_left", &priv->vbox_left,
2712 "scrolled_window_chat", &priv->scrolled_window_chat,
2713 "scrolled_window_input", &priv->scrolled_window_input,
2714 "hbox_topic", &priv->hbox_topic,
2715 "expander_topic", &priv->expander_topic,
2716 "label_topic", &priv->label_topic,
2717 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2718 "info_bar_vbox", &priv->info_bar_vbox,
2721 empathy_builder_connect (gui, chat,
2722 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2723 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2728 /* Add message view. */
2729 theme_mgr = empathy_theme_manager_dup_singleton ();
2730 chat->view = empathy_theme_manager_create_view (theme_mgr);
2731 g_object_unref (theme_mgr);
2732 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2733 and other types, even though it's non-editable and doesn't accept any
2734 drags. This steals drag motion for anything inside the scrollbars,
2735 making drag destinations on chat windows far less useful.
2737 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2738 g_signal_connect (chat->view, "focus_in_event",
2739 G_CALLBACK (chat_text_view_focus_in_event_cb),
2741 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2742 GTK_WIDGET (chat->view));
2743 gtk_widget_show (GTK_WIDGET (chat->view));
2745 /* Add input GtkTextView */
2746 chat->input_text_view = empathy_input_text_view_new ();
2747 g_signal_connect (chat->input_text_view, "notify::has-focus",
2748 G_CALLBACK (chat_input_has_focus_notify_cb),
2750 g_signal_connect (chat->input_text_view, "key-press-event",
2751 G_CALLBACK (chat_input_key_press_event_cb),
2753 g_signal_connect (chat->input_text_view, "realize",
2754 G_CALLBACK (chat_input_realize_cb),
2756 g_signal_connect (chat->input_text_view, "populate-popup",
2757 G_CALLBACK (chat_input_populate_popup_cb),
2759 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2760 tp_g_signal_connect_object (buffer, "changed",
2761 G_CALLBACK (chat_input_text_buffer_changed_cb),
2763 tp_g_signal_connect_object (priv->gsettings_chat,
2764 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2765 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2766 conf_spell_checking_cb (priv->gsettings_chat,
2767 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2768 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2769 chat->input_text_view);
2770 gtk_widget_show (chat->input_text_view);
2772 /* Add the (invisible) search bar */
2773 priv->search_bar = empathy_search_bar_new (chat->view);
2774 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2777 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2779 /* Initialy hide the topic, will be shown if not empty */
2780 gtk_widget_hide (priv->hbox_topic);
2782 g_signal_connect (priv->hpaned, "notify::position",
2783 G_CALLBACK (chat_hpaned_pos_changed_cb),
2786 /* Load the paned position */
2787 paned_pos = g_settings_get_int (priv->gsettings_ui,
2788 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2790 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2792 /* Set widget focus order */
2793 list = g_list_append (NULL, priv->search_bar);
2794 list = g_list_append (list, priv->scrolled_window_input);
2795 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2798 list = g_list_append (NULL, priv->vbox_left);
2799 list = g_list_append (list, priv->scrolled_window_contacts);
2800 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2803 list = g_list_append (NULL, priv->hpaned);
2804 list = g_list_append (list, priv->hbox_topic);
2805 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2808 /* Add the main widget in the chat widget */
2809 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2810 g_object_unref (gui);
2814 chat_size_allocate (GtkWidget *widget,
2815 GtkAllocation *allocation)
2817 GtkBin *bin = GTK_BIN (widget);
2818 GtkAllocation child_allocation;
2821 gtk_widget_set_allocation (widget, allocation);
2823 child = gtk_bin_get_child (bin);
2825 if (child && gtk_widget_get_visible (child))
2827 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2828 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2829 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2830 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2832 gtk_widget_size_allocate (child, &child_allocation);
2837 chat_finalize (GObject *object)
2840 EmpathyChatPriv *priv;
2842 chat = EMPATHY_CHAT (object);
2843 priv = GET_PRIV (chat);
2845 DEBUG ("Finalized: %p", object);
2847 if (priv->update_misspelled_words_id != 0)
2848 g_source_remove (priv->update_misspelled_words_id);
2850 if (priv->save_paned_pos_id != 0)
2851 g_source_remove (priv->save_paned_pos_id);
2853 g_object_unref (priv->gsettings_chat);
2854 g_object_unref (priv->gsettings_ui);
2856 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2857 g_list_free (priv->input_history);
2859 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2860 g_list_free (priv->compositors);
2862 chat_composing_remove_timeout (chat);
2864 g_object_unref (priv->account_manager);
2865 g_object_unref (priv->log_manager);
2867 if (priv->tp_chat) {
2868 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2869 chat_destroy_cb, chat);
2870 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2871 chat_message_received_cb, chat);
2872 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2873 chat_send_error_cb, chat);
2874 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2875 chat_state_changed_cb, chat);
2876 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2877 chat_property_changed_cb, chat);
2878 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2879 chat_members_changed_cb, chat);
2880 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2881 chat_remote_contact_changed_cb, chat);
2882 empathy_tp_chat_leave (priv->tp_chat, "");
2883 g_object_unref (priv->tp_chat);
2885 if (priv->account) {
2886 g_object_unref (priv->account);
2888 if (priv->remote_contact) {
2889 g_object_unref (priv->remote_contact);
2892 if (priv->block_events_timeout_id) {
2893 g_source_remove (priv->block_events_timeout_id);
2897 g_free (priv->name);
2898 g_free (priv->subject);
2899 g_completion_free (priv->completion);
2901 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2905 chat_constructed (GObject *object)
2907 EmpathyChat *chat = EMPATHY_CHAT (object);
2908 EmpathyChatPriv *priv = GET_PRIV (chat);
2910 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2911 /* First display logs from the logger and then display pending messages */
2912 chat_add_logs (chat);
2915 /* Just display pending messages for rooms */
2916 priv->can_show_pending = TRUE;
2917 show_pending_messages (chat);
2922 empathy_chat_class_init (EmpathyChatClass *klass)
2924 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2925 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2927 object_class->finalize = chat_finalize;
2928 object_class->get_property = chat_get_property;
2929 object_class->set_property = chat_set_property;
2930 object_class->constructed = chat_constructed;
2932 widget_class->size_allocate = chat_size_allocate;
2934 g_object_class_install_property (object_class,
2936 g_param_spec_object ("tp-chat",
2938 "The tp chat object",
2939 EMPATHY_TYPE_TP_CHAT,
2942 G_PARAM_STATIC_STRINGS));
2943 g_object_class_install_property (object_class,
2945 g_param_spec_object ("account",
2946 "Account of the chat",
2947 "The account of the chat",
2950 G_PARAM_STATIC_STRINGS));
2951 g_object_class_install_property (object_class,
2953 g_param_spec_string ("id",
2955 "The id of the chat",
2958 G_PARAM_STATIC_STRINGS));
2959 g_object_class_install_property (object_class,
2961 g_param_spec_string ("name",
2963 "The name of the chat",
2966 G_PARAM_STATIC_STRINGS));
2967 g_object_class_install_property (object_class,
2969 g_param_spec_string ("subject",
2971 "The subject or topic of the chat",
2974 G_PARAM_STATIC_STRINGS));
2975 g_object_class_install_property (object_class,
2976 PROP_REMOTE_CONTACT,
2977 g_param_spec_object ("remote-contact",
2978 "The remote contact",
2979 "The remote contact is any",
2980 EMPATHY_TYPE_CONTACT,
2982 G_PARAM_STATIC_STRINGS));
2983 g_object_class_install_property (object_class,
2985 g_param_spec_boolean ("show-contacts",
2986 "Contacts' visibility",
2987 "The visibility of the contacts' list",
2990 G_PARAM_STATIC_STRINGS));
2992 g_object_class_install_property (object_class,
2994 g_param_spec_boolean ("sms-channel",
2996 "TRUE if this channel is for sending SMSes",
2998 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3000 g_object_class_install_property (object_class,
3001 PROP_N_MESSAGES_SENDING,
3002 g_param_spec_uint ("n-messages-sending",
3003 "Num Messages Sending",
3004 "The number of messages being sent",
3006 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3008 signals[COMPOSING] =
3009 g_signal_new ("composing",
3010 G_OBJECT_CLASS_TYPE (object_class),
3014 g_cclosure_marshal_VOID__BOOLEAN,
3018 signals[NEW_MESSAGE] =
3019 g_signal_new ("new-message",
3020 G_OBJECT_CLASS_TYPE (object_class),
3024 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3026 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3028 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3032 chat_block_events_timeout_cb (gpointer data)
3034 EmpathyChatPriv *priv = GET_PRIV (data);
3036 priv->block_events_timeout_id = 0;
3042 account_manager_prepared_cb (GObject *source_object,
3043 GAsyncResult *result,
3046 GList *accounts, *l;
3047 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3048 EmpathyChat *chat = user_data;
3049 GError *error = NULL;
3051 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3052 DEBUG ("Failed to prepare the account manager: %s", error->message);
3053 g_error_free (error);
3057 accounts = tp_account_manager_get_valid_accounts (account_manager);
3059 for (l = accounts; l != NULL; l = l->next) {
3060 TpAccount *account = l->data;
3061 tp_g_signal_connect_object (account, "status-changed",
3062 G_CALLBACK (chat_new_connection_cb),
3066 g_list_free (accounts);
3070 empathy_chat_init (EmpathyChat *chat)
3072 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3073 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3076 priv->log_manager = tpl_log_manager_dup_singleton ();
3077 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3078 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3080 priv->contacts_width = -1;
3081 priv->input_history = NULL;
3082 priv->input_history_current = NULL;
3083 priv->account_manager = tp_account_manager_dup ();
3085 tp_account_manager_prepare_async (priv->account_manager, NULL,
3086 account_manager_prepared_cb, chat);
3088 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3089 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3091 /* Block events for some time to avoid having "has come online" or
3092 * "joined" messages. */
3093 priv->block_events_timeout_id =
3094 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3096 /* Add nick name completion */
3097 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3098 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3100 chat_create_ui (chat);
3104 empathy_chat_new (EmpathyTpChat *tp_chat)
3106 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3110 empathy_chat_get_tp_chat (EmpathyChat *chat)
3112 EmpathyChatPriv *priv = GET_PRIV (chat);
3114 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3116 return priv->tp_chat;
3122 GtkWidget *info_bar;
3132 passwd_remember_button_cb (GtkButton *button,
3135 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3139 passwd_not_now_button_cb (GtkButton *button,
3142 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3146 remember_password_infobar_response_cb (GtkWidget *info_bar,
3150 EmpathyChatPriv *priv = GET_PRIV (data->self);
3152 if (response_id == GTK_RESPONSE_OK) {
3153 DEBUG ("Saving room password");
3154 empathy_keyring_set_room_password_async (priv->account,
3155 empathy_tp_chat_get_id (priv->tp_chat),
3160 gtk_widget_destroy (info_bar);
3161 g_free (data->password);
3162 g_slice_free (PasswordData, data);
3166 chat_prompt_to_save_password (EmpathyChat *self,
3169 GtkWidget *content_area;
3176 /* save the password in case it needs to be saved */
3177 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3179 /* Remove all previous widgets */
3180 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3181 gtk_container_forall (GTK_CONTAINER (content_area),
3182 (GtkCallback) gtk_widget_destroy, NULL);
3183 data->button = NULL;
3186 data->spinner = NULL;
3188 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3189 GTK_MESSAGE_QUESTION);
3191 hbox = gtk_hbox_new (FALSE, 5);
3192 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3195 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3196 GTK_ICON_SIZE_DIALOG);
3197 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3200 label = gtk_label_new (_("Would you like to store this password?"));
3201 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3203 /* Add 'Remember' button */
3204 alig = gtk_alignment_new (0, 0.5, 1, 0);
3206 button = gtk_button_new_with_label (_("Remember"));
3207 gtk_container_add (GTK_CONTAINER (alig), button);
3208 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3210 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3213 /* Add 'Not now' button */
3214 alig = gtk_alignment_new (0, 0.5, 1, 0);
3216 button = gtk_button_new_with_label (_("Not now"));
3217 gtk_container_add (GTK_CONTAINER (alig), button);
3218 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3220 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3224 g_signal_handler_disconnect (data->info_bar, data->response_id);
3225 g_signal_connect (data->info_bar, "response",
3226 G_CALLBACK (remember_password_infobar_response_cb), data);
3228 gtk_widget_show_all (data->info_bar);
3232 provide_password_cb (GObject *tp_chat,
3236 PasswordData *data = user_data;
3237 EmpathyChat *self = data->self;
3238 EmpathyChatPriv *priv = GET_PRIV (self);
3239 GError *error = NULL;
3241 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3243 DEBUG ("error: %s", error->message);
3244 /* FIXME: what should we do if that's another error? Close the channel?
3245 * Display the raw D-Bus error to the user isn't very useful */
3246 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3248 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3249 gtk_widget_set_sensitive (data->entry, TRUE);
3250 gtk_widget_grab_focus (data->entry);
3253 gtk_info_bar_set_message_type (
3254 GTK_INFO_BAR (data->info_bar),
3258 gtk_widget_set_sensitive (data->button, TRUE);
3259 gtk_button_set_label (GTK_BUTTON (data->button),
3263 gtk_label_set_text (GTK_LABEL (data->label),
3264 _("Wrong password; please try again:"));
3267 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3268 gtk_widget_hide (data->spinner);
3270 g_error_free (error);
3274 if (empathy_keyring_is_available ()) {
3275 /* ask whether they want to save the password */
3276 chat_prompt_to_save_password (self, data);
3278 /* Get rid of the password info bar finally */
3279 gtk_widget_destroy (data->info_bar);
3280 g_slice_free (PasswordData, data);
3284 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3285 gtk_widget_grab_focus (self->input_text_view);
3289 password_infobar_response_cb (GtkWidget *info_bar,
3293 EmpathyChatPriv *priv = GET_PRIV (data->self);
3294 const gchar *password;
3296 if (response_id != GTK_RESPONSE_OK) {
3297 gtk_widget_destroy (info_bar);
3298 g_slice_free (PasswordData, data);
3302 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3304 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3305 provide_password_cb, data);
3307 gtk_widget_set_sensitive (data->button, FALSE);
3308 gtk_widget_set_sensitive (data->entry, FALSE);
3310 gtk_spinner_start (GTK_SPINNER (data->spinner));
3311 gtk_widget_show (data->spinner);
3315 password_entry_activate_cb (GtkWidget *entry,
3318 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3322 passwd_join_button_cb (GtkButton *button,
3325 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3329 clear_icon_released_cb (GtkEntry *entry,
3330 GtkEntryIconPosition icon_pos,
3334 gtk_entry_set_text (entry, "");
3338 password_entry_changed_cb (GtkEditable *entry,
3343 str = gtk_entry_get_text (GTK_ENTRY (entry));
3345 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3346 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3350 display_password_info_bar (EmpathyChat *self)
3352 EmpathyChatPriv *priv = GET_PRIV (self);
3353 GtkWidget *info_bar;
3354 GtkWidget *content_area;
3364 data = g_slice_new0 (PasswordData);
3366 info_bar = gtk_info_bar_new ();
3367 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3368 GTK_MESSAGE_QUESTION);
3370 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3372 hbox = gtk_hbox_new (FALSE, 5);
3373 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3376 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3377 GTK_ICON_SIZE_DIALOG);
3378 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3381 label = gtk_label_new (_("This room is protected by a password:"));
3382 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3384 /* Add password entry */
3385 entry = gtk_entry_new ();
3386 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3387 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3389 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3390 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3391 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3392 GTK_ENTRY_ICON_SECONDARY, FALSE);
3394 g_signal_connect (entry, "icon-release",
3395 G_CALLBACK (clear_icon_released_cb), data);
3396 g_signal_connect (entry, "changed",
3397 G_CALLBACK (password_entry_changed_cb), data);
3399 g_signal_connect (entry, "activate",
3400 G_CALLBACK (password_entry_activate_cb), data);
3402 /* Focus the password entry once it's realized */
3403 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3405 /* Add 'Join' button */
3406 alig = gtk_alignment_new (0, 0.5, 1, 0);
3408 button = gtk_button_new_with_label (_("Join"));
3409 gtk_container_add (GTK_CONTAINER (alig), button);
3410 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3412 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3416 spinner = gtk_spinner_new ();
3417 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3419 /* Save some data for messing around with later */
3421 data->info_bar = info_bar;
3422 data->button = button;
3423 data->label = label;
3424 data->entry = entry;
3425 data->spinner = spinner;
3427 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3429 gtk_widget_show_all (hbox);
3431 data->response_id = g_signal_connect (info_bar, "response",
3432 G_CALLBACK (password_infobar_response_cb), data);
3434 gtk_widget_show_all (info_bar);
3435 /* ... but hide the spinner */
3436 gtk_widget_hide (spinner);
3440 provide_saved_password_cb (GObject *tp_chat,
3444 EmpathyChat *self = user_data;
3445 EmpathyChatPriv *priv = GET_PRIV (self);
3446 GError *error = NULL;
3448 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3450 DEBUG ("error: %s", error->message);
3451 /* FIXME: what should we do if that's another error? Close the channel?
3452 * Display the raw D-Bus error to the user isn't very useful */
3453 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3454 display_password_info_bar (self);
3455 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3457 g_error_free (error);
3462 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3463 gtk_widget_grab_focus (self->input_text_view);
3467 chat_room_got_password_cb (GObject *source,
3468 GAsyncResult *result,
3471 EmpathyChat *self = user_data;
3472 EmpathyChatPriv *priv = GET_PRIV (self);
3473 const gchar *password;
3474 GError *error = NULL;
3476 password = empathy_keyring_get_room_password_finish (priv->account,
3479 if (error != NULL) {
3480 DEBUG ("Couldn't get room password: %s\n", error->message);
3481 g_clear_error (&error);
3483 display_password_info_bar (self);
3484 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3488 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3489 provide_saved_password_cb, self);
3493 chat_password_needed_changed_cb (EmpathyChat *self)
3495 EmpathyChatPriv *priv = GET_PRIV (self);
3497 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3498 empathy_keyring_get_room_password_async (priv->account,
3499 empathy_tp_chat_get_id (priv->tp_chat),
3500 chat_room_got_password_cb, self);
3505 chat_sms_channel_changed_cb (EmpathyChat *self)
3507 EmpathyChatPriv *priv = GET_PRIV (self);
3509 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3510 g_object_notify (G_OBJECT (self), "sms-channel");
3514 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3516 g_object_notify (G_OBJECT (self), "n-messages-sending");
3520 empathy_chat_set_tp_chat (EmpathyChat *chat,
3521 EmpathyTpChat *tp_chat)
3523 EmpathyChatPriv *priv = GET_PRIV (chat);
3524 GPtrArray *properties;
3526 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3527 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3528 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3530 if (priv->tp_chat) {
3534 if (priv->account) {
3535 g_object_unref (priv->account);
3538 priv->tp_chat = g_object_ref (tp_chat);
3539 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3541 g_signal_connect (tp_chat, "destroy",
3542 G_CALLBACK (chat_destroy_cb),
3544 g_signal_connect (tp_chat, "message-received",
3545 G_CALLBACK (chat_message_received_cb),
3547 g_signal_connect (tp_chat, "send-error",
3548 G_CALLBACK (chat_send_error_cb),
3550 g_signal_connect (tp_chat, "chat-state-changed",
3551 G_CALLBACK (chat_state_changed_cb),
3553 g_signal_connect (tp_chat, "property-changed",
3554 G_CALLBACK (chat_property_changed_cb),
3556 g_signal_connect (tp_chat, "members-changed",
3557 G_CALLBACK (chat_members_changed_cb),
3559 g_signal_connect (tp_chat, "member-renamed",
3560 G_CALLBACK (chat_member_renamed_cb),
3562 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3563 G_CALLBACK (chat_remote_contact_changed_cb),
3565 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3566 G_CALLBACK (chat_password_needed_changed_cb),
3568 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3569 G_CALLBACK (chat_sms_channel_changed_cb),
3571 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3572 G_CALLBACK (chat_n_messages_sending_changed_cb),
3575 /* Get initial value of properties */
3576 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3577 if (properties != NULL) {
3580 for (i = 0; i < properties->len; i++) {
3581 EmpathyTpChatProperty *property;
3583 property = g_ptr_array_index (properties, i);
3584 if (property->value == NULL)
3587 chat_property_changed_cb (priv->tp_chat,
3594 chat_sms_channel_changed_cb (chat);
3595 chat_remote_contact_changed_cb (chat);
3597 if (chat->input_text_view) {
3598 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3599 if (priv->block_events_timeout_id == 0) {
3600 empathy_chat_view_append_event (chat->view, _("Connected"));
3604 g_object_notify (G_OBJECT (chat), "tp-chat");
3605 g_object_notify (G_OBJECT (chat), "id");
3606 g_object_notify (G_OBJECT (chat), "account");
3608 /* This is a noop when tp-chat is set at object construction time and causes
3609 * the pending messages to be show when it's set on the object after it has
3611 show_pending_messages (chat);
3613 /* check if a password is needed */
3614 chat_password_needed_changed_cb (chat);
3618 empathy_chat_get_account (EmpathyChat *chat)
3620 EmpathyChatPriv *priv = GET_PRIV (chat);
3622 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3624 return priv->account;
3628 empathy_chat_get_id (EmpathyChat *chat)
3630 EmpathyChatPriv *priv = GET_PRIV (chat);
3632 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3638 empathy_chat_dup_name (EmpathyChat *chat)
3640 EmpathyChatPriv *priv = GET_PRIV (chat);
3643 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3647 if (!ret && priv->remote_contact) {
3648 ret = empathy_contact_get_alias (priv->remote_contact);
3655 ret = _("Conversation");
3657 if (priv->sms_channel)
3658 /* Translators: this string is a something like
3659 * "Escher Cat (SMS)" */
3660 return g_strdup_printf (_("%s (SMS)"), ret);
3662 return g_strdup (ret);
3666 empathy_chat_get_subject (EmpathyChat *chat)
3668 EmpathyChatPriv *priv = GET_PRIV (chat);
3670 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3672 return priv->subject;
3676 empathy_chat_get_remote_contact (EmpathyChat *chat)
3678 EmpathyChatPriv *priv = GET_PRIV (chat);
3680 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3682 return priv->remote_contact;
3686 empathy_chat_get_contact_menu (EmpathyChat *chat)
3688 EmpathyChatPriv *priv = GET_PRIV (chat);
3689 GtkWidget *menu = NULL;
3691 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3693 if (priv->remote_contact) {
3694 menu = empathy_contact_menu_new (priv->remote_contact,
3695 EMPATHY_CONTACT_FEATURE_CALL |
3696 EMPATHY_CONTACT_FEATURE_LOG |
3697 EMPATHY_CONTACT_FEATURE_INFO |
3698 EMPATHY_CONTACT_FEATURE_BLOCK);
3705 empathy_chat_clear (EmpathyChat *chat)
3707 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3709 empathy_chat_view_clear (chat->view);
3713 empathy_chat_scroll_down (EmpathyChat *chat)
3715 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3717 empathy_chat_view_scroll_down (chat->view);
3721 empathy_chat_cut (EmpathyChat *chat)
3723 GtkTextBuffer *buffer;
3725 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3727 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3728 if (gtk_text_buffer_get_has_selection (buffer)) {
3729 GtkClipboard *clipboard;
3731 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3733 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3738 empathy_chat_copy (EmpathyChat *chat)
3740 GtkTextBuffer *buffer;
3742 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3744 if (empathy_chat_view_get_has_selection (chat->view)) {
3745 empathy_chat_view_copy_clipboard (chat->view);
3749 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3750 if (gtk_text_buffer_get_has_selection (buffer)) {
3751 GtkClipboard *clipboard;
3753 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3755 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3760 EmpathyChatPriv *priv = GET_PRIV (chat);
3762 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3769 GtkClipboard *clipboard;
3771 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3772 start = g_utf8_offset_to_pointer (topic, start_offset);
3773 end = g_utf8_offset_to_pointer (topic, end_offset);
3774 selection = g_strndup (start, end - start);
3776 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3777 gtk_clipboard_set_text (clipboard, selection, -1);
3785 empathy_chat_paste (EmpathyChat *chat)
3787 GtkTextBuffer *buffer;
3788 GtkClipboard *clipboard;
3789 EmpathyChatPriv *priv;
3791 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3793 priv = GET_PRIV (chat);
3795 if (gtk_widget_get_visible (priv->search_bar)) {
3796 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3800 if (priv->tp_chat == NULL ||
3801 !gtk_widget_is_sensitive (chat->input_text_view))
3804 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3805 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3807 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3811 empathy_chat_find (EmpathyChat *chat)
3813 EmpathyChatPriv *priv;
3815 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3817 priv = GET_PRIV (chat);
3819 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3823 empathy_chat_correct_word (EmpathyChat *chat,
3826 const gchar *new_word)
3828 GtkTextBuffer *buffer;
3830 g_return_if_fail (chat != NULL);
3831 g_return_if_fail (new_word != NULL);
3833 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3835 gtk_text_buffer_delete (buffer, start, end);
3836 gtk_text_buffer_insert (buffer, start,
3842 empathy_chat_is_room (EmpathyChat *chat)
3844 EmpathyChatPriv *priv = GET_PRIV (chat);
3846 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3848 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3852 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3854 EmpathyChatPriv *priv = GET_PRIV (self);
3856 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3858 return priv->unread_messages;
3861 /* called when the messages have been read by user */
3863 empathy_chat_messages_read (EmpathyChat *self)
3865 EmpathyChatPriv *priv = GET_PRIV (self);
3867 g_return_if_fail (EMPATHY_IS_CHAT (self));
3869 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3870 * definition. If we are still retrieving the backlogs, do not ACK */
3871 if (priv->retrieving_backlogs)
3874 if (priv->tp_chat != NULL ) {
3875 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3877 priv->unread_messages = 0;
3879 empathy_chat_view_focus_toggled (self->view, TRUE);
3882 /* Return TRUE if on of the contacts in this chat is composing */
3884 empathy_chat_is_composing (EmpathyChat *chat)
3886 return chat->priv->compositors != NULL;
3890 empathy_chat_is_sms_channel (EmpathyChat *self)
3892 EmpathyChatPriv *priv = GET_PRIV (self);
3894 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3896 return priv->sms_channel;
3900 empathy_chat_get_n_messages_sending (EmpathyChat *self)
3902 EmpathyChatPriv *priv;
3904 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3906 priv = GET_PRIV (self);
3908 if (priv->tp_chat == NULL) {
3913 g_object_get (priv->tp_chat,
3914 "n-messages-sending", &n_messages,