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 empathy_get_current_action_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 empathy_get_current_action_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 /* translators: error used when user doesn't have enough credit on his
1298 * account to send the message. */
1299 error = _("insufficient balance to send message");
1300 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1301 error = _("not capable");
1304 if (error == NULL) {
1305 /* if we didn't find a dbus-error, try the old error */
1306 switch (error_code) {
1307 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1308 error = _("offline");
1310 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1311 error = _("invalid contact");
1313 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1314 error = _("permission denied");
1316 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1317 error = _("too long message");
1319 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1320 error = _("not implemented");
1322 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1324 error = _("unknown");
1329 if (message_body != NULL) {
1330 str = g_strdup_printf (_("Error sending message '%s': %s"),
1331 message_body, error);
1334 str = g_strdup_printf (_("Error sending message: %s"), error);
1337 empathy_chat_view_append_event (chat->view, str);
1342 chat_topic_label_size_allocate_cb (GtkLabel *label,
1343 GtkAllocation *allocation,
1346 EmpathyChatPriv *priv = GET_PRIV (chat);
1348 if (!gtk_label_get_line_wrap (label)) {
1349 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1350 gtk_widget_show (priv->expander_topic);
1352 gtk_widget_hide (priv->expander_topic);
1359 chat_topic_expander_activate_cb (GtkExpander *expander,
1360 GParamSpec *param_spec,
1363 EmpathyChatPriv *priv = GET_PRIV (chat);
1365 if (gtk_expander_get_expanded (expander)) {
1366 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1367 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1369 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1370 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1375 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1380 EmpathyChatPriv *priv = GET_PRIV (chat);
1382 if (!tp_strdiff (name, "subject")) {
1383 g_free (priv->subject);
1384 priv->subject = g_value_dup_string (value);
1385 g_object_notify (G_OBJECT (chat), "subject");
1387 if (EMP_STR_EMPTY (priv->subject)) {
1388 gtk_widget_hide (priv->hbox_topic);
1390 gchar *markup_topic;
1393 markup_topic = empathy_add_link_markup (priv->subject);
1394 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1395 _("Topic:"), markup_topic);
1397 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1398 g_free (markup_text);
1399 g_free (markup_topic);
1401 gtk_widget_show (priv->hbox_topic);
1403 if (priv->block_events_timeout_id == 0) {
1406 if (!EMP_STR_EMPTY (priv->subject)) {
1407 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1409 str = g_strdup (_("No topic defined"));
1411 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1415 else if (!tp_strdiff (name, "name")) {
1416 g_free (priv->name);
1417 priv->name = g_value_dup_string (value);
1418 g_object_notify (G_OBJECT (chat), "name");
1423 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1427 GtkTextIter word_start = *iter;
1428 GtkTextIter word_end = *iter;
1431 if (gtk_text_iter_inside_word (&word_end) &&
1432 !gtk_text_iter_ends_word (&word_end)) {
1433 gtk_text_iter_forward_word_end (&word_end);
1438 if (gtk_text_iter_get_char (&tmp) == '\'') {
1439 gtk_text_iter_forward_char (&tmp);
1441 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1442 gtk_text_iter_forward_word_end (&word_end);
1447 if (gtk_text_iter_inside_word (&word_start) ||
1448 gtk_text_iter_ends_word (&word_start)) {
1449 if (!gtk_text_iter_starts_word (&word_start) ||
1450 gtk_text_iter_equal (&word_start, &word_end)) {
1451 gtk_text_iter_backward_word_start (&word_start);
1455 gtk_text_iter_backward_char (&tmp);
1457 if (gtk_text_iter_get_char (&tmp) == '\'') {
1458 gtk_text_iter_backward_char (&tmp);
1460 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1461 gtk_text_iter_backward_word_start (&word_start);
1466 *start = word_start;
1472 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1473 GtkTextIter *location,
1478 GtkTextIter iter, pos;
1480 /* Remove all misspelled tags in the inserted text.
1481 * This happens when text is inserted within a misspelled word. */
1482 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1483 gtk_text_iter_get_offset (location) - len);
1484 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1487 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1490 GtkTextIter start, end;
1493 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1496 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1498 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1499 gtk_text_iter_equal (&pos, &end) ||
1500 empathy_spell_check (str)) {
1501 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1503 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1508 } while (gtk_text_iter_forward_word_end (&iter) &&
1509 gtk_text_iter_compare (&iter, location) <= 0);
1513 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1518 GtkTextIter word_start, word_end;
1520 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1521 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1522 &word_start, &word_end);
1527 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1530 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1531 chat_composing_stop (chat);
1533 chat_composing_start (chat);
1538 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1543 GtkTextIter prev_pos;
1544 GtkTextIter word_start;
1545 GtkTextIter word_end;
1549 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1551 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1552 gtk_text_buffer_get_insert (buffer));
1553 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1555 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1558 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1559 !gtk_text_iter_equal (&pos, &word_end)) {
1560 str = gtk_text_buffer_get_text (buffer,
1561 &word_start, &word_end, FALSE);
1563 if (!empathy_spell_check (str)) {
1564 gtk_text_buffer_apply_tag_by_name (buffer,
1565 "misspelled", &word_start, &word_end);
1567 gtk_text_buffer_remove_tag_by_name (buffer,
1568 "misspelled", &word_start, &word_end);
1575 gtk_text_buffer_move_mark (buffer, mark, &pos);
1579 empathy_isspace_cb (gunichar c,
1582 return g_unichar_isspace (c);
1586 chat_input_key_press_event_cb (GtkWidget *widget,
1590 EmpathyChatPriv *priv;
1593 GtkWidget *text_view_sw;
1595 priv = GET_PRIV (chat);
1597 /* Catch ctrl+up/down so we can traverse messages we sent */
1598 if ((event->state & GDK_CONTROL_MASK) &&
1599 (event->keyval == GDK_KEY_Up ||
1600 event->keyval == GDK_KEY_Down)) {
1601 GtkTextBuffer *buffer;
1604 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1605 chat_input_history_update (chat, buffer);
1607 if (event->keyval == GDK_KEY_Up) {
1608 str = chat_input_history_get_next (chat);
1610 str = chat_input_history_get_prev (chat);
1613 g_signal_handlers_block_by_func (buffer,
1614 chat_input_text_buffer_changed_cb,
1616 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1617 g_signal_handlers_unblock_by_func (buffer,
1618 chat_input_text_buffer_changed_cb,
1624 /* Catch enter but not ctrl/shift-enter */
1625 if (IS_ENTER (event->keyval) &&
1626 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1629 /* This is to make sure that kinput2 gets the enter. And if
1630 * it's handled there we shouldn't send on it. This is because
1631 * kinput2 uses Enter to commit letters. See:
1632 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1635 view = GTK_TEXT_VIEW (chat->input_text_view);
1636 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1637 gtk_text_view_reset_im_context (view);
1641 chat_input_text_view_send (chat);
1645 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1647 if (IS_ENTER (event->keyval) &&
1648 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1649 /* Newline for shift/control-enter. */
1652 if (!(event->state & GDK_CONTROL_MASK) &&
1653 event->keyval == GDK_KEY_Page_Up) {
1654 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1655 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1658 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1659 event->keyval == GDK_KEY_Page_Down) {
1660 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1661 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1662 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1663 gtk_adjustment_set_value (adj, val);
1666 if (event->keyval == GDK_KEY_Escape) {
1667 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1669 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1670 event->keyval == GDK_KEY_Tab) {
1671 GtkTextBuffer *buffer;
1672 GtkTextIter start, current;
1673 gchar *nick, *completed;
1674 GList *list, *completed_list;
1675 gboolean is_start_of_buffer;
1677 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1678 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1680 /* Get the start of the nick to complete. */
1681 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1682 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1683 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1685 is_start_of_buffer = gtk_text_iter_is_start (&start);
1687 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1688 g_completion_add_items (priv->completion, list);
1690 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1691 completed_list = g_completion_complete (priv->completion,
1700 GString *message = NULL;
1703 gtk_text_buffer_delete (buffer, &start, ¤t);
1705 len = g_list_length (completed_list);
1708 /* If we only have one hit, use that text
1709 * instead of the text in completed since the
1710 * completed text will use the typed string
1711 * which might be cased all wrong.
1714 text = empathy_contact_get_alias (completed_list->data);
1718 /* Print all hits to the scrollback view, so the
1719 * user knows what possibilities he has.
1722 message = g_string_new ("");
1723 for (l = completed_list; l != NULL; l = l->next) {
1724 g_string_append (message, empathy_contact_get_alias (l->data));
1725 g_string_append (message, " - ");
1727 empathy_chat_view_append_event (chat->view, message->str);
1728 g_string_free (message, TRUE);
1731 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1733 if (len == 1 && is_start_of_buffer) {
1734 gchar *complete_char;
1736 complete_char = g_settings_get_string (
1737 priv->gsettings_chat,
1738 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1740 if (complete_char != NULL) {
1741 gtk_text_buffer_insert_at_cursor (buffer,
1743 strlen (complete_char));
1744 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1745 g_free (complete_char);
1752 g_completion_clear_items (priv->completion);
1754 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1764 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1768 gtk_widget_grab_focus (chat->input_text_view);
1774 chat_input_realize_cb (GtkWidget *widget,
1777 DEBUG ("Setting focus to the input text view");
1778 if (gtk_widget_is_sensitive (widget)) {
1779 gtk_widget_grab_focus (widget);
1784 chat_input_has_focus_notify_cb (GtkWidget *widget,
1788 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1792 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1793 EmpathySmiley *smiley,
1796 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1797 GtkTextBuffer *buffer;
1800 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1802 gtk_text_buffer_get_end_iter (buffer, &iter);
1803 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1805 gtk_text_buffer_get_end_iter (buffer, &iter);
1806 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1817 static EmpathyChatSpell *
1818 chat_spell_new (EmpathyChat *chat,
1823 EmpathyChatSpell *chat_spell;
1825 chat_spell = g_slice_new0 (EmpathyChatSpell);
1827 chat_spell->chat = g_object_ref (chat);
1828 chat_spell->word = g_strdup (word);
1829 chat_spell->start = start;
1830 chat_spell->end = end;
1836 chat_spell_free (EmpathyChatSpell *chat_spell)
1838 g_object_unref (chat_spell->chat);
1839 g_free (chat_spell->word);
1840 g_slice_free (EmpathyChatSpell, chat_spell);
1844 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1845 EmpathyChatSpell *chat_spell)
1847 empathy_chat_correct_word (chat_spell->chat,
1848 &(chat_spell->start),
1850 gtk_menu_item_get_label (menu_item));
1855 chat_spelling_build_suggestions_menu (const gchar *code,
1856 EmpathyChatSpell *chat_spell)
1858 GList *suggestions, *l;
1859 GtkWidget *menu, *menu_item;
1861 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1862 if (suggestions == NULL)
1865 menu = gtk_menu_new ();
1866 for (l = suggestions; l; l = l->next) {
1867 menu_item = gtk_menu_item_new_with_label (l->data);
1868 g_signal_connect (G_OBJECT (menu_item), "activate",
1869 G_CALLBACK (chat_spelling_menu_activate_cb),
1871 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1873 empathy_spell_free_suggestions (suggestions);
1875 gtk_widget_show_all (menu);
1881 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1883 GtkWidget *menu, *submenu, *item;
1886 codes = empathy_spell_get_enabled_language_codes ();
1887 g_assert (codes != NULL);
1889 if (g_list_length (codes) > 1) {
1890 menu = gtk_menu_new ();
1892 for (l = codes; l; l = l->next) {
1893 const gchar *code, *name;
1896 name = empathy_spell_get_language_name (code);
1900 item = gtk_image_menu_item_new_with_label (name);
1902 submenu = chat_spelling_build_suggestions_menu (
1904 if (submenu == NULL)
1905 gtk_widget_set_sensitive (item, FALSE);
1907 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1909 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1912 menu = chat_spelling_build_suggestions_menu (codes->data,
1915 menu = gtk_menu_new ();
1916 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1917 gtk_widget_set_sensitive (item, FALSE);
1918 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1921 g_list_free (codes);
1923 gtk_widget_show_all (menu);
1934 static EmpathyChatWord *
1935 chat_word_new (EmpathyChat *chat,
1939 EmpathyChatWord *chat_word;
1941 chat_word = g_slice_new0 (EmpathyChatWord);
1943 chat_word->chat = g_object_ref (chat);
1944 chat_word->word = g_strdup (word);
1945 chat_word->code = g_strdup (code);
1951 chat_word_free (EmpathyChatWord *chat_word)
1953 g_object_unref (chat_word->chat);
1954 g_free (chat_word->word);
1955 g_free (chat_word->code);
1956 g_slice_free (EmpathyChatWord, chat_word);
1960 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1961 EmpathyChatWord *chat_word)
1963 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1965 empathy_spell_add_to_dictionary (chat_word->code,
1967 priv->update_misspelled_words_id =
1968 g_idle_add (update_misspelled_words, chat_word->chat);
1972 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1974 GtkWidget *menu, *item, *lang_item, *image;
1977 const gchar *code, *name;
1978 EmpathyChatWord *chat_word;
1980 codes = empathy_spell_get_enabled_language_codes ();
1981 g_assert (codes != NULL);
1982 if (g_list_length (codes) > 1) {
1983 /* translators: %s is the selected word */
1984 label = g_strdup_printf (_("Add '%s' to Dictionary"),
1986 item = gtk_image_menu_item_new_with_mnemonic (label);
1988 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1989 GTK_ICON_SIZE_MENU);
1990 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1993 menu = gtk_menu_new ();
1995 for (l = codes; l; l = l->next) {
1997 name = empathy_spell_get_language_name (code);
2001 lang_item = gtk_image_menu_item_new_with_label (name);
2003 chat_word= chat_word_new (chat_spell->chat,
2004 chat_spell->word, code);
2005 g_object_set_data_full (G_OBJECT (lang_item),
2006 "chat-word", chat_word,
2007 (GDestroyNotify) chat_word_free);
2009 g_signal_connect (G_OBJECT (lang_item), "activate",
2010 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2012 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2014 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2017 name = empathy_spell_get_language_name (code);
2018 g_assert (name != NULL);
2019 /* translators: first %s is the selected word,
2020 * second %s is the language name of the target dictionary */
2021 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2022 chat_spell->word, name);
2023 item = gtk_image_menu_item_new_with_mnemonic (label);
2025 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2026 GTK_ICON_SIZE_MENU);
2027 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2029 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2031 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2032 (GDestroyNotify) chat_word_free);
2034 g_signal_connect (G_OBJECT (item), "activate",
2035 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2038 g_list_free (codes);
2040 gtk_widget_show_all (item);
2046 chat_text_send_cb (GtkMenuItem *menuitem,
2049 chat_input_text_view_send (chat);
2053 chat_input_populate_popup_cb (GtkTextView *view,
2057 GtkTextBuffer *buffer;
2058 GtkTextTagTable *table;
2061 GtkTextIter iter, start, end;
2064 EmpathyChatSpell *chat_spell;
2065 GtkWidget *spell_menu;
2066 GtkWidget *spell_item;
2067 EmpathySmileyManager *smiley_manager;
2068 GtkWidget *smiley_menu;
2071 buffer = gtk_text_view_get_buffer (view);
2073 /* Add the emoticon menu. */
2074 item = gtk_separator_menu_item_new ();
2075 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2076 gtk_widget_show (item);
2078 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2079 image = gtk_image_new_from_icon_name ("face-smile",
2080 GTK_ICON_SIZE_MENU);
2081 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2082 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2083 gtk_widget_show (item);
2085 smiley_manager = empathy_smiley_manager_dup_singleton ();
2086 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2087 chat_insert_smiley_activate_cb,
2089 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2090 g_object_unref (smiley_manager);
2092 /* Add the Send menu item. */
2093 gtk_text_buffer_get_bounds (buffer, &start, &end);
2094 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2095 if (!EMP_STR_EMPTY (str)) {
2096 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2097 g_signal_connect (G_OBJECT (item), "activate",
2098 G_CALLBACK (chat_text_send_cb), chat);
2099 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2100 gtk_widget_show (item);
2104 /* Add the spell check menu item. */
2105 table = gtk_text_buffer_get_tag_table (buffer);
2106 tag = gtk_text_tag_table_lookup (table, "misspelled");
2107 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2108 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2109 GTK_TEXT_WINDOW_WIDGET,
2112 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2114 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2115 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2117 str = gtk_text_buffer_get_text (buffer,
2118 &start, &end, FALSE);
2120 if (!EMP_STR_EMPTY (str)) {
2121 chat_spell = chat_spell_new (chat, str, start, end);
2122 g_object_set_data_full (G_OBJECT (menu),
2123 "chat-spell", chat_spell,
2124 (GDestroyNotify) chat_spell_free);
2126 item = gtk_separator_menu_item_new ();
2127 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2128 gtk_widget_show (item);
2130 /* Spelling suggestions */
2131 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2132 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2133 GTK_ICON_SIZE_MENU);
2134 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2136 spell_menu = chat_spelling_build_menu (chat_spell);
2137 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2140 spell_item = gtk_separator_menu_item_new ();
2141 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2142 gtk_widget_show (spell_item);
2144 /* Add to dictionary */
2145 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2147 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2148 gtk_widget_show (spell_item);
2150 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2151 gtk_widget_show (item);
2157 chat_log_filter (TplEvent *log,
2160 EmpathyChat *chat = user_data;
2161 EmpathyMessage *message;
2162 EmpathyChatPriv *priv = GET_PRIV (chat);
2163 const GList *pending;
2165 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2166 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2168 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2169 message = empathy_message_from_tpl_log_event (log);
2171 for (; pending; pending = g_list_next (pending)) {
2172 if (empathy_message_equal (message, pending->data)) {
2173 g_object_unref (message);
2178 g_object_unref (message);
2184 show_pending_messages (EmpathyChat *chat) {
2185 EmpathyChatPriv *priv = GET_PRIV (chat);
2186 const GList *messages, *l;
2188 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2190 if (chat->view == NULL || priv->tp_chat == NULL)
2193 if (!priv->can_show_pending)
2196 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2198 for (l = messages; l != NULL ; l = g_list_next (l)) {
2199 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2200 chat_message_received (chat, message, TRUE);
2206 got_filtered_messages_cb (GObject *manager,
2207 GAsyncResult *result,
2212 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2213 EmpathyChatPriv *priv = GET_PRIV (chat);
2214 GError *error = NULL;
2216 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2217 result, &messages, &error)) {
2218 DEBUG ("%s. Aborting.", error->message);
2219 empathy_chat_view_append_event (chat->view,
2220 _("Failed to retrieve recent logs"));
2221 g_error_free (error);
2225 for (l = messages; l; l = g_list_next (l)) {
2226 EmpathyMessage *message;
2227 g_assert (TPL_IS_EVENT (l->data));
2229 message = empathy_message_from_tpl_log_event (l->data);
2230 g_object_unref (l->data);
2232 empathy_chat_view_append_message (chat->view, message);
2233 g_object_unref (message);
2235 g_list_free (messages);
2238 /* in case of TPL error, skip backlog and show pending messages */
2239 priv->can_show_pending = TRUE;
2240 show_pending_messages (chat);
2242 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2243 * about it in EmpathyChatPriv definition */
2244 priv->retrieving_backlogs = FALSE;
2245 empathy_chat_messages_read (chat);
2247 /* Turn back on scrolling */
2248 empathy_chat_view_scroll (chat->view, TRUE);
2252 chat_add_logs (EmpathyChat *chat)
2254 EmpathyChatPriv *priv = GET_PRIV (chat);
2261 /* Turn off scrolling temporarily */
2262 empathy_chat_view_scroll (chat->view, FALSE);
2264 /* Add messages from last conversation */
2265 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2266 target = tpl_entity_new_from_room_id (priv->id);
2268 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2270 priv->retrieving_backlogs = TRUE;
2271 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2274 TPL_EVENT_MASK_TEXT,
2278 got_filtered_messages_cb,
2281 g_object_unref (target);
2285 chat_contacts_completion_func (const gchar *s1,
2289 gchar *tmp, *nick1, *nick2;
2296 return s1 ? -1 : +1;
2299 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2300 nick1 = g_utf8_casefold (tmp, -1);
2303 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2304 nick2 = g_utf8_casefold (tmp, -1);
2307 ret = strncmp (nick1, nick2, n);
2316 build_part_message (guint reason,
2318 EmpathyContact *actor,
2319 const gchar *message)
2321 GString *s = g_string_new ("");
2322 const gchar *actor_name = NULL;
2324 if (actor != NULL) {
2325 actor_name = empathy_contact_get_alias (actor);
2328 /* Having an actor only really makes sense for a few actions... */
2330 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2331 g_string_append_printf (s, _("%s has disconnected"), name);
2333 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2334 if (actor_name != NULL) {
2335 /* translators: reverse the order of these arguments
2336 * if the kicked should come before the kicker in your locale.
2338 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2341 g_string_append_printf (s, _("%s was kicked"), name);
2344 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2345 if (actor_name != NULL) {
2346 /* translators: reverse the order of these arguments
2347 * if the banned should come before the banner in your locale.
2349 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2352 g_string_append_printf (s, _("%s was banned"), name);
2356 g_string_append_printf (s, _("%s has left the room"), name);
2359 if (!EMP_STR_EMPTY (message)) {
2360 /* Note to translators: this string is appended to
2361 * notifications like "foo has left the room", with the message
2362 * given by the user living the room. If this poses a problem,
2363 * please let us know. :-)
2365 g_string_append_printf (s, _(" (%s)"), message);
2368 return g_string_free (s, FALSE);
2372 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2373 EmpathyContact *contact,
2374 EmpathyContact *actor,
2380 EmpathyChatPriv *priv = GET_PRIV (chat);
2381 const gchar *name = empathy_contact_get_alias (contact);
2384 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2386 if (priv->block_events_timeout_id != 0)
2390 str = g_strdup_printf (_("%s has joined the room"),
2393 str = build_part_message (reason, name, actor, message);
2396 empathy_chat_view_append_event (chat->view, str);
2401 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2402 EmpathyContact *old_contact,
2403 EmpathyContact *new_contact,
2408 EmpathyChatPriv *priv = GET_PRIV (chat);
2410 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2412 if (priv->block_events_timeout_id == 0) {
2415 str = g_strdup_printf (_("%s is now known as %s"),
2416 empathy_contact_get_alias (old_contact),
2417 empathy_contact_get_alias (new_contact));
2418 empathy_chat_view_append_event (chat->view, str);
2425 chat_reset_size_request (gpointer widget)
2427 gtk_widget_set_size_request (widget, -1, -1);
2433 chat_update_contacts_visibility (EmpathyChat *chat,
2436 EmpathyChatPriv *priv = GET_PRIV (chat);
2437 GtkAllocation allocation;
2439 if (!priv->scrolled_window_contacts) {
2443 if (priv->remote_contact != NULL) {
2447 if (show && priv->contact_list_view == NULL) {
2448 EmpathyContactListStore *store;
2451 /* We are adding the contact list to the chat, we don't want the
2452 * chat view to become too small. If the chat view is already
2453 * smaller than 250 make sure that size won't change. If the
2454 * chat view is bigger the contact list will take some space on
2455 * it but we make sure the chat view don't become smaller than
2456 * 250. Relax the size request once the resize is done */
2457 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2458 min_width = MIN (allocation.width, 250);
2459 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2460 g_idle_add (chat_reset_size_request, priv->vbox_left);
2462 if (priv->contacts_width > 0) {
2463 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2464 priv->contacts_width);
2467 store = empathy_contact_list_store_new (
2468 EMPATHY_CONTACT_LIST (priv->tp_chat));
2469 empathy_contact_list_store_set_show_groups (
2470 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2472 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2473 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2474 EMPATHY_CONTACT_FEATURE_CHAT |
2475 EMPATHY_CONTACT_FEATURE_CALL |
2476 EMPATHY_CONTACT_FEATURE_LOG |
2477 EMPATHY_CONTACT_FEATURE_INFO));
2478 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2479 priv->contact_list_view);
2480 gtk_widget_show (priv->contact_list_view);
2481 gtk_widget_show (priv->scrolled_window_contacts);
2482 g_object_unref (store);
2484 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2485 gtk_widget_hide (priv->scrolled_window_contacts);
2486 if (priv->contact_list_view != NULL) {
2487 gtk_widget_destroy (priv->contact_list_view);
2488 priv->contact_list_view = NULL;
2494 empathy_chat_set_show_contacts (EmpathyChat *chat,
2497 EmpathyChatPriv *priv = GET_PRIV (chat);
2499 priv->show_contacts = show;
2501 chat_update_contacts_visibility (chat, show);
2503 g_object_notify (G_OBJECT (chat), "show-contacts");
2507 chat_remote_contact_changed_cb (EmpathyChat *chat)
2509 EmpathyChatPriv *priv = GET_PRIV (chat);
2511 if (priv->remote_contact != NULL) {
2512 g_object_unref (priv->remote_contact);
2513 priv->remote_contact = NULL;
2518 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2519 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2520 if (priv->remote_contact != NULL) {
2521 g_object_ref (priv->remote_contact);
2522 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2524 else if (priv->tp_chat != NULL) {
2527 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2528 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2531 chat_update_contacts_visibility (chat, priv->show_contacts);
2533 g_object_notify (G_OBJECT (chat), "remote-contact");
2534 g_object_notify (G_OBJECT (chat), "id");
2538 chat_destroy_cb (EmpathyTpChat *tp_chat,
2541 EmpathyChatPriv *priv;
2543 priv = GET_PRIV (chat);
2545 if (!priv->tp_chat) {
2549 chat_composing_remove_timeout (chat);
2550 g_object_unref (priv->tp_chat);
2551 priv->tp_chat = NULL;
2552 g_object_notify (G_OBJECT (chat), "tp-chat");
2554 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2555 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2557 chat_update_contacts_visibility (chat, FALSE);
2561 update_misspelled_words (gpointer data)
2563 EmpathyChat *chat = EMPATHY_CHAT (data);
2564 EmpathyChatPriv *priv = GET_PRIV (chat);
2565 GtkTextBuffer *buffer;
2569 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2571 gtk_text_buffer_get_end_iter (buffer, &iter);
2572 length = gtk_text_iter_get_offset (&iter);
2573 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2574 NULL, length, chat);
2576 priv->update_misspelled_words_id = 0;
2582 conf_spell_checking_cb (GSettings *gsettings_chat,
2586 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2587 EmpathyChatPriv *priv = GET_PRIV (chat);
2588 gboolean spell_checker;
2589 GtkTextBuffer *buffer;
2591 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2594 spell_checker = g_settings_get_boolean (gsettings_chat,
2595 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2597 if (!empathy_spell_supported ()) {
2598 spell_checker = FALSE;
2601 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2603 if (spell_checker == priv->spell_checking_enabled) {
2604 if (spell_checker) {
2605 /* Possibly changed dictionaries,
2606 * update misspelled words. Need to do so in idle
2607 * so the spell checker is updated. */
2608 priv->update_misspelled_words_id =
2609 g_idle_add (update_misspelled_words, chat);
2615 if (spell_checker) {
2618 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2619 buffer, "notify::cursor-position",
2620 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2622 priv->insert_text_id = tp_g_signal_connect_object (
2623 buffer, "insert-text",
2624 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2625 chat, G_CONNECT_AFTER);
2626 priv->delete_range_id = tp_g_signal_connect_object (
2627 buffer, "delete-range",
2628 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2629 chat, G_CONNECT_AFTER);
2631 gtk_text_buffer_create_tag (buffer, "misspelled",
2632 "underline", PANGO_UNDERLINE_ERROR,
2635 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2636 gtk_text_buffer_get_insert (buffer));
2637 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2640 /* Mark misspelled words in the existing buffer.
2641 * Need to do so in idle so the spell checker is updated. */
2642 priv->update_misspelled_words_id =
2643 g_idle_add (update_misspelled_words, chat);
2645 GtkTextTagTable *table;
2648 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2649 priv->notify_cursor_position_id = 0;
2650 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2651 priv->insert_text_id = 0;
2652 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2653 priv->delete_range_id = 0;
2655 table = gtk_text_buffer_get_tag_table (buffer);
2656 tag = gtk_text_tag_table_lookup (table, "misspelled");
2657 gtk_text_tag_table_remove (table, tag);
2659 gtk_text_buffer_delete_mark_by_name (buffer,
2660 "previous-cursor-position");
2663 priv->spell_checking_enabled = spell_checker;
2667 save_paned_pos_timeout (gpointer data)
2669 EmpathyChat *self = data;
2672 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2674 g_settings_set_int (self->priv->gsettings_ui,
2675 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2682 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2686 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2688 if (chat->priv->save_paned_pos_id != 0)
2689 g_source_remove (chat->priv->save_paned_pos_id);
2691 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2692 save_paned_pos_timeout, chat);
2698 chat_create_ui (EmpathyChat *chat)
2700 EmpathyChatPriv *priv = GET_PRIV (chat);
2704 GtkTextBuffer *buffer;
2706 EmpathyThemeManager *theme_mgr;
2708 filename = empathy_file_lookup ("empathy-chat.ui",
2710 gui = empathy_builder_get_file (filename,
2711 "chat_widget", &priv->widget,
2712 "hpaned", &priv->hpaned,
2713 "vbox_left", &priv->vbox_left,
2714 "scrolled_window_chat", &priv->scrolled_window_chat,
2715 "scrolled_window_input", &priv->scrolled_window_input,
2716 "hbox_topic", &priv->hbox_topic,
2717 "expander_topic", &priv->expander_topic,
2718 "label_topic", &priv->label_topic,
2719 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2720 "info_bar_vbox", &priv->info_bar_vbox,
2723 empathy_builder_connect (gui, chat,
2724 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2725 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2730 /* Add message view. */
2731 theme_mgr = empathy_theme_manager_dup_singleton ();
2732 chat->view = empathy_theme_manager_create_view (theme_mgr);
2733 g_object_unref (theme_mgr);
2734 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2735 and other types, even though it's non-editable and doesn't accept any
2736 drags. This steals drag motion for anything inside the scrollbars,
2737 making drag destinations on chat windows far less useful.
2739 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2740 g_signal_connect (chat->view, "focus_in_event",
2741 G_CALLBACK (chat_text_view_focus_in_event_cb),
2743 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2744 GTK_WIDGET (chat->view));
2745 gtk_widget_show (GTK_WIDGET (chat->view));
2747 /* Add input GtkTextView */
2748 chat->input_text_view = empathy_input_text_view_new ();
2749 g_signal_connect (chat->input_text_view, "notify::has-focus",
2750 G_CALLBACK (chat_input_has_focus_notify_cb),
2752 g_signal_connect (chat->input_text_view, "key-press-event",
2753 G_CALLBACK (chat_input_key_press_event_cb),
2755 g_signal_connect (chat->input_text_view, "realize",
2756 G_CALLBACK (chat_input_realize_cb),
2758 g_signal_connect (chat->input_text_view, "populate-popup",
2759 G_CALLBACK (chat_input_populate_popup_cb),
2761 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2762 tp_g_signal_connect_object (buffer, "changed",
2763 G_CALLBACK (chat_input_text_buffer_changed_cb),
2765 tp_g_signal_connect_object (priv->gsettings_chat,
2766 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2767 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2768 conf_spell_checking_cb (priv->gsettings_chat,
2769 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2770 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2771 chat->input_text_view);
2772 gtk_widget_show (chat->input_text_view);
2774 /* Add the (invisible) search bar */
2775 priv->search_bar = empathy_search_bar_new (chat->view);
2776 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2779 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2781 /* Initialy hide the topic, will be shown if not empty */
2782 gtk_widget_hide (priv->hbox_topic);
2784 g_signal_connect (priv->hpaned, "notify::position",
2785 G_CALLBACK (chat_hpaned_pos_changed_cb),
2788 /* Load the paned position */
2789 paned_pos = g_settings_get_int (priv->gsettings_ui,
2790 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2792 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2794 /* Set widget focus order */
2795 list = g_list_append (NULL, priv->search_bar);
2796 list = g_list_append (list, priv->scrolled_window_input);
2797 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2800 list = g_list_append (NULL, priv->vbox_left);
2801 list = g_list_append (list, priv->scrolled_window_contacts);
2802 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2805 list = g_list_append (NULL, priv->hpaned);
2806 list = g_list_append (list, priv->hbox_topic);
2807 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2810 /* Add the main widget in the chat widget */
2811 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2812 g_object_unref (gui);
2816 chat_size_allocate (GtkWidget *widget,
2817 GtkAllocation *allocation)
2819 GtkBin *bin = GTK_BIN (widget);
2820 GtkAllocation child_allocation;
2823 gtk_widget_set_allocation (widget, allocation);
2825 child = gtk_bin_get_child (bin);
2827 if (child && gtk_widget_get_visible (child))
2829 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2830 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2831 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2832 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2834 gtk_widget_size_allocate (child, &child_allocation);
2839 chat_finalize (GObject *object)
2842 EmpathyChatPriv *priv;
2844 chat = EMPATHY_CHAT (object);
2845 priv = GET_PRIV (chat);
2847 DEBUG ("Finalized: %p", object);
2849 if (priv->update_misspelled_words_id != 0)
2850 g_source_remove (priv->update_misspelled_words_id);
2852 if (priv->save_paned_pos_id != 0)
2853 g_source_remove (priv->save_paned_pos_id);
2855 g_object_unref (priv->gsettings_chat);
2856 g_object_unref (priv->gsettings_ui);
2858 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2859 g_list_free (priv->input_history);
2861 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2862 g_list_free (priv->compositors);
2864 chat_composing_remove_timeout (chat);
2866 g_object_unref (priv->account_manager);
2867 g_object_unref (priv->log_manager);
2869 if (priv->tp_chat) {
2870 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2871 chat_destroy_cb, chat);
2872 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2873 chat_message_received_cb, chat);
2874 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2875 chat_send_error_cb, chat);
2876 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2877 chat_state_changed_cb, chat);
2878 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2879 chat_property_changed_cb, chat);
2880 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2881 chat_members_changed_cb, chat);
2882 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2883 chat_remote_contact_changed_cb, chat);
2884 empathy_tp_chat_leave (priv->tp_chat, "");
2885 g_object_unref (priv->tp_chat);
2887 if (priv->account) {
2888 g_object_unref (priv->account);
2890 if (priv->remote_contact) {
2891 g_object_unref (priv->remote_contact);
2894 if (priv->block_events_timeout_id) {
2895 g_source_remove (priv->block_events_timeout_id);
2899 g_free (priv->name);
2900 g_free (priv->subject);
2901 g_completion_free (priv->completion);
2903 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2907 chat_constructed (GObject *object)
2909 EmpathyChat *chat = EMPATHY_CHAT (object);
2910 EmpathyChatPriv *priv = GET_PRIV (chat);
2912 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2913 /* First display logs from the logger and then display pending messages */
2914 chat_add_logs (chat);
2917 /* Just display pending messages for rooms */
2918 priv->can_show_pending = TRUE;
2919 show_pending_messages (chat);
2924 empathy_chat_class_init (EmpathyChatClass *klass)
2926 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2927 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2929 object_class->finalize = chat_finalize;
2930 object_class->get_property = chat_get_property;
2931 object_class->set_property = chat_set_property;
2932 object_class->constructed = chat_constructed;
2934 widget_class->size_allocate = chat_size_allocate;
2936 g_object_class_install_property (object_class,
2938 g_param_spec_object ("tp-chat",
2940 "The tp chat object",
2941 EMPATHY_TYPE_TP_CHAT,
2944 G_PARAM_STATIC_STRINGS));
2945 g_object_class_install_property (object_class,
2947 g_param_spec_object ("account",
2948 "Account of the chat",
2949 "The account of the chat",
2952 G_PARAM_STATIC_STRINGS));
2953 g_object_class_install_property (object_class,
2955 g_param_spec_string ("id",
2957 "The id of the chat",
2960 G_PARAM_STATIC_STRINGS));
2961 g_object_class_install_property (object_class,
2963 g_param_spec_string ("name",
2965 "The name of the chat",
2968 G_PARAM_STATIC_STRINGS));
2969 g_object_class_install_property (object_class,
2971 g_param_spec_string ("subject",
2973 "The subject or topic of the chat",
2976 G_PARAM_STATIC_STRINGS));
2977 g_object_class_install_property (object_class,
2978 PROP_REMOTE_CONTACT,
2979 g_param_spec_object ("remote-contact",
2980 "The remote contact",
2981 "The remote contact is any",
2982 EMPATHY_TYPE_CONTACT,
2984 G_PARAM_STATIC_STRINGS));
2985 g_object_class_install_property (object_class,
2987 g_param_spec_boolean ("show-contacts",
2988 "Contacts' visibility",
2989 "The visibility of the contacts' list",
2992 G_PARAM_STATIC_STRINGS));
2994 g_object_class_install_property (object_class,
2996 g_param_spec_boolean ("sms-channel",
2998 "TRUE if this channel is for sending SMSes",
3000 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3002 g_object_class_install_property (object_class,
3003 PROP_N_MESSAGES_SENDING,
3004 g_param_spec_uint ("n-messages-sending",
3005 "Num Messages Sending",
3006 "The number of messages being sent",
3008 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3010 signals[COMPOSING] =
3011 g_signal_new ("composing",
3012 G_OBJECT_CLASS_TYPE (object_class),
3016 g_cclosure_marshal_VOID__BOOLEAN,
3020 signals[NEW_MESSAGE] =
3021 g_signal_new ("new-message",
3022 G_OBJECT_CLASS_TYPE (object_class),
3026 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3028 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3030 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3034 chat_block_events_timeout_cb (gpointer data)
3036 EmpathyChatPriv *priv = GET_PRIV (data);
3038 priv->block_events_timeout_id = 0;
3044 account_manager_prepared_cb (GObject *source_object,
3045 GAsyncResult *result,
3048 GList *accounts, *l;
3049 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3050 EmpathyChat *chat = user_data;
3051 GError *error = NULL;
3053 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3054 DEBUG ("Failed to prepare the account manager: %s", error->message);
3055 g_error_free (error);
3059 accounts = tp_account_manager_get_valid_accounts (account_manager);
3061 for (l = accounts; l != NULL; l = l->next) {
3062 TpAccount *account = l->data;
3063 tp_g_signal_connect_object (account, "status-changed",
3064 G_CALLBACK (chat_new_connection_cb),
3068 g_list_free (accounts);
3072 empathy_chat_init (EmpathyChat *chat)
3074 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3075 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3078 priv->log_manager = tpl_log_manager_dup_singleton ();
3079 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3080 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3082 priv->contacts_width = -1;
3083 priv->input_history = NULL;
3084 priv->input_history_current = NULL;
3085 priv->account_manager = tp_account_manager_dup ();
3087 tp_account_manager_prepare_async (priv->account_manager, NULL,
3088 account_manager_prepared_cb, chat);
3090 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3091 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3093 /* Block events for some time to avoid having "has come online" or
3094 * "joined" messages. */
3095 priv->block_events_timeout_id =
3096 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3098 /* Add nick name completion */
3099 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3100 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3102 chat_create_ui (chat);
3106 empathy_chat_new (EmpathyTpChat *tp_chat)
3108 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3112 empathy_chat_get_tp_chat (EmpathyChat *chat)
3114 EmpathyChatPriv *priv = GET_PRIV (chat);
3116 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3118 return priv->tp_chat;
3124 GtkWidget *info_bar;
3134 passwd_remember_button_cb (GtkButton *button,
3137 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3141 passwd_not_now_button_cb (GtkButton *button,
3144 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3148 remember_password_infobar_response_cb (GtkWidget *info_bar,
3152 EmpathyChatPriv *priv = GET_PRIV (data->self);
3154 if (response_id == GTK_RESPONSE_OK) {
3155 DEBUG ("Saving room password");
3156 empathy_keyring_set_room_password_async (priv->account,
3157 empathy_tp_chat_get_id (priv->tp_chat),
3162 gtk_widget_destroy (info_bar);
3163 g_free (data->password);
3164 g_slice_free (PasswordData, data);
3168 chat_prompt_to_save_password (EmpathyChat *self,
3171 GtkWidget *content_area;
3178 /* save the password in case it needs to be saved */
3179 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3181 /* Remove all previous widgets */
3182 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3183 gtk_container_forall (GTK_CONTAINER (content_area),
3184 (GtkCallback) gtk_widget_destroy, NULL);
3185 data->button = NULL;
3188 data->spinner = NULL;
3190 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3191 GTK_MESSAGE_QUESTION);
3193 hbox = gtk_hbox_new (FALSE, 5);
3194 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3197 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3198 GTK_ICON_SIZE_DIALOG);
3199 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3202 label = gtk_label_new (_("Would you like to store this password?"));
3203 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3205 /* Add 'Remember' button */
3206 alig = gtk_alignment_new (0, 0.5, 1, 0);
3208 button = gtk_button_new_with_label (_("Remember"));
3209 gtk_container_add (GTK_CONTAINER (alig), button);
3210 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3212 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3215 /* Add 'Not now' button */
3216 alig = gtk_alignment_new (0, 0.5, 1, 0);
3218 button = gtk_button_new_with_label (_("Not now"));
3219 gtk_container_add (GTK_CONTAINER (alig), button);
3220 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3222 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3226 g_signal_handler_disconnect (data->info_bar, data->response_id);
3227 g_signal_connect (data->info_bar, "response",
3228 G_CALLBACK (remember_password_infobar_response_cb), data);
3230 gtk_widget_show_all (data->info_bar);
3234 provide_password_cb (GObject *tp_chat,
3238 PasswordData *data = user_data;
3239 EmpathyChat *self = data->self;
3240 EmpathyChatPriv *priv = GET_PRIV (self);
3241 GError *error = NULL;
3243 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3245 DEBUG ("error: %s", error->message);
3246 /* FIXME: what should we do if that's another error? Close the channel?
3247 * Display the raw D-Bus error to the user isn't very useful */
3248 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3250 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3251 gtk_widget_set_sensitive (data->entry, TRUE);
3252 gtk_widget_grab_focus (data->entry);
3255 gtk_info_bar_set_message_type (
3256 GTK_INFO_BAR (data->info_bar),
3260 gtk_widget_set_sensitive (data->button, TRUE);
3261 gtk_button_set_label (GTK_BUTTON (data->button),
3265 gtk_label_set_text (GTK_LABEL (data->label),
3266 _("Wrong password; please try again:"));
3269 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3270 gtk_widget_hide (data->spinner);
3272 g_error_free (error);
3276 if (empathy_keyring_is_available ()) {
3277 /* ask whether they want to save the password */
3278 chat_prompt_to_save_password (self, data);
3280 /* Get rid of the password info bar finally */
3281 gtk_widget_destroy (data->info_bar);
3282 g_slice_free (PasswordData, data);
3286 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3287 gtk_widget_grab_focus (self->input_text_view);
3291 password_infobar_response_cb (GtkWidget *info_bar,
3295 EmpathyChatPriv *priv = GET_PRIV (data->self);
3296 const gchar *password;
3298 if (response_id != GTK_RESPONSE_OK) {
3299 gtk_widget_destroy (info_bar);
3300 g_slice_free (PasswordData, data);
3304 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3306 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3307 provide_password_cb, data);
3309 gtk_widget_set_sensitive (data->button, FALSE);
3310 gtk_widget_set_sensitive (data->entry, FALSE);
3312 gtk_spinner_start (GTK_SPINNER (data->spinner));
3313 gtk_widget_show (data->spinner);
3317 password_entry_activate_cb (GtkWidget *entry,
3320 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3324 passwd_join_button_cb (GtkButton *button,
3327 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3331 clear_icon_released_cb (GtkEntry *entry,
3332 GtkEntryIconPosition icon_pos,
3336 gtk_entry_set_text (entry, "");
3340 password_entry_changed_cb (GtkEditable *entry,
3345 str = gtk_entry_get_text (GTK_ENTRY (entry));
3347 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3348 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3352 display_password_info_bar (EmpathyChat *self)
3354 EmpathyChatPriv *priv = GET_PRIV (self);
3355 GtkWidget *info_bar;
3356 GtkWidget *content_area;
3366 data = g_slice_new0 (PasswordData);
3368 info_bar = gtk_info_bar_new ();
3369 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3370 GTK_MESSAGE_QUESTION);
3372 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3374 hbox = gtk_hbox_new (FALSE, 5);
3375 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3378 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3379 GTK_ICON_SIZE_DIALOG);
3380 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3383 label = gtk_label_new (_("This room is protected by a password:"));
3384 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3386 /* Add password entry */
3387 entry = gtk_entry_new ();
3388 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3389 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3391 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3392 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3393 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3394 GTK_ENTRY_ICON_SECONDARY, FALSE);
3396 g_signal_connect (entry, "icon-release",
3397 G_CALLBACK (clear_icon_released_cb), data);
3398 g_signal_connect (entry, "changed",
3399 G_CALLBACK (password_entry_changed_cb), data);
3401 g_signal_connect (entry, "activate",
3402 G_CALLBACK (password_entry_activate_cb), data);
3404 /* Focus the password entry once it's realized */
3405 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3407 /* Add 'Join' button */
3408 alig = gtk_alignment_new (0, 0.5, 1, 0);
3410 button = gtk_button_new_with_label (_("Join"));
3411 gtk_container_add (GTK_CONTAINER (alig), button);
3412 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3414 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3418 spinner = gtk_spinner_new ();
3419 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3421 /* Save some data for messing around with later */
3423 data->info_bar = info_bar;
3424 data->button = button;
3425 data->label = label;
3426 data->entry = entry;
3427 data->spinner = spinner;
3429 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3431 gtk_widget_show_all (hbox);
3433 data->response_id = g_signal_connect (info_bar, "response",
3434 G_CALLBACK (password_infobar_response_cb), data);
3436 gtk_widget_show_all (info_bar);
3437 /* ... but hide the spinner */
3438 gtk_widget_hide (spinner);
3442 provide_saved_password_cb (GObject *tp_chat,
3446 EmpathyChat *self = user_data;
3447 EmpathyChatPriv *priv = GET_PRIV (self);
3448 GError *error = NULL;
3450 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3452 DEBUG ("error: %s", error->message);
3453 /* FIXME: what should we do if that's another error? Close the channel?
3454 * Display the raw D-Bus error to the user isn't very useful */
3455 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3456 display_password_info_bar (self);
3457 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3459 g_error_free (error);
3464 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3465 gtk_widget_grab_focus (self->input_text_view);
3469 chat_room_got_password_cb (GObject *source,
3470 GAsyncResult *result,
3473 EmpathyChat *self = user_data;
3474 EmpathyChatPriv *priv = GET_PRIV (self);
3475 const gchar *password;
3476 GError *error = NULL;
3478 password = empathy_keyring_get_room_password_finish (priv->account,
3481 if (error != NULL) {
3482 DEBUG ("Couldn't get room password: %s\n", error->message);
3483 g_clear_error (&error);
3485 display_password_info_bar (self);
3486 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3490 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3491 provide_saved_password_cb, self);
3495 chat_password_needed_changed_cb (EmpathyChat *self)
3497 EmpathyChatPriv *priv = GET_PRIV (self);
3499 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3500 empathy_keyring_get_room_password_async (priv->account,
3501 empathy_tp_chat_get_id (priv->tp_chat),
3502 chat_room_got_password_cb, self);
3507 chat_sms_channel_changed_cb (EmpathyChat *self)
3509 EmpathyChatPriv *priv = GET_PRIV (self);
3511 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3512 g_object_notify (G_OBJECT (self), "sms-channel");
3516 chat_n_messages_sending_changed_cb (EmpathyChat *self)
3518 g_object_notify (G_OBJECT (self), "n-messages-sending");
3522 empathy_chat_set_tp_chat (EmpathyChat *chat,
3523 EmpathyTpChat *tp_chat)
3525 EmpathyChatPriv *priv = GET_PRIV (chat);
3526 GPtrArray *properties;
3528 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3529 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3530 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3532 if (priv->tp_chat) {
3536 if (priv->account) {
3537 g_object_unref (priv->account);
3540 priv->tp_chat = g_object_ref (tp_chat);
3541 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3543 g_signal_connect (tp_chat, "destroy",
3544 G_CALLBACK (chat_destroy_cb),
3546 g_signal_connect (tp_chat, "message-received",
3547 G_CALLBACK (chat_message_received_cb),
3549 g_signal_connect (tp_chat, "send-error",
3550 G_CALLBACK (chat_send_error_cb),
3552 g_signal_connect (tp_chat, "chat-state-changed",
3553 G_CALLBACK (chat_state_changed_cb),
3555 g_signal_connect (tp_chat, "property-changed",
3556 G_CALLBACK (chat_property_changed_cb),
3558 g_signal_connect (tp_chat, "members-changed",
3559 G_CALLBACK (chat_members_changed_cb),
3561 g_signal_connect (tp_chat, "member-renamed",
3562 G_CALLBACK (chat_member_renamed_cb),
3564 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3565 G_CALLBACK (chat_remote_contact_changed_cb),
3567 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3568 G_CALLBACK (chat_password_needed_changed_cb),
3570 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3571 G_CALLBACK (chat_sms_channel_changed_cb),
3573 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
3574 G_CALLBACK (chat_n_messages_sending_changed_cb),
3577 /* Get initial value of properties */
3578 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3579 if (properties != NULL) {
3582 for (i = 0; i < properties->len; i++) {
3583 EmpathyTpChatProperty *property;
3585 property = g_ptr_array_index (properties, i);
3586 if (property->value == NULL)
3589 chat_property_changed_cb (priv->tp_chat,
3596 chat_sms_channel_changed_cb (chat);
3597 chat_remote_contact_changed_cb (chat);
3599 if (chat->input_text_view) {
3600 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3601 if (priv->block_events_timeout_id == 0) {
3602 empathy_chat_view_append_event (chat->view, _("Connected"));
3606 g_object_notify (G_OBJECT (chat), "tp-chat");
3607 g_object_notify (G_OBJECT (chat), "id");
3608 g_object_notify (G_OBJECT (chat), "account");
3610 /* This is a noop when tp-chat is set at object construction time and causes
3611 * the pending messages to be show when it's set on the object after it has
3613 show_pending_messages (chat);
3615 /* check if a password is needed */
3616 chat_password_needed_changed_cb (chat);
3620 empathy_chat_get_account (EmpathyChat *chat)
3622 EmpathyChatPriv *priv = GET_PRIV (chat);
3624 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3626 return priv->account;
3630 empathy_chat_get_id (EmpathyChat *chat)
3632 EmpathyChatPriv *priv = GET_PRIV (chat);
3634 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3640 empathy_chat_dup_name (EmpathyChat *chat)
3642 EmpathyChatPriv *priv = GET_PRIV (chat);
3645 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3649 if (!ret && priv->remote_contact) {
3650 ret = empathy_contact_get_alias (priv->remote_contact);
3657 ret = _("Conversation");
3659 if (priv->sms_channel)
3660 /* Translators: this string is a something like
3661 * "Escher Cat (SMS)" */
3662 return g_strdup_printf (_("%s (SMS)"), ret);
3664 return g_strdup (ret);
3668 empathy_chat_get_subject (EmpathyChat *chat)
3670 EmpathyChatPriv *priv = GET_PRIV (chat);
3672 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3674 return priv->subject;
3678 empathy_chat_get_remote_contact (EmpathyChat *chat)
3680 EmpathyChatPriv *priv = GET_PRIV (chat);
3682 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3684 return priv->remote_contact;
3688 empathy_chat_get_contact_menu (EmpathyChat *chat)
3690 EmpathyChatPriv *priv = GET_PRIV (chat);
3691 GtkWidget *menu = NULL;
3693 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3695 if (priv->remote_contact) {
3696 menu = empathy_contact_menu_new (priv->remote_contact,
3697 EMPATHY_CONTACT_FEATURE_CALL |
3698 EMPATHY_CONTACT_FEATURE_LOG |
3699 EMPATHY_CONTACT_FEATURE_INFO |
3700 EMPATHY_CONTACT_FEATURE_BLOCK);
3707 empathy_chat_clear (EmpathyChat *chat)
3709 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3711 empathy_chat_view_clear (chat->view);
3715 empathy_chat_scroll_down (EmpathyChat *chat)
3717 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3719 empathy_chat_view_scroll_down (chat->view);
3723 empathy_chat_cut (EmpathyChat *chat)
3725 GtkTextBuffer *buffer;
3727 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3729 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3730 if (gtk_text_buffer_get_has_selection (buffer)) {
3731 GtkClipboard *clipboard;
3733 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3735 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3740 empathy_chat_copy (EmpathyChat *chat)
3742 GtkTextBuffer *buffer;
3744 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3746 if (empathy_chat_view_get_has_selection (chat->view)) {
3747 empathy_chat_view_copy_clipboard (chat->view);
3751 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3752 if (gtk_text_buffer_get_has_selection (buffer)) {
3753 GtkClipboard *clipboard;
3755 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3757 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3762 EmpathyChatPriv *priv = GET_PRIV (chat);
3764 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3771 GtkClipboard *clipboard;
3773 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3774 start = g_utf8_offset_to_pointer (topic, start_offset);
3775 end = g_utf8_offset_to_pointer (topic, end_offset);
3776 selection = g_strndup (start, end - start);
3778 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3779 gtk_clipboard_set_text (clipboard, selection, -1);
3787 empathy_chat_paste (EmpathyChat *chat)
3789 GtkTextBuffer *buffer;
3790 GtkClipboard *clipboard;
3791 EmpathyChatPriv *priv;
3793 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3795 priv = GET_PRIV (chat);
3797 if (gtk_widget_get_visible (priv->search_bar)) {
3798 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3802 if (priv->tp_chat == NULL ||
3803 !gtk_widget_is_sensitive (chat->input_text_view))
3806 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3807 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3809 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3813 empathy_chat_find (EmpathyChat *chat)
3815 EmpathyChatPriv *priv;
3817 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3819 priv = GET_PRIV (chat);
3821 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3825 empathy_chat_correct_word (EmpathyChat *chat,
3828 const gchar *new_word)
3830 GtkTextBuffer *buffer;
3832 g_return_if_fail (chat != NULL);
3833 g_return_if_fail (new_word != NULL);
3835 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3837 gtk_text_buffer_delete (buffer, start, end);
3838 gtk_text_buffer_insert (buffer, start,
3844 empathy_chat_is_room (EmpathyChat *chat)
3846 EmpathyChatPriv *priv = GET_PRIV (chat);
3848 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3850 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3854 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3856 EmpathyChatPriv *priv = GET_PRIV (self);
3858 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3860 return priv->unread_messages;
3863 /* called when the messages have been read by user */
3865 empathy_chat_messages_read (EmpathyChat *self)
3867 EmpathyChatPriv *priv = GET_PRIV (self);
3869 g_return_if_fail (EMPATHY_IS_CHAT (self));
3871 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3872 * definition. If we are still retrieving the backlogs, do not ACK */
3873 if (priv->retrieving_backlogs)
3876 if (priv->tp_chat != NULL ) {
3877 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3879 priv->unread_messages = 0;
3881 empathy_chat_view_focus_toggled (self->view, TRUE);
3884 /* Return TRUE if on of the contacts in this chat is composing */
3886 empathy_chat_is_composing (EmpathyChat *chat)
3888 return chat->priv->compositors != NULL;
3892 empathy_chat_is_sms_channel (EmpathyChat *self)
3894 EmpathyChatPriv *priv = GET_PRIV (self);
3896 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3898 return priv->sms_channel;
3902 empathy_chat_get_n_messages_sending (EmpathyChat *self)
3904 EmpathyChatPriv *priv;
3906 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3908 priv = GET_PRIV (self);
3910 if (priv->tp_chat == NULL) {
3915 g_object_get (priv->tp_chat,
3916 "n-messages-sending", &n_messages,