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 */
177 static guint signals[LAST_SIGNAL] = { 0 };
179 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
181 static gboolean update_misspelled_words (gpointer data);
184 chat_get_property (GObject *object,
189 EmpathyChat *chat = EMPATHY_CHAT (object);
190 EmpathyChatPriv *priv = GET_PRIV (object);
194 g_value_set_object (value, priv->tp_chat);
197 g_value_set_object (value, priv->account);
200 g_value_take_string (value, empathy_chat_dup_name (chat));
203 g_value_set_string (value, priv->id);
206 g_value_set_string (value, priv->subject);
208 case PROP_REMOTE_CONTACT:
209 g_value_set_object (value, priv->remote_contact);
211 case PROP_SHOW_CONTACTS:
212 g_value_set_boolean (value, priv->show_contacts);
214 case PROP_SMS_CHANNEL:
215 g_value_set_boolean (value, priv->sms_channel);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
224 chat_set_property (GObject *object,
229 EmpathyChat *chat = EMPATHY_CHAT (object);
233 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
235 case PROP_SHOW_CONTACTS:
236 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
245 account_reconnected (EmpathyChat *chat,
248 EmpathyChatPriv *priv = GET_PRIV (chat);
250 DEBUG ("Account reconnected, request a new Text channel");
252 /* FIXME: Ideally we should ask to handle ourself the channel so we can
253 * report the error if any but this is blocked by
254 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
255 switch (priv->handle_type) {
256 case TP_HANDLE_TYPE_CONTACT:
257 if (priv->sms_channel)
258 empathy_sms_contact_id (
260 TP_USER_ACTION_TIME_NOT_USER_ACTION);
262 empathy_chat_with_contact_id (
264 TP_USER_ACTION_TIME_NOT_USER_ACTION);
266 case TP_HANDLE_TYPE_ROOM:
267 empathy_join_muc (account, priv->id,
268 TP_USER_ACTION_TIME_NOT_USER_ACTION);
270 case TP_HANDLE_TYPE_NONE:
271 case TP_HANDLE_TYPE_LIST:
272 case TP_HANDLE_TYPE_GROUP:
274 g_assert_not_reached ();
278 g_object_unref (chat);
282 chat_new_connection_cb (TpAccount *account,
286 gchar *dbus_error_name,
290 EmpathyChatPriv *priv = GET_PRIV (chat);
292 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
295 if (priv->tp_chat != NULL || account != priv->account ||
296 priv->handle_type == TP_HANDLE_TYPE_NONE ||
297 EMP_STR_EMPTY (priv->id))
302 account_reconnected (chat, account);
306 chat_composing_remove_timeout (EmpathyChat *chat)
308 EmpathyChatPriv *priv;
310 priv = GET_PRIV (chat);
312 if (priv->composing_stop_timeout_id) {
313 g_source_remove (priv->composing_stop_timeout_id);
314 priv->composing_stop_timeout_id = 0;
319 chat_composing_stop_timeout_cb (EmpathyChat *chat)
321 EmpathyChatPriv *priv;
323 priv = GET_PRIV (chat);
325 priv->composing_stop_timeout_id = 0;
326 empathy_tp_chat_set_state (priv->tp_chat,
327 TP_CHANNEL_CHAT_STATE_PAUSED);
333 chat_composing_start (EmpathyChat *chat)
335 EmpathyChatPriv *priv;
337 priv = GET_PRIV (chat);
339 if (priv->composing_stop_timeout_id) {
340 /* Just restart the timeout */
341 chat_composing_remove_timeout (chat);
343 empathy_tp_chat_set_state (priv->tp_chat,
344 TP_CHANNEL_CHAT_STATE_COMPOSING);
347 priv->composing_stop_timeout_id = g_timeout_add_seconds (
348 COMPOSING_STOP_TIMEOUT,
349 (GSourceFunc) chat_composing_stop_timeout_cb,
354 chat_composing_stop (EmpathyChat *chat)
356 EmpathyChatPriv *priv;
358 priv = GET_PRIV (chat);
360 chat_composing_remove_timeout (chat);
361 empathy_tp_chat_set_state (priv->tp_chat,
362 TP_CHANNEL_CHAT_STATE_ACTIVE);
366 chat_input_history_entry_cmp (InputHistoryEntry *entry,
369 if (!tp_strdiff (entry->text, text)) {
370 if (entry->modified_text != NULL) {
371 /* Modified entry and single string cannot be equal. */
379 static InputHistoryEntry *
380 chat_input_history_entry_new_with_text (const gchar *text)
382 InputHistoryEntry *entry;
383 entry = g_slice_new0 (InputHistoryEntry);
384 entry->text = g_strdup (text);
390 chat_input_history_entry_free (InputHistoryEntry *entry)
392 g_free (entry->text);
393 g_free (entry->modified_text);
394 g_slice_free (InputHistoryEntry, entry);
398 chat_input_history_entry_revert (InputHistoryEntry *entry)
400 g_free (entry->modified_text);
401 entry->modified_text = NULL;
405 chat_input_history_entry_update_text (InputHistoryEntry *entry,
410 if (!tp_strdiff (text, entry->text)) {
411 g_free (entry->modified_text);
412 entry->modified_text = NULL;
416 old = entry->modified_text;
417 entry->modified_text = g_strdup (text);
422 chat_input_history_entry_get_text (InputHistoryEntry *entry)
428 if (entry->modified_text != NULL) {
429 return entry->modified_text;
435 chat_input_history_remove_item (GList *list,
438 list = g_list_remove_link (list, item);
439 chat_input_history_entry_free (item->data);
440 g_list_free_1 (item);
445 chat_input_history_revert (EmpathyChat *chat)
447 EmpathyChatPriv *priv;
451 InputHistoryEntry *entry;
453 priv = GET_PRIV (chat);
454 list = priv->input_history;
457 DEBUG ("No input history");
461 /* Delete temporary entry */
462 if (priv->input_history_current != NULL) {
464 list = chat_input_history_remove_item (list, item1);
465 if (priv->input_history_current == item1) {
466 /* Removed temporary entry was current entry */
467 priv->input_history = list;
468 priv->input_history_current = NULL;
473 /* There is no entry to revert */
477 /* Restore the current history entry to original value */
478 item1 = priv->input_history_current;
480 chat_input_history_entry_revert (entry);
482 /* Remove restored entry if there is other occurance before this entry */
483 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
484 (GCompareFunc) chat_input_history_entry_cmp);
485 if (item2 != item1) {
486 list = chat_input_history_remove_item (list, item1);
489 /* Remove other occurance of the restored entry */
490 item2 = g_list_find_custom (item1->next,
491 chat_input_history_entry_get_text (entry),
492 (GCompareFunc) chat_input_history_entry_cmp);
494 list = chat_input_history_remove_item (list, item2);
498 priv->input_history_current = NULL;
499 priv->input_history = list;
503 chat_input_history_add (EmpathyChat *chat,
507 EmpathyChatPriv *priv;
510 InputHistoryEntry *entry;
512 priv = GET_PRIV (chat);
514 list = priv->input_history;
516 /* Remove any other occurances of this entry, if not temporary */
518 while ((item = g_list_find_custom (list, str,
519 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
520 list = chat_input_history_remove_item (list, item);
523 /* Trim the list to the last 10 items */
524 while (g_list_length (list) > 10) {
525 item = g_list_last (list);
527 list = chat_input_history_remove_item (list, item);
535 entry = chat_input_history_entry_new_with_text (str);
536 list = g_list_prepend (list, entry);
538 /* Set the list and the current item pointer */
539 priv->input_history = list;
541 priv->input_history_current = list;
544 priv->input_history_current = NULL;
549 chat_input_history_get_next (EmpathyChat *chat)
551 EmpathyChatPriv *priv;
555 priv = GET_PRIV (chat);
557 if (priv->input_history == NULL) {
558 DEBUG ("No input history, next entry is NULL");
561 g_assert (priv->input_history_current != NULL);
563 if ((item = g_list_next (priv->input_history_current)) == NULL)
565 item = priv->input_history_current;
568 msg = chat_input_history_entry_get_text (item->data);
570 DEBUG ("Returning next entry: '%s'", msg);
572 priv->input_history_current = item;
578 chat_input_history_get_prev (EmpathyChat *chat)
580 EmpathyChatPriv *priv;
584 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
586 priv = GET_PRIV (chat);
588 if (priv->input_history == NULL) {
589 DEBUG ("No input history, previous entry is NULL");
593 if (priv->input_history_current == NULL)
597 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
599 item = priv->input_history_current;
602 msg = chat_input_history_entry_get_text (item->data);
604 DEBUG ("Returning previous entry: '%s'", msg);
606 priv->input_history_current = item;
612 chat_input_history_update (EmpathyChat *chat,
613 GtkTextBuffer *buffer)
615 EmpathyChatPriv *priv;
616 GtkTextIter start, end;
618 InputHistoryEntry *entry;
620 priv = GET_PRIV (chat);
622 gtk_text_buffer_get_bounds (buffer, &start, &end);
623 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
625 if (priv->input_history_current == NULL) {
626 /* Add the current text temporarily to the history */
627 chat_input_history_add (chat, text, TRUE);
632 /* Save the changes in the history */
633 entry = priv->input_history_current->data;
634 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
635 chat_input_history_entry_update_text (entry, text);
644 } ChatCommandMsgData;
647 chat_command_msg_cb (GObject *source,
648 GAsyncResult *result,
651 ChatCommandMsgData *data = user_data;
652 GError *error = NULL;
655 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
656 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
658 if (channel == NULL) {
659 DEBUG ("Failed to get channel: %s", error->message);
660 g_error_free (error);
662 empathy_chat_view_append_event (data->chat->view,
663 _("Failed to open private chat"));
667 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
668 TpTextChannel *text = (TpTextChannel *) channel;
671 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
674 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
676 g_object_unref (msg);
679 g_object_unref (channel);
682 g_free (data->message);
683 g_slice_free (ChatCommandMsgData, data);
687 nick_command_supported (EmpathyChat *chat)
689 EmpathyChatPriv * priv = GET_PRIV (chat);
690 TpConnection *connection;
692 connection = empathy_tp_chat_get_connection (priv->tp_chat);
693 return tp_proxy_has_interface_by_id (connection,
694 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
699 part_command_supported (EmpathyChat *chat)
701 EmpathyChatPriv * priv = GET_PRIV (chat);
704 channel = empathy_tp_chat_get_channel (priv->tp_chat);
705 return tp_proxy_has_interface_by_id (channel,
706 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
711 chat_command_clear (EmpathyChat *chat,
714 empathy_chat_view_clear (chat->view);
718 chat_command_topic (EmpathyChat *chat,
721 EmpathyChatPriv *priv = GET_PRIV (chat);
722 EmpathyTpChatProperty *property;
723 GValue value = {0, };
725 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
726 if (property == NULL) {
727 empathy_chat_view_append_event (chat->view,
728 _("Topic not supported on this conversation"));
732 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
733 empathy_chat_view_append_event (chat->view,
734 _("You are not allowed to change the topic"));
738 g_value_init (&value, G_TYPE_STRING);
739 g_value_set_string (&value, strv[1]);
740 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
741 g_value_unset (&value);
745 empathy_chat_join_muc (EmpathyChat *chat,
748 EmpathyChatPriv *priv = GET_PRIV (chat);
750 empathy_join_muc (priv->account, room,
751 gtk_get_current_event_time ());
755 chat_command_join (EmpathyChat *chat,
760 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
762 /* FIXME: Ideally we should ask to handle ourself the channel so we can
763 * report the error if any but this is blocked by
764 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
765 while (rooms[i] != NULL) {
766 /* ignore empty strings */
767 if (!EMP_STR_EMPTY (rooms[i])) {
768 empathy_chat_join_muc (chat, rooms[i]);
776 empathy_chat_leave_chat (EmpathyChat *chat)
778 EmpathyChatPriv *priv = GET_PRIV (chat);
780 empathy_tp_chat_leave (priv->tp_chat, "");
785 chat_command_part (EmpathyChat *chat,
788 EmpathyChatPriv *priv = GET_PRIV (chat);
789 EmpathyChat *chat_to_be_parted;
791 if (strv[1] == NULL) {
792 empathy_tp_chat_leave (priv->tp_chat, "");
795 chat_to_be_parted = empathy_chat_window_find_chat (priv->account, strv[1]);
797 if (chat_to_be_parted != NULL) {
798 empathy_tp_chat_leave (empathy_chat_get_tp_chat (chat_to_be_parted), strv[2]);
800 empathy_tp_chat_leave (priv->tp_chat, strv[1]);
806 chat_command_msg_internal (EmpathyChat *chat,
807 const gchar *contact_id,
808 const gchar *message)
810 EmpathyChatPriv *priv = GET_PRIV (chat);
811 ChatCommandMsgData *data;
812 TpAccountChannelRequest *req;
815 request = tp_asv_new (
816 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
817 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
818 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
821 req = tp_account_channel_request_new (priv->account, request,
822 tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
824 /* FIXME: We should probably search in members alias. But this
825 * is enough for IRC */
826 data = g_slice_new (ChatCommandMsgData);
828 data->message = g_strdup (message);
830 tp_account_channel_request_ensure_and_observe_channel_async (req,
831 EMPATHY_CHAT_BUS_NAME, NULL, chat_command_msg_cb, data);
833 g_object_unref (req);
834 g_hash_table_unref (request);
838 chat_command_query (EmpathyChat *chat,
841 /* If <message> part is not defined,
842 * strv[2] will be the terminal NULL */
843 chat_command_msg_internal (chat, strv[1], strv[2]);
847 chat_command_msg (EmpathyChat *chat,
850 chat_command_msg_internal (chat, strv[1], strv[2]);
854 callback_for_request_rename (TpProxy *proxy,
857 GObject *weak_object)
860 DEBUG ("Call to RequestRename method failed: %s",error->message);
865 chat_command_nick (EmpathyChat *chat,
868 EmpathyChatPriv *priv = GET_PRIV (chat);
871 proxy = TP_PROXY (tp_account_get_connection (priv->account));
873 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
874 strv[1], callback_for_request_rename, NULL, NULL, NULL);
878 chat_command_me (EmpathyChat *chat,
881 EmpathyChatPriv *priv = GET_PRIV (chat);
885 channel = empathy_tp_chat_get_channel (priv->tp_chat);
887 /* Strictly speaking we don't depend yet on Messages so best to check that
888 * the channel is actually a TpTextChannel before casting it. */
889 if (TP_IS_TEXT_CHANNEL (channel) &&
890 !tp_text_channel_supports_message_type (TP_TEXT_CHANNEL (channel),
891 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
892 /* Action message are not supported, 'simulate' the action */
893 EmpathyContact *self_contact;
896 self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
897 /* The TpChat can't be ready if it doesn't have the self contact */
898 g_assert (self_contact != NULL);
900 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (self_contact),
902 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
907 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
911 empathy_tp_chat_send (priv->tp_chat, message);
912 g_object_unref (message);
916 chat_command_say (EmpathyChat *chat,
919 EmpathyChatPriv *priv = GET_PRIV (chat);
922 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
924 empathy_tp_chat_send (priv->tp_chat, message);
925 g_object_unref (message);
928 static void chat_command_help (EmpathyChat *chat, GStrv strv);
930 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
936 ChatCommandFunc func;
937 gboolean (*is_supported)(EmpathyChat *chat);
941 static ChatCommandItem commands[] = {
942 {"clear", 1, 1, chat_command_clear, NULL,
943 N_("/clear: clear all messages from the current conversation")},
945 {"topic", 2, 2, chat_command_topic, NULL,
946 N_("/topic <topic>: set the topic of the current conversation")},
948 {"join", 2, 2, chat_command_join, NULL,
949 N_("/join <chat room ID>: join a new chat room")},
951 {"j", 2, 2, chat_command_join, NULL,
952 N_("/j <chat room ID>: join a new chat room")},
955 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
956 {"part", 1, 3, chat_command_part, part_command_supported,
957 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
958 "by default the current one")},
961 {"query", 2, 3, chat_command_query, NULL,
962 N_("/query <contact ID> [<message>]: open a private chat")},
964 {"msg", 3, 3, chat_command_msg, NULL,
965 N_("/msg <contact ID> <message>: open a private chat")},
967 {"nick", 2, 2, chat_command_nick, nick_command_supported,
968 N_("/nick <nickname>: change your nickname on the current server")},
970 {"me", 2, 2, chat_command_me, NULL,
971 N_("/me <message>: send an ACTION message to the current conversation")},
973 {"say", 2, 2, chat_command_say, NULL,
974 N_("/say <message>: send <message> to the current conversation. "
975 "This is used to send a message starting with a '/'. For example: "
976 "\"/say /join is used to join a new chat room\"")},
978 {"help", 1, 2, chat_command_help, NULL,
979 N_("/help [<command>]: show all supported commands. "
980 "If <command> is defined, show its usage.")},
984 chat_command_show_help (EmpathyChat *chat,
985 ChatCommandItem *item)
989 str = g_strdup_printf (_("Usage: %s"), _(item->help));
990 empathy_chat_view_append_event (chat->view, str);
995 chat_command_help (EmpathyChat *chat,
1000 /* If <command> part is not defined,
1001 * strv[1] will be the terminal NULL */
1002 if (strv[1] == NULL) {
1003 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1004 if (commands[i].is_supported != NULL) {
1005 if (!commands[i].is_supported (chat)) {
1009 empathy_chat_view_append_event (chat->view,
1010 _(commands[i].help));
1015 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1016 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1017 if (commands[i].is_supported != NULL) {
1018 if (!commands[i].is_supported (chat)) {
1022 chat_command_show_help (chat, &commands[i]);
1027 empathy_chat_view_append_event (chat->view,
1028 _("Unknown command"));
1032 chat_command_parse (const gchar *text, guint max_parts)
1037 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1039 array = g_ptr_array_sized_new (max_parts + 1);
1040 while (max_parts > 1) {
1043 /* Skip white spaces */
1044 while (g_ascii_isspace (*text)) {
1048 /* Search the end of this part, until first space. */
1049 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1055 item = g_strndup (text, end - text);
1056 g_ptr_array_add (array, item);
1057 DEBUG ("\tITEM: \"%s\"", item);
1063 /* Append last part if not empty */
1064 item = g_strstrip (g_strdup (text));
1065 if (!EMP_STR_EMPTY (item)) {
1066 g_ptr_array_add (array, item);
1067 DEBUG ("\tITEM: \"%s\"", item);
1072 /* Make the array NULL-terminated */
1073 g_ptr_array_add (array, NULL);
1075 return (GStrv) g_ptr_array_free (array, FALSE);
1079 has_prefix_case (const gchar *s,
1080 const gchar *prefix)
1082 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1086 chat_send (EmpathyChat *chat,
1089 EmpathyChatPriv *priv;
1093 if (EMP_STR_EMPTY (msg)) {
1097 priv = GET_PRIV (chat);
1099 chat_input_history_add (chat, msg, FALSE);
1101 if (msg[0] == '/') {
1102 gboolean second_slash = FALSE;
1103 const gchar *iter = msg + 1;
1105 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1110 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1113 c = *(msg + 1 + strlen (commands[i].prefix));
1114 if (c != '\0' && !g_ascii_isspace (c)) {
1117 if (commands[i].is_supported != NULL) {
1118 if (!commands[i].is_supported (chat)) {
1123 /* We can't use g_strsplit here because it does
1124 * not deal correctly if we have more than one space
1126 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1128 strv_len = g_strv_length (strv);
1129 if (strv_len < commands[i].min_parts ||
1130 strv_len > commands[i].max_parts) {
1131 chat_command_show_help (chat, &commands[i]);
1136 commands[i].func (chat, strv);
1141 /* Also allow messages with two slashes before the
1142 * first space, so it is possible to send a /unix/path.
1143 * This heuristic is kind of crap. */
1144 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1146 second_slash = TRUE;
1152 if (!second_slash) {
1153 empathy_chat_view_append_event (chat->view,
1154 _("Unknown command; see /help for the available"
1160 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1162 empathy_tp_chat_send (priv->tp_chat, message);
1163 g_object_unref (message);
1167 chat_input_text_view_send (EmpathyChat *chat)
1169 GtkTextBuffer *buffer;
1170 GtkTextIter start, end;
1173 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1175 gtk_text_buffer_get_bounds (buffer, &start, &end);
1176 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1178 /* clear the input field */
1179 gtk_text_buffer_set_text (buffer, "", -1);
1180 /* delete input history modifications */
1181 chat_input_history_revert (chat);
1183 chat_send (chat, msg);
1188 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1189 EmpathyContact *contact,
1190 TpChannelChatState state,
1193 EmpathyChatPriv *priv;
1195 gboolean was_composing;
1197 priv = GET_PRIV (chat);
1199 if (empathy_contact_is_user (contact)) {
1200 /* We don't care about our own chat state */
1204 was_composing = (priv->compositors != NULL);
1206 /* Find the contact in the list. After that l is the list elem or NULL */
1207 for (l = priv->compositors; l; l = l->next) {
1208 if (contact == l->data) {
1214 case TP_CHANNEL_CHAT_STATE_GONE:
1215 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1216 case TP_CHANNEL_CHAT_STATE_PAUSED:
1217 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1218 /* Contact is not composing */
1220 priv->compositors = g_list_remove_link (priv->compositors, l);
1221 g_object_unref (l->data);
1225 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1226 /* Contact is composing */
1228 priv->compositors = g_list_prepend (priv->compositors,
1229 g_object_ref (contact));
1233 g_assert_not_reached ();
1236 DEBUG ("Was composing: %s now composing: %s",
1237 was_composing ? "yes" : "no",
1238 priv->compositors ? "yes" : "no");
1240 if ((was_composing && !priv->compositors) ||
1241 (!was_composing && priv->compositors)) {
1242 /* Composing state changed */
1243 g_signal_emit (chat, signals[COMPOSING], 0,
1244 priv->compositors != NULL);
1249 chat_message_received (EmpathyChat *chat,
1250 EmpathyMessage *message,
1253 EmpathyChatPriv *priv = GET_PRIV (chat);
1254 EmpathyContact *sender;
1256 sender = empathy_message_get_sender (message);
1258 DEBUG ("Appending new message from %s (%d)",
1259 empathy_contact_get_alias (sender),
1260 empathy_contact_get_handle (sender));
1262 empathy_chat_view_append_message (chat->view, message);
1264 /* We received a message so the contact is no longer composing */
1265 chat_state_changed_cb (priv->tp_chat, sender,
1266 TP_CHANNEL_CHAT_STATE_ACTIVE,
1269 priv->unread_messages++;
1270 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1274 chat_message_received_cb (EmpathyTpChat *tp_chat,
1275 EmpathyMessage *message,
1278 chat_message_received (chat, message, FALSE);
1282 chat_send_error_cb (EmpathyTpChat *tp_chat,
1283 const gchar *message_body,
1284 TpChannelTextSendError error_code,
1290 switch (error_code) {
1291 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1292 error = _("offline");
1294 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1295 error = _("invalid contact");
1297 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1298 error = _("permission denied");
1300 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1301 error = _("too long message");
1303 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1304 error = _("not implemented");
1306 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1308 error = _("unknown");
1312 if (message_body != NULL) {
1313 str = g_strdup_printf (_("Error sending message '%s': %s"),
1314 message_body, error);
1317 str = g_strdup_printf (_("Error sending message: %s"), error);
1320 empathy_chat_view_append_event (chat->view, str);
1325 chat_topic_label_size_allocate_cb (GtkLabel *label,
1326 GtkAllocation *allocation,
1329 EmpathyChatPriv *priv = GET_PRIV (chat);
1331 if (!gtk_label_get_line_wrap (label)) {
1332 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1333 gtk_widget_show (priv->expander_topic);
1335 gtk_widget_hide (priv->expander_topic);
1342 chat_topic_expander_activate_cb (GtkExpander *expander,
1343 GParamSpec *param_spec,
1346 EmpathyChatPriv *priv = GET_PRIV (chat);
1348 if (gtk_expander_get_expanded (expander)) {
1349 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1350 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1352 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1353 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1358 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1363 EmpathyChatPriv *priv = GET_PRIV (chat);
1365 if (!tp_strdiff (name, "subject")) {
1366 g_free (priv->subject);
1367 priv->subject = g_value_dup_string (value);
1368 g_object_notify (G_OBJECT (chat), "subject");
1370 if (EMP_STR_EMPTY (priv->subject)) {
1371 gtk_widget_hide (priv->hbox_topic);
1373 gchar *markup_topic;
1376 markup_topic = empathy_add_link_markup (priv->subject);
1377 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1378 _("Topic:"), markup_topic);
1380 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1381 g_free (markup_text);
1382 g_free (markup_topic);
1384 gtk_widget_show (priv->hbox_topic);
1386 if (priv->block_events_timeout_id == 0) {
1389 if (!EMP_STR_EMPTY (priv->subject)) {
1390 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1392 str = g_strdup (_("No topic defined"));
1394 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1398 else if (!tp_strdiff (name, "name")) {
1399 g_free (priv->name);
1400 priv->name = g_value_dup_string (value);
1401 g_object_notify (G_OBJECT (chat), "name");
1406 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1410 GtkTextIter word_start = *iter;
1411 GtkTextIter word_end = *iter;
1414 if (gtk_text_iter_inside_word (&word_end) &&
1415 !gtk_text_iter_ends_word (&word_end)) {
1416 gtk_text_iter_forward_word_end (&word_end);
1421 if (gtk_text_iter_get_char (&tmp) == '\'') {
1422 gtk_text_iter_forward_char (&tmp);
1424 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1425 gtk_text_iter_forward_word_end (&word_end);
1430 if (gtk_text_iter_inside_word (&word_start) ||
1431 gtk_text_iter_ends_word (&word_start)) {
1432 if (!gtk_text_iter_starts_word (&word_start) ||
1433 gtk_text_iter_equal (&word_start, &word_end)) {
1434 gtk_text_iter_backward_word_start (&word_start);
1438 gtk_text_iter_backward_char (&tmp);
1440 if (gtk_text_iter_get_char (&tmp) == '\'') {
1441 gtk_text_iter_backward_char (&tmp);
1443 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1444 gtk_text_iter_backward_word_start (&word_start);
1449 *start = word_start;
1455 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1456 GtkTextIter *location,
1461 GtkTextIter iter, pos;
1463 /* Remove all misspelled tags in the inserted text.
1464 * This happens when text is inserted within a misspelled word. */
1465 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1466 gtk_text_iter_get_offset (location) - len);
1467 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1470 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1473 GtkTextIter start, end;
1476 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1479 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1481 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1482 gtk_text_iter_equal (&pos, &end) ||
1483 empathy_spell_check (str)) {
1484 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1486 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1491 } while (gtk_text_iter_forward_word_end (&iter) &&
1492 gtk_text_iter_compare (&iter, location) <= 0);
1496 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1501 GtkTextIter word_start, word_end;
1503 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1504 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1505 &word_start, &word_end);
1510 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1513 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1514 chat_composing_stop (chat);
1516 chat_composing_start (chat);
1521 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1526 GtkTextIter prev_pos;
1527 GtkTextIter word_start;
1528 GtkTextIter word_end;
1532 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1534 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1535 gtk_text_buffer_get_insert (buffer));
1536 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1538 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1541 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1542 !gtk_text_iter_equal (&pos, &word_end)) {
1543 str = gtk_text_buffer_get_text (buffer,
1544 &word_start, &word_end, FALSE);
1546 if (!empathy_spell_check (str)) {
1547 gtk_text_buffer_apply_tag_by_name (buffer,
1548 "misspelled", &word_start, &word_end);
1550 gtk_text_buffer_remove_tag_by_name (buffer,
1551 "misspelled", &word_start, &word_end);
1558 gtk_text_buffer_move_mark (buffer, mark, &pos);
1562 empathy_isspace_cb (gunichar c,
1565 return g_unichar_isspace (c);
1569 chat_input_key_press_event_cb (GtkWidget *widget,
1573 EmpathyChatPriv *priv;
1576 GtkWidget *text_view_sw;
1578 priv = GET_PRIV (chat);
1580 /* Catch ctrl+up/down so we can traverse messages we sent */
1581 if ((event->state & GDK_CONTROL_MASK) &&
1582 (event->keyval == GDK_KEY_Up ||
1583 event->keyval == GDK_KEY_Down)) {
1584 GtkTextBuffer *buffer;
1587 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1588 chat_input_history_update (chat, buffer);
1590 if (event->keyval == GDK_KEY_Up) {
1591 str = chat_input_history_get_next (chat);
1593 str = chat_input_history_get_prev (chat);
1596 g_signal_handlers_block_by_func (buffer,
1597 chat_input_text_buffer_changed_cb,
1599 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1600 g_signal_handlers_unblock_by_func (buffer,
1601 chat_input_text_buffer_changed_cb,
1607 /* Catch enter but not ctrl/shift-enter */
1608 if (IS_ENTER (event->keyval) &&
1609 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1612 /* This is to make sure that kinput2 gets the enter. And if
1613 * it's handled there we shouldn't send on it. This is because
1614 * kinput2 uses Enter to commit letters. See:
1615 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1618 view = GTK_TEXT_VIEW (chat->input_text_view);
1619 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1620 gtk_text_view_reset_im_context (view);
1624 chat_input_text_view_send (chat);
1628 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1630 if (IS_ENTER (event->keyval) &&
1631 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1632 /* Newline for shift/control-enter. */
1635 if (!(event->state & GDK_CONTROL_MASK) &&
1636 event->keyval == GDK_KEY_Page_Up) {
1637 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1638 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1641 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1642 event->keyval == GDK_KEY_Page_Down) {
1643 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1644 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1645 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1646 gtk_adjustment_set_value (adj, val);
1649 if (event->keyval == GDK_KEY_Escape) {
1650 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1652 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1653 event->keyval == GDK_KEY_Tab) {
1654 GtkTextBuffer *buffer;
1655 GtkTextIter start, current;
1656 gchar *nick, *completed;
1657 GList *list, *completed_list;
1658 gboolean is_start_of_buffer;
1660 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1661 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1663 /* Get the start of the nick to complete. */
1664 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1665 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1666 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1668 is_start_of_buffer = gtk_text_iter_is_start (&start);
1670 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1671 g_completion_add_items (priv->completion, list);
1673 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1674 completed_list = g_completion_complete (priv->completion,
1683 GString *message = NULL;
1686 gtk_text_buffer_delete (buffer, &start, ¤t);
1688 len = g_list_length (completed_list);
1691 /* If we only have one hit, use that text
1692 * instead of the text in completed since the
1693 * completed text will use the typed string
1694 * which might be cased all wrong.
1697 text = empathy_contact_get_alias (completed_list->data);
1701 /* Print all hits to the scrollback view, so the
1702 * user knows what possibilities he has.
1705 message = g_string_new ("");
1706 for (l = completed_list; l != NULL; l = l->next) {
1707 g_string_append (message, empathy_contact_get_alias (l->data));
1708 g_string_append (message, " - ");
1710 empathy_chat_view_append_event (chat->view, message->str);
1711 g_string_free (message, TRUE);
1714 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1716 if (len == 1 && is_start_of_buffer) {
1717 gchar *complete_char;
1719 complete_char = g_settings_get_string (
1720 priv->gsettings_chat,
1721 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1723 if (complete_char != NULL) {
1724 gtk_text_buffer_insert_at_cursor (buffer,
1726 strlen (complete_char));
1727 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1728 g_free (complete_char);
1735 g_completion_clear_items (priv->completion);
1737 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1747 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1751 gtk_widget_grab_focus (chat->input_text_view);
1757 chat_input_realize_cb (GtkWidget *widget,
1760 DEBUG ("Setting focus to the input text view");
1761 if (gtk_widget_is_sensitive (widget)) {
1762 gtk_widget_grab_focus (widget);
1767 chat_input_has_focus_notify_cb (GtkWidget *widget,
1771 empathy_chat_view_focus_toggled (chat->view, gtk_widget_has_focus (widget));
1775 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1776 EmpathySmiley *smiley,
1779 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1780 GtkTextBuffer *buffer;
1783 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1785 gtk_text_buffer_get_end_iter (buffer, &iter);
1786 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1788 gtk_text_buffer_get_end_iter (buffer, &iter);
1789 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1800 static EmpathyChatSpell *
1801 chat_spell_new (EmpathyChat *chat,
1806 EmpathyChatSpell *chat_spell;
1808 chat_spell = g_slice_new0 (EmpathyChatSpell);
1810 chat_spell->chat = g_object_ref (chat);
1811 chat_spell->word = g_strdup (word);
1812 chat_spell->start = start;
1813 chat_spell->end = end;
1819 chat_spell_free (EmpathyChatSpell *chat_spell)
1821 g_object_unref (chat_spell->chat);
1822 g_free (chat_spell->word);
1823 g_slice_free (EmpathyChatSpell, chat_spell);
1827 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1828 EmpathyChatSpell *chat_spell)
1830 empathy_chat_correct_word (chat_spell->chat,
1831 &(chat_spell->start),
1833 gtk_menu_item_get_label (menu_item));
1838 chat_spelling_build_suggestions_menu (const gchar *code,
1839 EmpathyChatSpell *chat_spell)
1841 GList *suggestions, *l;
1842 GtkWidget *menu, *menu_item;
1844 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1845 if (suggestions == NULL)
1848 menu = gtk_menu_new ();
1849 for (l = suggestions; l; l = l->next) {
1850 menu_item = gtk_menu_item_new_with_label (l->data);
1851 g_signal_connect (G_OBJECT (menu_item), "activate",
1852 G_CALLBACK (chat_spelling_menu_activate_cb),
1854 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1856 empathy_spell_free_suggestions (suggestions);
1858 gtk_widget_show_all (menu);
1864 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1866 GtkWidget *menu, *submenu, *item;
1869 codes = empathy_spell_get_enabled_language_codes ();
1870 g_assert (codes != NULL);
1872 if (g_list_length (codes) > 1) {
1873 menu = gtk_menu_new ();
1875 for (l = codes; l; l = l->next) {
1876 const gchar *code, *name;
1879 name = empathy_spell_get_language_name (code);
1883 item = gtk_image_menu_item_new_with_label (name);
1885 submenu = chat_spelling_build_suggestions_menu (
1887 if (submenu == NULL)
1888 gtk_widget_set_sensitive (item, FALSE);
1890 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1892 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1895 menu = chat_spelling_build_suggestions_menu (codes->data,
1898 menu = gtk_menu_new ();
1899 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1900 gtk_widget_set_sensitive (item, FALSE);
1901 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1904 g_list_free (codes);
1906 gtk_widget_show_all (menu);
1917 static EmpathyChatWord *
1918 chat_word_new (EmpathyChat *chat,
1922 EmpathyChatWord *chat_word;
1924 chat_word = g_slice_new0 (EmpathyChatWord);
1926 chat_word->chat = g_object_ref (chat);
1927 chat_word->word = g_strdup (word);
1928 chat_word->code = g_strdup (code);
1934 chat_word_free (EmpathyChatWord *chat_word)
1936 g_object_unref (chat_word->chat);
1937 g_free (chat_word->word);
1938 g_free (chat_word->code);
1939 g_slice_free (EmpathyChatWord, chat_word);
1943 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1944 EmpathyChatWord *chat_word)
1946 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1948 empathy_spell_add_to_dictionary (chat_word->code,
1950 priv->update_misspelled_words_id =
1951 g_idle_add (update_misspelled_words, chat_word->chat);
1955 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1957 GtkWidget *menu, *item, *lang_item, *image;
1960 const gchar *code, *name;
1961 EmpathyChatWord *chat_word;
1963 codes = empathy_spell_get_enabled_language_codes ();
1964 g_assert (codes != NULL);
1965 if (g_list_length (codes) > 1) {
1966 /* translators: %s is the selected word */
1967 label = g_strdup_printf (_("Add '%s' to Dictionary"),
1969 item = gtk_image_menu_item_new_with_mnemonic (label);
1971 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1972 GTK_ICON_SIZE_MENU);
1973 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1976 menu = gtk_menu_new ();
1978 for (l = codes; l; l = l->next) {
1980 name = empathy_spell_get_language_name (code);
1984 lang_item = gtk_image_menu_item_new_with_label (name);
1986 chat_word= chat_word_new (chat_spell->chat,
1987 chat_spell->word, code);
1988 g_object_set_data_full (G_OBJECT (lang_item),
1989 "chat-word", chat_word,
1990 (GDestroyNotify) chat_word_free);
1992 g_signal_connect (G_OBJECT (lang_item), "activate",
1993 G_CALLBACK (chat_add_to_dictionary_activate_cb),
1995 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
1997 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2000 name = empathy_spell_get_language_name (code);
2001 g_assert (name != NULL);
2002 /* translators: first %s is the selected word,
2003 * second %s is the language name of the target dictionary */
2004 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
2005 chat_spell->word, name);
2006 item = gtk_image_menu_item_new_with_mnemonic (label);
2008 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2009 GTK_ICON_SIZE_MENU);
2010 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2012 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2014 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2015 (GDestroyNotify) chat_word_free);
2017 g_signal_connect (G_OBJECT (item), "activate",
2018 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2021 g_list_free (codes);
2023 gtk_widget_show_all (item);
2029 chat_text_send_cb (GtkMenuItem *menuitem,
2032 chat_input_text_view_send (chat);
2036 chat_input_populate_popup_cb (GtkTextView *view,
2040 GtkTextBuffer *buffer;
2041 GtkTextTagTable *table;
2044 GtkTextIter iter, start, end;
2047 EmpathyChatSpell *chat_spell;
2048 GtkWidget *spell_menu;
2049 GtkWidget *spell_item;
2050 EmpathySmileyManager *smiley_manager;
2051 GtkWidget *smiley_menu;
2054 buffer = gtk_text_view_get_buffer (view);
2056 /* Add the emoticon menu. */
2057 item = gtk_separator_menu_item_new ();
2058 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2059 gtk_widget_show (item);
2061 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2062 image = gtk_image_new_from_icon_name ("face-smile",
2063 GTK_ICON_SIZE_MENU);
2064 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2065 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2066 gtk_widget_show (item);
2068 smiley_manager = empathy_smiley_manager_dup_singleton ();
2069 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2070 chat_insert_smiley_activate_cb,
2072 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2073 g_object_unref (smiley_manager);
2075 /* Add the Send menu item. */
2076 gtk_text_buffer_get_bounds (buffer, &start, &end);
2077 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2078 if (!EMP_STR_EMPTY (str)) {
2079 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2080 g_signal_connect (G_OBJECT (item), "activate",
2081 G_CALLBACK (chat_text_send_cb), chat);
2082 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2083 gtk_widget_show (item);
2087 /* Add the spell check menu item. */
2088 table = gtk_text_buffer_get_tag_table (buffer);
2089 tag = gtk_text_tag_table_lookup (table, "misspelled");
2090 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
2091 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2092 GTK_TEXT_WINDOW_WIDGET,
2095 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2097 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2098 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2100 str = gtk_text_buffer_get_text (buffer,
2101 &start, &end, FALSE);
2103 if (!EMP_STR_EMPTY (str)) {
2104 chat_spell = chat_spell_new (chat, str, start, end);
2105 g_object_set_data_full (G_OBJECT (menu),
2106 "chat-spell", chat_spell,
2107 (GDestroyNotify) chat_spell_free);
2109 item = gtk_separator_menu_item_new ();
2110 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2111 gtk_widget_show (item);
2113 /* Spelling suggestions */
2114 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2115 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2116 GTK_ICON_SIZE_MENU);
2117 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2119 spell_menu = chat_spelling_build_menu (chat_spell);
2120 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2123 spell_item = gtk_separator_menu_item_new ();
2124 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2125 gtk_widget_show (spell_item);
2127 /* Add to dictionary */
2128 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2130 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2131 gtk_widget_show (spell_item);
2133 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2134 gtk_widget_show (item);
2140 chat_log_filter (TplEvent *log,
2143 EmpathyChat *chat = user_data;
2144 EmpathyMessage *message;
2145 EmpathyChatPriv *priv = GET_PRIV (chat);
2146 const GList *pending;
2148 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2149 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2151 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2152 message = empathy_message_from_tpl_log_event (log);
2154 for (; pending; pending = g_list_next (pending)) {
2155 if (empathy_message_equal (message, pending->data)) {
2156 g_object_unref (message);
2161 g_object_unref (message);
2167 show_pending_messages (EmpathyChat *chat) {
2168 EmpathyChatPriv *priv = GET_PRIV (chat);
2169 const GList *messages, *l;
2171 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2173 if (chat->view == NULL || priv->tp_chat == NULL)
2176 if (!priv->can_show_pending)
2179 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2181 for (l = messages; l != NULL ; l = g_list_next (l)) {
2182 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2183 chat_message_received (chat, message, TRUE);
2189 got_filtered_messages_cb (GObject *manager,
2190 GAsyncResult *result,
2195 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2196 EmpathyChatPriv *priv = GET_PRIV (chat);
2197 GError *error = NULL;
2199 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2200 result, &messages, &error)) {
2201 DEBUG ("%s. Aborting.", error->message);
2202 empathy_chat_view_append_event (chat->view,
2203 _("Failed to retrieve recent logs"));
2204 g_error_free (error);
2208 for (l = messages; l; l = g_list_next (l)) {
2209 EmpathyMessage *message;
2210 g_assert (TPL_IS_EVENT (l->data));
2212 message = empathy_message_from_tpl_log_event (l->data);
2213 g_object_unref (l->data);
2215 empathy_chat_view_append_message (chat->view, message);
2216 g_object_unref (message);
2218 g_list_free (messages);
2221 /* in case of TPL error, skip backlog and show pending messages */
2222 priv->can_show_pending = TRUE;
2223 show_pending_messages (chat);
2225 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2226 * about it in EmpathyChatPriv definition */
2227 priv->retrieving_backlogs = FALSE;
2228 empathy_chat_messages_read (chat);
2230 /* Turn back on scrolling */
2231 empathy_chat_view_scroll (chat->view, TRUE);
2235 chat_add_logs (EmpathyChat *chat)
2237 EmpathyChatPriv *priv = GET_PRIV (chat);
2244 /* Turn off scrolling temporarily */
2245 empathy_chat_view_scroll (chat->view, FALSE);
2247 /* Add messages from last conversation */
2248 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2249 target = tpl_entity_new_from_room_id (priv->id);
2251 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2253 priv->retrieving_backlogs = TRUE;
2254 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2257 TPL_EVENT_MASK_TEXT,
2261 got_filtered_messages_cb,
2264 g_object_unref (target);
2268 chat_contacts_completion_func (const gchar *s1,
2272 gchar *tmp, *nick1, *nick2;
2279 return s1 ? -1 : +1;
2282 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2283 nick1 = g_utf8_casefold (tmp, -1);
2286 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2287 nick2 = g_utf8_casefold (tmp, -1);
2290 ret = strncmp (nick1, nick2, n);
2299 build_part_message (guint reason,
2301 EmpathyContact *actor,
2302 const gchar *message)
2304 GString *s = g_string_new ("");
2305 const gchar *actor_name = NULL;
2307 if (actor != NULL) {
2308 actor_name = empathy_contact_get_alias (actor);
2311 /* Having an actor only really makes sense for a few actions... */
2313 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2314 g_string_append_printf (s, _("%s has disconnected"), name);
2316 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2317 if (actor_name != NULL) {
2318 /* translators: reverse the order of these arguments
2319 * if the kicked should come before the kicker in your locale.
2321 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2324 g_string_append_printf (s, _("%s was kicked"), name);
2327 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2328 if (actor_name != NULL) {
2329 /* translators: reverse the order of these arguments
2330 * if the banned should come before the banner in your locale.
2332 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2335 g_string_append_printf (s, _("%s was banned"), name);
2339 g_string_append_printf (s, _("%s has left the room"), name);
2342 if (!EMP_STR_EMPTY (message)) {
2343 /* Note to translators: this string is appended to
2344 * notifications like "foo has left the room", with the message
2345 * given by the user living the room. If this poses a problem,
2346 * please let us know. :-)
2348 g_string_append_printf (s, _(" (%s)"), message);
2351 return g_string_free (s, FALSE);
2355 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2356 EmpathyContact *contact,
2357 EmpathyContact *actor,
2363 EmpathyChatPriv *priv = GET_PRIV (chat);
2364 const gchar *name = empathy_contact_get_alias (contact);
2367 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2369 if (priv->block_events_timeout_id != 0)
2373 str = g_strdup_printf (_("%s has joined the room"),
2376 str = build_part_message (reason, name, actor, message);
2379 empathy_chat_view_append_event (chat->view, str);
2384 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2385 EmpathyContact *old_contact,
2386 EmpathyContact *new_contact,
2391 EmpathyChatPriv *priv = GET_PRIV (chat);
2393 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2395 if (priv->block_events_timeout_id == 0) {
2398 str = g_strdup_printf (_("%s is now known as %s"),
2399 empathy_contact_get_alias (old_contact),
2400 empathy_contact_get_alias (new_contact));
2401 empathy_chat_view_append_event (chat->view, str);
2408 chat_reset_size_request (gpointer widget)
2410 gtk_widget_set_size_request (widget, -1, -1);
2416 chat_update_contacts_visibility (EmpathyChat *chat,
2419 EmpathyChatPriv *priv = GET_PRIV (chat);
2420 GtkAllocation allocation;
2422 if (!priv->scrolled_window_contacts) {
2426 if (priv->remote_contact != NULL) {
2430 if (show && priv->contact_list_view == NULL) {
2431 EmpathyContactListStore *store;
2434 /* We are adding the contact list to the chat, we don't want the
2435 * chat view to become too small. If the chat view is already
2436 * smaller than 250 make sure that size won't change. If the
2437 * chat view is bigger the contact list will take some space on
2438 * it but we make sure the chat view don't become smaller than
2439 * 250. Relax the size request once the resize is done */
2440 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2441 min_width = MIN (allocation.width, 250);
2442 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2443 g_idle_add (chat_reset_size_request, priv->vbox_left);
2445 if (priv->contacts_width > 0) {
2446 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2447 priv->contacts_width);
2450 store = empathy_contact_list_store_new (
2451 EMPATHY_CONTACT_LIST (priv->tp_chat));
2452 empathy_contact_list_store_set_show_groups (
2453 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2455 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2456 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2457 EMPATHY_CONTACT_FEATURE_CHAT |
2458 EMPATHY_CONTACT_FEATURE_CALL |
2459 EMPATHY_CONTACT_FEATURE_LOG |
2460 EMPATHY_CONTACT_FEATURE_INFO));
2461 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2462 priv->contact_list_view);
2463 gtk_widget_show (priv->contact_list_view);
2464 gtk_widget_show (priv->scrolled_window_contacts);
2465 g_object_unref (store);
2467 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2468 gtk_widget_hide (priv->scrolled_window_contacts);
2469 if (priv->contact_list_view != NULL) {
2470 gtk_widget_destroy (priv->contact_list_view);
2471 priv->contact_list_view = NULL;
2477 empathy_chat_set_show_contacts (EmpathyChat *chat,
2480 EmpathyChatPriv *priv = GET_PRIV (chat);
2482 priv->show_contacts = show;
2484 chat_update_contacts_visibility (chat, show);
2486 g_object_notify (G_OBJECT (chat), "show-contacts");
2490 chat_remote_contact_changed_cb (EmpathyChat *chat)
2492 EmpathyChatPriv *priv = GET_PRIV (chat);
2494 if (priv->remote_contact != NULL) {
2495 g_object_unref (priv->remote_contact);
2496 priv->remote_contact = NULL;
2501 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2502 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2503 if (priv->remote_contact != NULL) {
2504 g_object_ref (priv->remote_contact);
2505 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2507 else if (priv->tp_chat != NULL) {
2510 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2511 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2514 chat_update_contacts_visibility (chat, priv->show_contacts);
2516 g_object_notify (G_OBJECT (chat), "remote-contact");
2517 g_object_notify (G_OBJECT (chat), "id");
2521 chat_destroy_cb (EmpathyTpChat *tp_chat,
2524 EmpathyChatPriv *priv;
2526 priv = GET_PRIV (chat);
2528 if (!priv->tp_chat) {
2532 chat_composing_remove_timeout (chat);
2533 g_object_unref (priv->tp_chat);
2534 priv->tp_chat = NULL;
2535 g_object_notify (G_OBJECT (chat), "tp-chat");
2537 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2538 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2540 chat_update_contacts_visibility (chat, FALSE);
2544 update_misspelled_words (gpointer data)
2546 EmpathyChat *chat = EMPATHY_CHAT (data);
2547 EmpathyChatPriv *priv = GET_PRIV (chat);
2548 GtkTextBuffer *buffer;
2552 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2554 gtk_text_buffer_get_end_iter (buffer, &iter);
2555 length = gtk_text_iter_get_offset (&iter);
2556 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2557 NULL, length, chat);
2559 priv->update_misspelled_words_id = 0;
2565 conf_spell_checking_cb (GSettings *gsettings_chat,
2569 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2570 EmpathyChatPriv *priv = GET_PRIV (chat);
2571 gboolean spell_checker;
2572 GtkTextBuffer *buffer;
2574 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2577 spell_checker = g_settings_get_boolean (gsettings_chat,
2578 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2580 if (!empathy_spell_supported ()) {
2581 spell_checker = FALSE;
2584 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2586 if (spell_checker == priv->spell_checking_enabled) {
2587 if (spell_checker) {
2588 /* Possibly changed dictionaries,
2589 * update misspelled words. Need to do so in idle
2590 * so the spell checker is updated. */
2591 priv->update_misspelled_words_id =
2592 g_idle_add (update_misspelled_words, chat);
2598 if (spell_checker) {
2601 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2602 buffer, "notify::cursor-position",
2603 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2605 priv->insert_text_id = tp_g_signal_connect_object (
2606 buffer, "insert-text",
2607 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2608 chat, G_CONNECT_AFTER);
2609 priv->delete_range_id = tp_g_signal_connect_object (
2610 buffer, "delete-range",
2611 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2612 chat, G_CONNECT_AFTER);
2614 gtk_text_buffer_create_tag (buffer, "misspelled",
2615 "underline", PANGO_UNDERLINE_ERROR,
2618 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2619 gtk_text_buffer_get_insert (buffer));
2620 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2623 /* Mark misspelled words in the existing buffer.
2624 * Need to do so in idle so the spell checker is updated. */
2625 priv->update_misspelled_words_id =
2626 g_idle_add (update_misspelled_words, chat);
2628 GtkTextTagTable *table;
2631 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2632 priv->notify_cursor_position_id = 0;
2633 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2634 priv->insert_text_id = 0;
2635 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2636 priv->delete_range_id = 0;
2638 table = gtk_text_buffer_get_tag_table (buffer);
2639 tag = gtk_text_tag_table_lookup (table, "misspelled");
2640 gtk_text_tag_table_remove (table, tag);
2642 gtk_text_buffer_delete_mark_by_name (buffer,
2643 "previous-cursor-position");
2646 priv->spell_checking_enabled = spell_checker;
2650 save_paned_pos_timeout (gpointer data)
2652 EmpathyChat *self = data;
2655 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2657 g_settings_set_int (self->priv->gsettings_ui,
2658 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2665 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2669 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2671 if (chat->priv->save_paned_pos_id != 0)
2672 g_source_remove (chat->priv->save_paned_pos_id);
2674 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2675 save_paned_pos_timeout, chat);
2681 chat_create_ui (EmpathyChat *chat)
2683 EmpathyChatPriv *priv = GET_PRIV (chat);
2687 GtkTextBuffer *buffer;
2689 EmpathyThemeManager *theme_mgr;
2691 filename = empathy_file_lookup ("empathy-chat.ui",
2693 gui = empathy_builder_get_file (filename,
2694 "chat_widget", &priv->widget,
2695 "hpaned", &priv->hpaned,
2696 "vbox_left", &priv->vbox_left,
2697 "scrolled_window_chat", &priv->scrolled_window_chat,
2698 "scrolled_window_input", &priv->scrolled_window_input,
2699 "hbox_topic", &priv->hbox_topic,
2700 "expander_topic", &priv->expander_topic,
2701 "label_topic", &priv->label_topic,
2702 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2703 "info_bar_vbox", &priv->info_bar_vbox,
2706 empathy_builder_connect (gui, chat,
2707 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2708 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2713 /* Add message view. */
2714 theme_mgr = empathy_theme_manager_dup_singleton ();
2715 chat->view = empathy_theme_manager_create_view (theme_mgr);
2716 g_object_unref (theme_mgr);
2717 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2718 and other types, even though it's non-editable and doesn't accept any
2719 drags. This steals drag motion for anything inside the scrollbars,
2720 making drag destinations on chat windows far less useful.
2722 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2723 g_signal_connect (chat->view, "focus_in_event",
2724 G_CALLBACK (chat_text_view_focus_in_event_cb),
2726 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2727 GTK_WIDGET (chat->view));
2728 gtk_widget_show (GTK_WIDGET (chat->view));
2730 /* Add input GtkTextView */
2731 chat->input_text_view = empathy_input_text_view_new ();
2732 g_signal_connect (chat->input_text_view, "notify::has-focus",
2733 G_CALLBACK (chat_input_has_focus_notify_cb),
2735 g_signal_connect (chat->input_text_view, "key-press-event",
2736 G_CALLBACK (chat_input_key_press_event_cb),
2738 g_signal_connect (chat->input_text_view, "realize",
2739 G_CALLBACK (chat_input_realize_cb),
2741 g_signal_connect (chat->input_text_view, "populate-popup",
2742 G_CALLBACK (chat_input_populate_popup_cb),
2744 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2745 tp_g_signal_connect_object (buffer, "changed",
2746 G_CALLBACK (chat_input_text_buffer_changed_cb),
2748 tp_g_signal_connect_object (priv->gsettings_chat,
2749 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2750 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2751 conf_spell_checking_cb (priv->gsettings_chat,
2752 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2753 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2754 chat->input_text_view);
2755 gtk_widget_show (chat->input_text_view);
2757 /* Add the (invisible) search bar */
2758 priv->search_bar = empathy_search_bar_new (chat->view);
2759 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2762 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2764 /* Initialy hide the topic, will be shown if not empty */
2765 gtk_widget_hide (priv->hbox_topic);
2767 g_signal_connect (priv->hpaned, "notify::position",
2768 G_CALLBACK (chat_hpaned_pos_changed_cb),
2771 /* Load the paned position */
2772 paned_pos = g_settings_get_int (priv->gsettings_ui,
2773 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2775 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2777 /* Set widget focus order */
2778 list = g_list_append (NULL, priv->search_bar);
2779 list = g_list_append (list, priv->scrolled_window_input);
2780 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2783 list = g_list_append (NULL, priv->vbox_left);
2784 list = g_list_append (list, priv->scrolled_window_contacts);
2785 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2788 list = g_list_append (NULL, priv->hpaned);
2789 list = g_list_append (list, priv->hbox_topic);
2790 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2793 /* Add the main widget in the chat widget */
2794 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2795 g_object_unref (gui);
2799 chat_size_allocate (GtkWidget *widget,
2800 GtkAllocation *allocation)
2802 GtkBin *bin = GTK_BIN (widget);
2803 GtkAllocation child_allocation;
2806 gtk_widget_set_allocation (widget, allocation);
2808 child = gtk_bin_get_child (bin);
2810 if (child && gtk_widget_get_visible (child))
2812 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2813 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2814 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2815 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2817 gtk_widget_size_allocate (child, &child_allocation);
2822 chat_finalize (GObject *object)
2825 EmpathyChatPriv *priv;
2827 chat = EMPATHY_CHAT (object);
2828 priv = GET_PRIV (chat);
2830 DEBUG ("Finalized: %p", object);
2832 if (priv->update_misspelled_words_id != 0)
2833 g_source_remove (priv->update_misspelled_words_id);
2835 if (priv->save_paned_pos_id != 0)
2836 g_source_remove (priv->save_paned_pos_id);
2838 g_object_unref (priv->gsettings_chat);
2839 g_object_unref (priv->gsettings_ui);
2841 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2842 g_list_free (priv->input_history);
2844 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2845 g_list_free (priv->compositors);
2847 chat_composing_remove_timeout (chat);
2849 g_object_unref (priv->account_manager);
2850 g_object_unref (priv->log_manager);
2852 if (priv->tp_chat) {
2853 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2854 chat_destroy_cb, chat);
2855 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2856 chat_message_received_cb, chat);
2857 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2858 chat_send_error_cb, chat);
2859 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2860 chat_state_changed_cb, chat);
2861 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2862 chat_property_changed_cb, chat);
2863 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2864 chat_members_changed_cb, chat);
2865 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2866 chat_remote_contact_changed_cb, chat);
2867 empathy_tp_chat_leave (priv->tp_chat, "");
2868 g_object_unref (priv->tp_chat);
2870 if (priv->account) {
2871 g_object_unref (priv->account);
2873 if (priv->remote_contact) {
2874 g_object_unref (priv->remote_contact);
2877 if (priv->block_events_timeout_id) {
2878 g_source_remove (priv->block_events_timeout_id);
2882 g_free (priv->name);
2883 g_free (priv->subject);
2884 g_completion_free (priv->completion);
2886 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2890 chat_constructed (GObject *object)
2892 EmpathyChat *chat = EMPATHY_CHAT (object);
2893 EmpathyChatPriv *priv = GET_PRIV (chat);
2895 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2896 /* First display logs from the logger and then display pending messages */
2897 chat_add_logs (chat);
2900 /* Just display pending messages for rooms */
2901 priv->can_show_pending = TRUE;
2902 show_pending_messages (chat);
2907 empathy_chat_class_init (EmpathyChatClass *klass)
2909 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2910 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2912 object_class->finalize = chat_finalize;
2913 object_class->get_property = chat_get_property;
2914 object_class->set_property = chat_set_property;
2915 object_class->constructed = chat_constructed;
2917 widget_class->size_allocate = chat_size_allocate;
2919 g_object_class_install_property (object_class,
2921 g_param_spec_object ("tp-chat",
2923 "The tp chat object",
2924 EMPATHY_TYPE_TP_CHAT,
2927 G_PARAM_STATIC_STRINGS));
2928 g_object_class_install_property (object_class,
2930 g_param_spec_object ("account",
2931 "Account of the chat",
2932 "The account of the chat",
2935 G_PARAM_STATIC_STRINGS));
2936 g_object_class_install_property (object_class,
2938 g_param_spec_string ("id",
2940 "The id of the chat",
2943 G_PARAM_STATIC_STRINGS));
2944 g_object_class_install_property (object_class,
2946 g_param_spec_string ("name",
2948 "The name of the chat",
2951 G_PARAM_STATIC_STRINGS));
2952 g_object_class_install_property (object_class,
2954 g_param_spec_string ("subject",
2956 "The subject or topic of the chat",
2959 G_PARAM_STATIC_STRINGS));
2960 g_object_class_install_property (object_class,
2961 PROP_REMOTE_CONTACT,
2962 g_param_spec_object ("remote-contact",
2963 "The remote contact",
2964 "The remote contact is any",
2965 EMPATHY_TYPE_CONTACT,
2967 G_PARAM_STATIC_STRINGS));
2968 g_object_class_install_property (object_class,
2970 g_param_spec_boolean ("show-contacts",
2971 "Contacts' visibility",
2972 "The visibility of the contacts' list",
2975 G_PARAM_STATIC_STRINGS));
2977 g_object_class_install_property (object_class,
2979 g_param_spec_boolean ("sms-channel",
2981 "TRUE if this channel is for sending SMSes",
2983 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
2985 signals[COMPOSING] =
2986 g_signal_new ("composing",
2987 G_OBJECT_CLASS_TYPE (object_class),
2991 g_cclosure_marshal_VOID__BOOLEAN,
2995 signals[NEW_MESSAGE] =
2996 g_signal_new ("new-message",
2997 G_OBJECT_CLASS_TYPE (object_class),
3001 _empathy_gtk_marshal_VOID__OBJECT_BOOLEAN,
3003 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
3005 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3009 chat_block_events_timeout_cb (gpointer data)
3011 EmpathyChatPriv *priv = GET_PRIV (data);
3013 priv->block_events_timeout_id = 0;
3019 account_manager_prepared_cb (GObject *source_object,
3020 GAsyncResult *result,
3023 GList *accounts, *l;
3024 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3025 EmpathyChat *chat = user_data;
3026 GError *error = NULL;
3028 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
3029 DEBUG ("Failed to prepare the account manager: %s", error->message);
3030 g_error_free (error);
3034 accounts = tp_account_manager_get_valid_accounts (account_manager);
3036 for (l = accounts; l != NULL; l = l->next) {
3037 TpAccount *account = l->data;
3038 tp_g_signal_connect_object (account, "status-changed",
3039 G_CALLBACK (chat_new_connection_cb),
3043 g_list_free (accounts);
3047 empathy_chat_init (EmpathyChat *chat)
3049 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3050 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3053 priv->log_manager = tpl_log_manager_dup_singleton ();
3054 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3055 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3057 priv->contacts_width = -1;
3058 priv->input_history = NULL;
3059 priv->input_history_current = NULL;
3060 priv->account_manager = tp_account_manager_dup ();
3062 tp_account_manager_prepare_async (priv->account_manager, NULL,
3063 account_manager_prepared_cb, chat);
3065 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3066 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3068 /* Block events for some time to avoid having "has come online" or
3069 * "joined" messages. */
3070 priv->block_events_timeout_id =
3071 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3073 /* Add nick name completion */
3074 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3075 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3077 chat_create_ui (chat);
3081 empathy_chat_new (EmpathyTpChat *tp_chat)
3083 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3087 empathy_chat_get_tp_chat (EmpathyChat *chat)
3089 EmpathyChatPriv *priv = GET_PRIV (chat);
3091 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3093 return priv->tp_chat;
3099 GtkWidget *info_bar;
3109 passwd_remember_button_cb (GtkButton *button,
3112 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3116 passwd_not_now_button_cb (GtkButton *button,
3119 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3123 remember_password_infobar_response_cb (GtkWidget *info_bar,
3127 EmpathyChatPriv *priv = GET_PRIV (data->self);
3129 if (response_id == GTK_RESPONSE_OK) {
3130 DEBUG ("Saving room password");
3131 empathy_keyring_set_room_password_async (priv->account,
3132 empathy_tp_chat_get_id (priv->tp_chat),
3137 gtk_widget_destroy (info_bar);
3138 g_free (data->password);
3139 g_slice_free (PasswordData, data);
3143 chat_prompt_to_save_password (EmpathyChat *self,
3146 GtkWidget *content_area;
3153 /* save the password in case it needs to be saved */
3154 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3156 /* Remove all previous widgets */
3157 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3158 gtk_container_forall (GTK_CONTAINER (content_area),
3159 (GtkCallback) gtk_widget_destroy, NULL);
3160 data->button = NULL;
3163 data->spinner = NULL;
3165 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3166 GTK_MESSAGE_QUESTION);
3168 hbox = gtk_hbox_new (FALSE, 5);
3169 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3172 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3173 GTK_ICON_SIZE_DIALOG);
3174 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3177 label = gtk_label_new (_("Would you like to store this password?"));
3178 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3180 /* Add 'Remember' button */
3181 alig = gtk_alignment_new (0, 0.5, 1, 0);
3183 button = gtk_button_new_with_label (_("Remember"));
3184 gtk_container_add (GTK_CONTAINER (alig), button);
3185 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3187 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3190 /* Add 'Not now' button */
3191 alig = gtk_alignment_new (0, 0.5, 1, 0);
3193 button = gtk_button_new_with_label (_("Not now"));
3194 gtk_container_add (GTK_CONTAINER (alig), button);
3195 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3197 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3201 g_signal_handler_disconnect (data->info_bar, data->response_id);
3202 g_signal_connect (data->info_bar, "response",
3203 G_CALLBACK (remember_password_infobar_response_cb), data);
3205 gtk_widget_show_all (data->info_bar);
3209 provide_password_cb (GObject *tp_chat,
3213 PasswordData *data = user_data;
3214 EmpathyChat *self = data->self;
3215 EmpathyChatPriv *priv = GET_PRIV (self);
3216 GError *error = NULL;
3218 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3220 DEBUG ("error: %s", error->message);
3221 /* FIXME: what should we do if that's another error? Close the channel?
3222 * Display the raw D-Bus error to the user isn't very useful */
3223 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3225 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3226 gtk_widget_set_sensitive (data->entry, TRUE);
3227 gtk_widget_grab_focus (data->entry);
3230 gtk_info_bar_set_message_type (
3231 GTK_INFO_BAR (data->info_bar),
3235 gtk_widget_set_sensitive (data->button, TRUE);
3236 gtk_button_set_label (GTK_BUTTON (data->button),
3240 gtk_label_set_text (GTK_LABEL (data->label),
3241 _("Wrong password; please try again:"));
3244 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3245 gtk_widget_hide (data->spinner);
3247 g_error_free (error);
3251 if (empathy_keyring_is_available ()) {
3252 /* ask whether they want to save the password */
3253 chat_prompt_to_save_password (self, data);
3255 /* Get rid of the password info bar finally */
3256 gtk_widget_destroy (data->info_bar);
3257 g_slice_free (PasswordData, data);
3261 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3262 gtk_widget_grab_focus (self->input_text_view);
3266 password_infobar_response_cb (GtkWidget *info_bar,
3270 EmpathyChatPriv *priv = GET_PRIV (data->self);
3271 const gchar *password;
3273 if (response_id != GTK_RESPONSE_OK) {
3274 gtk_widget_destroy (info_bar);
3275 g_slice_free (PasswordData, data);
3279 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3281 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3282 provide_password_cb, data);
3284 gtk_widget_set_sensitive (data->button, FALSE);
3285 gtk_widget_set_sensitive (data->entry, FALSE);
3287 gtk_spinner_start (GTK_SPINNER (data->spinner));
3288 gtk_widget_show (data->spinner);
3292 password_entry_activate_cb (GtkWidget *entry,
3295 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3299 passwd_join_button_cb (GtkButton *button,
3302 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3306 clear_icon_released_cb (GtkEntry *entry,
3307 GtkEntryIconPosition icon_pos,
3311 gtk_entry_set_text (entry, "");
3315 password_entry_changed_cb (GtkEditable *entry,
3320 str = gtk_entry_get_text (GTK_ENTRY (entry));
3322 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3323 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3327 display_password_info_bar (EmpathyChat *self)
3329 EmpathyChatPriv *priv = GET_PRIV (self);
3330 GtkWidget *info_bar;
3331 GtkWidget *content_area;
3341 data = g_slice_new0 (PasswordData);
3343 info_bar = gtk_info_bar_new ();
3344 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3345 GTK_MESSAGE_QUESTION);
3347 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3349 hbox = gtk_hbox_new (FALSE, 5);
3350 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3353 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3354 GTK_ICON_SIZE_DIALOG);
3355 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3358 label = gtk_label_new (_("This room is protected by a password:"));
3359 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3361 /* Add password entry */
3362 entry = gtk_entry_new ();
3363 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3364 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3366 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3367 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3368 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3369 GTK_ENTRY_ICON_SECONDARY, FALSE);
3371 g_signal_connect (entry, "icon-release",
3372 G_CALLBACK (clear_icon_released_cb), data);
3373 g_signal_connect (entry, "changed",
3374 G_CALLBACK (password_entry_changed_cb), data);
3376 g_signal_connect (entry, "activate",
3377 G_CALLBACK (password_entry_activate_cb), data);
3379 /* Focus the password entry once it's realized */
3380 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3382 /* Add 'Join' button */
3383 alig = gtk_alignment_new (0, 0.5, 1, 0);
3385 button = gtk_button_new_with_label (_("Join"));
3386 gtk_container_add (GTK_CONTAINER (alig), button);
3387 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3389 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3393 spinner = gtk_spinner_new ();
3394 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3396 /* Save some data for messing around with later */
3398 data->info_bar = info_bar;
3399 data->button = button;
3400 data->label = label;
3401 data->entry = entry;
3402 data->spinner = spinner;
3404 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3406 gtk_widget_show_all (hbox);
3408 data->response_id = g_signal_connect (info_bar, "response",
3409 G_CALLBACK (password_infobar_response_cb), data);
3411 gtk_widget_show_all (info_bar);
3412 /* ... but hide the spinner */
3413 gtk_widget_hide (spinner);
3417 provide_saved_password_cb (GObject *tp_chat,
3421 EmpathyChat *self = user_data;
3422 EmpathyChatPriv *priv = GET_PRIV (self);
3423 GError *error = NULL;
3425 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3427 DEBUG ("error: %s", error->message);
3428 /* FIXME: what should we do if that's another error? Close the channel?
3429 * Display the raw D-Bus error to the user isn't very useful */
3430 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3431 display_password_info_bar (self);
3432 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3434 g_error_free (error);
3439 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3440 gtk_widget_grab_focus (self->input_text_view);
3444 chat_room_got_password_cb (GObject *source,
3445 GAsyncResult *result,
3448 EmpathyChat *self = user_data;
3449 EmpathyChatPriv *priv = GET_PRIV (self);
3450 const gchar *password;
3451 GError *error = NULL;
3453 password = empathy_keyring_get_room_password_finish (priv->account,
3456 if (error != NULL) {
3457 DEBUG ("Couldn't get room password: %s\n", error->message);
3458 g_clear_error (&error);
3460 display_password_info_bar (self);
3461 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3465 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3466 provide_saved_password_cb, self);
3470 chat_password_needed_changed_cb (EmpathyChat *self)
3472 EmpathyChatPriv *priv = GET_PRIV (self);
3474 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3475 empathy_keyring_get_room_password_async (priv->account,
3476 empathy_tp_chat_get_id (priv->tp_chat),
3477 chat_room_got_password_cb, self);
3482 chat_sms_channel_changed_cb (EmpathyChat *self)
3484 EmpathyChatPriv *priv = GET_PRIV (self);
3486 priv->sms_channel = empathy_tp_chat_is_sms_channel (priv->tp_chat);
3487 g_object_notify (G_OBJECT (self), "sms-channel");
3491 empathy_chat_set_tp_chat (EmpathyChat *chat,
3492 EmpathyTpChat *tp_chat)
3494 EmpathyChatPriv *priv = GET_PRIV (chat);
3495 GPtrArray *properties;
3497 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3498 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3499 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3501 if (priv->tp_chat) {
3505 if (priv->account) {
3506 g_object_unref (priv->account);
3509 priv->tp_chat = g_object_ref (tp_chat);
3510 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3512 g_signal_connect (tp_chat, "destroy",
3513 G_CALLBACK (chat_destroy_cb),
3515 g_signal_connect (tp_chat, "message-received",
3516 G_CALLBACK (chat_message_received_cb),
3518 g_signal_connect (tp_chat, "send-error",
3519 G_CALLBACK (chat_send_error_cb),
3521 g_signal_connect (tp_chat, "chat-state-changed",
3522 G_CALLBACK (chat_state_changed_cb),
3524 g_signal_connect (tp_chat, "property-changed",
3525 G_CALLBACK (chat_property_changed_cb),
3527 g_signal_connect (tp_chat, "members-changed",
3528 G_CALLBACK (chat_members_changed_cb),
3530 g_signal_connect (tp_chat, "member-renamed",
3531 G_CALLBACK (chat_member_renamed_cb),
3533 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3534 G_CALLBACK (chat_remote_contact_changed_cb),
3536 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3537 G_CALLBACK (chat_password_needed_changed_cb),
3539 g_signal_connect_swapped (tp_chat, "notify::sms-channel",
3540 G_CALLBACK (chat_sms_channel_changed_cb),
3543 /* Get initial value of properties */
3544 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3545 if (properties != NULL) {
3548 for (i = 0; i < properties->len; i++) {
3549 EmpathyTpChatProperty *property;
3551 property = g_ptr_array_index (properties, i);
3552 if (property->value == NULL)
3555 chat_property_changed_cb (priv->tp_chat,
3562 chat_sms_channel_changed_cb (chat);
3563 chat_remote_contact_changed_cb (chat);
3565 if (chat->input_text_view) {
3566 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3567 if (priv->block_events_timeout_id == 0) {
3568 empathy_chat_view_append_event (chat->view, _("Connected"));
3572 g_object_notify (G_OBJECT (chat), "tp-chat");
3573 g_object_notify (G_OBJECT (chat), "id");
3574 g_object_notify (G_OBJECT (chat), "account");
3576 /* This is a noop when tp-chat is set at object construction time and causes
3577 * the pending messages to be show when it's set on the object after it has
3579 show_pending_messages (chat);
3581 /* check if a password is needed */
3582 chat_password_needed_changed_cb (chat);
3586 empathy_chat_get_account (EmpathyChat *chat)
3588 EmpathyChatPriv *priv = GET_PRIV (chat);
3590 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3592 return priv->account;
3596 empathy_chat_get_id (EmpathyChat *chat)
3598 EmpathyChatPriv *priv = GET_PRIV (chat);
3600 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3606 empathy_chat_dup_name (EmpathyChat *chat)
3608 EmpathyChatPriv *priv = GET_PRIV (chat);
3611 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3614 if (!ret && priv->remote_contact) {
3615 ret = empathy_contact_get_alias (priv->remote_contact);
3621 return g_strdup (ret ? ret : _("Conversation"));
3625 empathy_chat_get_subject (EmpathyChat *chat)
3627 EmpathyChatPriv *priv = GET_PRIV (chat);
3629 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3631 return priv->subject;
3635 empathy_chat_get_remote_contact (EmpathyChat *chat)
3637 EmpathyChatPriv *priv = GET_PRIV (chat);
3639 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3641 return priv->remote_contact;
3645 empathy_chat_get_contact_menu (EmpathyChat *chat)
3647 EmpathyChatPriv *priv = GET_PRIV (chat);
3648 GtkWidget *menu = NULL;
3650 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3652 if (priv->remote_contact) {
3653 menu = empathy_contact_menu_new (priv->remote_contact,
3654 EMPATHY_CONTACT_FEATURE_CALL |
3655 EMPATHY_CONTACT_FEATURE_LOG |
3656 EMPATHY_CONTACT_FEATURE_INFO |
3657 EMPATHY_CONTACT_FEATURE_BLOCK);
3664 empathy_chat_clear (EmpathyChat *chat)
3666 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3668 empathy_chat_view_clear (chat->view);
3672 empathy_chat_scroll_down (EmpathyChat *chat)
3674 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3676 empathy_chat_view_scroll_down (chat->view);
3680 empathy_chat_cut (EmpathyChat *chat)
3682 GtkTextBuffer *buffer;
3684 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3686 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3687 if (gtk_text_buffer_get_has_selection (buffer)) {
3688 GtkClipboard *clipboard;
3690 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3692 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3697 empathy_chat_copy (EmpathyChat *chat)
3699 GtkTextBuffer *buffer;
3701 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3703 if (empathy_chat_view_get_has_selection (chat->view)) {
3704 empathy_chat_view_copy_clipboard (chat->view);
3708 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3709 if (gtk_text_buffer_get_has_selection (buffer)) {
3710 GtkClipboard *clipboard;
3712 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3714 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3719 EmpathyChatPriv *priv = GET_PRIV (chat);
3721 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3728 GtkClipboard *clipboard;
3730 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3731 start = g_utf8_offset_to_pointer (topic, start_offset);
3732 end = g_utf8_offset_to_pointer (topic, end_offset);
3733 selection = g_strndup (start, end - start);
3735 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3736 gtk_clipboard_set_text (clipboard, selection, -1);
3744 empathy_chat_paste (EmpathyChat *chat)
3746 GtkTextBuffer *buffer;
3747 GtkClipboard *clipboard;
3748 EmpathyChatPriv *priv;
3750 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3752 priv = GET_PRIV (chat);
3754 if (gtk_widget_get_visible (priv->search_bar)) {
3755 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3759 if (priv->tp_chat == NULL ||
3760 !gtk_widget_is_sensitive (chat->input_text_view))
3763 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3764 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3766 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3770 empathy_chat_find (EmpathyChat *chat)
3772 EmpathyChatPriv *priv;
3774 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3776 priv = GET_PRIV (chat);
3778 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3782 empathy_chat_correct_word (EmpathyChat *chat,
3785 const gchar *new_word)
3787 GtkTextBuffer *buffer;
3789 g_return_if_fail (chat != NULL);
3790 g_return_if_fail (new_word != NULL);
3792 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3794 gtk_text_buffer_delete (buffer, start, end);
3795 gtk_text_buffer_insert (buffer, start,
3801 empathy_chat_is_room (EmpathyChat *chat)
3803 EmpathyChatPriv *priv = GET_PRIV (chat);
3805 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3807 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3811 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3813 EmpathyChatPriv *priv = GET_PRIV (self);
3815 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3817 return priv->unread_messages;
3820 /* called when the messages have been read by user */
3822 empathy_chat_messages_read (EmpathyChat *self)
3824 EmpathyChatPriv *priv = GET_PRIV (self);
3826 g_return_if_fail (EMPATHY_IS_CHAT (self));
3828 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3829 * definition. If we are still retrieving the backlogs, do not ACK */
3830 if (priv->retrieving_backlogs)
3833 if (priv->tp_chat != NULL ) {
3834 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3836 priv->unread_messages = 0;
3838 empathy_chat_view_focus_toggled (self->view, TRUE);
3841 /* Return TRUE if on of the contacts in this chat is composing */
3843 empathy_chat_is_composing (EmpathyChat *chat)
3845 return chat->priv->compositors != NULL;
3849 empathy_chat_is_sms_channel (EmpathyChat *self)
3851 EmpathyChatPriv *priv = GET_PRIV (self);
3853 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3855 return priv->sms_channel;