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-dispatcher.h>
46 #include <libempathy/empathy-marshal.h>
48 #include "empathy-chat.h"
49 #include "empathy-spell.h"
50 #include "empathy-contact-list-store.h"
51 #include "empathy-contact-list-view.h"
52 #include "empathy-contact-menu.h"
53 #include "empathy-input-text-view.h"
54 #include "empathy-search-bar.h"
55 #include "empathy-theme-manager.h"
56 #include "empathy-smiley-manager.h"
57 #include "empathy-ui-utils.h"
58 #include "empathy-string-parser.h"
59 #include "extensions/extensions.h"
61 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
62 #include <libempathy/empathy-debug.h>
64 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
65 #define COMPOSING_STOP_TIMEOUT 5
67 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
68 struct _EmpathyChatPriv {
69 EmpathyTpChat *tp_chat;
74 EmpathyContact *remote_contact;
75 gboolean show_contacts;
77 GSettings *gsettings_chat;
78 GSettings *gsettings_ui;
80 TplLogManager *log_manager;
81 TpAccountManager *account_manager;
83 GList *input_history_current;
85 GCompletion *completion;
86 guint composing_stop_timeout_id;
87 guint block_events_timeout_id;
88 TpHandleType handle_type;
90 gboolean has_input_vscroll;
92 /* TRUE if spell checking is enabled, FALSE otherwise.
93 * This is to keep track of the last state of spell checking
95 gboolean spell_checking_enabled;
97 /* These store the signal handler ids for the enclosed text entry. */
98 gulong insert_text_id;
99 gulong delete_range_id;
100 gulong notify_cursor_position_id;
102 /* Source func ID for update_misspelled_words () */
103 guint update_misspelled_words_id;
104 /* Source func ID for save_paned_pos_timeout () */
105 guint save_paned_pos_id;
109 GtkWidget *vbox_left;
110 GtkWidget *scrolled_window_chat;
111 GtkWidget *scrolled_window_input;
112 GtkWidget *scrolled_window_contacts;
113 GtkWidget *hbox_topic;
114 GtkWidget *expander_topic;
115 GtkWidget *label_topic;
116 GtkWidget *contact_list_view;
117 GtkWidget *info_bar_vbox;
118 GtkWidget *search_bar;
120 guint unread_messages;
121 /* TRUE if the pending messages can be displayed. This is to avoid to show
122 * pending messages *before* messages from logs. (#603980) */
123 gboolean can_show_pending;
125 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
126 * be differently handled since it introduces another race condition, which
127 * is really hard to occur, but still possible.
129 * With the current workaround (which has the race above), we need to be
130 * sure to ACK any pending messages only when the retrieval of backlogs is
131 * finished, that's why using retrieving_backlogs flag.
132 * empathy_chat_messages_read () will check this variable and not ACK
133 * anything when TRUE. It will be set TRUE at chat_constructed () and set
134 * back to FALSE when the backlog has been retrieved and the pending
135 * messages actually showed to the user.
137 * Race condition introduced with this workaround:
138 * Scenario: a message is pending, the user is notified and selects the tab.
139 * the tab with a pending message is focused before the messages are properly
140 * shown (since the preparation of the window is slower AND async WRT the
141 * tab showing), which means the user won't see any new messages (rare but
142 * possible), if he/she will change tab focus before the messages are
143 * properly shown, the tab will be set as 'seen' and the user won't be
144 * notified again about the already notified pending messages when the
145 * messages in tab will be properly shown */
146 gboolean retrieving_backlogs;
150 gchar *text; /* Original message that was specified
151 * upon entry creation. */
152 gchar *modified_text; /* Message that was modified by user.
153 * When no modifications were made, it is NULL */
173 static guint signals[LAST_SIGNAL] = { 0 };
175 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
177 static gboolean update_misspelled_words (gpointer data);
180 chat_get_property (GObject *object,
185 EmpathyChat *chat = EMPATHY_CHAT (object);
186 EmpathyChatPriv *priv = GET_PRIV (object);
190 g_value_set_object (value, priv->tp_chat);
193 g_value_set_object (value, priv->account);
196 g_value_set_string (value, empathy_chat_get_name (chat));
199 g_value_set_string (value, priv->id);
202 g_value_set_string (value, priv->subject);
204 case PROP_REMOTE_CONTACT:
205 g_value_set_object (value, priv->remote_contact);
207 case PROP_SHOW_CONTACTS:
208 g_value_set_boolean (value, priv->show_contacts);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
217 chat_set_property (GObject *object,
222 EmpathyChat *chat = EMPATHY_CHAT (object);
226 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
228 case PROP_SHOW_CONTACTS:
229 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
238 account_reconnected (EmpathyChat *chat,
241 EmpathyChatPriv *priv = GET_PRIV (chat);
243 DEBUG ("Account reconnected, request a new Text channel");
245 /* FIXME: Ideally we should ask to handle ourself the channel so we can
246 * report the error if any but this is blocked by
247 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
248 switch (priv->handle_type) {
249 case TP_HANDLE_TYPE_CONTACT:
250 empathy_dispatcher_chat_with_contact_id (
251 account, priv->id, TP_USER_ACTION_TIME_NOT_USER_ACTION);
253 case TP_HANDLE_TYPE_ROOM:
254 empathy_dispatcher_join_muc (account, priv->id,
255 TP_USER_ACTION_TIME_NOT_USER_ACTION);
257 case TP_HANDLE_TYPE_NONE:
258 case TP_HANDLE_TYPE_LIST:
259 case TP_HANDLE_TYPE_GROUP:
261 g_assert_not_reached ();
265 g_object_unref (chat);
269 chat_new_connection_cb (TpAccount *account,
273 gchar *dbus_error_name,
277 EmpathyChatPriv *priv = GET_PRIV (chat);
278 TpConnection *connection;
280 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
283 connection = tp_account_get_connection (account);
285 if (priv->tp_chat != NULL || account != priv->account ||
286 priv->handle_type == TP_HANDLE_TYPE_NONE ||
287 EMP_STR_EMPTY (priv->id))
292 account_reconnected (chat, account);
296 chat_composing_remove_timeout (EmpathyChat *chat)
298 EmpathyChatPriv *priv;
300 priv = GET_PRIV (chat);
302 if (priv->composing_stop_timeout_id) {
303 g_source_remove (priv->composing_stop_timeout_id);
304 priv->composing_stop_timeout_id = 0;
309 chat_composing_stop_timeout_cb (EmpathyChat *chat)
311 EmpathyChatPriv *priv;
313 priv = GET_PRIV (chat);
315 priv->composing_stop_timeout_id = 0;
316 empathy_tp_chat_set_state (priv->tp_chat,
317 TP_CHANNEL_CHAT_STATE_PAUSED);
323 chat_composing_start (EmpathyChat *chat)
325 EmpathyChatPriv *priv;
327 priv = GET_PRIV (chat);
329 if (priv->composing_stop_timeout_id) {
330 /* Just restart the timeout */
331 chat_composing_remove_timeout (chat);
333 empathy_tp_chat_set_state (priv->tp_chat,
334 TP_CHANNEL_CHAT_STATE_COMPOSING);
337 priv->composing_stop_timeout_id = g_timeout_add_seconds (
338 COMPOSING_STOP_TIMEOUT,
339 (GSourceFunc) chat_composing_stop_timeout_cb,
344 chat_composing_stop (EmpathyChat *chat)
346 EmpathyChatPriv *priv;
348 priv = GET_PRIV (chat);
350 chat_composing_remove_timeout (chat);
351 empathy_tp_chat_set_state (priv->tp_chat,
352 TP_CHANNEL_CHAT_STATE_ACTIVE);
356 chat_input_history_entry_cmp (InputHistoryEntry *entry,
359 if (!tp_strdiff (entry->text, text)) {
360 if (entry->modified_text != NULL) {
361 /* Modified entry and single string cannot be equal. */
369 static InputHistoryEntry *
370 chat_input_history_entry_new_with_text (const gchar *text)
372 InputHistoryEntry *entry;
373 entry = g_slice_new0 (InputHistoryEntry);
374 entry->text = g_strdup (text);
380 chat_input_history_entry_free (InputHistoryEntry *entry)
382 g_free (entry->text);
383 g_free (entry->modified_text);
384 g_slice_free (InputHistoryEntry, entry);
388 chat_input_history_entry_revert (InputHistoryEntry *entry)
390 g_free (entry->modified_text);
391 entry->modified_text = NULL;
395 chat_input_history_entry_update_text (InputHistoryEntry *entry,
400 if (!tp_strdiff (text, entry->text)) {
401 g_free (entry->modified_text);
402 entry->modified_text = NULL;
406 old = entry->modified_text;
407 entry->modified_text = g_strdup (text);
412 chat_input_history_entry_get_text (InputHistoryEntry *entry)
418 if (entry->modified_text != NULL) {
419 return entry->modified_text;
425 chat_input_history_remove_item (GList *list,
428 list = g_list_remove_link (list, item);
429 chat_input_history_entry_free (item->data);
430 g_list_free_1 (item);
435 chat_input_history_revert (EmpathyChat *chat)
437 EmpathyChatPriv *priv;
441 InputHistoryEntry *entry;
443 priv = GET_PRIV (chat);
444 list = priv->input_history;
447 DEBUG ("No input history");
451 /* Delete temporary entry */
452 if (priv->input_history_current != NULL) {
454 list = chat_input_history_remove_item (list, item1);
455 if (priv->input_history_current == item1) {
456 /* Removed temporary entry was current entry */
457 priv->input_history = list;
458 priv->input_history_current = NULL;
463 /* There is no entry to revert */
467 /* Restore the current history entry to original value */
468 item1 = priv->input_history_current;
470 chat_input_history_entry_revert (entry);
472 /* Remove restored entry if there is other occurance before this entry */
473 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
474 (GCompareFunc) chat_input_history_entry_cmp);
475 if (item2 != item1) {
476 list = chat_input_history_remove_item (list, item1);
479 /* Remove other occurance of the restored entry */
480 item2 = g_list_find_custom (item1->next,
481 chat_input_history_entry_get_text (entry),
482 (GCompareFunc) chat_input_history_entry_cmp);
484 list = chat_input_history_remove_item (list, item2);
488 priv->input_history_current = NULL;
489 priv->input_history = list;
493 chat_input_history_add (EmpathyChat *chat,
497 EmpathyChatPriv *priv;
500 InputHistoryEntry *entry;
502 priv = GET_PRIV (chat);
504 list = priv->input_history;
506 /* Remove any other occurances of this entry, if not temporary */
508 while ((item = g_list_find_custom (list, str,
509 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
510 list = chat_input_history_remove_item (list, item);
513 /* Trim the list to the last 10 items */
514 while (g_list_length (list) > 10) {
515 item = g_list_last (list);
517 list = chat_input_history_remove_item (list, item);
525 entry = chat_input_history_entry_new_with_text (str);
526 list = g_list_prepend (list, entry);
528 /* Set the list and the current item pointer */
529 priv->input_history = list;
531 priv->input_history_current = list;
534 priv->input_history_current = NULL;
539 chat_input_history_get_next (EmpathyChat *chat)
541 EmpathyChatPriv *priv;
545 priv = GET_PRIV (chat);
547 if (priv->input_history == NULL) {
548 DEBUG ("No input history, next entry is NULL");
551 g_assert (priv->input_history_current != NULL);
553 if ((item = g_list_next (priv->input_history_current)) == NULL)
555 item = priv->input_history_current;
558 msg = chat_input_history_entry_get_text (item->data);
560 DEBUG ("Returning next entry: '%s'", msg);
562 priv->input_history_current = item;
568 chat_input_history_get_prev (EmpathyChat *chat)
570 EmpathyChatPriv *priv;
574 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
576 priv = GET_PRIV (chat);
578 if (priv->input_history == NULL) {
579 DEBUG ("No input history, previous entry is NULL");
583 if (priv->input_history_current == NULL)
587 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
589 item = priv->input_history_current;
592 msg = chat_input_history_entry_get_text (item->data);
594 DEBUG ("Returning previous entry: '%s'", msg);
596 priv->input_history_current = item;
602 chat_input_history_update (EmpathyChat *chat,
603 GtkTextBuffer *buffer)
605 EmpathyChatPriv *priv;
606 GtkTextIter start, end;
608 InputHistoryEntry *entry;
610 priv = GET_PRIV (chat);
612 gtk_text_buffer_get_bounds (buffer, &start, &end);
613 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
615 if (priv->input_history_current == NULL) {
616 /* Add the current text temporarily to the history */
617 chat_input_history_add (chat, text, TRUE);
622 /* Save the changes in the history */
623 entry = priv->input_history_current->data;
624 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
625 chat_input_history_entry_update_text (entry, text);
634 } ChatCommandMsgData;
637 chat_command_msg_cb (GObject *source,
638 GAsyncResult *result,
641 ChatCommandMsgData *data = user_data;
642 GError *error = NULL;
645 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
646 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
648 if (channel == NULL) {
649 DEBUG ("Failed to get channel: %s", error->message);
650 g_error_free (error);
652 empathy_chat_view_append_event (data->chat->view,
653 _("Failed to open private chat"));
657 if (!EMP_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
658 TpTextChannel *text = (TpTextChannel *) channel;
661 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
664 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
666 g_object_unref (msg);
669 g_object_unref (channel);
672 g_free (data->message);
673 g_slice_free (ChatCommandMsgData, data);
677 nick_command_supported (EmpathyChat *chat)
679 EmpathyChatPriv * priv = GET_PRIV (chat);
680 TpConnection *connection;
682 connection = empathy_tp_chat_get_connection (priv->tp_chat);
683 return tp_proxy_has_interface_by_id (connection,
684 EMP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
688 chat_command_clear (EmpathyChat *chat,
691 empathy_chat_view_clear (chat->view);
695 chat_command_topic (EmpathyChat *chat,
698 EmpathyChatPriv *priv = GET_PRIV (chat);
699 EmpathyTpChatProperty *property;
700 GValue value = {0, };
702 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
703 if (property == NULL) {
704 empathy_chat_view_append_event (chat->view,
705 _("Topic not supported on this conversation"));
709 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
710 empathy_chat_view_append_event (chat->view,
711 _("You are not allowed to change the topic"));
715 g_value_init (&value, G_TYPE_STRING);
716 g_value_set_string (&value, strv[1]);
717 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
718 g_value_unset (&value);
722 chat_command_join (EmpathyChat *chat,
726 EmpathyChatPriv *priv = GET_PRIV (chat);
728 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
730 /* FIXME: Ideally we should ask to handle ourself the channel so we can
731 * report the error if any but this is blocked by
732 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
733 while (rooms[i] != NULL) {
734 /* ignore empty strings */
735 if (!EMP_STR_EMPTY (rooms[i])) {
736 TpConnection *connection;
738 connection = empathy_tp_chat_get_connection (priv->tp_chat);
739 empathy_dispatcher_join_muc (priv->account, rooms[i],
740 gtk_get_current_event_time ());
748 chat_command_msg_internal (EmpathyChat *chat,
749 const gchar *contact_id,
750 const gchar *message)
752 EmpathyChatPriv *priv = GET_PRIV (chat);
753 ChatCommandMsgData *data;
754 TpAccountChannelRequest *req;
757 request = tp_asv_new (
758 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
759 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
760 TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
763 req = tp_account_channel_request_new (priv->account, request,
764 tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
766 /* FIXME: We should probably search in members alias. But this
767 * is enough for IRC */
768 data = g_slice_new (ChatCommandMsgData);
770 data->message = g_strdup (message);
772 tp_account_channel_request_ensure_and_observe_channel_async (req, NULL, NULL,
773 chat_command_msg_cb, data);
775 g_object_unref (req);
776 g_hash_table_unref (request);
780 chat_command_query (EmpathyChat *chat,
783 /* If <message> part is not defined,
784 * strv[2] will be the terminal NULL */
785 chat_command_msg_internal (chat, strv[1], strv[2]);
789 chat_command_msg (EmpathyChat *chat,
792 chat_command_msg_internal (chat, strv[1], strv[2]);
796 callback_for_request_rename (TpProxy *proxy,
799 GObject *weak_object)
802 DEBUG ("Call to RequestRename method failed: %s",error->message);
807 chat_command_nick (EmpathyChat *chat,
810 EmpathyChatPriv *priv = GET_PRIV (chat);
813 proxy = TP_PROXY (tp_account_get_connection (priv->account));
815 emp_cli_connection_interface_renaming_call_request_rename (proxy, -1,
816 strv[1], callback_for_request_rename, NULL, NULL, NULL);
820 chat_command_me (EmpathyChat *chat,
823 EmpathyChatPriv *priv = GET_PRIV (chat);
824 EmpathyMessage *message;
826 message = empathy_message_new (strv[1]);
827 empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
828 empathy_tp_chat_send (priv->tp_chat, message);
829 g_object_unref (message);
833 chat_command_say (EmpathyChat *chat,
836 EmpathyChatPriv *priv = GET_PRIV (chat);
837 EmpathyMessage *message;
839 message = empathy_message_new (strv[1]);
840 empathy_tp_chat_send (priv->tp_chat, message);
841 g_object_unref (message);
844 static void chat_command_help (EmpathyChat *chat, GStrv strv);
846 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
852 ChatCommandFunc func;
853 gboolean (*is_supported)(EmpathyChat *chat);
857 static ChatCommandItem commands[] = {
858 {"clear", 1, 1, chat_command_clear, NULL,
859 N_("/clear: clear all messages from the current conversation")},
861 {"topic", 2, 2, chat_command_topic, NULL,
862 N_("/topic <topic>: set the topic of the current conversation")},
864 {"join", 2, 2, chat_command_join, NULL,
865 N_("/join <chat room ID>: join a new chat room")},
867 {"j", 2, 2, chat_command_join, NULL,
868 N_("/j <chat room ID>: join a new chat room")},
870 {"query", 2, 3, chat_command_query, NULL,
871 N_("/query <contact ID> [<message>]: open a private chat")},
873 {"msg", 3, 3, chat_command_msg, NULL,
874 N_("/msg <contact ID> <message>: open a private chat")},
876 {"nick", 2, 2, chat_command_nick, nick_command_supported,
877 N_("/nick <nickname>: change your nickname on the current server")},
879 {"me", 2, 2, chat_command_me, NULL,
880 N_("/me <message>: send an ACTION message to the current conversation")},
882 {"say", 2, 2, chat_command_say, NULL,
883 N_("/say <message>: send <message> to the current conversation. "
884 "This is used to send a message starting with a '/'. For example: "
885 "\"/say /join is used to join a new chat room\"")},
887 {"help", 1, 2, chat_command_help, NULL,
888 N_("/help [<command>]: show all supported commands. "
889 "If <command> is defined, show its usage.")},
893 chat_command_show_help (EmpathyChat *chat,
894 ChatCommandItem *item)
898 str = g_strdup_printf (_("Usage: %s"), _(item->help));
899 empathy_chat_view_append_event (chat->view, str);
904 chat_command_help (EmpathyChat *chat,
907 EmpathyChatPriv *priv;
910 priv = GET_PRIV (chat);
912 /* If <command> part is not defined,
913 * strv[1] will be the terminal NULL */
914 if (strv[1] == NULL) {
915 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
916 if (commands[i].is_supported != NULL) {
917 if (!commands[i].is_supported (chat)) {
921 empathy_chat_view_append_event (chat->view,
922 _(commands[i].help));
927 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
928 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
929 if (commands[i].is_supported != NULL) {
930 if (!commands[i].is_supported (chat)) {
934 chat_command_show_help (chat, &commands[i]);
939 empathy_chat_view_append_event (chat->view,
940 _("Unknown command"));
944 chat_command_parse (const gchar *text, guint max_parts)
949 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
951 array = g_ptr_array_sized_new (max_parts + 1);
952 while (max_parts > 1) {
955 /* Skip white spaces */
956 while (g_ascii_isspace (*text)) {
960 /* Search the end of this part, until first space. */
961 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
967 item = g_strndup (text, end - text);
968 g_ptr_array_add (array, item);
969 DEBUG ("\tITEM: \"%s\"", item);
975 /* Append last part if not empty */
976 item = g_strstrip (g_strdup (text));
977 if (!EMP_STR_EMPTY (item)) {
978 g_ptr_array_add (array, item);
979 DEBUG ("\tITEM: \"%s\"", item);
984 /* Make the array NULL-terminated */
985 g_ptr_array_add (array, NULL);
987 return (GStrv) g_ptr_array_free (array, FALSE);
991 has_prefix_case (const gchar *s,
994 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
998 chat_send (EmpathyChat *chat,
1001 EmpathyChatPriv *priv;
1002 EmpathyMessage *message;
1005 if (EMP_STR_EMPTY (msg)) {
1009 priv = GET_PRIV (chat);
1011 chat_input_history_add (chat, msg, FALSE);
1013 if (msg[0] == '/') {
1014 gboolean second_slash = FALSE;
1015 const gchar *iter = msg + 1;
1017 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1022 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1025 c = *(msg + 1 + strlen (commands[i].prefix));
1026 if (c != '\0' && !g_ascii_isspace (c)) {
1029 if (commands[i].is_supported != NULL) {
1030 if (!commands[i].is_supported (chat)) {
1035 /* We can't use g_strsplit here because it does
1036 * not deal correctly if we have more than one space
1038 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1040 strv_len = g_strv_length (strv);
1041 if (strv_len < commands[i].min_parts ||
1042 strv_len > commands[i].max_parts) {
1043 chat_command_show_help (chat, &commands[i]);
1048 commands[i].func (chat, strv);
1053 /* Also allow messages with two slashes before the
1054 * first space, so it is possible to send a /unix/path.
1055 * This heuristic is kind of crap. */
1056 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1058 second_slash = TRUE;
1064 if (!second_slash) {
1065 empathy_chat_view_append_event (chat->view,
1066 _("Unknown command; see /help for the available"
1072 message = empathy_message_new (msg);
1073 empathy_tp_chat_send (priv->tp_chat, message);
1074 g_object_unref (message);
1078 chat_input_text_view_send (EmpathyChat *chat)
1080 EmpathyChatPriv *priv;
1081 GtkTextBuffer *buffer;
1082 GtkTextIter start, end;
1085 priv = GET_PRIV (chat);
1087 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1089 gtk_text_buffer_get_bounds (buffer, &start, &end);
1090 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1092 /* clear the input field */
1093 gtk_text_buffer_set_text (buffer, "", -1);
1094 /* delete input history modifications */
1095 chat_input_history_revert (chat);
1097 chat_send (chat, msg);
1102 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1103 EmpathyContact *contact,
1104 TpChannelChatState state,
1107 EmpathyChatPriv *priv;
1109 gboolean was_composing;
1111 priv = GET_PRIV (chat);
1113 if (empathy_contact_is_user (contact)) {
1114 /* We don't care about our own chat state */
1118 was_composing = (priv->compositors != NULL);
1120 /* Find the contact in the list. After that l is the list elem or NULL */
1121 for (l = priv->compositors; l; l = l->next) {
1122 if (contact == l->data) {
1128 case TP_CHANNEL_CHAT_STATE_GONE:
1129 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1130 case TP_CHANNEL_CHAT_STATE_PAUSED:
1131 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1132 /* Contact is not composing */
1134 priv->compositors = g_list_remove_link (priv->compositors, l);
1135 g_object_unref (l->data);
1139 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1140 /* Contact is composing */
1142 priv->compositors = g_list_prepend (priv->compositors,
1143 g_object_ref (contact));
1147 g_assert_not_reached ();
1150 DEBUG ("Was composing: %s now composing: %s",
1151 was_composing ? "yes" : "no",
1152 priv->compositors ? "yes" : "no");
1154 if ((was_composing && !priv->compositors) ||
1155 (!was_composing && priv->compositors)) {
1156 /* Composing state changed */
1157 g_signal_emit (chat, signals[COMPOSING], 0,
1158 priv->compositors != NULL);
1163 chat_message_received (EmpathyChat *chat,
1164 EmpathyMessage *message,
1167 EmpathyChatPriv *priv = GET_PRIV (chat);
1168 EmpathyContact *sender;
1170 sender = empathy_message_get_sender (message);
1172 DEBUG ("Appending new message from %s (%d)",
1173 empathy_contact_get_alias (sender),
1174 empathy_contact_get_handle (sender));
1176 empathy_chat_view_append_message (chat->view, message);
1178 /* We received a message so the contact is no longer composing */
1179 chat_state_changed_cb (priv->tp_chat, sender,
1180 TP_CHANNEL_CHAT_STATE_ACTIVE,
1183 priv->unread_messages++;
1184 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending);
1188 chat_message_received_cb (EmpathyTpChat *tp_chat,
1189 EmpathyMessage *message,
1192 chat_message_received (chat, message, FALSE);
1196 chat_send_error_cb (EmpathyTpChat *tp_chat,
1197 const gchar *message_body,
1198 TpChannelTextSendError error_code,
1204 switch (error_code) {
1205 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1206 error = _("offline");
1208 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1209 error = _("invalid contact");
1211 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1212 error = _("permission denied");
1214 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1215 error = _("too long message");
1217 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1218 error = _("not implemented");
1220 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1222 error = _("unknown");
1226 str = g_strdup_printf (_("Error sending message '%s': %s"),
1229 empathy_chat_view_append_event (chat->view, str);
1234 chat_topic_label_size_allocate_cb (GtkLabel *label,
1235 GtkAllocation *allocation,
1238 EmpathyChatPriv *priv = GET_PRIV (chat);
1240 if (!gtk_label_get_line_wrap (label)) {
1241 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1242 gtk_widget_show (priv->expander_topic);
1244 gtk_widget_hide (priv->expander_topic);
1251 chat_topic_expander_activate_cb (GtkExpander *expander,
1252 GParamSpec *param_spec,
1255 EmpathyChatPriv *priv = GET_PRIV (chat);
1257 if (gtk_expander_get_expanded (expander)) {
1258 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1259 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1261 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1262 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1267 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1272 EmpathyChatPriv *priv = GET_PRIV (chat);
1274 if (!tp_strdiff (name, "subject")) {
1275 g_free (priv->subject);
1276 priv->subject = g_value_dup_string (value);
1277 g_object_notify (G_OBJECT (chat), "subject");
1279 if (EMP_STR_EMPTY (priv->subject)) {
1280 gtk_widget_hide (priv->hbox_topic);
1282 gchar *markup_topic;
1285 markup_topic = empathy_add_link_markup (priv->subject);
1286 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1287 _("Topic:"), markup_topic);
1289 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1290 g_free (markup_text);
1291 g_free (markup_topic);
1293 gtk_widget_show (priv->hbox_topic);
1295 if (priv->block_events_timeout_id == 0) {
1298 if (!EMP_STR_EMPTY (priv->subject)) {
1299 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1301 str = g_strdup (_("No topic defined"));
1303 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1307 else if (!tp_strdiff (name, "name")) {
1308 g_free (priv->name);
1309 priv->name = g_value_dup_string (value);
1310 g_object_notify (G_OBJECT (chat), "name");
1315 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1319 GtkTextIter word_start = *iter;
1320 GtkTextIter word_end = *iter;
1323 if (gtk_text_iter_inside_word (&word_end) &&
1324 !gtk_text_iter_ends_word (&word_end)) {
1325 gtk_text_iter_forward_word_end (&word_end);
1330 if (gtk_text_iter_get_char (&tmp) == '\'') {
1331 gtk_text_iter_forward_char (&tmp);
1333 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1334 gtk_text_iter_forward_word_end (&word_end);
1339 if (gtk_text_iter_inside_word (&word_start) ||
1340 gtk_text_iter_ends_word (&word_start)) {
1341 if (!gtk_text_iter_starts_word (&word_start) ||
1342 gtk_text_iter_equal (&word_start, &word_end)) {
1343 gtk_text_iter_backward_word_start (&word_start);
1347 gtk_text_iter_backward_char (&tmp);
1349 if (gtk_text_iter_get_char (&tmp) == '\'') {
1350 gtk_text_iter_backward_char (&tmp);
1352 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1353 gtk_text_iter_backward_word_start (&word_start);
1358 *start = word_start;
1364 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1365 GtkTextIter *location,
1370 GtkTextIter iter, pos;
1372 /* Remove all misspelled tags in the inserted text.
1373 * This happens when text is inserted within a misspelled word. */
1374 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1375 gtk_text_iter_get_offset (location) - len);
1376 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1379 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1382 GtkTextIter start, end;
1385 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1388 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1390 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1391 gtk_text_iter_equal (&pos, &end) ||
1392 empathy_spell_check (str)) {
1393 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1395 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1400 } while (gtk_text_iter_forward_word_end (&iter) &&
1401 gtk_text_iter_compare (&iter, location) <= 0);
1405 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1410 GtkTextIter word_start, word_end;
1412 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1413 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1414 &word_start, &word_end);
1419 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1422 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1423 chat_composing_stop (chat);
1425 chat_composing_start (chat);
1430 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1435 GtkTextIter prev_pos;
1436 GtkTextIter word_start;
1437 GtkTextIter word_end;
1441 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1443 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1444 gtk_text_buffer_get_insert (buffer));
1445 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1447 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1450 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1451 !gtk_text_iter_equal (&pos, &word_end)) {
1452 str = gtk_text_buffer_get_text (buffer,
1453 &word_start, &word_end, FALSE);
1455 if (!empathy_spell_check (str)) {
1456 gtk_text_buffer_apply_tag_by_name (buffer,
1457 "misspelled", &word_start, &word_end);
1459 gtk_text_buffer_remove_tag_by_name (buffer,
1460 "misspelled", &word_start, &word_end);
1467 gtk_text_buffer_move_mark (buffer, mark, &pos);
1471 empathy_isspace_cb (gunichar c,
1474 return g_unichar_isspace (c);
1478 chat_input_key_press_event_cb (GtkWidget *widget,
1482 EmpathyChatPriv *priv;
1485 GtkWidget *text_view_sw;
1487 priv = GET_PRIV (chat);
1489 /* Catch ctrl+up/down so we can traverse messages we sent */
1490 if ((event->state & GDK_CONTROL_MASK) &&
1491 (event->keyval == GDK_KEY_Up ||
1492 event->keyval == GDK_KEY_Down)) {
1493 GtkTextBuffer *buffer;
1496 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1497 chat_input_history_update (chat, buffer);
1499 if (event->keyval == GDK_KEY_Up) {
1500 str = chat_input_history_get_next (chat);
1502 str = chat_input_history_get_prev (chat);
1505 g_signal_handlers_block_by_func (buffer,
1506 chat_input_text_buffer_changed_cb,
1508 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1509 g_signal_handlers_unblock_by_func (buffer,
1510 chat_input_text_buffer_changed_cb,
1516 /* Catch enter but not ctrl/shift-enter */
1517 if (IS_ENTER (event->keyval) &&
1518 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1521 /* This is to make sure that kinput2 gets the enter. And if
1522 * it's handled there we shouldn't send on it. This is because
1523 * kinput2 uses Enter to commit letters. See:
1524 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1527 view = GTK_TEXT_VIEW (chat->input_text_view);
1528 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1529 gtk_text_view_reset_im_context (view);
1533 chat_input_text_view_send (chat);
1537 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1539 if (IS_ENTER (event->keyval) &&
1540 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1541 /* Newline for shift/control-enter. */
1544 if (!(event->state & GDK_CONTROL_MASK) &&
1545 event->keyval == GDK_KEY_Page_Up) {
1546 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1547 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1550 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1551 event->keyval == GDK_KEY_Page_Down) {
1552 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1553 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1554 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1555 gtk_adjustment_set_value (adj, val);
1558 if (event->keyval == GDK_KEY_Escape) {
1559 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1561 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1562 event->keyval == GDK_KEY_Tab) {
1563 GtkTextBuffer *buffer;
1564 GtkTextIter start, current;
1565 gchar *nick, *completed;
1566 GList *list, *completed_list;
1567 gboolean is_start_of_buffer;
1569 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1570 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1572 /* Get the start of the nick to complete. */
1573 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1574 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1575 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1577 is_start_of_buffer = gtk_text_iter_is_start (&start);
1579 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1580 g_completion_add_items (priv->completion, list);
1582 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1583 completed_list = g_completion_complete (priv->completion,
1592 GString *message = NULL;
1595 gtk_text_buffer_delete (buffer, &start, ¤t);
1597 len = g_list_length (completed_list);
1600 /* If we only have one hit, use that text
1601 * instead of the text in completed since the
1602 * completed text will use the typed string
1603 * which might be cased all wrong.
1606 text = empathy_contact_get_alias (completed_list->data);
1610 /* Print all hits to the scrollback view, so the
1611 * user knows what possibilities he has.
1614 message = g_string_new ("");
1615 for (l = completed_list; l != NULL; l = l->next) {
1616 g_string_append (message, empathy_contact_get_alias (l->data));
1617 g_string_append (message, " - ");
1619 empathy_chat_view_append_event (chat->view, message->str);
1620 g_string_free (message, TRUE);
1623 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1625 if (len == 1 && is_start_of_buffer) {
1626 gchar *complete_char;
1628 complete_char = g_settings_get_string (
1629 priv->gsettings_chat,
1630 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1632 if (complete_char != NULL) {
1633 gtk_text_buffer_insert_at_cursor (buffer,
1635 strlen (complete_char));
1636 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1637 g_free (complete_char);
1644 g_completion_clear_items (priv->completion);
1646 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1656 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1660 gtk_widget_grab_focus (chat->input_text_view);
1666 chat_input_realize_cb (GtkWidget *widget,
1669 DEBUG ("Setting focus to the input text view");
1670 if (gtk_widget_is_sensitive (widget)) {
1671 gtk_widget_grab_focus (widget);
1676 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1677 EmpathySmiley *smiley,
1680 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1681 GtkTextBuffer *buffer;
1684 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1686 gtk_text_buffer_get_end_iter (buffer, &iter);
1687 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1689 gtk_text_buffer_get_end_iter (buffer, &iter);
1690 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1701 static EmpathyChatSpell *
1702 chat_spell_new (EmpathyChat *chat,
1707 EmpathyChatSpell *chat_spell;
1709 chat_spell = g_slice_new0 (EmpathyChatSpell);
1711 chat_spell->chat = g_object_ref (chat);
1712 chat_spell->word = g_strdup (word);
1713 chat_spell->start = start;
1714 chat_spell->end = end;
1720 chat_spell_free (EmpathyChatSpell *chat_spell)
1722 g_object_unref (chat_spell->chat);
1723 g_free (chat_spell->word);
1724 g_slice_free (EmpathyChatSpell, chat_spell);
1728 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1729 EmpathyChatSpell *chat_spell)
1731 empathy_chat_correct_word (chat_spell->chat,
1732 &(chat_spell->start),
1734 gtk_menu_item_get_label (menu_item));
1739 chat_spelling_build_suggestions_menu (const gchar *code,
1740 EmpathyChatSpell *chat_spell)
1742 GList *suggestions, *l;
1743 GtkWidget *menu, *menu_item;
1745 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
1746 if (suggestions == NULL)
1749 menu = gtk_menu_new ();
1750 for (l = suggestions; l; l = l->next) {
1751 menu_item = gtk_menu_item_new_with_label (l->data);
1752 g_signal_connect (G_OBJECT (menu_item), "activate",
1753 G_CALLBACK (chat_spelling_menu_activate_cb),
1755 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1757 empathy_spell_free_suggestions (suggestions);
1759 gtk_widget_show_all (menu);
1765 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1767 GtkWidget *menu, *submenu, *item;
1770 codes = empathy_spell_get_enabled_language_codes ();
1771 g_assert (codes != NULL);
1773 if (g_list_length (codes) > 1) {
1774 menu = gtk_menu_new ();
1776 for (l = codes; l; l = l->next) {
1777 const gchar *code, *name;
1780 name = empathy_spell_get_language_name (code);
1784 item = gtk_image_menu_item_new_with_label (name);
1786 submenu = chat_spelling_build_suggestions_menu (
1788 if (submenu == NULL)
1789 gtk_widget_set_sensitive (item, FALSE);
1791 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
1793 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1796 menu = chat_spelling_build_suggestions_menu (codes->data,
1799 menu = gtk_menu_new ();
1800 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1801 gtk_widget_set_sensitive (item, FALSE);
1802 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1805 g_list_free (codes);
1807 gtk_widget_show_all (menu);
1818 static EmpathyChatWord *
1819 chat_word_new (EmpathyChat *chat,
1823 EmpathyChatWord *chat_word;
1825 chat_word = g_slice_new0 (EmpathyChatWord);
1827 chat_word->chat = g_object_ref (chat);
1828 chat_word->word = g_strdup (word);
1829 chat_word->code = g_strdup (code);
1835 chat_word_free (EmpathyChatWord *chat_word)
1837 g_object_unref (chat_word->chat);
1838 g_free (chat_word->word);
1839 g_free (chat_word->code);
1840 g_slice_free (EmpathyChatWord, chat_word);
1844 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
1845 EmpathyChatWord *chat_word)
1847 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
1849 empathy_spell_add_to_dictionary (chat_word->code,
1851 priv->update_misspelled_words_id =
1852 g_idle_add (update_misspelled_words, chat_word->chat);
1856 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
1858 GtkWidget *menu, *item, *lang_item, *image;
1861 const gchar *code, *name;
1862 EmpathyChatWord *chat_word;
1864 codes = empathy_spell_get_enabled_language_codes ();
1865 g_assert (codes != NULL);
1866 if (g_list_length (codes) > 1) {
1867 /* translators: %s is the selected word */
1868 label = g_strdup_printf (_("Add '%s' to Dictionary"),
1870 item = gtk_image_menu_item_new_with_mnemonic (label);
1872 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1873 GTK_ICON_SIZE_MENU);
1874 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1877 menu = gtk_menu_new ();
1879 for (l = codes; l; l = l->next) {
1881 name = empathy_spell_get_language_name (code);
1885 lang_item = gtk_image_menu_item_new_with_label (name);
1887 chat_word= chat_word_new (chat_spell->chat,
1888 chat_spell->word, code);
1889 g_object_set_data_full (G_OBJECT (lang_item),
1890 "chat-word", chat_word,
1891 (GDestroyNotify) chat_word_free);
1893 g_signal_connect (G_OBJECT (lang_item), "activate",
1894 G_CALLBACK (chat_add_to_dictionary_activate_cb),
1896 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
1898 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
1901 name = empathy_spell_get_language_name (code);
1902 g_assert (name != NULL);
1903 /* translators: first %s is the selected word,
1904 * second %s is the language name of the target dictionary */
1905 label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
1906 chat_spell->word, name);
1907 item = gtk_image_menu_item_new_with_mnemonic (label);
1909 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
1910 GTK_ICON_SIZE_MENU);
1911 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1913 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
1915 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
1916 (GDestroyNotify) chat_word_free);
1918 g_signal_connect (G_OBJECT (item), "activate",
1919 G_CALLBACK (chat_add_to_dictionary_activate_cb),
1922 g_list_free (codes);
1924 gtk_widget_show_all (item);
1930 chat_text_send_cb (GtkMenuItem *menuitem,
1933 chat_input_text_view_send (chat);
1937 chat_input_populate_popup_cb (GtkTextView *view,
1941 EmpathyChatPriv *priv;
1942 GtkTextBuffer *buffer;
1943 GtkTextTagTable *table;
1946 GtkTextIter iter, start, end;
1949 EmpathyChatSpell *chat_spell;
1950 GtkWidget *spell_menu;
1951 GtkWidget *spell_item;
1952 EmpathySmileyManager *smiley_manager;
1953 GtkWidget *smiley_menu;
1956 priv = GET_PRIV (chat);
1957 buffer = gtk_text_view_get_buffer (view);
1959 /* Add the emoticon menu. */
1960 item = gtk_separator_menu_item_new ();
1961 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1962 gtk_widget_show (item);
1964 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1965 image = gtk_image_new_from_icon_name ("face-smile",
1966 GTK_ICON_SIZE_MENU);
1967 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1968 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1969 gtk_widget_show (item);
1971 smiley_manager = empathy_smiley_manager_dup_singleton ();
1972 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1973 chat_insert_smiley_activate_cb,
1975 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1976 g_object_unref (smiley_manager);
1978 /* Add the Send menu item. */
1979 gtk_text_buffer_get_bounds (buffer, &start, &end);
1980 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1981 if (!EMP_STR_EMPTY (str)) {
1982 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1983 g_signal_connect (G_OBJECT (item), "activate",
1984 G_CALLBACK (chat_text_send_cb), chat);
1985 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1986 gtk_widget_show (item);
1990 /* Add the spell check menu item. */
1991 table = gtk_text_buffer_get_tag_table (buffer);
1992 tag = gtk_text_tag_table_lookup (table, "misspelled");
1993 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1994 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1995 GTK_TEXT_WINDOW_WIDGET,
1998 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
2000 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2001 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2003 str = gtk_text_buffer_get_text (buffer,
2004 &start, &end, FALSE);
2006 if (!EMP_STR_EMPTY (str)) {
2007 chat_spell = chat_spell_new (chat, str, start, end);
2008 g_object_set_data_full (G_OBJECT (menu),
2009 "chat-spell", chat_spell,
2010 (GDestroyNotify) chat_spell_free);
2012 item = gtk_separator_menu_item_new ();
2013 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2014 gtk_widget_show (item);
2016 /* Spelling suggestions */
2017 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2018 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2019 GTK_ICON_SIZE_MENU);
2020 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2022 spell_menu = chat_spelling_build_menu (chat_spell);
2023 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2026 spell_item = gtk_separator_menu_item_new ();
2027 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2028 gtk_widget_show (spell_item);
2030 /* Add to dictionary */
2031 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2033 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2034 gtk_widget_show (spell_item);
2036 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2037 gtk_widget_show (item);
2043 chat_log_filter (TplEvent *log,
2046 EmpathyChat *chat = user_data;
2047 EmpathyMessage *message;
2048 EmpathyChatPriv *priv = GET_PRIV (chat);
2049 const GList *pending;
2051 g_return_val_if_fail (TPL_IS_EVENT (log), FALSE);
2052 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2054 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2055 message = empathy_message_from_tpl_log_event (log);
2057 for (; pending; pending = g_list_next (pending)) {
2058 if (empathy_message_equal (message, pending->data)) {
2059 g_object_unref (message);
2064 g_object_unref (message);
2070 show_pending_messages (EmpathyChat *chat) {
2071 EmpathyChatPriv *priv = GET_PRIV (chat);
2072 const GList *messages, *l;
2074 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2076 if (chat->view == NULL || priv->tp_chat == NULL)
2079 if (!priv->can_show_pending)
2082 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2084 for (l = messages; l != NULL ; l = g_list_next (l)) {
2085 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2086 chat_message_received (chat, message, TRUE);
2092 got_filtered_messages_cb (GObject *manager,
2093 GAsyncResult *result,
2098 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2099 EmpathyChatPriv *priv = GET_PRIV (chat);
2100 GError *error = NULL;
2102 if (!tpl_log_manager_get_filtered_events_finish (TPL_LOG_MANAGER (manager),
2103 result, &messages, &error)) {
2104 DEBUG ("%s. Aborting.", error->message);
2105 empathy_chat_view_append_event (chat->view,
2106 _("Failed to retrieve recent logs"));
2107 g_error_free (error);
2111 for (l = messages; l; l = g_list_next (l)) {
2112 EmpathyMessage *message;
2113 g_assert (TPL_IS_EVENT (l->data));
2115 message = empathy_message_from_tpl_log_event (l->data);
2116 g_object_unref (l->data);
2118 empathy_chat_view_append_message (chat->view, message);
2119 g_object_unref (message);
2121 g_list_free (messages);
2124 /* in case of TPL error, skip backlog and show pending messages */
2125 priv->can_show_pending = TRUE;
2126 show_pending_messages (chat);
2128 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2129 * about it in EmpathyChatPriv definition */
2130 priv->retrieving_backlogs = FALSE;
2131 empathy_chat_messages_read (chat);
2133 /* Turn back on scrolling */
2134 empathy_chat_view_scroll (chat->view, TRUE);
2138 chat_add_logs (EmpathyChat *chat)
2140 EmpathyChatPriv *priv = GET_PRIV (chat);
2147 /* Turn off scrolling temporarily */
2148 empathy_chat_view_scroll (chat->view, FALSE);
2150 /* Add messages from last conversation */
2151 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
2152 target = tpl_entity_new_from_room_id (priv->id);
2154 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
2156 priv->retrieving_backlogs = TRUE;
2157 tpl_log_manager_get_filtered_events_async (priv->log_manager,
2160 TPL_EVENT_MASK_TEXT,
2164 got_filtered_messages_cb,
2169 chat_contacts_completion_func (const gchar *s1,
2173 gchar *tmp, *nick1, *nick2;
2180 return s1 ? -1 : +1;
2183 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2184 nick1 = g_utf8_casefold (tmp, -1);
2187 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2188 nick2 = g_utf8_casefold (tmp, -1);
2191 ret = strncmp (nick1, nick2, n);
2200 build_part_message (guint reason,
2202 EmpathyContact *actor,
2203 const gchar *message)
2205 GString *s = g_string_new ("");
2206 const gchar *actor_name = NULL;
2208 if (actor != NULL) {
2209 actor_name = empathy_contact_get_alias (actor);
2212 /* Having an actor only really makes sense for a few actions... */
2214 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2215 g_string_append_printf (s, _("%s has disconnected"), name);
2217 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2218 if (actor_name != NULL) {
2219 /* translators: reverse the order of these arguments
2220 * if the kicked should come before the kicker in your locale.
2222 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2225 g_string_append_printf (s, _("%s was kicked"), name);
2228 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2229 if (actor_name != NULL) {
2230 /* translators: reverse the order of these arguments
2231 * if the banned should come before the banner in your locale.
2233 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2236 g_string_append_printf (s, _("%s was banned"), name);
2240 g_string_append_printf (s, _("%s has left the room"), name);
2243 if (!EMP_STR_EMPTY (message)) {
2244 /* Note to translators: this string is appended to
2245 * notifications like "foo has left the room", with the message
2246 * given by the user living the room. If this poses a problem,
2247 * please let us know. :-)
2249 g_string_append_printf (s, _(" (%s)"), message);
2252 return g_string_free (s, FALSE);
2256 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2257 EmpathyContact *contact,
2258 EmpathyContact *actor,
2264 EmpathyChatPriv *priv = GET_PRIV (chat);
2265 const gchar *name = empathy_contact_get_alias (contact);
2268 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2270 if (priv->block_events_timeout_id != 0)
2274 str = g_strdup_printf (_("%s has joined the room"),
2277 str = build_part_message (reason, name, actor, message);
2280 empathy_chat_view_append_event (chat->view, str);
2285 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2286 EmpathyContact *old_contact,
2287 EmpathyContact *new_contact,
2292 EmpathyChatPriv *priv = GET_PRIV (chat);
2294 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2296 if (priv->block_events_timeout_id == 0) {
2299 str = g_strdup_printf (_("%s is now known as %s"),
2300 empathy_contact_get_alias (old_contact),
2301 empathy_contact_get_alias (new_contact));
2302 empathy_chat_view_append_event (chat->view, str);
2309 chat_reset_size_request (gpointer widget)
2311 gtk_widget_set_size_request (widget, -1, -1);
2317 chat_update_contacts_visibility (EmpathyChat *chat,
2320 EmpathyChatPriv *priv = GET_PRIV (chat);
2321 GtkAllocation allocation;
2323 if (!priv->scrolled_window_contacts) {
2327 if (priv->remote_contact != NULL) {
2331 if (show && priv->contact_list_view == NULL) {
2332 EmpathyContactListStore *store;
2335 /* We are adding the contact list to the chat, we don't want the
2336 * chat view to become too small. If the chat view is already
2337 * smaller than 250 make sure that size won't change. If the
2338 * chat view is bigger the contact list will take some space on
2339 * it but we make sure the chat view don't become smaller than
2340 * 250. Relax the size request once the resize is done */
2341 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2342 min_width = MIN (allocation.width, 250);
2343 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2344 g_idle_add (chat_reset_size_request, priv->vbox_left);
2346 if (priv->contacts_width > 0) {
2347 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2348 priv->contacts_width);
2351 store = empathy_contact_list_store_new (
2352 EMPATHY_CONTACT_LIST (priv->tp_chat));
2353 empathy_contact_list_store_set_show_groups (
2354 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2356 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2357 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2358 EMPATHY_CONTACT_FEATURE_CHAT |
2359 EMPATHY_CONTACT_FEATURE_CALL |
2360 EMPATHY_CONTACT_FEATURE_LOG |
2361 EMPATHY_CONTACT_FEATURE_INFO));
2362 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2363 priv->contact_list_view);
2364 gtk_widget_show (priv->contact_list_view);
2365 gtk_widget_show (priv->scrolled_window_contacts);
2366 g_object_unref (store);
2368 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2369 gtk_widget_hide (priv->scrolled_window_contacts);
2370 if (priv->contact_list_view != NULL) {
2371 gtk_widget_destroy (priv->contact_list_view);
2372 priv->contact_list_view = NULL;
2378 empathy_chat_set_show_contacts (EmpathyChat *chat,
2381 EmpathyChatPriv *priv = GET_PRIV (chat);
2383 priv->show_contacts = show;
2385 chat_update_contacts_visibility (chat, show);
2387 g_object_notify (G_OBJECT (chat), "show-contacts");
2391 chat_remote_contact_changed_cb (EmpathyChat *chat)
2393 EmpathyChatPriv *priv = GET_PRIV (chat);
2395 if (priv->remote_contact != NULL) {
2396 g_object_unref (priv->remote_contact);
2397 priv->remote_contact = NULL;
2402 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2403 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2404 if (priv->remote_contact != NULL) {
2405 g_object_ref (priv->remote_contact);
2406 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2408 else if (priv->tp_chat != NULL) {
2411 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2412 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2415 chat_update_contacts_visibility (chat, priv->show_contacts);
2417 g_object_notify (G_OBJECT (chat), "remote-contact");
2418 g_object_notify (G_OBJECT (chat), "id");
2422 chat_destroy_cb (EmpathyTpChat *tp_chat,
2425 EmpathyChatPriv *priv;
2427 priv = GET_PRIV (chat);
2429 if (!priv->tp_chat) {
2433 chat_composing_remove_timeout (chat);
2434 g_object_unref (priv->tp_chat);
2435 priv->tp_chat = NULL;
2436 g_object_notify (G_OBJECT (chat), "tp-chat");
2438 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2439 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2441 chat_update_contacts_visibility (chat, FALSE);
2445 update_misspelled_words (gpointer data)
2447 EmpathyChat *chat = EMPATHY_CHAT (data);
2448 EmpathyChatPriv *priv = GET_PRIV (chat);
2449 GtkTextBuffer *buffer;
2453 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2455 gtk_text_buffer_get_end_iter (buffer, &iter);
2456 length = gtk_text_iter_get_offset (&iter);
2457 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2458 NULL, length, chat);
2460 priv->update_misspelled_words_id = 0;
2466 conf_spell_checking_cb (GSettings *gsettings_chat,
2470 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2471 EmpathyChatPriv *priv = GET_PRIV (chat);
2472 gboolean spell_checker;
2473 GtkTextBuffer *buffer;
2475 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2478 spell_checker = g_settings_get_boolean (gsettings_chat,
2479 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2481 if (!empathy_spell_supported ()) {
2482 spell_checker = FALSE;
2485 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2487 if (spell_checker == priv->spell_checking_enabled) {
2488 if (spell_checker) {
2489 /* Possibly changed dictionaries,
2490 * update misspelled words. Need to do so in idle
2491 * so the spell checker is updated. */
2492 priv->update_misspelled_words_id =
2493 g_idle_add (update_misspelled_words, chat);
2499 if (spell_checker) {
2502 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2503 buffer, "notify::cursor-position",
2504 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2506 priv->insert_text_id = tp_g_signal_connect_object (
2507 buffer, "insert-text",
2508 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2509 chat, G_CONNECT_AFTER);
2510 priv->delete_range_id = tp_g_signal_connect_object (
2511 buffer, "delete-range",
2512 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2513 chat, G_CONNECT_AFTER);
2515 gtk_text_buffer_create_tag (buffer, "misspelled",
2516 "underline", PANGO_UNDERLINE_ERROR,
2519 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2520 gtk_text_buffer_get_insert (buffer));
2521 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2524 /* Mark misspelled words in the existing buffer.
2525 * Need to do so in idle so the spell checker is updated. */
2526 priv->update_misspelled_words_id =
2527 g_idle_add (update_misspelled_words, chat);
2529 GtkTextTagTable *table;
2532 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2533 priv->notify_cursor_position_id = 0;
2534 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2535 priv->insert_text_id = 0;
2536 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2537 priv->delete_range_id = 0;
2539 table = gtk_text_buffer_get_tag_table (buffer);
2540 tag = gtk_text_tag_table_lookup (table, "misspelled");
2541 gtk_text_tag_table_remove (table, tag);
2543 gtk_text_buffer_delete_mark_by_name (buffer,
2544 "previous-cursor-position");
2547 priv->spell_checking_enabled = spell_checker;
2551 save_paned_pos_timeout (gpointer data)
2553 EmpathyChat *self = data;
2556 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2558 g_settings_set_int (self->priv->gsettings_ui,
2559 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2566 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2570 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2572 if (chat->priv->save_paned_pos_id != 0)
2573 g_source_remove (chat->priv->save_paned_pos_id);
2575 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2576 save_paned_pos_timeout, chat);
2582 chat_create_ui (EmpathyChat *chat)
2584 EmpathyChatPriv *priv = GET_PRIV (chat);
2588 GtkTextBuffer *buffer;
2590 EmpathyThemeManager *theme_mgr;
2592 filename = empathy_file_lookup ("empathy-chat.ui",
2594 gui = empathy_builder_get_file (filename,
2595 "chat_widget", &priv->widget,
2596 "hpaned", &priv->hpaned,
2597 "vbox_left", &priv->vbox_left,
2598 "scrolled_window_chat", &priv->scrolled_window_chat,
2599 "scrolled_window_input", &priv->scrolled_window_input,
2600 "hbox_topic", &priv->hbox_topic,
2601 "expander_topic", &priv->expander_topic,
2602 "label_topic", &priv->label_topic,
2603 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2604 "info_bar_vbox", &priv->info_bar_vbox,
2607 empathy_builder_connect (gui, chat,
2608 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2609 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2614 /* Add message view. */
2615 theme_mgr = empathy_theme_manager_dup_singleton ();
2616 chat->view = empathy_theme_manager_create_view (theme_mgr);
2617 g_object_unref (theme_mgr);
2618 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2619 and other types, even though it's non-editable and doesn't accept any
2620 drags. This steals drag motion for anything inside the scrollbars,
2621 making drag destinations on chat windows far less useful.
2623 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2624 g_signal_connect (chat->view, "focus_in_event",
2625 G_CALLBACK (chat_text_view_focus_in_event_cb),
2627 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2628 GTK_WIDGET (chat->view));
2629 gtk_widget_show (GTK_WIDGET (chat->view));
2631 /* Add input GtkTextView */
2632 chat->input_text_view = empathy_input_text_view_new ();
2634 g_signal_connect (chat->input_text_view, "key-press-event",
2635 G_CALLBACK (chat_input_key_press_event_cb),
2637 g_signal_connect (chat->input_text_view, "realize",
2638 G_CALLBACK (chat_input_realize_cb),
2640 g_signal_connect (chat->input_text_view, "populate-popup",
2641 G_CALLBACK (chat_input_populate_popup_cb),
2643 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2644 tp_g_signal_connect_object (buffer, "changed",
2645 G_CALLBACK (chat_input_text_buffer_changed_cb),
2647 tp_g_signal_connect_object (priv->gsettings_chat,
2648 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2649 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2650 conf_spell_checking_cb (priv->gsettings_chat,
2651 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2652 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2653 chat->input_text_view);
2654 gtk_widget_show (chat->input_text_view);
2656 /* Add the (invisible) search bar */
2657 priv->search_bar = empathy_search_bar_new (chat->view);
2658 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2661 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2663 /* Initialy hide the topic, will be shown if not empty */
2664 gtk_widget_hide (priv->hbox_topic);
2666 g_signal_connect (priv->hpaned, "notify::position",
2667 G_CALLBACK (chat_hpaned_pos_changed_cb),
2670 /* Load the paned position */
2671 paned_pos = g_settings_get_int (priv->gsettings_ui,
2672 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2674 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2676 /* Set widget focus order */
2677 list = g_list_append (NULL, priv->search_bar);
2678 list = g_list_append (list, priv->scrolled_window_input);
2679 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2682 list = g_list_append (NULL, priv->vbox_left);
2683 list = g_list_append (list, priv->scrolled_window_contacts);
2684 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2687 list = g_list_append (NULL, priv->hpaned);
2688 list = g_list_append (list, priv->hbox_topic);
2689 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2692 /* Add the main widget in the chat widget */
2693 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2694 g_object_unref (gui);
2698 chat_size_allocate (GtkWidget *widget,
2699 GtkAllocation *allocation)
2701 GtkBin *bin = GTK_BIN (widget);
2702 GtkAllocation child_allocation;
2705 gtk_widget_set_allocation (widget, allocation);
2707 child = gtk_bin_get_child (bin);
2709 if (child && gtk_widget_get_visible (child))
2711 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2712 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2713 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2714 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2716 gtk_widget_size_allocate (child, &child_allocation);
2721 chat_finalize (GObject *object)
2724 EmpathyChatPriv *priv;
2726 chat = EMPATHY_CHAT (object);
2727 priv = GET_PRIV (chat);
2729 DEBUG ("Finalized: %p", object);
2731 if (priv->update_misspelled_words_id != 0)
2732 g_source_remove (priv->update_misspelled_words_id);
2734 if (priv->save_paned_pos_id != 0)
2735 g_source_remove (priv->save_paned_pos_id);
2737 g_object_unref (priv->gsettings_chat);
2738 g_object_unref (priv->gsettings_ui);
2740 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2741 g_list_free (priv->input_history);
2743 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2744 g_list_free (priv->compositors);
2746 chat_composing_remove_timeout (chat);
2748 g_object_unref (priv->account_manager);
2749 g_object_unref (priv->log_manager);
2751 if (priv->tp_chat) {
2752 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2753 chat_destroy_cb, chat);
2754 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2755 chat_message_received_cb, chat);
2756 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2757 chat_send_error_cb, chat);
2758 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2759 chat_state_changed_cb, chat);
2760 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2761 chat_property_changed_cb, chat);
2762 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2763 chat_members_changed_cb, chat);
2764 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2765 chat_remote_contact_changed_cb, chat);
2766 empathy_tp_chat_leave (priv->tp_chat);
2767 g_object_unref (priv->tp_chat);
2769 if (priv->account) {
2770 g_object_unref (priv->account);
2772 if (priv->remote_contact) {
2773 g_object_unref (priv->remote_contact);
2776 if (priv->block_events_timeout_id) {
2777 g_source_remove (priv->block_events_timeout_id);
2781 g_free (priv->name);
2782 g_free (priv->subject);
2783 g_completion_free (priv->completion);
2785 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2789 chat_constructed (GObject *object)
2791 EmpathyChat *chat = EMPATHY_CHAT (object);
2792 EmpathyChatPriv *priv = GET_PRIV (chat);
2794 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2795 /* First display logs from the logger and then display pending messages */
2796 chat_add_logs (chat);
2799 /* Just display pending messages for rooms */
2800 priv->can_show_pending = TRUE;
2801 show_pending_messages (chat);
2806 empathy_chat_class_init (EmpathyChatClass *klass)
2808 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2809 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2811 object_class->finalize = chat_finalize;
2812 object_class->get_property = chat_get_property;
2813 object_class->set_property = chat_set_property;
2814 object_class->constructed = chat_constructed;
2816 widget_class->size_allocate = chat_size_allocate;
2818 g_object_class_install_property (object_class,
2820 g_param_spec_object ("tp-chat",
2822 "The tp chat object",
2823 EMPATHY_TYPE_TP_CHAT,
2826 G_PARAM_STATIC_STRINGS));
2827 g_object_class_install_property (object_class,
2829 g_param_spec_object ("account",
2830 "Account of the chat",
2831 "The account of the chat",
2834 G_PARAM_STATIC_STRINGS));
2835 g_object_class_install_property (object_class,
2837 g_param_spec_string ("id",
2839 "The id of the chat",
2842 G_PARAM_STATIC_STRINGS));
2843 g_object_class_install_property (object_class,
2845 g_param_spec_string ("name",
2847 "The name of the chat",
2850 G_PARAM_STATIC_STRINGS));
2851 g_object_class_install_property (object_class,
2853 g_param_spec_string ("subject",
2855 "The subject or topic of the chat",
2858 G_PARAM_STATIC_STRINGS));
2859 g_object_class_install_property (object_class,
2860 PROP_REMOTE_CONTACT,
2861 g_param_spec_object ("remote-contact",
2862 "The remote contact",
2863 "The remote contact is any",
2864 EMPATHY_TYPE_CONTACT,
2866 G_PARAM_STATIC_STRINGS));
2867 g_object_class_install_property (object_class,
2869 g_param_spec_boolean ("show-contacts",
2870 "Contacts' visibility",
2871 "The visibility of the contacts' list",
2874 G_PARAM_STATIC_STRINGS));
2876 signals[COMPOSING] =
2877 g_signal_new ("composing",
2878 G_OBJECT_CLASS_TYPE (object_class),
2882 g_cclosure_marshal_VOID__BOOLEAN,
2886 signals[NEW_MESSAGE] =
2887 g_signal_new ("new-message",
2888 G_OBJECT_CLASS_TYPE (object_class),
2892 _empathy_marshal_VOID__OBJECT_BOOLEAN,
2894 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
2896 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2900 chat_block_events_timeout_cb (gpointer data)
2902 EmpathyChatPriv *priv = GET_PRIV (data);
2904 priv->block_events_timeout_id = 0;
2910 account_manager_prepared_cb (GObject *source_object,
2911 GAsyncResult *result,
2914 GList *accounts, *l;
2915 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2916 EmpathyChat *chat = user_data;
2917 GError *error = NULL;
2919 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2920 DEBUG ("Failed to prepare the account manager: %s", error->message);
2921 g_error_free (error);
2925 accounts = tp_account_manager_get_valid_accounts (account_manager);
2927 for (l = accounts; l != NULL; l = l->next) {
2928 TpAccount *account = l->data;
2929 tp_g_signal_connect_object (account, "status-changed",
2930 G_CALLBACK (chat_new_connection_cb),
2934 g_list_free (accounts);
2938 empathy_chat_init (EmpathyChat *chat)
2940 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2941 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2944 priv->log_manager = tpl_log_manager_dup_singleton ();
2945 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
2946 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2948 priv->contacts_width = -1;
2949 priv->input_history = NULL;
2950 priv->input_history_current = NULL;
2951 priv->account_manager = tp_account_manager_dup ();
2953 tp_account_manager_prepare_async (priv->account_manager, NULL,
2954 account_manager_prepared_cb, chat);
2956 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
2957 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
2959 /* Block events for some time to avoid having "has come online" or
2960 * "joined" messages. */
2961 priv->block_events_timeout_id =
2962 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2964 /* Add nick name completion */
2965 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
2966 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2968 chat_create_ui (chat);
2972 empathy_chat_new (EmpathyTpChat *tp_chat)
2974 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2978 empathy_chat_get_tp_chat (EmpathyChat *chat)
2980 EmpathyChatPriv *priv = GET_PRIV (chat);
2982 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2984 return priv->tp_chat;
2990 GtkWidget *info_bar;
3000 passwd_remember_button_cb (GtkButton *button,
3003 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3007 passwd_not_now_button_cb (GtkButton *button,
3010 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3014 remember_password_infobar_response_cb (GtkWidget *info_bar,
3018 EmpathyChatPriv *priv = GET_PRIV (data->self);
3020 if (response_id == GTK_RESPONSE_OK) {
3021 DEBUG ("Saving room password");
3022 empathy_keyring_set_room_password_async (priv->account,
3023 empathy_tp_chat_get_id (priv->tp_chat),
3028 gtk_widget_destroy (info_bar);
3029 g_free (data->password);
3030 g_slice_free (PasswordData, data);
3034 chat_prompt_to_save_password (EmpathyChat *self,
3037 GtkWidget *content_area;
3044 /* save the password in case it needs to be saved */
3045 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3047 /* Remove all previous widgets */
3048 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3049 gtk_container_forall (GTK_CONTAINER (content_area),
3050 (GtkCallback) gtk_widget_destroy, NULL);
3051 data->button = NULL;
3054 data->spinner = NULL;
3056 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3057 GTK_MESSAGE_QUESTION);
3059 hbox = gtk_hbox_new (FALSE, 5);
3060 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3063 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3064 GTK_ICON_SIZE_DIALOG);
3065 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3068 label = gtk_label_new (_("Would you like to store this password?"));
3069 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3071 /* Add 'Remember' button */
3072 alig = gtk_alignment_new (0, 0.5, 1, 0);
3074 button = gtk_button_new_with_label (_("Remember"));
3075 gtk_container_add (GTK_CONTAINER (alig), button);
3076 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3078 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3081 /* Add 'Not now' button */
3082 alig = gtk_alignment_new (0, 0.5, 1, 0);
3084 button = gtk_button_new_with_label (_("Not now"));
3085 gtk_container_add (GTK_CONTAINER (alig), button);
3086 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3088 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3092 g_signal_handler_disconnect (data->info_bar, data->response_id);
3093 g_signal_connect (data->info_bar, "response",
3094 G_CALLBACK (remember_password_infobar_response_cb), data);
3096 gtk_widget_show_all (data->info_bar);
3100 provide_password_cb (GObject *tp_chat,
3104 PasswordData *data = user_data;
3105 EmpathyChat *self = data->self;
3106 EmpathyChatPriv *priv = GET_PRIV (self);
3107 GError *error = NULL;
3109 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3111 DEBUG ("error: %s", error->message);
3112 /* FIXME: what should we do if that's another error? Close the channel?
3113 * Display the raw D-Bus error to the user isn't very useful */
3114 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3116 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3117 gtk_widget_set_sensitive (data->entry, TRUE);
3118 gtk_widget_grab_focus (data->entry);
3121 gtk_info_bar_set_message_type (
3122 GTK_INFO_BAR (data->info_bar),
3126 gtk_widget_set_sensitive (data->button, TRUE);
3127 gtk_button_set_label (GTK_BUTTON (data->button),
3131 gtk_label_set_text (GTK_LABEL (data->label),
3132 _("Wrong password; please try again:"));
3135 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3136 gtk_widget_hide (data->spinner);
3138 g_error_free (error);
3142 if (empathy_keyring_is_available ()) {
3143 /* ask whether they want to save the password */
3144 chat_prompt_to_save_password (self, data);
3146 /* Get rid of the password info bar finally */
3147 gtk_widget_destroy (data->info_bar);
3148 g_slice_free (PasswordData, data);
3152 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3153 gtk_widget_grab_focus (self->input_text_view);
3157 password_infobar_response_cb (GtkWidget *info_bar,
3161 EmpathyChatPriv *priv = GET_PRIV (data->self);
3162 const gchar *password;
3164 if (response_id != GTK_RESPONSE_OK) {
3165 gtk_widget_destroy (info_bar);
3166 g_slice_free (PasswordData, data);
3170 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3172 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3173 provide_password_cb, data);
3175 gtk_widget_set_sensitive (data->button, FALSE);
3176 gtk_widget_set_sensitive (data->entry, FALSE);
3178 gtk_spinner_start (GTK_SPINNER (data->spinner));
3179 gtk_widget_show (data->spinner);
3183 password_entry_activate_cb (GtkWidget *entry,
3186 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3190 passwd_join_button_cb (GtkButton *button,
3193 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3197 clear_icon_released_cb (GtkEntry *entry,
3198 GtkEntryIconPosition icon_pos,
3202 gtk_entry_set_text (entry, "");
3206 password_entry_changed_cb (GtkEditable *entry,
3211 str = gtk_entry_get_text (GTK_ENTRY (entry));
3213 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3214 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3218 display_password_info_bar (EmpathyChat *self)
3220 EmpathyChatPriv *priv = GET_PRIV (self);
3221 GtkWidget *info_bar;
3222 GtkWidget *content_area;
3232 data = g_slice_new0 (PasswordData);
3234 info_bar = gtk_info_bar_new ();
3235 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3236 GTK_MESSAGE_QUESTION);
3238 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3240 hbox = gtk_hbox_new (FALSE, 5);
3241 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3244 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3245 GTK_ICON_SIZE_DIALOG);
3246 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3249 label = gtk_label_new (_("This room is protected by a password:"));
3250 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3252 /* Add password entry */
3253 entry = gtk_entry_new ();
3254 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3255 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3257 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3258 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3259 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3260 GTK_ENTRY_ICON_SECONDARY, FALSE);
3262 g_signal_connect (entry, "icon-release",
3263 G_CALLBACK (clear_icon_released_cb), data);
3264 g_signal_connect (entry, "changed",
3265 G_CALLBACK (password_entry_changed_cb), data);
3267 g_signal_connect (entry, "activate",
3268 G_CALLBACK (password_entry_activate_cb), data);
3270 /* Focus the password entry once it's realized */
3271 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3273 /* Add 'Join' button */
3274 alig = gtk_alignment_new (0, 0.5, 1, 0);
3276 button = gtk_button_new_with_label (_("Join"));
3277 gtk_container_add (GTK_CONTAINER (alig), button);
3278 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3280 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3284 spinner = gtk_spinner_new ();
3285 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3287 /* Save some data for messing around with later */
3289 data->info_bar = info_bar;
3290 data->button = button;
3291 data->label = label;
3292 data->entry = entry;
3293 data->spinner = spinner;
3295 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3297 gtk_widget_show_all (hbox);
3299 data->response_id = g_signal_connect (info_bar, "response",
3300 G_CALLBACK (password_infobar_response_cb), data);
3302 gtk_widget_show_all (info_bar);
3303 /* ... but hide the spinner */
3304 gtk_widget_hide (spinner);
3308 provide_saved_password_cb (GObject *tp_chat,
3312 EmpathyChat *self = user_data;
3313 EmpathyChatPriv *priv = GET_PRIV (self);
3314 GError *error = NULL;
3316 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3318 DEBUG ("error: %s", error->message);
3319 /* FIXME: what should we do if that's another error? Close the channel?
3320 * Display the raw D-Bus error to the user isn't very useful */
3321 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3322 display_password_info_bar (self);
3323 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3325 g_error_free (error);
3330 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3331 gtk_widget_grab_focus (self->input_text_view);
3335 chat_room_got_password_cb (GObject *source,
3336 GAsyncResult *result,
3339 EmpathyChat *self = user_data;
3340 EmpathyChatPriv *priv = GET_PRIV (self);
3341 const gchar *password;
3342 GError *error = NULL;
3344 password = empathy_keyring_get_room_password_finish (priv->account,
3347 if (error != NULL) {
3348 DEBUG ("Couldn't get room password: %s\n", error->message);
3349 g_clear_error (&error);
3351 display_password_info_bar (self);
3352 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3356 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3357 provide_saved_password_cb, self);
3361 chat_password_needed_changed_cb (EmpathyChat *self)
3363 EmpathyChatPriv *priv = GET_PRIV (self);
3365 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3366 empathy_keyring_get_room_password_async (priv->account,
3367 empathy_tp_chat_get_id (priv->tp_chat),
3368 chat_room_got_password_cb, self);
3373 empathy_chat_set_tp_chat (EmpathyChat *chat,
3374 EmpathyTpChat *tp_chat)
3376 EmpathyChatPriv *priv = GET_PRIV (chat);
3377 GPtrArray *properties;
3379 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3380 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3381 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3383 if (priv->tp_chat) {
3387 if (priv->account) {
3388 g_object_unref (priv->account);
3391 priv->tp_chat = g_object_ref (tp_chat);
3392 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3394 g_signal_connect (tp_chat, "destroy",
3395 G_CALLBACK (chat_destroy_cb),
3397 g_signal_connect (tp_chat, "message-received",
3398 G_CALLBACK (chat_message_received_cb),
3400 g_signal_connect (tp_chat, "send-error",
3401 G_CALLBACK (chat_send_error_cb),
3403 g_signal_connect (tp_chat, "chat-state-changed",
3404 G_CALLBACK (chat_state_changed_cb),
3406 g_signal_connect (tp_chat, "property-changed",
3407 G_CALLBACK (chat_property_changed_cb),
3409 g_signal_connect (tp_chat, "members-changed",
3410 G_CALLBACK (chat_members_changed_cb),
3412 g_signal_connect (tp_chat, "member-renamed",
3413 G_CALLBACK (chat_member_renamed_cb),
3415 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3416 G_CALLBACK (chat_remote_contact_changed_cb),
3418 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3419 G_CALLBACK (chat_password_needed_changed_cb),
3422 /* Get initial value of properties */
3423 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3424 if (properties != NULL) {
3427 for (i = 0; i < properties->len; i++) {
3428 EmpathyTpChatProperty *property;
3430 property = g_ptr_array_index (properties, i);
3431 if (property->value == NULL)
3434 chat_property_changed_cb (priv->tp_chat,
3441 chat_remote_contact_changed_cb (chat);
3443 if (chat->input_text_view) {
3444 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3445 if (priv->block_events_timeout_id == 0) {
3446 empathy_chat_view_append_event (chat->view, _("Connected"));
3450 g_object_notify (G_OBJECT (chat), "tp-chat");
3451 g_object_notify (G_OBJECT (chat), "id");
3452 g_object_notify (G_OBJECT (chat), "account");
3454 /* This is a noop when tp-chat is set at object construction time and causes
3455 * the pending messages to be show when it's set on the object after it has
3457 show_pending_messages (chat);
3459 /* check if a password is needed */
3460 chat_password_needed_changed_cb (chat);
3464 empathy_chat_get_account (EmpathyChat *chat)
3466 EmpathyChatPriv *priv = GET_PRIV (chat);
3468 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3470 return priv->account;
3474 empathy_chat_get_id (EmpathyChat *chat)
3476 EmpathyChatPriv *priv = GET_PRIV (chat);
3478 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3484 empathy_chat_get_name (EmpathyChat *chat)
3486 EmpathyChatPriv *priv = GET_PRIV (chat);
3489 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3492 if (!ret && priv->remote_contact) {
3493 ret = empathy_contact_get_alias (priv->remote_contact);
3499 return ret ? ret : _("Conversation");
3503 empathy_chat_get_subject (EmpathyChat *chat)
3505 EmpathyChatPriv *priv = GET_PRIV (chat);
3507 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3509 return priv->subject;
3513 empathy_chat_get_remote_contact (EmpathyChat *chat)
3515 EmpathyChatPriv *priv = GET_PRIV (chat);
3517 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3519 return priv->remote_contact;
3523 empathy_chat_get_contact_menu (EmpathyChat *chat)
3525 EmpathyChatPriv *priv = GET_PRIV (chat);
3526 GtkWidget *menu = NULL;
3528 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3530 if (priv->remote_contact) {
3531 menu = empathy_contact_menu_new (priv->remote_contact,
3532 EMPATHY_CONTACT_FEATURE_CALL |
3533 EMPATHY_CONTACT_FEATURE_LOG |
3534 EMPATHY_CONTACT_FEATURE_INFO);
3541 empathy_chat_clear (EmpathyChat *chat)
3543 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3545 empathy_chat_view_clear (chat->view);
3549 empathy_chat_scroll_down (EmpathyChat *chat)
3551 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3553 empathy_chat_view_scroll_down (chat->view);
3557 empathy_chat_cut (EmpathyChat *chat)
3559 GtkTextBuffer *buffer;
3561 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3563 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3564 if (gtk_text_buffer_get_has_selection (buffer)) {
3565 GtkClipboard *clipboard;
3567 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3569 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3574 empathy_chat_copy (EmpathyChat *chat)
3576 GtkTextBuffer *buffer;
3578 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3580 if (empathy_chat_view_get_has_selection (chat->view)) {
3581 empathy_chat_view_copy_clipboard (chat->view);
3585 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3586 if (gtk_text_buffer_get_has_selection (buffer)) {
3587 GtkClipboard *clipboard;
3589 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3591 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3596 EmpathyChatPriv *priv = GET_PRIV (chat);
3598 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3605 GtkClipboard *clipboard;
3607 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3608 start = g_utf8_offset_to_pointer (topic, start_offset);
3609 end = g_utf8_offset_to_pointer (topic, end_offset);
3610 selection = g_strndup (start, end - start);
3612 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3613 gtk_clipboard_set_text (clipboard, selection, -1);
3621 empathy_chat_paste (EmpathyChat *chat)
3623 GtkTextBuffer *buffer;
3624 GtkClipboard *clipboard;
3625 EmpathyChatPriv *priv;
3627 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3629 priv = GET_PRIV (chat);
3631 if (gtk_widget_get_visible (priv->search_bar)) {
3632 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3636 if (priv->tp_chat == NULL ||
3637 !gtk_widget_is_sensitive (chat->input_text_view))
3640 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3641 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3643 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3647 empathy_chat_find (EmpathyChat *chat)
3649 EmpathyChatPriv *priv;
3651 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3653 priv = GET_PRIV (chat);
3655 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3659 empathy_chat_correct_word (EmpathyChat *chat,
3662 const gchar *new_word)
3664 GtkTextBuffer *buffer;
3666 g_return_if_fail (chat != NULL);
3667 g_return_if_fail (new_word != NULL);
3669 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3671 gtk_text_buffer_delete (buffer, start, end);
3672 gtk_text_buffer_insert (buffer, start,
3678 empathy_chat_is_room (EmpathyChat *chat)
3680 EmpathyChatPriv *priv = GET_PRIV (chat);
3682 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3684 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3688 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3690 EmpathyChatPriv *priv = GET_PRIV (self);
3692 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3694 return priv->unread_messages;
3697 /* called when the messages have been read by user */
3699 empathy_chat_messages_read (EmpathyChat *self)
3701 EmpathyChatPriv *priv = GET_PRIV (self);
3703 g_return_if_fail (EMPATHY_IS_CHAT (self));
3705 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3706 * definition. If we are still retrieving the backlogs, do not ACK */
3707 if (priv->retrieving_backlogs)
3710 if (priv->tp_chat != NULL ) {
3711 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3713 priv->unread_messages = 0;
3716 /* Return TRUE if on of the contacts in this chat is composing */
3718 empathy_chat_is_composing (EmpathyChat *chat)
3720 return chat->priv->compositors != NULL;