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,
2167 g_object_unref (target);
2171 chat_contacts_completion_func (const gchar *s1,
2175 gchar *tmp, *nick1, *nick2;
2182 return s1 ? -1 : +1;
2185 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2186 nick1 = g_utf8_casefold (tmp, -1);
2189 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2190 nick2 = g_utf8_casefold (tmp, -1);
2193 ret = strncmp (nick1, nick2, n);
2202 build_part_message (guint reason,
2204 EmpathyContact *actor,
2205 const gchar *message)
2207 GString *s = g_string_new ("");
2208 const gchar *actor_name = NULL;
2210 if (actor != NULL) {
2211 actor_name = empathy_contact_get_alias (actor);
2214 /* Having an actor only really makes sense for a few actions... */
2216 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2217 g_string_append_printf (s, _("%s has disconnected"), name);
2219 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2220 if (actor_name != NULL) {
2221 /* translators: reverse the order of these arguments
2222 * if the kicked should come before the kicker in your locale.
2224 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2227 g_string_append_printf (s, _("%s was kicked"), name);
2230 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2231 if (actor_name != NULL) {
2232 /* translators: reverse the order of these arguments
2233 * if the banned should come before the banner in your locale.
2235 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2238 g_string_append_printf (s, _("%s was banned"), name);
2242 g_string_append_printf (s, _("%s has left the room"), name);
2245 if (!EMP_STR_EMPTY (message)) {
2246 /* Note to translators: this string is appended to
2247 * notifications like "foo has left the room", with the message
2248 * given by the user living the room. If this poses a problem,
2249 * please let us know. :-)
2251 g_string_append_printf (s, _(" (%s)"), message);
2254 return g_string_free (s, FALSE);
2258 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2259 EmpathyContact *contact,
2260 EmpathyContact *actor,
2266 EmpathyChatPriv *priv = GET_PRIV (chat);
2267 const gchar *name = empathy_contact_get_alias (contact);
2270 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2272 if (priv->block_events_timeout_id != 0)
2276 str = g_strdup_printf (_("%s has joined the room"),
2279 str = build_part_message (reason, name, actor, message);
2282 empathy_chat_view_append_event (chat->view, str);
2287 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2288 EmpathyContact *old_contact,
2289 EmpathyContact *new_contact,
2294 EmpathyChatPriv *priv = GET_PRIV (chat);
2296 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2298 if (priv->block_events_timeout_id == 0) {
2301 str = g_strdup_printf (_("%s is now known as %s"),
2302 empathy_contact_get_alias (old_contact),
2303 empathy_contact_get_alias (new_contact));
2304 empathy_chat_view_append_event (chat->view, str);
2311 chat_reset_size_request (gpointer widget)
2313 gtk_widget_set_size_request (widget, -1, -1);
2319 chat_update_contacts_visibility (EmpathyChat *chat,
2322 EmpathyChatPriv *priv = GET_PRIV (chat);
2323 GtkAllocation allocation;
2325 if (!priv->scrolled_window_contacts) {
2329 if (priv->remote_contact != NULL) {
2333 if (show && priv->contact_list_view == NULL) {
2334 EmpathyContactListStore *store;
2337 /* We are adding the contact list to the chat, we don't want the
2338 * chat view to become too small. If the chat view is already
2339 * smaller than 250 make sure that size won't change. If the
2340 * chat view is bigger the contact list will take some space on
2341 * it but we make sure the chat view don't become smaller than
2342 * 250. Relax the size request once the resize is done */
2343 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2344 min_width = MIN (allocation.width, 250);
2345 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2346 g_idle_add (chat_reset_size_request, priv->vbox_left);
2348 if (priv->contacts_width > 0) {
2349 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2350 priv->contacts_width);
2353 store = empathy_contact_list_store_new (
2354 EMPATHY_CONTACT_LIST (priv->tp_chat));
2355 empathy_contact_list_store_set_show_groups (
2356 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2358 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2359 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2360 EMPATHY_CONTACT_FEATURE_CHAT |
2361 EMPATHY_CONTACT_FEATURE_CALL |
2362 EMPATHY_CONTACT_FEATURE_LOG |
2363 EMPATHY_CONTACT_FEATURE_INFO));
2364 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2365 priv->contact_list_view);
2366 gtk_widget_show (priv->contact_list_view);
2367 gtk_widget_show (priv->scrolled_window_contacts);
2368 g_object_unref (store);
2370 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2371 gtk_widget_hide (priv->scrolled_window_contacts);
2372 if (priv->contact_list_view != NULL) {
2373 gtk_widget_destroy (priv->contact_list_view);
2374 priv->contact_list_view = NULL;
2380 empathy_chat_set_show_contacts (EmpathyChat *chat,
2383 EmpathyChatPriv *priv = GET_PRIV (chat);
2385 priv->show_contacts = show;
2387 chat_update_contacts_visibility (chat, show);
2389 g_object_notify (G_OBJECT (chat), "show-contacts");
2393 chat_remote_contact_changed_cb (EmpathyChat *chat)
2395 EmpathyChatPriv *priv = GET_PRIV (chat);
2397 if (priv->remote_contact != NULL) {
2398 g_object_unref (priv->remote_contact);
2399 priv->remote_contact = NULL;
2404 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2405 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2406 if (priv->remote_contact != NULL) {
2407 g_object_ref (priv->remote_contact);
2408 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2410 else if (priv->tp_chat != NULL) {
2413 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2414 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2417 chat_update_contacts_visibility (chat, priv->show_contacts);
2419 g_object_notify (G_OBJECT (chat), "remote-contact");
2420 g_object_notify (G_OBJECT (chat), "id");
2424 chat_destroy_cb (EmpathyTpChat *tp_chat,
2427 EmpathyChatPriv *priv;
2429 priv = GET_PRIV (chat);
2431 if (!priv->tp_chat) {
2435 chat_composing_remove_timeout (chat);
2436 g_object_unref (priv->tp_chat);
2437 priv->tp_chat = NULL;
2438 g_object_notify (G_OBJECT (chat), "tp-chat");
2440 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2441 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2443 chat_update_contacts_visibility (chat, FALSE);
2447 update_misspelled_words (gpointer data)
2449 EmpathyChat *chat = EMPATHY_CHAT (data);
2450 EmpathyChatPriv *priv = GET_PRIV (chat);
2451 GtkTextBuffer *buffer;
2455 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2457 gtk_text_buffer_get_end_iter (buffer, &iter);
2458 length = gtk_text_iter_get_offset (&iter);
2459 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2460 NULL, length, chat);
2462 priv->update_misspelled_words_id = 0;
2468 conf_spell_checking_cb (GSettings *gsettings_chat,
2472 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2473 EmpathyChatPriv *priv = GET_PRIV (chat);
2474 gboolean spell_checker;
2475 GtkTextBuffer *buffer;
2477 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2480 spell_checker = g_settings_get_boolean (gsettings_chat,
2481 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2483 if (!empathy_spell_supported ()) {
2484 spell_checker = FALSE;
2487 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2489 if (spell_checker == priv->spell_checking_enabled) {
2490 if (spell_checker) {
2491 /* Possibly changed dictionaries,
2492 * update misspelled words. Need to do so in idle
2493 * so the spell checker is updated. */
2494 priv->update_misspelled_words_id =
2495 g_idle_add (update_misspelled_words, chat);
2501 if (spell_checker) {
2504 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2505 buffer, "notify::cursor-position",
2506 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2508 priv->insert_text_id = tp_g_signal_connect_object (
2509 buffer, "insert-text",
2510 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2511 chat, G_CONNECT_AFTER);
2512 priv->delete_range_id = tp_g_signal_connect_object (
2513 buffer, "delete-range",
2514 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2515 chat, G_CONNECT_AFTER);
2517 gtk_text_buffer_create_tag (buffer, "misspelled",
2518 "underline", PANGO_UNDERLINE_ERROR,
2521 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2522 gtk_text_buffer_get_insert (buffer));
2523 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2526 /* Mark misspelled words in the existing buffer.
2527 * Need to do so in idle so the spell checker is updated. */
2528 priv->update_misspelled_words_id =
2529 g_idle_add (update_misspelled_words, chat);
2531 GtkTextTagTable *table;
2534 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2535 priv->notify_cursor_position_id = 0;
2536 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2537 priv->insert_text_id = 0;
2538 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2539 priv->delete_range_id = 0;
2541 table = gtk_text_buffer_get_tag_table (buffer);
2542 tag = gtk_text_tag_table_lookup (table, "misspelled");
2543 gtk_text_tag_table_remove (table, tag);
2545 gtk_text_buffer_delete_mark_by_name (buffer,
2546 "previous-cursor-position");
2549 priv->spell_checking_enabled = spell_checker;
2553 save_paned_pos_timeout (gpointer data)
2555 EmpathyChat *self = data;
2558 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
2560 g_settings_set_int (self->priv->gsettings_ui,
2561 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2568 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
2572 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2574 if (chat->priv->save_paned_pos_id != 0)
2575 g_source_remove (chat->priv->save_paned_pos_id);
2577 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
2578 save_paned_pos_timeout, chat);
2584 chat_create_ui (EmpathyChat *chat)
2586 EmpathyChatPriv *priv = GET_PRIV (chat);
2590 GtkTextBuffer *buffer;
2592 EmpathyThemeManager *theme_mgr;
2594 filename = empathy_file_lookup ("empathy-chat.ui",
2596 gui = empathy_builder_get_file (filename,
2597 "chat_widget", &priv->widget,
2598 "hpaned", &priv->hpaned,
2599 "vbox_left", &priv->vbox_left,
2600 "scrolled_window_chat", &priv->scrolled_window_chat,
2601 "scrolled_window_input", &priv->scrolled_window_input,
2602 "hbox_topic", &priv->hbox_topic,
2603 "expander_topic", &priv->expander_topic,
2604 "label_topic", &priv->label_topic,
2605 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2606 "info_bar_vbox", &priv->info_bar_vbox,
2609 empathy_builder_connect (gui, chat,
2610 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2611 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2616 /* Add message view. */
2617 theme_mgr = empathy_theme_manager_dup_singleton ();
2618 chat->view = empathy_theme_manager_create_view (theme_mgr);
2619 g_object_unref (theme_mgr);
2620 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2621 and other types, even though it's non-editable and doesn't accept any
2622 drags. This steals drag motion for anything inside the scrollbars,
2623 making drag destinations on chat windows far less useful.
2625 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2626 g_signal_connect (chat->view, "focus_in_event",
2627 G_CALLBACK (chat_text_view_focus_in_event_cb),
2629 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2630 GTK_WIDGET (chat->view));
2631 gtk_widget_show (GTK_WIDGET (chat->view));
2633 /* Add input GtkTextView */
2634 chat->input_text_view = empathy_input_text_view_new ();
2636 g_signal_connect (chat->input_text_view, "key-press-event",
2637 G_CALLBACK (chat_input_key_press_event_cb),
2639 g_signal_connect (chat->input_text_view, "realize",
2640 G_CALLBACK (chat_input_realize_cb),
2642 g_signal_connect (chat->input_text_view, "populate-popup",
2643 G_CALLBACK (chat_input_populate_popup_cb),
2645 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2646 tp_g_signal_connect_object (buffer, "changed",
2647 G_CALLBACK (chat_input_text_buffer_changed_cb),
2649 tp_g_signal_connect_object (priv->gsettings_chat,
2650 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2651 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2652 conf_spell_checking_cb (priv->gsettings_chat,
2653 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2654 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2655 chat->input_text_view);
2656 gtk_widget_show (chat->input_text_view);
2658 /* Add the (invisible) search bar */
2659 priv->search_bar = empathy_search_bar_new (chat->view);
2660 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2663 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2665 /* Initialy hide the topic, will be shown if not empty */
2666 gtk_widget_hide (priv->hbox_topic);
2668 g_signal_connect (priv->hpaned, "notify::position",
2669 G_CALLBACK (chat_hpaned_pos_changed_cb),
2672 /* Load the paned position */
2673 paned_pos = g_settings_get_int (priv->gsettings_ui,
2674 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2676 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2678 /* Set widget focus order */
2679 list = g_list_append (NULL, priv->search_bar);
2680 list = g_list_append (list, priv->scrolled_window_input);
2681 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2684 list = g_list_append (NULL, priv->vbox_left);
2685 list = g_list_append (list, priv->scrolled_window_contacts);
2686 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2689 list = g_list_append (NULL, priv->hpaned);
2690 list = g_list_append (list, priv->hbox_topic);
2691 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2694 /* Add the main widget in the chat widget */
2695 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2696 g_object_unref (gui);
2700 chat_size_allocate (GtkWidget *widget,
2701 GtkAllocation *allocation)
2703 GtkBin *bin = GTK_BIN (widget);
2704 GtkAllocation child_allocation;
2707 gtk_widget_set_allocation (widget, allocation);
2709 child = gtk_bin_get_child (bin);
2711 if (child && gtk_widget_get_visible (child))
2713 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2714 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2715 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2716 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2718 gtk_widget_size_allocate (child, &child_allocation);
2723 chat_finalize (GObject *object)
2726 EmpathyChatPriv *priv;
2728 chat = EMPATHY_CHAT (object);
2729 priv = GET_PRIV (chat);
2731 DEBUG ("Finalized: %p", object);
2733 if (priv->update_misspelled_words_id != 0)
2734 g_source_remove (priv->update_misspelled_words_id);
2736 if (priv->save_paned_pos_id != 0)
2737 g_source_remove (priv->save_paned_pos_id);
2739 g_object_unref (priv->gsettings_chat);
2740 g_object_unref (priv->gsettings_ui);
2742 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2743 g_list_free (priv->input_history);
2745 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2746 g_list_free (priv->compositors);
2748 chat_composing_remove_timeout (chat);
2750 g_object_unref (priv->account_manager);
2751 g_object_unref (priv->log_manager);
2753 if (priv->tp_chat) {
2754 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2755 chat_destroy_cb, chat);
2756 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2757 chat_message_received_cb, chat);
2758 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2759 chat_send_error_cb, chat);
2760 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2761 chat_state_changed_cb, chat);
2762 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2763 chat_property_changed_cb, chat);
2764 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2765 chat_members_changed_cb, chat);
2766 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2767 chat_remote_contact_changed_cb, chat);
2768 empathy_tp_chat_leave (priv->tp_chat);
2769 g_object_unref (priv->tp_chat);
2771 if (priv->account) {
2772 g_object_unref (priv->account);
2774 if (priv->remote_contact) {
2775 g_object_unref (priv->remote_contact);
2778 if (priv->block_events_timeout_id) {
2779 g_source_remove (priv->block_events_timeout_id);
2783 g_free (priv->name);
2784 g_free (priv->subject);
2785 g_completion_free (priv->completion);
2787 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2791 chat_constructed (GObject *object)
2793 EmpathyChat *chat = EMPATHY_CHAT (object);
2794 EmpathyChatPriv *priv = GET_PRIV (chat);
2796 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
2797 /* First display logs from the logger and then display pending messages */
2798 chat_add_logs (chat);
2801 /* Just display pending messages for rooms */
2802 priv->can_show_pending = TRUE;
2803 show_pending_messages (chat);
2808 empathy_chat_class_init (EmpathyChatClass *klass)
2810 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2811 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2813 object_class->finalize = chat_finalize;
2814 object_class->get_property = chat_get_property;
2815 object_class->set_property = chat_set_property;
2816 object_class->constructed = chat_constructed;
2818 widget_class->size_allocate = chat_size_allocate;
2820 g_object_class_install_property (object_class,
2822 g_param_spec_object ("tp-chat",
2824 "The tp chat object",
2825 EMPATHY_TYPE_TP_CHAT,
2828 G_PARAM_STATIC_STRINGS));
2829 g_object_class_install_property (object_class,
2831 g_param_spec_object ("account",
2832 "Account of the chat",
2833 "The account of the chat",
2836 G_PARAM_STATIC_STRINGS));
2837 g_object_class_install_property (object_class,
2839 g_param_spec_string ("id",
2841 "The id of the chat",
2844 G_PARAM_STATIC_STRINGS));
2845 g_object_class_install_property (object_class,
2847 g_param_spec_string ("name",
2849 "The name of the chat",
2852 G_PARAM_STATIC_STRINGS));
2853 g_object_class_install_property (object_class,
2855 g_param_spec_string ("subject",
2857 "The subject or topic of the chat",
2860 G_PARAM_STATIC_STRINGS));
2861 g_object_class_install_property (object_class,
2862 PROP_REMOTE_CONTACT,
2863 g_param_spec_object ("remote-contact",
2864 "The remote contact",
2865 "The remote contact is any",
2866 EMPATHY_TYPE_CONTACT,
2868 G_PARAM_STATIC_STRINGS));
2869 g_object_class_install_property (object_class,
2871 g_param_spec_boolean ("show-contacts",
2872 "Contacts' visibility",
2873 "The visibility of the contacts' list",
2876 G_PARAM_STATIC_STRINGS));
2878 signals[COMPOSING] =
2879 g_signal_new ("composing",
2880 G_OBJECT_CLASS_TYPE (object_class),
2884 g_cclosure_marshal_VOID__BOOLEAN,
2888 signals[NEW_MESSAGE] =
2889 g_signal_new ("new-message",
2890 G_OBJECT_CLASS_TYPE (object_class),
2894 _empathy_marshal_VOID__OBJECT_BOOLEAN,
2896 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
2898 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2902 chat_block_events_timeout_cb (gpointer data)
2904 EmpathyChatPriv *priv = GET_PRIV (data);
2906 priv->block_events_timeout_id = 0;
2912 account_manager_prepared_cb (GObject *source_object,
2913 GAsyncResult *result,
2916 GList *accounts, *l;
2917 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2918 EmpathyChat *chat = user_data;
2919 GError *error = NULL;
2921 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2922 DEBUG ("Failed to prepare the account manager: %s", error->message);
2923 g_error_free (error);
2927 accounts = tp_account_manager_get_valid_accounts (account_manager);
2929 for (l = accounts; l != NULL; l = l->next) {
2930 TpAccount *account = l->data;
2931 tp_g_signal_connect_object (account, "status-changed",
2932 G_CALLBACK (chat_new_connection_cb),
2936 g_list_free (accounts);
2940 empathy_chat_init (EmpathyChat *chat)
2942 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2943 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2946 priv->log_manager = tpl_log_manager_dup_singleton ();
2947 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
2948 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2950 priv->contacts_width = -1;
2951 priv->input_history = NULL;
2952 priv->input_history_current = NULL;
2953 priv->account_manager = tp_account_manager_dup ();
2955 tp_account_manager_prepare_async (priv->account_manager, NULL,
2956 account_manager_prepared_cb, chat);
2958 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
2959 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
2961 /* Block events for some time to avoid having "has come online" or
2962 * "joined" messages. */
2963 priv->block_events_timeout_id =
2964 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2966 /* Add nick name completion */
2967 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
2968 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2970 chat_create_ui (chat);
2974 empathy_chat_new (EmpathyTpChat *tp_chat)
2976 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2980 empathy_chat_get_tp_chat (EmpathyChat *chat)
2982 EmpathyChatPriv *priv = GET_PRIV (chat);
2984 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2986 return priv->tp_chat;
2992 GtkWidget *info_bar;
3002 passwd_remember_button_cb (GtkButton *button,
3005 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3009 passwd_not_now_button_cb (GtkButton *button,
3012 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3016 remember_password_infobar_response_cb (GtkWidget *info_bar,
3020 EmpathyChatPriv *priv = GET_PRIV (data->self);
3022 if (response_id == GTK_RESPONSE_OK) {
3023 DEBUG ("Saving room password");
3024 empathy_keyring_set_room_password_async (priv->account,
3025 empathy_tp_chat_get_id (priv->tp_chat),
3030 gtk_widget_destroy (info_bar);
3031 g_free (data->password);
3032 g_slice_free (PasswordData, data);
3036 chat_prompt_to_save_password (EmpathyChat *self,
3039 GtkWidget *content_area;
3046 /* save the password in case it needs to be saved */
3047 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3049 /* Remove all previous widgets */
3050 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3051 gtk_container_forall (GTK_CONTAINER (content_area),
3052 (GtkCallback) gtk_widget_destroy, NULL);
3053 data->button = NULL;
3056 data->spinner = NULL;
3058 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3059 GTK_MESSAGE_QUESTION);
3061 hbox = gtk_hbox_new (FALSE, 5);
3062 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3065 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3066 GTK_ICON_SIZE_DIALOG);
3067 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3070 label = gtk_label_new (_("Would you like to store this password?"));
3071 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3073 /* Add 'Remember' button */
3074 alig = gtk_alignment_new (0, 0.5, 1, 0);
3076 button = gtk_button_new_with_label (_("Remember"));
3077 gtk_container_add (GTK_CONTAINER (alig), button);
3078 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3080 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3083 /* Add 'Not now' button */
3084 alig = gtk_alignment_new (0, 0.5, 1, 0);
3086 button = gtk_button_new_with_label (_("Not now"));
3087 gtk_container_add (GTK_CONTAINER (alig), button);
3088 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3090 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3094 g_signal_handler_disconnect (data->info_bar, data->response_id);
3095 g_signal_connect (data->info_bar, "response",
3096 G_CALLBACK (remember_password_infobar_response_cb), data);
3098 gtk_widget_show_all (data->info_bar);
3102 provide_password_cb (GObject *tp_chat,
3106 PasswordData *data = user_data;
3107 EmpathyChat *self = data->self;
3108 EmpathyChatPriv *priv = GET_PRIV (self);
3109 GError *error = NULL;
3111 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3113 DEBUG ("error: %s", error->message);
3114 /* FIXME: what should we do if that's another error? Close the channel?
3115 * Display the raw D-Bus error to the user isn't very useful */
3116 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3118 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3119 gtk_widget_set_sensitive (data->entry, TRUE);
3120 gtk_widget_grab_focus (data->entry);
3123 gtk_info_bar_set_message_type (
3124 GTK_INFO_BAR (data->info_bar),
3128 gtk_widget_set_sensitive (data->button, TRUE);
3129 gtk_button_set_label (GTK_BUTTON (data->button),
3133 gtk_label_set_text (GTK_LABEL (data->label),
3134 _("Wrong password; please try again:"));
3137 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3138 gtk_widget_hide (data->spinner);
3140 g_error_free (error);
3144 if (empathy_keyring_is_available ()) {
3145 /* ask whether they want to save the password */
3146 chat_prompt_to_save_password (self, data);
3148 /* Get rid of the password info bar finally */
3149 gtk_widget_destroy (data->info_bar);
3150 g_slice_free (PasswordData, data);
3154 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3155 gtk_widget_grab_focus (self->input_text_view);
3159 password_infobar_response_cb (GtkWidget *info_bar,
3163 EmpathyChatPriv *priv = GET_PRIV (data->self);
3164 const gchar *password;
3166 if (response_id != GTK_RESPONSE_OK) {
3167 gtk_widget_destroy (info_bar);
3168 g_slice_free (PasswordData, data);
3172 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3174 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3175 provide_password_cb, data);
3177 gtk_widget_set_sensitive (data->button, FALSE);
3178 gtk_widget_set_sensitive (data->entry, FALSE);
3180 gtk_spinner_start (GTK_SPINNER (data->spinner));
3181 gtk_widget_show (data->spinner);
3185 password_entry_activate_cb (GtkWidget *entry,
3188 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3192 passwd_join_button_cb (GtkButton *button,
3195 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3199 clear_icon_released_cb (GtkEntry *entry,
3200 GtkEntryIconPosition icon_pos,
3204 gtk_entry_set_text (entry, "");
3208 password_entry_changed_cb (GtkEditable *entry,
3213 str = gtk_entry_get_text (GTK_ENTRY (entry));
3215 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3216 GTK_ENTRY_ICON_SECONDARY, !EMP_STR_EMPTY (str));
3220 display_password_info_bar (EmpathyChat *self)
3222 EmpathyChatPriv *priv = GET_PRIV (self);
3223 GtkWidget *info_bar;
3224 GtkWidget *content_area;
3234 data = g_slice_new0 (PasswordData);
3236 info_bar = gtk_info_bar_new ();
3237 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3238 GTK_MESSAGE_QUESTION);
3240 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3242 hbox = gtk_hbox_new (FALSE, 5);
3243 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3246 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3247 GTK_ICON_SIZE_DIALOG);
3248 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3251 label = gtk_label_new (_("This room is protected by a password:"));
3252 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3254 /* Add password entry */
3255 entry = gtk_entry_new ();
3256 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3257 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3259 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3260 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3261 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3262 GTK_ENTRY_ICON_SECONDARY, FALSE);
3264 g_signal_connect (entry, "icon-release",
3265 G_CALLBACK (clear_icon_released_cb), data);
3266 g_signal_connect (entry, "changed",
3267 G_CALLBACK (password_entry_changed_cb), data);
3269 g_signal_connect (entry, "activate",
3270 G_CALLBACK (password_entry_activate_cb), data);
3272 /* Focus the password entry once it's realized */
3273 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3275 /* Add 'Join' button */
3276 alig = gtk_alignment_new (0, 0.5, 1, 0);
3278 button = gtk_button_new_with_label (_("Join"));
3279 gtk_container_add (GTK_CONTAINER (alig), button);
3280 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3282 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3286 spinner = gtk_spinner_new ();
3287 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
3289 /* Save some data for messing around with later */
3291 data->info_bar = info_bar;
3292 data->button = button;
3293 data->label = label;
3294 data->entry = entry;
3295 data->spinner = spinner;
3297 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3299 gtk_widget_show_all (hbox);
3301 data->response_id = g_signal_connect (info_bar, "response",
3302 G_CALLBACK (password_infobar_response_cb), data);
3304 gtk_widget_show_all (info_bar);
3305 /* ... but hide the spinner */
3306 gtk_widget_hide (spinner);
3310 provide_saved_password_cb (GObject *tp_chat,
3314 EmpathyChat *self = user_data;
3315 EmpathyChatPriv *priv = GET_PRIV (self);
3316 GError *error = NULL;
3318 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
3320 DEBUG ("error: %s", error->message);
3321 /* FIXME: what should we do if that's another error? Close the channel?
3322 * Display the raw D-Bus error to the user isn't very useful */
3323 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED)) {
3324 display_password_info_bar (self);
3325 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3327 g_error_free (error);
3332 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3333 gtk_widget_grab_focus (self->input_text_view);
3337 chat_room_got_password_cb (GObject *source,
3338 GAsyncResult *result,
3341 EmpathyChat *self = user_data;
3342 EmpathyChatPriv *priv = GET_PRIV (self);
3343 const gchar *password;
3344 GError *error = NULL;
3346 password = empathy_keyring_get_room_password_finish (priv->account,
3349 if (error != NULL) {
3350 DEBUG ("Couldn't get room password: %s\n", error->message);
3351 g_clear_error (&error);
3353 display_password_info_bar (self);
3354 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3358 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
3359 provide_saved_password_cb, self);
3363 chat_password_needed_changed_cb (EmpathyChat *self)
3365 EmpathyChatPriv *priv = GET_PRIV (self);
3367 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3368 empathy_keyring_get_room_password_async (priv->account,
3369 empathy_tp_chat_get_id (priv->tp_chat),
3370 chat_room_got_password_cb, self);
3375 empathy_chat_set_tp_chat (EmpathyChat *chat,
3376 EmpathyTpChat *tp_chat)
3378 EmpathyChatPriv *priv = GET_PRIV (chat);
3379 GPtrArray *properties;
3381 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3382 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3383 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3385 if (priv->tp_chat) {
3389 if (priv->account) {
3390 g_object_unref (priv->account);
3393 priv->tp_chat = g_object_ref (tp_chat);
3394 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
3396 g_signal_connect (tp_chat, "destroy",
3397 G_CALLBACK (chat_destroy_cb),
3399 g_signal_connect (tp_chat, "message-received",
3400 G_CALLBACK (chat_message_received_cb),
3402 g_signal_connect (tp_chat, "send-error",
3403 G_CALLBACK (chat_send_error_cb),
3405 g_signal_connect (tp_chat, "chat-state-changed",
3406 G_CALLBACK (chat_state_changed_cb),
3408 g_signal_connect (tp_chat, "property-changed",
3409 G_CALLBACK (chat_property_changed_cb),
3411 g_signal_connect (tp_chat, "members-changed",
3412 G_CALLBACK (chat_members_changed_cb),
3414 g_signal_connect (tp_chat, "member-renamed",
3415 G_CALLBACK (chat_member_renamed_cb),
3417 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3418 G_CALLBACK (chat_remote_contact_changed_cb),
3420 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3421 G_CALLBACK (chat_password_needed_changed_cb),
3424 /* Get initial value of properties */
3425 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3426 if (properties != NULL) {
3429 for (i = 0; i < properties->len; i++) {
3430 EmpathyTpChatProperty *property;
3432 property = g_ptr_array_index (properties, i);
3433 if (property->value == NULL)
3436 chat_property_changed_cb (priv->tp_chat,
3443 chat_remote_contact_changed_cb (chat);
3445 if (chat->input_text_view) {
3446 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3447 if (priv->block_events_timeout_id == 0) {
3448 empathy_chat_view_append_event (chat->view, _("Connected"));
3452 g_object_notify (G_OBJECT (chat), "tp-chat");
3453 g_object_notify (G_OBJECT (chat), "id");
3454 g_object_notify (G_OBJECT (chat), "account");
3456 /* This is a noop when tp-chat is set at object construction time and causes
3457 * the pending messages to be show when it's set on the object after it has
3459 show_pending_messages (chat);
3461 /* check if a password is needed */
3462 chat_password_needed_changed_cb (chat);
3466 empathy_chat_get_account (EmpathyChat *chat)
3468 EmpathyChatPriv *priv = GET_PRIV (chat);
3470 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3472 return priv->account;
3476 empathy_chat_get_id (EmpathyChat *chat)
3478 EmpathyChatPriv *priv = GET_PRIV (chat);
3480 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3486 empathy_chat_get_name (EmpathyChat *chat)
3488 EmpathyChatPriv *priv = GET_PRIV (chat);
3491 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3494 if (!ret && priv->remote_contact) {
3495 ret = empathy_contact_get_alias (priv->remote_contact);
3501 return ret ? ret : _("Conversation");
3505 empathy_chat_get_subject (EmpathyChat *chat)
3507 EmpathyChatPriv *priv = GET_PRIV (chat);
3509 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3511 return priv->subject;
3515 empathy_chat_get_remote_contact (EmpathyChat *chat)
3517 EmpathyChatPriv *priv = GET_PRIV (chat);
3519 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3521 return priv->remote_contact;
3525 empathy_chat_get_contact_menu (EmpathyChat *chat)
3527 EmpathyChatPriv *priv = GET_PRIV (chat);
3528 GtkWidget *menu = NULL;
3530 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3532 if (priv->remote_contact) {
3533 menu = empathy_contact_menu_new (priv->remote_contact,
3534 EMPATHY_CONTACT_FEATURE_CALL |
3535 EMPATHY_CONTACT_FEATURE_LOG |
3536 EMPATHY_CONTACT_FEATURE_INFO);
3543 empathy_chat_clear (EmpathyChat *chat)
3545 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3547 empathy_chat_view_clear (chat->view);
3551 empathy_chat_scroll_down (EmpathyChat *chat)
3553 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3555 empathy_chat_view_scroll_down (chat->view);
3559 empathy_chat_cut (EmpathyChat *chat)
3561 GtkTextBuffer *buffer;
3563 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3565 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3566 if (gtk_text_buffer_get_has_selection (buffer)) {
3567 GtkClipboard *clipboard;
3569 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3571 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3576 empathy_chat_copy (EmpathyChat *chat)
3578 GtkTextBuffer *buffer;
3580 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3582 if (empathy_chat_view_get_has_selection (chat->view)) {
3583 empathy_chat_view_copy_clipboard (chat->view);
3587 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3588 if (gtk_text_buffer_get_has_selection (buffer)) {
3589 GtkClipboard *clipboard;
3591 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3593 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3598 EmpathyChatPriv *priv = GET_PRIV (chat);
3600 if (gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
3607 GtkClipboard *clipboard;
3609 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
3610 start = g_utf8_offset_to_pointer (topic, start_offset);
3611 end = g_utf8_offset_to_pointer (topic, end_offset);
3612 selection = g_strndup (start, end - start);
3614 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3615 gtk_clipboard_set_text (clipboard, selection, -1);
3623 empathy_chat_paste (EmpathyChat *chat)
3625 GtkTextBuffer *buffer;
3626 GtkClipboard *clipboard;
3627 EmpathyChatPriv *priv;
3629 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3631 priv = GET_PRIV (chat);
3633 if (gtk_widget_get_visible (priv->search_bar)) {
3634 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
3638 if (priv->tp_chat == NULL ||
3639 !gtk_widget_is_sensitive (chat->input_text_view))
3642 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3643 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3645 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3649 empathy_chat_find (EmpathyChat *chat)
3651 EmpathyChatPriv *priv;
3653 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3655 priv = GET_PRIV (chat);
3657 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3661 empathy_chat_correct_word (EmpathyChat *chat,
3664 const gchar *new_word)
3666 GtkTextBuffer *buffer;
3668 g_return_if_fail (chat != NULL);
3669 g_return_if_fail (new_word != NULL);
3671 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3673 gtk_text_buffer_delete (buffer, start, end);
3674 gtk_text_buffer_insert (buffer, start,
3680 empathy_chat_is_room (EmpathyChat *chat)
3682 EmpathyChatPriv *priv = GET_PRIV (chat);
3684 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3686 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3690 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3692 EmpathyChatPriv *priv = GET_PRIV (self);
3694 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3696 return priv->unread_messages;
3699 /* called when the messages have been read by user */
3701 empathy_chat_messages_read (EmpathyChat *self)
3703 EmpathyChatPriv *priv = GET_PRIV (self);
3705 g_return_if_fail (EMPATHY_IS_CHAT (self));
3707 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3708 * definition. If we are still retrieving the backlogs, do not ACK */
3709 if (priv->retrieving_backlogs)
3712 if (priv->tp_chat != NULL ) {
3713 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3715 priv->unread_messages = 0;
3718 /* Return TRUE if on of the contacts in this chat is composing */
3720 empathy_chat_is_composing (EmpathyChat *chat)
3722 return chat->priv->compositors != NULL;