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-2008 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 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n-lib.h>
37 #include <telepathy-glib/account-manager.h>
38 #include <telepathy-glib/util.h>
40 #include <telepathy-logger/log-manager.h>
43 #include <libempathy/empathy-log-manager.h>
44 #endif /* ENABLE_TPL */
45 #include <libempathy/empathy-contact-list.h>
46 #include <libempathy/empathy-gsettings.h>
47 #include <libempathy/empathy-utils.h>
48 #include <libempathy/empathy-dispatcher.h>
50 #include "empathy-chat.h"
51 #include "empathy-spell.h"
52 #include "empathy-contact-list-store.h"
53 #include "empathy-contact-list-view.h"
54 #include "empathy-contact-menu.h"
55 #include "empathy-search-bar.h"
56 #include "empathy-theme-manager.h"
57 #include "empathy-smiley-manager.h"
58 #include "empathy-ui-utils.h"
59 #include "empathy-string-parser.h"
61 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
62 #include <libempathy/empathy-debug.h>
64 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
65 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
66 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
67 #define MAX_INPUT_HEIGHT 150
68 #define COMPOSING_STOP_TIMEOUT 5
70 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
72 EmpathyTpChat *tp_chat;
77 EmpathyContact *remote_contact;
78 gboolean show_contacts;
80 GSettings *gsettings_chat;
81 GSettings *gsettings_ui;
84 TplLogManager *log_manager;
86 EmpathyLogManager *log_manager;
87 #endif /* ENABLE_TPL */
88 TpAccountManager *account_manager;
90 GList *input_history_current;
92 GCompletion *completion;
93 guint composing_stop_timeout_id;
94 guint block_events_timeout_id;
95 TpHandleType handle_type;
97 gboolean has_input_vscroll;
100 /* TRUE if spell checking is enabled, FALSE otherwise.
101 * This is to keep track of the last state of spell checking
102 * when it changes. */
103 gboolean spell_checking_enabled;
105 /* These store the signal handler ids for the enclosed text entry. */
106 gulong insert_text_id;
107 gulong delete_range_id;
108 gulong notify_cursor_position_id;
112 GtkWidget *vbox_left;
113 GtkWidget *scrolled_window_chat;
114 GtkWidget *scrolled_window_input;
115 GtkWidget *scrolled_window_contacts;
116 GtkWidget *hbox_topic;
117 GtkWidget *expander_topic;
118 GtkWidget *label_topic;
119 GtkWidget *contact_list_view;
120 GtkWidget *info_bar_vbox;
121 GtkWidget *search_bar;
123 guint unread_messages;
124 /* TRUE if the pending messages can be displayed. This is to avoid to show
125 * pending messages *before* messages from logs. (#603980) */
126 gboolean can_show_pending;
128 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
129 * be differently handled since it introduces another race condition, which
130 * is really hard to occur, but still possible.
132 * With the current workaround (which has the race above), we need to be
133 * sure to ACK any pending messages only when the retrieval of backlogs is
134 * finished, that's why using retrieving_backlogs flag.
135 * empathy_chat_messages_read () will check this variable and not ACK
136 * anything when TRUE. It will be set TRUE at chat_constructed () and set
137 * back to FALSE when the backlog has been retrieved and the pending
138 * messages actually showed to the user.
140 * Race condition introduced with this workaround:
141 * Scenario: a message is pending, the user is notified and selects the tab.
142 * the tab with a pending message is focused before the messages are properly
143 * shown (since the preparation of the window is slower AND async WRT the
144 * tab showing), which means the user won't see any new messages (rare but
145 * possible), if he/she will change tab focus before the messages are
146 * properly shown, the tab will be set as 'seen' and the user won't be
147 * notified again about the already notified pending messages when the
148 * messages in tab will be properly shown */
149 gboolean retrieving_backlogs;
153 gchar *text; /* Original message that was specified
154 * upon entry creation. */
155 gchar *modified_text; /* Message that was modified by user.
156 * When no modifications were made, it is NULL */
176 static guint signals[LAST_SIGNAL] = { 0 };
178 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
181 chat_get_property (GObject *object,
186 EmpathyChat *chat = EMPATHY_CHAT (object);
187 EmpathyChatPriv *priv = GET_PRIV (object);
191 g_value_set_object (value, priv->tp_chat);
194 g_value_set_object (value, priv->account);
197 g_value_set_string (value, empathy_chat_get_name (chat));
200 g_value_set_string (value, priv->id);
203 g_value_set_string (value, priv->subject);
205 case PROP_REMOTE_CONTACT:
206 g_value_set_object (value, priv->remote_contact);
208 case PROP_SHOW_CONTACTS:
209 g_value_set_boolean (value, priv->show_contacts);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
218 chat_set_property (GObject *object,
223 EmpathyChat *chat = EMPATHY_CHAT (object);
227 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
229 case PROP_SHOW_CONTACTS:
230 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
239 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
243 EmpathyChat *chat = EMPATHY_CHAT (user_data);
244 EmpathyTpChat *tpchat;
247 empathy_chat_view_append_event (chat->view,
248 _("Failed to reconnect this chat"));
252 tpchat = EMPATHY_TP_CHAT (
253 empathy_dispatch_operation_get_channel_wrapper (dispatch));
255 if (empathy_dispatch_operation_claim (dispatch)) {
256 empathy_chat_set_tp_chat (chat, tpchat);
261 reconnected_connection_ready_cb (TpConnection *connection,
265 EmpathyChat *chat = user_data;
266 EmpathyChatPriv *priv = GET_PRIV (chat);
269 DEBUG ("connection is not ready: %s", error->message);
273 DEBUG ("Account reconnected, request a new Text channel");
275 switch (priv->handle_type) {
276 case TP_HANDLE_TYPE_CONTACT:
277 empathy_dispatcher_chat_with_contact_id (
278 connection, priv->id, EMPATHY_DISPATCHER_NON_USER_ACTION,
279 chat_connect_channel_reconnected,
282 case TP_HANDLE_TYPE_ROOM:
283 empathy_dispatcher_join_muc (connection,
284 priv->id, EMPATHY_DISPATCHER_NON_USER_ACTION,
285 chat_connect_channel_reconnected,
289 g_assert_not_reached ();
294 g_object_unref (chat);
298 chat_new_connection_cb (TpAccount *account,
302 gchar *dbus_error_name,
306 EmpathyChatPriv *priv = GET_PRIV (chat);
307 TpConnection *connection;
309 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
312 connection = tp_account_get_connection (account);
314 if (priv->tp_chat != NULL || account != priv->account ||
315 priv->handle_type == TP_HANDLE_TYPE_NONE ||
316 EMP_STR_EMPTY (priv->id))
320 tp_connection_call_when_ready (connection, reconnected_connection_ready_cb,
325 chat_composing_remove_timeout (EmpathyChat *chat)
327 EmpathyChatPriv *priv;
329 priv = GET_PRIV (chat);
331 if (priv->composing_stop_timeout_id) {
332 g_source_remove (priv->composing_stop_timeout_id);
333 priv->composing_stop_timeout_id = 0;
338 chat_composing_stop_timeout_cb (EmpathyChat *chat)
340 EmpathyChatPriv *priv;
342 priv = GET_PRIV (chat);
344 priv->composing_stop_timeout_id = 0;
345 empathy_tp_chat_set_state (priv->tp_chat,
346 TP_CHANNEL_CHAT_STATE_PAUSED);
352 chat_composing_start (EmpathyChat *chat)
354 EmpathyChatPriv *priv;
356 priv = GET_PRIV (chat);
358 if (priv->composing_stop_timeout_id) {
359 /* Just restart the timeout */
360 chat_composing_remove_timeout (chat);
362 empathy_tp_chat_set_state (priv->tp_chat,
363 TP_CHANNEL_CHAT_STATE_COMPOSING);
366 priv->composing_stop_timeout_id = g_timeout_add_seconds (
367 COMPOSING_STOP_TIMEOUT,
368 (GSourceFunc) chat_composing_stop_timeout_cb,
373 chat_composing_stop (EmpathyChat *chat)
375 EmpathyChatPriv *priv;
377 priv = GET_PRIV (chat);
379 chat_composing_remove_timeout (chat);
380 empathy_tp_chat_set_state (priv->tp_chat,
381 TP_CHANNEL_CHAT_STATE_ACTIVE);
385 chat_input_history_entry_cmp (InputHistoryEntry *entry,
388 if (!tp_strdiff (entry->text, text)) {
389 if (entry->modified_text != NULL) {
390 /* Modified entry and single string cannot be equal. */
398 static InputHistoryEntry *
399 chat_input_history_entry_new_with_text (const gchar *text)
401 InputHistoryEntry *entry;
402 entry = g_slice_new0 (InputHistoryEntry);
403 entry->text = g_strdup (text);
409 chat_input_history_entry_free (InputHistoryEntry *entry)
411 g_free (entry->text);
412 g_free (entry->modified_text);
413 g_slice_free (InputHistoryEntry, entry);
417 chat_input_history_entry_revert (InputHistoryEntry *entry)
419 g_free (entry->modified_text);
420 entry->modified_text = NULL;
424 chat_input_history_entry_update_text (InputHistoryEntry *entry,
429 if (!tp_strdiff (text, entry->text)) {
430 g_free (entry->modified_text);
431 entry->modified_text = NULL;
435 old = entry->modified_text;
436 entry->modified_text = g_strdup (text);
441 chat_input_history_entry_get_text (InputHistoryEntry *entry)
447 if (entry->modified_text != NULL) {
448 return entry->modified_text;
454 chat_input_history_remove_item (GList *list,
457 list = g_list_remove_link (list, item);
458 chat_input_history_entry_free (item->data);
459 g_list_free_1 (item);
464 chat_input_history_revert (EmpathyChat *chat)
466 EmpathyChatPriv *priv;
470 InputHistoryEntry *entry;
472 priv = GET_PRIV (chat);
473 list = priv->input_history;
476 DEBUG ("No input history");
480 /* Delete temporary entry */
481 if (priv->input_history_current != NULL) {
483 list = chat_input_history_remove_item (list, item1);
484 if (priv->input_history_current == item1) {
485 /* Removed temporary entry was current entry */
486 priv->input_history = list;
487 priv->input_history_current = NULL;
492 /* There is no entry to revert */
496 /* Restore the current history entry to original value */
497 item1 = priv->input_history_current;
499 chat_input_history_entry_revert (entry);
501 /* Remove restored entry if there is other occurance before this entry */
502 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
503 (GCompareFunc) chat_input_history_entry_cmp);
504 if (item2 != item1) {
505 list = chat_input_history_remove_item (list, item1);
508 /* Remove other occurance of the restored entry */
509 item2 = g_list_find_custom (item1->next,
510 chat_input_history_entry_get_text (entry),
511 (GCompareFunc) chat_input_history_entry_cmp);
513 list = chat_input_history_remove_item (list, item2);
517 priv->input_history_current = NULL;
518 priv->input_history = list;
522 chat_input_history_add (EmpathyChat *chat,
526 EmpathyChatPriv *priv;
529 InputHistoryEntry *entry;
531 priv = GET_PRIV (chat);
533 list = priv->input_history;
535 /* Remove any other occurances of this entry, if not temporary */
537 while ((item = g_list_find_custom (list, str,
538 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
539 list = chat_input_history_remove_item (list, item);
542 /* Trim the list to the last 10 items */
543 while (g_list_length (list) > 10) {
544 item = g_list_last (list);
546 list = chat_input_history_remove_item (list, item);
554 entry = chat_input_history_entry_new_with_text (str);
555 list = g_list_prepend (list, entry);
557 /* Set the list and the current item pointer */
558 priv->input_history = list;
560 priv->input_history_current = list;
563 priv->input_history_current = NULL;
568 chat_input_history_get_next (EmpathyChat *chat)
570 EmpathyChatPriv *priv;
574 priv = GET_PRIV (chat);
576 if (priv->input_history == NULL) {
577 DEBUG ("No input history, next entry is NULL");
580 g_assert (priv->input_history_current != NULL);
582 if ((item = g_list_next (priv->input_history_current)) == NULL)
584 item = priv->input_history_current;
587 msg = chat_input_history_entry_get_text (item->data);
589 DEBUG ("Returning next entry: '%s'", msg);
591 priv->input_history_current = item;
597 chat_input_history_get_prev (EmpathyChat *chat)
599 EmpathyChatPriv *priv;
603 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
605 priv = GET_PRIV (chat);
607 if (priv->input_history == NULL) {
608 DEBUG ("No input history, previous entry is NULL");
612 if (priv->input_history_current == NULL)
616 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
618 item = priv->input_history_current;
621 msg = chat_input_history_entry_get_text (item->data);
623 DEBUG ("Returning previous entry: '%s'", msg);
625 priv->input_history_current = item;
631 chat_input_history_update (EmpathyChat *chat,
632 GtkTextBuffer *buffer)
634 EmpathyChatPriv *priv;
635 GtkTextIter start, end;
637 InputHistoryEntry *entry;
639 priv = GET_PRIV (chat);
641 gtk_text_buffer_get_bounds (buffer, &start, &end);
642 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
644 if (priv->input_history_current == NULL) {
645 /* Add the current text temporarily to the history */
646 chat_input_history_add (chat, text, TRUE);
651 /* Save the changes in the history */
652 entry = priv->input_history_current->data;
653 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
654 chat_input_history_entry_update_text (entry, text);
661 chat_command_join_cb (EmpathyDispatchOperation *dispatch,
665 EmpathyChat *chat = user_data;
668 DEBUG ("Error: %s", error->message);
669 empathy_chat_view_append_event (chat->view,
670 _("Failed to join chat room"));
677 } ChatCommandMsgData;
680 chat_command_msg_cb (EmpathyDispatchOperation *dispatch,
684 ChatCommandMsgData *data = user_data;
687 empathy_chat_view_append_event (data->chat->view,
688 _("Failed to open private chat"));
692 if (!EMP_STR_EMPTY (data->message)) {
693 EmpathyTpChat *tpchat;
694 EmpathyMessage *message;
696 tpchat = EMPATHY_TP_CHAT (
697 empathy_dispatch_operation_get_channel_wrapper (dispatch));
699 message = empathy_message_new (data->message);
700 empathy_tp_chat_send (tpchat, message);
701 g_object_unref (message);
705 g_free (data->message);
706 g_slice_free (ChatCommandMsgData, data);
710 chat_command_clear (EmpathyChat *chat,
713 empathy_chat_view_clear (chat->view);
717 chat_command_topic (EmpathyChat *chat,
720 EmpathyChatPriv *priv = GET_PRIV (chat);
721 EmpathyTpChatProperty *property;
722 GValue value = {0, };
724 property = empathy_tp_chat_get_property (priv->tp_chat, "subject");
725 if (property == NULL) {
726 empathy_chat_view_append_event (chat->view,
727 _("Topic not supported on this conversation"));
731 if (!(property->flags & TP_PROPERTY_FLAG_WRITE)) {
732 empathy_chat_view_append_event (chat->view,
733 _("You are not allowed to change the topic"));
737 g_value_init (&value, G_TYPE_STRING);
738 g_value_set_string (&value, strv[1]);
739 empathy_tp_chat_set_property (priv->tp_chat, "subject", &value);
740 g_value_unset (&value);
744 chat_command_join (EmpathyChat *chat,
748 EmpathyChatPriv *priv = GET_PRIV (chat);
750 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
752 while (rooms[i] != NULL) {
753 /* ignore empty strings */
754 if (!EMP_STR_EMPTY (rooms[i])) {
755 TpConnection *connection;
757 connection = empathy_tp_chat_get_connection (priv->tp_chat);
758 empathy_dispatcher_join_muc (connection, rooms[i],
759 gtk_get_current_event_time (),
760 chat_command_join_cb,
769 chat_command_msg_internal (EmpathyChat *chat,
770 const gchar *contact_id,
771 const gchar *message)
773 EmpathyChatPriv *priv = GET_PRIV (chat);
774 TpConnection *connection;
775 ChatCommandMsgData *data;
777 /* FIXME: We should probably search in members alias. But this
778 * is enough for IRC */
779 data = g_slice_new (ChatCommandMsgData);
781 data->message = g_strdup (message);
782 connection = empathy_tp_chat_get_connection (priv->tp_chat);
783 empathy_dispatcher_chat_with_contact_id (connection, contact_id,
784 gtk_get_current_event_time (),
790 chat_command_query (EmpathyChat *chat,
793 /* If <message> part is not defined,
794 * strv[2] will be the terminal NULL */
795 chat_command_msg_internal (chat, strv[1], strv[2]);
799 chat_command_msg (EmpathyChat *chat,
802 chat_command_msg_internal (chat, strv[1], strv[2]);
806 chat_command_nick (EmpathyChat *chat,
809 EmpathyChatPriv *priv = GET_PRIV (chat);
810 TpConnection *connection;
811 GHashTable *new_alias;
814 connection = tp_account_get_connection (priv->account);
815 handle = tp_connection_get_self_handle (connection);
816 new_alias = g_hash_table_new (g_direct_hash, g_direct_equal);
817 g_hash_table_insert (new_alias, GUINT_TO_POINTER (handle), strv[1]);
819 tp_cli_connection_interface_aliasing_call_set_aliases (connection, -1,
820 new_alias, NULL, NULL, NULL, NULL);
822 g_hash_table_destroy (new_alias);
826 chat_command_me (EmpathyChat *chat,
829 EmpathyChatPriv *priv = GET_PRIV (chat);
830 EmpathyMessage *message;
832 message = empathy_message_new (strv[1]);
833 empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
834 empathy_tp_chat_send (priv->tp_chat, message);
835 g_object_unref (message);
839 chat_command_say (EmpathyChat *chat,
842 EmpathyChatPriv *priv = GET_PRIV (chat);
843 EmpathyMessage *message;
845 message = empathy_message_new (strv[1]);
846 empathy_tp_chat_send (priv->tp_chat, message);
847 g_object_unref (message);
850 static void chat_command_help (EmpathyChat *chat, GStrv strv);
852 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
858 ChatCommandFunc func;
862 static ChatCommandItem commands[] = {
863 {"clear", 1, 1, chat_command_clear,
864 N_("/clear: clear all messages from the current conversation")},
866 {"topic", 2, 2, chat_command_topic,
867 N_("/topic <topic>: set the topic of the current conversation")},
869 {"join", 2, 2, chat_command_join,
870 N_("/join <chat room ID>: join a new chat room")},
872 {"j", 2, 2, chat_command_join,
873 N_("/j <chat room ID>: join a new chat room")},
875 {"query", 2, 3, chat_command_query,
876 N_("/query <contact ID> [<message>]: open a private chat")},
878 {"msg", 3, 3, chat_command_msg,
879 N_("/msg <contact ID> <message>: open a private chat")},
881 {"nick", 2, 2, chat_command_nick,
882 N_("/nick <nickname>: change your nickname on the current server")},
884 {"me", 2, 2, chat_command_me,
885 N_("/me <message>: send an ACTION message to the current conversation")},
887 {"say", 2, 2, chat_command_say,
888 N_("/say <message>: send <message> to the current conversation. "
889 "This is used to send a message starting with a '/'. For example: "
890 "\"/say /join is used to join a new chat room\"")},
892 {"help", 1, 2, chat_command_help,
893 N_("/help [<command>]: show all supported commands. "
894 "If <command> is defined, show its usage.")},
898 chat_command_show_help (EmpathyChat *chat,
899 ChatCommandItem *item)
903 str = g_strdup_printf (_("Usage: %s"), _(item->help));
904 empathy_chat_view_append_event (chat->view, str);
909 chat_command_help (EmpathyChat *chat,
914 /* If <command> part is not defined,
915 * strv[1] will be the terminal NULL */
916 if (strv[1] == NULL) {
917 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
918 empathy_chat_view_append_event (chat->view,
919 _(commands[i].help));
924 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
925 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
926 chat_command_show_help (chat, &commands[i]);
931 empathy_chat_view_append_event (chat->view,
932 _("Unknown command"));
936 chat_command_parse (const gchar *text, guint max_parts)
941 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
943 array = g_ptr_array_sized_new (max_parts + 1);
944 while (max_parts > 1) {
947 /* Skip white spaces */
948 while (g_ascii_isspace (*text)) {
952 /* Search the end of this part, until first space. */
953 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
959 item = g_strndup (text, end - text);
960 g_ptr_array_add (array, item);
961 DEBUG ("\tITEM: \"%s\"", item);
967 /* Append last part if not empty */
968 item = g_strstrip (g_strdup (text));
969 if (!EMP_STR_EMPTY (item)) {
970 g_ptr_array_add (array, item);
971 DEBUG ("\tITEM: \"%s\"", item);
976 /* Make the array NULL-terminated */
977 g_ptr_array_add (array, NULL);
979 return (GStrv) g_ptr_array_free (array, FALSE);
983 has_prefix_case (const gchar *s,
986 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
990 chat_send (EmpathyChat *chat,
993 EmpathyChatPriv *priv;
994 EmpathyMessage *message;
997 if (EMP_STR_EMPTY (msg)) {
1001 priv = GET_PRIV (chat);
1003 chat_input_history_add (chat, msg, FALSE);
1005 if (msg[0] == '/') {
1006 gboolean second_slash = FALSE;
1007 const gchar *iter = msg + 1;
1009 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1014 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1017 c = *(msg + 1 + strlen (commands[i].prefix));
1018 if (c != '\0' && !g_ascii_isspace (c)) {
1022 /* We can't use g_strsplit here because it does
1023 * not deal correctly if we have more than one space
1025 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1027 strv_len = g_strv_length (strv);
1028 if (strv_len < commands[i].min_parts ||
1029 strv_len > commands[i].max_parts) {
1030 chat_command_show_help (chat, &commands[i]);
1035 commands[i].func (chat, strv);
1040 /* Also allow messages with two slashes before the
1041 * first space, so it is possible to send a /unix/path.
1042 * This heuristic is kind of crap. */
1043 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1045 second_slash = TRUE;
1051 if (!second_slash) {
1052 empathy_chat_view_append_event (chat->view,
1053 _("Unknown command; see /help for the available"
1059 message = empathy_message_new (msg);
1060 empathy_tp_chat_send (priv->tp_chat, message);
1061 g_object_unref (message);
1065 chat_input_text_view_send (EmpathyChat *chat)
1067 EmpathyChatPriv *priv;
1068 GtkTextBuffer *buffer;
1069 GtkTextIter start, end;
1072 priv = GET_PRIV (chat);
1074 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1076 gtk_text_buffer_get_bounds (buffer, &start, &end);
1077 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1079 /* clear the input field */
1080 gtk_text_buffer_set_text (buffer, "", -1);
1081 /* delete input history modifications */
1082 chat_input_history_revert (chat);
1084 chat_send (chat, msg);
1089 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1090 EmpathyContact *contact,
1091 TpChannelChatState state,
1094 EmpathyChatPriv *priv;
1096 gboolean was_composing;
1098 priv = GET_PRIV (chat);
1100 if (empathy_contact_is_user (contact)) {
1101 /* We don't care about our own chat state */
1105 was_composing = (priv->compositors != NULL);
1107 /* Find the contact in the list. After that l is the list elem or NULL */
1108 for (l = priv->compositors; l; l = l->next) {
1109 if (contact == l->data) {
1115 case TP_CHANNEL_CHAT_STATE_GONE:
1116 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1117 case TP_CHANNEL_CHAT_STATE_PAUSED:
1118 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1119 /* Contact is not composing */
1121 priv->compositors = g_list_remove_link (priv->compositors, l);
1122 g_object_unref (l->data);
1126 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1127 /* Contact is composing */
1129 priv->compositors = g_list_prepend (priv->compositors,
1130 g_object_ref (contact));
1134 g_assert_not_reached ();
1137 DEBUG ("Was composing: %s now composing: %s",
1138 was_composing ? "yes" : "no",
1139 priv->compositors ? "yes" : "no");
1141 if ((was_composing && !priv->compositors) ||
1142 (!was_composing && priv->compositors)) {
1143 /* Composing state changed */
1144 g_signal_emit (chat, signals[COMPOSING], 0,
1145 priv->compositors != NULL);
1150 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
1152 EmpathyChatPriv *priv = GET_PRIV (chat);
1153 EmpathyContact *sender;
1155 sender = empathy_message_get_sender (message);
1157 DEBUG ("Appending new message from %s (%d)",
1158 empathy_contact_get_name (sender),
1159 empathy_contact_get_handle (sender));
1161 empathy_chat_view_append_message (chat->view, message);
1163 /* We received a message so the contact is no longer composing */
1164 chat_state_changed_cb (priv->tp_chat, sender,
1165 TP_CHANNEL_CHAT_STATE_ACTIVE,
1168 priv->unread_messages++;
1169 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
1173 chat_message_received_cb (EmpathyTpChat *tp_chat,
1174 EmpathyMessage *message,
1177 chat_message_received (chat, message);
1181 chat_send_error_cb (EmpathyTpChat *tp_chat,
1182 const gchar *message_body,
1183 TpChannelTextSendError error_code,
1189 switch (error_code) {
1190 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1191 error = _("offline");
1193 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1194 error = _("invalid contact");
1196 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1197 error = _("permission denied");
1199 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1200 error = _("too long message");
1202 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1203 error = _("not implemented");
1206 error = _("unknown");
1210 str = g_strdup_printf (_("Error sending message '%s': %s"),
1213 empathy_chat_view_append_event (chat->view, str);
1217 /* WARNING: EXPLICIT CONTENT, keep away childrens!
1219 * When a GtkLabel is set to wrap, it assume and hardcoded width. To change
1220 * that width we have to set a size request on the label... but that's not
1221 * possible because we want the window to be able to shrink, so we MUST request
1222 * width of 1. Note that the height of a wrapping label depends on its width.
1224 * To work around that, here is what happens:
1225 * 1) size-request is first called, an hardcoded small width is requested by
1226 * GtkLabel, which means also a too big height. We do nothing.
1227 * 2) size-allocate is called with the full width available, that's the width
1228 * we really want to make wrap the label. We save that width and restart a
1229 * size-request/size-allocate round.
1230 * 3) size-request is called a 2nd time, now we can tell the pango layout its
1231 * width (we can't do that in step 2 because GtkLabel::size-request recreate
1232 * the layout each time). When the layout has its width, we can know the
1233 * height of the label and set its requisition. The width request is set to
1234 * 1px to make sure the window can shrink, the layout will fill all the
1235 * available width anyway.
1238 chat_topic_label_size_request_cb (GtkLabel *label,
1239 GtkRequisition *requisition,
1242 EmpathyChatPriv *priv = GET_PRIV (chat);
1244 if (gtk_label_get_line_wrap (label) && priv->topic_width > 0) {
1245 PangoLayout *layout;
1246 PangoRectangle rect;
1249 layout = gtk_label_get_layout (label);
1250 pango_layout_set_width (layout, priv->topic_width * PANGO_SCALE);
1251 pango_layout_get_extents (layout, NULL, &rect);
1252 gtk_misc_get_padding (GTK_MISC (label), NULL, &ypad);
1254 requisition->width = 1;
1255 requisition->height = PANGO_PIXELS (rect.height) + ypad * 2;
1260 chat_topic_label_size_allocate_cb (GtkLabel *label,
1261 GtkAllocation *allocation,
1264 EmpathyChatPriv *priv = GET_PRIV (chat);
1266 if (!gtk_label_get_line_wrap (label)) {
1267 priv->topic_width = -1;
1269 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1270 gtk_widget_show (priv->expander_topic);
1272 gtk_widget_hide (priv->expander_topic);
1277 if (priv->topic_width != allocation->width) {
1278 priv->topic_width = allocation->width;
1279 gtk_widget_queue_resize (GTK_WIDGET (label));
1284 chat_topic_expander_activate_cb (GtkExpander *expander,
1285 GParamSpec *param_spec,
1288 EmpathyChatPriv *priv = GET_PRIV (chat);
1290 if (gtk_expander_get_expanded (expander)) {
1291 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1292 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1294 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1295 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1300 chat_property_changed_cb (EmpathyTpChat *tp_chat,
1305 EmpathyChatPriv *priv = GET_PRIV (chat);
1307 if (!tp_strdiff (name, "subject")) {
1308 g_free (priv->subject);
1309 priv->subject = g_value_dup_string (value);
1310 g_object_notify (G_OBJECT (chat), "subject");
1312 if (EMP_STR_EMPTY (priv->subject)) {
1313 gtk_widget_hide (priv->hbox_topic);
1315 gchar *markup_topic;
1318 markup_topic = empathy_add_link_markup (priv->subject);
1319 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1320 _("Topic:"), markup_topic);
1322 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1323 g_free (markup_text);
1324 g_free (markup_topic);
1326 gtk_widget_show (priv->hbox_topic);
1328 if (priv->block_events_timeout_id == 0) {
1331 if (!EMP_STR_EMPTY (priv->subject)) {
1332 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1334 str = g_strdup (_("No topic defined"));
1336 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
1340 else if (!tp_strdiff (name, "name")) {
1341 g_free (priv->name);
1342 priv->name = g_value_dup_string (value);
1343 g_object_notify (G_OBJECT (chat), "name");
1348 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1352 GtkTextIter word_start = *iter;
1353 GtkTextIter word_end = *iter;
1356 if (gtk_text_iter_inside_word (&word_end) &&
1357 !gtk_text_iter_ends_word (&word_end)) {
1358 gtk_text_iter_forward_word_end (&word_end);
1363 if (gtk_text_iter_get_char (&tmp) == '\'') {
1364 gtk_text_iter_forward_char (&tmp);
1366 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1367 gtk_text_iter_forward_word_end (&word_end);
1372 if (gtk_text_iter_inside_word (&word_start) ||
1373 gtk_text_iter_ends_word (&word_start)) {
1374 if (!gtk_text_iter_starts_word (&word_start) ||
1375 gtk_text_iter_equal (&word_start, &word_end)) {
1376 gtk_text_iter_backward_word_start (&word_start);
1380 gtk_text_iter_backward_char (&tmp);
1382 if (gtk_text_iter_get_char (&tmp) == '\'') {
1383 gtk_text_iter_backward_char (&tmp);
1385 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1386 gtk_text_iter_backward_word_start (&word_start);
1391 *start = word_start;
1397 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1398 GtkTextIter *location,
1403 GtkTextIter iter, pos;
1405 /* Remove all misspelled tags in the inserted text.
1406 * This happens when text is inserted within a misspelled word. */
1407 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1408 gtk_text_iter_get_offset (location) - len);
1409 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1412 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1415 GtkTextIter start, end;
1418 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1421 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1423 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1424 gtk_text_iter_equal (&pos, &end) ||
1425 empathy_spell_check (str)) {
1426 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1428 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1433 } while (gtk_text_iter_forward_word_end (&iter) &&
1434 gtk_text_iter_compare (&iter, location) <= 0);
1438 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1443 GtkTextIter word_start, word_end;
1445 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1446 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1447 &word_start, &word_end);
1452 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1455 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1456 chat_composing_stop (chat);
1458 chat_composing_start (chat);
1463 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1468 GtkTextIter prev_pos;
1469 GtkTextIter word_start;
1470 GtkTextIter word_end;
1474 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1476 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1477 gtk_text_buffer_get_insert (buffer));
1478 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1480 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1483 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1484 !gtk_text_iter_equal (&pos, &word_end)) {
1485 str = gtk_text_buffer_get_text (buffer,
1486 &word_start, &word_end, FALSE);
1488 if (!empathy_spell_check (str)) {
1489 gtk_text_buffer_apply_tag_by_name (buffer,
1490 "misspelled", &word_start, &word_end);
1492 gtk_text_buffer_remove_tag_by_name (buffer,
1493 "misspelled", &word_start, &word_end);
1500 gtk_text_buffer_move_mark (buffer, mark, &pos);
1504 empathy_isspace_cb (gunichar c,
1507 return g_unichar_isspace (c);
1511 chat_input_key_press_event_cb (GtkWidget *widget,
1515 EmpathyChatPriv *priv;
1518 GtkWidget *text_view_sw;
1520 priv = GET_PRIV (chat);
1522 /* Catch ctrl+up/down so we can traverse messages we sent */
1523 if ((event->state & GDK_CONTROL_MASK) &&
1524 (event->keyval == GDK_Up ||
1525 event->keyval == GDK_Down)) {
1526 GtkTextBuffer *buffer;
1529 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1530 chat_input_history_update (chat, buffer);
1532 if (event->keyval == GDK_Up) {
1533 str = chat_input_history_get_next (chat);
1535 str = chat_input_history_get_prev (chat);
1538 g_signal_handlers_block_by_func (buffer,
1539 chat_input_text_buffer_changed_cb,
1541 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1542 g_signal_handlers_unblock_by_func (buffer,
1543 chat_input_text_buffer_changed_cb,
1549 /* Catch enter but not ctrl/shift-enter */
1550 if (IS_ENTER (event->keyval) &&
1551 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1554 /* This is to make sure that kinput2 gets the enter. And if
1555 * it's handled there we shouldn't send on it. This is because
1556 * kinput2 uses Enter to commit letters. See:
1557 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1560 view = GTK_TEXT_VIEW (chat->input_text_view);
1561 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1562 gtk_text_view_reset_im_context (view);
1566 chat_input_text_view_send (chat);
1570 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1572 if (IS_ENTER (event->keyval) &&
1573 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1574 /* Newline for shift/control-enter. */
1577 if (!(event->state & GDK_CONTROL_MASK) &&
1578 event->keyval == GDK_Page_Up) {
1579 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1580 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1583 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1584 event->keyval == GDK_Page_Down) {
1585 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1586 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1587 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1588 gtk_adjustment_set_value (adj, val);
1591 if (event->keyval == GDK_Escape) {
1592 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1594 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1595 event->keyval == GDK_Tab) {
1596 GtkTextBuffer *buffer;
1597 GtkTextIter start, current;
1598 gchar *nick, *completed;
1599 GList *list, *completed_list;
1600 gboolean is_start_of_buffer;
1602 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1603 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
1605 /* Get the start of the nick to complete. */
1606 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1607 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1608 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1610 is_start_of_buffer = gtk_text_iter_is_start (&start);
1612 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
1613 g_completion_add_items (priv->completion, list);
1615 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
1616 completed_list = g_completion_complete (priv->completion,
1625 GString *message = NULL;
1628 gtk_text_buffer_delete (buffer, &start, ¤t);
1630 len = g_list_length (completed_list);
1633 /* If we only have one hit, use that text
1634 * instead of the text in completed since the
1635 * completed text will use the typed string
1636 * which might be cased all wrong.
1639 text = empathy_contact_get_name (completed_list->data);
1643 /* Print all hits to the scrollback view, so the
1644 * user knows what possibilities he has.
1647 message = g_string_new ("");
1648 for (l = completed_list; l != NULL; l = l->next) {
1649 g_string_append (message, empathy_contact_get_name (l->data));
1650 g_string_append (message, " - ");
1652 empathy_chat_view_append_event (chat->view, message->str);
1653 g_string_free (message, TRUE);
1656 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1658 if (len == 1 && is_start_of_buffer) {
1659 gchar *complete_char;
1661 complete_char = g_settings_get_string (
1662 priv->gsettings_chat,
1663 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
1665 if (complete_char != NULL) {
1666 gtk_text_buffer_insert_at_cursor (buffer,
1668 strlen (complete_char));
1669 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1670 g_free (complete_char);
1677 g_completion_clear_items (priv->completion);
1679 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1689 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1693 gtk_widget_grab_focus (chat->input_text_view);
1699 chat_input_set_size_request_idle (gpointer sw)
1701 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1707 chat_input_size_request_cb (GtkWidget *widget,
1708 GtkRequisition *requisition,
1711 EmpathyChatPriv *priv = GET_PRIV (chat);
1714 sw = gtk_widget_get_parent (widget);
1715 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1716 g_idle_add (chat_input_set_size_request_idle, sw);
1717 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1720 priv->has_input_vscroll = TRUE;
1723 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1724 gtk_widget_set_size_request (sw, -1, -1);
1725 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1728 priv->has_input_vscroll = FALSE;
1733 chat_input_realize_cb (GtkWidget *widget,
1736 DEBUG ("Setting focus to the input text view");
1737 if (gtk_widget_is_sensitive (widget)) {
1738 gtk_widget_grab_focus (widget);
1743 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1744 EmpathySmiley *smiley,
1747 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1748 GtkTextBuffer *buffer;
1751 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1753 gtk_text_buffer_get_end_iter (buffer, &iter);
1754 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1756 gtk_text_buffer_get_end_iter (buffer, &iter);
1757 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1768 static EmpathyChatSpell *
1769 chat_spell_new (EmpathyChat *chat,
1774 EmpathyChatSpell *chat_spell;
1776 chat_spell = g_slice_new0 (EmpathyChatSpell);
1778 chat_spell->chat = g_object_ref (chat);
1779 chat_spell->word = g_strdup (word);
1780 chat_spell->start = start;
1781 chat_spell->end = end;
1787 chat_spell_free (EmpathyChatSpell *chat_spell)
1789 g_object_unref (chat_spell->chat);
1790 g_free (chat_spell->word);
1791 g_slice_free (EmpathyChatSpell, chat_spell);
1795 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1796 EmpathyChatSpell *chat_spell)
1798 empathy_chat_correct_word (chat_spell->chat,
1799 &(chat_spell->start),
1801 gtk_menu_item_get_label (menu_item));
1805 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1807 GtkWidget *menu, *menu_item;
1808 GList *suggestions, *l;
1810 menu = gtk_menu_new ();
1811 suggestions = empathy_spell_get_suggestions (chat_spell->word);
1812 if (suggestions == NULL) {
1813 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1814 gtk_widget_set_sensitive (menu_item, FALSE);
1815 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1817 for (l = suggestions; l; l = l->next) {
1818 menu_item = gtk_menu_item_new_with_label (l->data);
1819 g_signal_connect (G_OBJECT (menu_item),
1821 G_CALLBACK (chat_spelling_menu_activate_cb),
1823 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1826 empathy_spell_free_suggestions (suggestions);
1828 gtk_widget_show_all (menu);
1834 chat_text_send_cb (GtkMenuItem *menuitem,
1837 chat_input_text_view_send (chat);
1841 chat_input_populate_popup_cb (GtkTextView *view,
1845 EmpathyChatPriv *priv;
1846 GtkTextBuffer *buffer;
1847 GtkTextTagTable *table;
1850 GtkTextIter iter, start, end;
1853 EmpathyChatSpell *chat_spell;
1854 GtkWidget *spell_menu;
1855 EmpathySmileyManager *smiley_manager;
1856 GtkWidget *smiley_menu;
1859 priv = GET_PRIV (chat);
1860 buffer = gtk_text_view_get_buffer (view);
1862 /* Add the emoticon menu. */
1863 item = gtk_separator_menu_item_new ();
1864 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1865 gtk_widget_show (item);
1867 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1868 image = gtk_image_new_from_icon_name ("face-smile",
1869 GTK_ICON_SIZE_MENU);
1870 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1871 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1872 gtk_widget_show (item);
1874 smiley_manager = empathy_smiley_manager_dup_singleton ();
1875 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1876 chat_insert_smiley_activate_cb,
1878 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1879 g_object_unref (smiley_manager);
1881 /* Add the Send menu item. */
1882 gtk_text_buffer_get_bounds (buffer, &start, &end);
1883 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1884 if (!EMP_STR_EMPTY (str)) {
1885 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1886 g_signal_connect (G_OBJECT (item), "activate",
1887 G_CALLBACK (chat_text_send_cb), chat);
1888 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1889 gtk_widget_show (item);
1893 /* Add the spell check menu item. */
1894 table = gtk_text_buffer_get_tag_table (buffer);
1895 tag = gtk_text_tag_table_lookup (table, "misspelled");
1896 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1897 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1898 GTK_TEXT_WINDOW_WIDGET,
1901 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1903 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1904 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1906 str = gtk_text_buffer_get_text (buffer,
1907 &start, &end, FALSE);
1909 if (!EMP_STR_EMPTY (str)) {
1910 chat_spell = chat_spell_new (chat, str, start, end);
1911 g_object_set_data_full (G_OBJECT (menu),
1912 "chat_spell", chat_spell,
1913 (GDestroyNotify) chat_spell_free);
1915 item = gtk_separator_menu_item_new ();
1916 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1917 gtk_widget_show (item);
1919 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1920 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1921 GTK_ICON_SIZE_MENU);
1922 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1924 spell_menu = chat_spelling_build_menu (chat_spell);
1925 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1927 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1928 gtk_widget_show (item);
1935 chat_log_filter (TplLogEntry *log,
1939 chat_log_filter (EmpathyMessage *message,
1941 #endif /* ENABLE_TPL */
1943 EmpathyChat *chat = user_data;
1945 EmpathyMessage *message;
1946 #endif /* ENABLE_TPL */
1947 EmpathyChatPriv *priv = GET_PRIV (chat);
1948 const GList *pending;
1951 g_return_val_if_fail (TPL_IS_LOG_ENTRY (log), FALSE);
1953 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1954 #endif /* ENABLE_TPL */
1955 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1957 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1959 message = empathy_message_from_tpl_log_entry (log);
1960 #endif /* ENABLE_TPL */
1962 for (; pending; pending = g_list_next (pending)) {
1963 if (empathy_message_equal (message, pending->data)) {
1968 g_object_unref (message);
1975 show_pending_messages (EmpathyChat *chat) {
1976 EmpathyChatPriv *priv = GET_PRIV (chat);
1977 const GList *messages, *l;
1979 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1981 if (chat->view == NULL || priv->tp_chat == NULL)
1984 if (!priv->can_show_pending)
1987 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1989 for (l = messages; l != NULL ; l = g_list_next (l)) {
1990 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1991 chat_message_received (chat, message);
1998 got_filtered_messages_cb (GObject *manager,
1999 GAsyncResult *result,
2004 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2005 EmpathyChatPriv *priv = GET_PRIV (chat);
2006 GError *error = NULL;
2008 if (!tpl_log_manager_get_filtered_messages_finish (TPL_LOG_MANAGER (manager),
2009 result, &messages, &error)) {
2010 DEBUG ("%s. Aborting.", error->message);
2011 empathy_chat_view_append_event (chat->view,
2012 _("Failed to retrieve recent logs"));
2013 g_error_free (error);
2017 for (l = messages; l; l = g_list_next (l)) {
2018 EmpathyMessage *message;
2019 g_assert (TPL_IS_LOG_ENTRY (l->data));
2021 message = empathy_message_from_tpl_log_entry (l->data);
2022 g_object_unref (l->data);
2024 empathy_chat_view_append_message (chat->view, message);
2025 g_object_unref (message);
2027 g_list_free (messages);
2030 /* in case of TPL error, skip backlog and show pending messages */
2031 priv->can_show_pending = TRUE;
2032 show_pending_messages (chat);
2034 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2035 * about it in EmpathyChatPriv definition */
2036 priv->retrieving_backlogs = FALSE;
2037 empathy_chat_messages_read (chat);
2039 /* Turn back on scrolling */
2040 empathy_chat_view_scroll (chat->view, TRUE);
2042 #endif /* ENABLE_TPL */
2046 chat_add_logs (EmpathyChat *chat)
2048 EmpathyChatPriv *priv = GET_PRIV (chat);
2049 gboolean is_chatroom;
2051 GList *messages, *l;
2052 #endif /* ENABLE_TPL */
2058 /* Turn off scrolling temporarily */
2059 empathy_chat_view_scroll (chat->view, FALSE);
2061 /* Add messages from last conversation */
2062 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
2065 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
2073 for (l = messages; l; l = g_list_next (l)) {
2074 empathy_chat_view_append_message (chat->view, l->data);
2075 g_object_unref (l->data);
2078 g_list_free (messages);
2080 /* Turn back on scrolling */
2081 empathy_chat_view_scroll (chat->view, TRUE);
2083 priv->retrieving_backlogs = TRUE;
2084 tpl_log_manager_get_filtered_messages_async (priv->log_manager,
2091 got_filtered_messages_cb,
2093 #endif /* ENABLE_TPL */
2097 chat_contacts_completion_func (const gchar *s1,
2101 gchar *tmp, *nick1, *nick2;
2108 return s1 ? -1 : +1;
2111 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2112 nick1 = g_utf8_casefold (tmp, -1);
2115 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2116 nick2 = g_utf8_casefold (tmp, -1);
2119 ret = strncmp (nick1, nick2, n);
2128 build_part_message (guint reason,
2130 EmpathyContact *actor,
2131 const gchar *message)
2133 GString *s = g_string_new ("");
2134 const gchar *actor_name = NULL;
2136 if (actor != NULL) {
2137 actor_name = empathy_contact_get_name (actor);
2140 /* Having an actor only really makes sense for a few actions... */
2142 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2143 g_string_append_printf (s, _("%s has disconnected"), name);
2145 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2146 if (actor_name != NULL) {
2147 /* translators: reverse the order of these arguments
2148 * if the kicked should come before the kicker in your locale.
2150 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2153 g_string_append_printf (s, _("%s was kicked"), name);
2156 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2157 if (actor_name != NULL) {
2158 /* translators: reverse the order of these arguments
2159 * if the banned should come before the banner in your locale.
2161 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2164 g_string_append_printf (s, _("%s was banned"), name);
2168 g_string_append_printf (s, _("%s has left the room"), name);
2171 if (!EMP_STR_EMPTY (message)) {
2172 /* Note to translators: this string is appended to
2173 * notifications like "foo has left the room", with the message
2174 * given by the user living the room. If this poses a problem,
2175 * please let us know. :-)
2177 g_string_append_printf (s, _(" (%s)"), message);
2180 return g_string_free (s, FALSE);
2184 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2185 EmpathyContact *contact,
2186 EmpathyContact *actor,
2192 EmpathyChatPriv *priv = GET_PRIV (chat);
2193 const gchar *name = empathy_contact_get_name (contact);
2196 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2198 if (priv->block_events_timeout_id != 0)
2202 str = g_strdup_printf (_("%s has joined the room"),
2205 str = build_part_message (reason, name, actor, message);
2208 empathy_chat_view_append_event (chat->view, str);
2213 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2214 EmpathyContact *old_contact,
2215 EmpathyContact *new_contact,
2220 EmpathyChatPriv *priv = GET_PRIV (chat);
2222 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2224 if (priv->block_events_timeout_id == 0) {
2227 str = g_strdup_printf (_("%s is now known as %s"),
2228 empathy_contact_get_name (old_contact),
2229 empathy_contact_get_name (new_contact));
2230 empathy_chat_view_append_event (chat->view, str);
2237 chat_reset_size_request (gpointer widget)
2239 gtk_widget_set_size_request (widget, -1, -1);
2245 chat_update_contacts_visibility (EmpathyChat *chat,
2248 EmpathyChatPriv *priv = GET_PRIV (chat);
2249 GtkAllocation allocation;
2251 if (!priv->scrolled_window_contacts) {
2255 if (priv->remote_contact != NULL) {
2259 if (show && priv->contact_list_view == NULL) {
2260 EmpathyContactListStore *store;
2263 /* We are adding the contact list to the chat, we don't want the
2264 * chat view to become too small. If the chat view is already
2265 * smaller than 250 make sure that size won't change. If the
2266 * chat view is bigger the contact list will take some space on
2267 * it but we make sure the chat view don't become smaller than
2268 * 250. Relax the size request once the resize is done */
2269 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2270 min_width = MIN (allocation.width, 250);
2271 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2272 g_idle_add (chat_reset_size_request, priv->vbox_left);
2274 if (priv->contacts_width > 0) {
2275 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2276 priv->contacts_width);
2279 store = empathy_contact_list_store_new (
2280 EMPATHY_CONTACT_LIST (priv->tp_chat));
2281 empathy_contact_list_store_set_show_groups (
2282 EMPATHY_CONTACT_LIST_STORE (store), FALSE);
2284 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
2285 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
2286 EMPATHY_CONTACT_FEATURE_CHAT |
2287 EMPATHY_CONTACT_FEATURE_CALL |
2288 EMPATHY_CONTACT_FEATURE_LOG |
2289 EMPATHY_CONTACT_FEATURE_INFO));
2290 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2291 priv->contact_list_view);
2292 gtk_widget_show (priv->contact_list_view);
2293 gtk_widget_show (priv->scrolled_window_contacts);
2294 g_object_unref (store);
2296 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2297 gtk_widget_hide (priv->scrolled_window_contacts);
2298 if (priv->contact_list_view != NULL) {
2299 gtk_widget_destroy (priv->contact_list_view);
2300 priv->contact_list_view = NULL;
2306 empathy_chat_set_show_contacts (EmpathyChat *chat,
2309 EmpathyChatPriv *priv = GET_PRIV (chat);
2311 priv->show_contacts = show;
2313 chat_update_contacts_visibility (chat, show);
2315 g_object_notify (G_OBJECT (chat), "show-contacts");
2319 chat_remote_contact_changed_cb (EmpathyChat *chat)
2321 EmpathyChatPriv *priv = GET_PRIV (chat);
2323 if (priv->remote_contact != NULL) {
2324 g_object_unref (priv->remote_contact);
2325 priv->remote_contact = NULL;
2330 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
2331 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
2332 if (priv->remote_contact != NULL) {
2333 g_object_ref (priv->remote_contact);
2334 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
2336 else if (priv->tp_chat != NULL) {
2339 channel = empathy_tp_chat_get_channel (priv->tp_chat);
2340 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
2343 chat_update_contacts_visibility (chat, priv->show_contacts);
2345 g_object_notify (G_OBJECT (chat), "remote-contact");
2346 g_object_notify (G_OBJECT (chat), "id");
2350 chat_destroy_cb (EmpathyTpChat *tp_chat,
2353 EmpathyChatPriv *priv;
2355 priv = GET_PRIV (chat);
2357 if (!priv->tp_chat) {
2361 chat_composing_remove_timeout (chat);
2362 g_object_unref (priv->tp_chat);
2363 priv->tp_chat = NULL;
2364 g_object_notify (G_OBJECT (chat), "tp-chat");
2366 empathy_chat_view_append_event (chat->view, _("Disconnected"));
2367 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
2369 chat_update_contacts_visibility (chat, FALSE);
2373 update_misspelled_words (gpointer data)
2375 EmpathyChat *chat = EMPATHY_CHAT (data);
2376 GtkTextBuffer *buffer;
2380 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2382 gtk_text_buffer_get_end_iter (buffer, &iter);
2383 length = gtk_text_iter_get_offset (&iter);
2384 chat_input_text_buffer_insert_text_cb (buffer, &iter,
2385 NULL, length, chat);
2390 conf_spell_checking_cb (GSettings *gsettings_chat,
2394 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2395 EmpathyChatPriv *priv = GET_PRIV (chat);
2396 gboolean spell_checker;
2397 GtkTextBuffer *buffer;
2399 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
2402 spell_checker = g_settings_get_boolean (gsettings_chat,
2403 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
2405 if (!empathy_spell_supported ()) {
2406 spell_checker = FALSE;
2409 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2411 if (spell_checker == priv->spell_checking_enabled) {
2412 if (spell_checker) {
2413 /* Possibly changed dictionaries,
2414 * update misspelled words. Need to do so in idle
2415 * so the spell checker is updated. */
2416 g_idle_add (update_misspelled_words, chat);
2422 if (spell_checker) {
2425 priv->notify_cursor_position_id = tp_g_signal_connect_object (
2426 buffer, "notify::cursor-position",
2427 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
2429 priv->insert_text_id = tp_g_signal_connect_object (
2430 buffer, "insert-text",
2431 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
2432 chat, G_CONNECT_AFTER);
2433 priv->delete_range_id = tp_g_signal_connect_object (
2434 buffer, "delete-range",
2435 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
2436 chat, G_CONNECT_AFTER);
2438 gtk_text_buffer_create_tag (buffer, "misspelled",
2439 "underline", PANGO_UNDERLINE_ERROR,
2442 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2443 gtk_text_buffer_get_insert (buffer));
2444 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
2447 /* Mark misspelled words in the existing buffer.
2448 * Need to do so in idle so the spell checker is updated. */
2449 g_idle_add (update_misspelled_words, chat);
2451 GtkTextTagTable *table;
2454 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
2455 priv->notify_cursor_position_id = 0;
2456 g_signal_handler_disconnect (buffer, priv->insert_text_id);
2457 priv->insert_text_id = 0;
2458 g_signal_handler_disconnect (buffer, priv->delete_range_id);
2459 priv->delete_range_id = 0;
2461 table = gtk_text_buffer_get_tag_table (buffer);
2462 tag = gtk_text_tag_table_lookup (table, "misspelled");
2463 gtk_text_tag_table_remove (table, tag);
2465 gtk_text_buffer_delete_mark_by_name (buffer,
2466 "previous-cursor-position");
2469 priv->spell_checking_enabled = spell_checker;
2473 chat_hpaned_pos_changed_cb (GtkWidget* hpaned, gpointer user_data)
2475 GSettings *gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
2478 hpaned_pos = gtk_paned_get_position (GTK_PANED(hpaned));
2479 g_settings_set_int (gsettings_chat,
2480 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
2483 g_object_unref (gsettings_chat);
2489 chat_create_ui (EmpathyChat *chat)
2491 EmpathyChatPriv *priv = GET_PRIV (chat);
2495 GtkTextBuffer *buffer;
2498 filename = empathy_file_lookup ("empathy-chat.ui",
2500 gui = empathy_builder_get_file (filename,
2501 "chat_widget", &priv->widget,
2502 "hpaned", &priv->hpaned,
2503 "vbox_left", &priv->vbox_left,
2504 "scrolled_window_chat", &priv->scrolled_window_chat,
2505 "scrolled_window_input", &priv->scrolled_window_input,
2506 "hbox_topic", &priv->hbox_topic,
2507 "expander_topic", &priv->expander_topic,
2508 "label_topic", &priv->label_topic,
2509 "scrolled_window_contacts", &priv->scrolled_window_contacts,
2510 "info_bar_vbox", &priv->info_bar_vbox,
2513 empathy_builder_connect (gui, chat,
2514 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
2515 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
2516 "label_topic", "size-request", chat_topic_label_size_request_cb,
2521 /* Add message view. */
2522 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
2523 /* If this is a GtkTextView, it's set as a drag destination for text/plain
2524 and other types, even though it's non-editable and doesn't accept any
2525 drags. This steals drag motion for anything inside the scrollbars,
2526 making drag destinations on chat windows far less useful.
2528 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
2529 g_signal_connect (chat->view, "focus_in_event",
2530 G_CALLBACK (chat_text_view_focus_in_event_cb),
2532 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
2533 GTK_WIDGET (chat->view));
2534 gtk_widget_show (GTK_WIDGET (chat->view));
2536 /* Add input GtkTextView */
2537 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
2538 "pixels-above-lines", 2,
2539 "pixels-below-lines", 2,
2540 "pixels-inside-wrap", 1,
2543 "wrap-mode", GTK_WRAP_WORD_CHAR,
2545 g_signal_connect (chat->input_text_view, "key-press-event",
2546 G_CALLBACK (chat_input_key_press_event_cb),
2548 g_signal_connect (chat->input_text_view, "size-request",
2549 G_CALLBACK (chat_input_size_request_cb),
2551 g_signal_connect (chat->input_text_view, "realize",
2552 G_CALLBACK (chat_input_realize_cb),
2554 g_signal_connect (chat->input_text_view, "populate-popup",
2555 G_CALLBACK (chat_input_populate_popup_cb),
2557 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2558 tp_g_signal_connect_object (buffer, "changed",
2559 G_CALLBACK (chat_input_text_buffer_changed_cb),
2561 tp_g_signal_connect_object (priv->gsettings_chat,
2562 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
2563 G_CALLBACK (conf_spell_checking_cb), chat, 0);
2564 conf_spell_checking_cb (priv->gsettings_chat,
2565 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
2566 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
2567 chat->input_text_view);
2568 gtk_widget_show (chat->input_text_view);
2570 /* Add the (invisible) search bar */
2571 priv->search_bar = empathy_search_bar_new (chat->view);
2572 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
2575 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
2577 /* Initialy hide the topic, will be shown if not empty */
2578 gtk_widget_hide (priv->hbox_topic);
2580 g_signal_connect (priv->hpaned, "notify::position",
2581 G_CALLBACK (chat_hpaned_pos_changed_cb),
2584 /* Load the paned position */
2585 paned_pos = g_settings_get_int (priv->gsettings_ui,
2586 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
2588 gtk_paned_set_position (GTK_PANED(priv->hpaned), paned_pos);
2590 /* Set widget focus order */
2591 list = g_list_append (NULL, priv->search_bar);
2592 list = g_list_append (list, priv->scrolled_window_input);
2593 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
2596 list = g_list_append (NULL, priv->vbox_left);
2597 list = g_list_append (list, priv->scrolled_window_contacts);
2598 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
2601 list = g_list_append (NULL, priv->hpaned);
2602 list = g_list_append (list, priv->hbox_topic);
2603 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
2606 /* Add the main widget in the chat widget */
2607 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
2608 g_object_unref (gui);
2612 chat_size_request (GtkWidget *widget,
2613 GtkRequisition *requisition)
2615 GtkBin *bin = GTK_BIN (widget);
2618 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2619 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
2621 child = gtk_bin_get_child (bin);
2623 if (child && gtk_widget_get_visible (child))
2625 GtkRequisition child_requisition;
2627 gtk_widget_size_request (child, &child_requisition);
2629 requisition->width += child_requisition.width;
2630 requisition->height += child_requisition.height;
2635 chat_size_allocate (GtkWidget *widget,
2636 GtkAllocation *allocation)
2638 GtkBin *bin = GTK_BIN (widget);
2639 GtkAllocation child_allocation;
2642 gtk_widget_set_allocation (widget, allocation);
2644 child = gtk_bin_get_child (bin);
2646 if (child && gtk_widget_get_visible (child))
2648 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
2649 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
2650 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2651 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
2653 gtk_widget_size_allocate (child, &child_allocation);
2658 chat_finalize (GObject *object)
2661 EmpathyChatPriv *priv;
2663 chat = EMPATHY_CHAT (object);
2664 priv = GET_PRIV (chat);
2666 DEBUG ("Finalized: %p", object);
2668 g_object_unref (priv->gsettings_chat);
2669 g_object_unref (priv->gsettings_ui);
2671 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
2672 g_list_free (priv->input_history);
2674 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
2675 g_list_free (priv->compositors);
2677 chat_composing_remove_timeout (chat);
2679 g_object_unref (priv->account_manager);
2680 g_object_unref (priv->log_manager);
2682 if (priv->tp_chat) {
2683 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2684 chat_destroy_cb, chat);
2685 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2686 chat_message_received_cb, chat);
2687 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2688 chat_send_error_cb, chat);
2689 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2690 chat_state_changed_cb, chat);
2691 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2692 chat_property_changed_cb, chat);
2693 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2694 chat_members_changed_cb, chat);
2695 g_signal_handlers_disconnect_by_func (priv->tp_chat,
2696 chat_remote_contact_changed_cb, chat);
2697 empathy_tp_chat_leave (priv->tp_chat);
2698 g_object_unref (priv->tp_chat);
2700 if (priv->account) {
2701 g_object_unref (priv->account);
2703 if (priv->remote_contact) {
2704 g_object_unref (priv->remote_contact);
2707 if (priv->block_events_timeout_id) {
2708 g_source_remove (priv->block_events_timeout_id);
2712 g_free (priv->name);
2713 g_free (priv->subject);
2714 g_completion_free (priv->completion);
2716 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
2720 chat_constructed (GObject *object)
2722 EmpathyChat *chat = EMPATHY_CHAT (object);
2723 EmpathyChatPriv *priv = GET_PRIV (chat);
2725 if (priv->handle_type != TP_HANDLE_TYPE_ROOM)
2726 chat_add_logs (chat);
2728 /* When async API are involved, pending message are shown at the end of the
2729 * callbacks' chain fired by chat_add_logs */
2730 priv->can_show_pending = TRUE;
2731 show_pending_messages (chat);
2732 #endif /* ENABLE_TPL */
2736 empathy_chat_class_init (EmpathyChatClass *klass)
2738 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
2739 GObjectClass *object_class = G_OBJECT_CLASS (klass);
2741 object_class->finalize = chat_finalize;
2742 object_class->get_property = chat_get_property;
2743 object_class->set_property = chat_set_property;
2744 object_class->constructed = chat_constructed;
2746 widget_class->size_request = chat_size_request;
2747 widget_class->size_allocate = chat_size_allocate;
2749 g_object_class_install_property (object_class,
2751 g_param_spec_object ("tp-chat",
2753 "The tp chat object",
2754 EMPATHY_TYPE_TP_CHAT,
2757 G_PARAM_STATIC_STRINGS));
2758 g_object_class_install_property (object_class,
2760 g_param_spec_object ("account",
2761 "Account of the chat",
2762 "The account of the chat",
2765 G_PARAM_STATIC_STRINGS));
2766 g_object_class_install_property (object_class,
2768 g_param_spec_string ("id",
2770 "The id of the chat",
2773 G_PARAM_STATIC_STRINGS));
2774 g_object_class_install_property (object_class,
2776 g_param_spec_string ("name",
2778 "The name of the chat",
2781 G_PARAM_STATIC_STRINGS));
2782 g_object_class_install_property (object_class,
2784 g_param_spec_string ("subject",
2786 "The subject or topic of the chat",
2789 G_PARAM_STATIC_STRINGS));
2790 g_object_class_install_property (object_class,
2791 PROP_REMOTE_CONTACT,
2792 g_param_spec_object ("remote-contact",
2793 "The remote contact",
2794 "The remote contact is any",
2795 EMPATHY_TYPE_CONTACT,
2797 G_PARAM_STATIC_STRINGS));
2798 g_object_class_install_property (object_class,
2800 g_param_spec_boolean ("show-contacts",
2801 "Contacts' visibility",
2802 "The visibility of the contacts' list",
2805 G_PARAM_STATIC_STRINGS));
2807 signals[COMPOSING] =
2808 g_signal_new ("composing",
2809 G_OBJECT_CLASS_TYPE (object_class),
2813 g_cclosure_marshal_VOID__BOOLEAN,
2817 signals[NEW_MESSAGE] =
2818 g_signal_new ("new-message",
2819 G_OBJECT_CLASS_TYPE (object_class),
2823 g_cclosure_marshal_VOID__OBJECT,
2825 1, EMPATHY_TYPE_MESSAGE);
2827 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
2831 chat_block_events_timeout_cb (gpointer data)
2833 EmpathyChatPriv *priv = GET_PRIV (data);
2835 priv->block_events_timeout_id = 0;
2841 account_manager_prepared_cb (GObject *source_object,
2842 GAsyncResult *result,
2845 GList *accounts, *l;
2846 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
2847 EmpathyChat *chat = user_data;
2848 GError *error = NULL;
2850 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
2851 DEBUG ("Failed to prepare the account manager: %s", error->message);
2852 g_error_free (error);
2856 accounts = tp_account_manager_get_valid_accounts (account_manager);
2858 for (l = accounts; l != NULL; l = l->next) {
2859 TpAccount *account = l->data;
2860 tp_g_signal_connect_object (account, "status-changed",
2861 G_CALLBACK (chat_new_connection_cb),
2865 g_list_free (accounts);
2869 empathy_chat_init (EmpathyChat *chat)
2871 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
2872 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
2876 priv->log_manager = empathy_log_manager_dup_singleton ();
2878 priv->log_manager = tpl_log_manager_dup_singleton ();
2879 #endif /* ENABLE_TPL */
2880 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
2881 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
2883 priv->contacts_width = -1;
2884 priv->input_history = NULL;
2885 priv->input_history_current = NULL;
2886 priv->account_manager = tp_account_manager_dup ();
2888 tp_account_manager_prepare_async (priv->account_manager, NULL,
2889 account_manager_prepared_cb, chat);
2891 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
2892 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
2894 /* Block events for some time to avoid having "has come online" or
2895 * "joined" messages. */
2896 priv->block_events_timeout_id =
2897 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
2899 /* Add nick name completion */
2900 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
2901 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
2903 chat_create_ui (chat);
2907 empathy_chat_new (EmpathyTpChat *tp_chat)
2909 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
2913 empathy_chat_get_tp_chat (EmpathyChat *chat)
2915 EmpathyChatPriv *priv = GET_PRIV (chat);
2917 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2919 return priv->tp_chat;
2922 static void display_password_info_bar (EmpathyChat *self,
2926 provide_password_cb (GObject *tp_chat,
2930 EmpathyChat *self = EMPATHY_CHAT (user_data);
2931 EmpathyChatPriv *priv = GET_PRIV (self);
2932 GError *error = NULL;
2934 if (!empathy_tp_chat_provide_password_finish (EMPATHY_TP_CHAT (tp_chat), res,
2936 DEBUG ("error: %s", error->message);
2937 /* FIXME: what should we do if that's another error? Close the channel?
2938 * Display the raw D-Bus error to the user isn't very useful */
2939 if (g_error_matches (error, TP_ERRORS, TP_ERROR_AUTHENTICATION_FAILED))
2940 display_password_info_bar (self, TRUE);
2941 g_error_free (error);
2946 gtk_widget_set_sensitive (priv->hpaned, TRUE);
2947 gtk_widget_grab_focus (self->input_text_view);
2951 password_infobar_response_cb (GtkWidget *info_bar,
2955 EmpathyChatPriv *priv = GET_PRIV (self);
2957 const gchar *password;
2959 if (response_id != GTK_RESPONSE_OK)
2962 entry = g_object_get_data (G_OBJECT (info_bar), "password-entry");
2963 g_assert (entry != NULL);
2965 password = gtk_entry_get_text (GTK_ENTRY (entry));
2967 empathy_tp_chat_provide_password_async (priv->tp_chat, password,
2968 provide_password_cb, self);
2971 gtk_widget_destroy (info_bar);
2975 password_entry_activate_cb (GtkWidget *entry,
2976 GtkWidget *info_bar)
2978 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2982 passwd_join_button_cb (GtkButton *button,
2983 GtkWidget *info_bar)
2985 gtk_info_bar_response (GTK_INFO_BAR (info_bar), GTK_RESPONSE_OK);
2989 display_password_info_bar (EmpathyChat *self,
2992 EmpathyChatPriv *priv = GET_PRIV (self);
2993 GtkWidget *info_bar;
2994 GtkWidget *content_area;
3001 GtkMessageType type;
3002 const gchar *msg, *button_label;
3005 /* Previous password was wrong */
3006 type = GTK_MESSAGE_ERROR;
3007 msg = _("Wrong password; please try again:");
3008 button_label = _("Retry");
3011 /* First time we're trying to join */
3012 type = GTK_MESSAGE_QUESTION;
3013 msg = _("This room is protected by a password:");
3014 button_label = _("Join");
3017 info_bar = gtk_info_bar_new ();
3018 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), type);
3020 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3022 hbox = gtk_hbox_new (FALSE, 3);
3023 gtk_container_add (GTK_CONTAINER (content_area), hbox);
3026 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3027 GTK_ICON_SIZE_DIALOG);
3028 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3031 label = gtk_label_new (msg);
3032 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3034 /* Add password entry */
3035 entry = gtk_entry_new ();
3036 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3037 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3039 g_signal_connect (entry, "activate",
3040 G_CALLBACK (password_entry_activate_cb), info_bar);
3042 /* Focus the password entry once it's realized */
3043 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
3045 /* Add 'Join' button */
3046 alig = gtk_alignment_new (0, 0.5, 0, 0);
3048 button = gtk_button_new_with_label (button_label);
3049 gtk_container_add (GTK_CONTAINER (alig), button);
3050 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3052 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
3055 g_object_set_data (G_OBJECT (info_bar), "password-entry", entry);
3057 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
3059 gtk_widget_show_all (hbox);
3061 g_signal_connect (info_bar, "response",
3062 G_CALLBACK (password_infobar_response_cb), self);
3064 gtk_widget_show_all (info_bar);
3068 chat_password_needed_changed_cb (EmpathyChat *self)
3070 EmpathyChatPriv *priv = GET_PRIV (self);
3072 if (empathy_tp_chat_password_needed (priv->tp_chat)) {
3073 display_password_info_bar (self, FALSE);
3074 gtk_widget_set_sensitive (priv->hpaned, FALSE);
3079 empathy_chat_set_tp_chat (EmpathyChat *chat,
3080 EmpathyTpChat *tp_chat)
3082 EmpathyChatPriv *priv = GET_PRIV (chat);
3083 TpConnection *connection;
3084 GPtrArray *properties;
3086 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3087 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
3088 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
3090 if (priv->tp_chat) {
3094 if (priv->account) {
3095 g_object_unref (priv->account);
3098 priv->tp_chat = g_object_ref (tp_chat);
3099 connection = empathy_tp_chat_get_connection (priv->tp_chat);
3100 priv->account = g_object_ref (empathy_get_account_for_connection (connection));
3102 g_signal_connect (tp_chat, "destroy",
3103 G_CALLBACK (chat_destroy_cb),
3105 g_signal_connect (tp_chat, "message-received",
3106 G_CALLBACK (chat_message_received_cb),
3108 g_signal_connect (tp_chat, "send-error",
3109 G_CALLBACK (chat_send_error_cb),
3111 g_signal_connect (tp_chat, "chat-state-changed",
3112 G_CALLBACK (chat_state_changed_cb),
3114 g_signal_connect (tp_chat, "property-changed",
3115 G_CALLBACK (chat_property_changed_cb),
3117 g_signal_connect (tp_chat, "members-changed",
3118 G_CALLBACK (chat_members_changed_cb),
3120 g_signal_connect (tp_chat, "member-renamed",
3121 G_CALLBACK (chat_member_renamed_cb),
3123 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
3124 G_CALLBACK (chat_remote_contact_changed_cb),
3126 g_signal_connect_swapped (tp_chat, "notify::password-needed",
3127 G_CALLBACK (chat_password_needed_changed_cb),
3130 /* Get initial value of properties */
3131 properties = empathy_tp_chat_get_properties (priv->tp_chat);
3132 if (properties != NULL) {
3135 for (i = 0; i < properties->len; i++) {
3136 EmpathyTpChatProperty *property;
3138 property = g_ptr_array_index (properties, i);
3139 if (property->value == NULL)
3142 chat_property_changed_cb (priv->tp_chat,
3149 chat_remote_contact_changed_cb (chat);
3151 if (chat->input_text_view) {
3152 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
3153 if (priv->block_events_timeout_id == 0) {
3154 empathy_chat_view_append_event (chat->view, _("Connected"));
3158 g_object_notify (G_OBJECT (chat), "tp-chat");
3159 g_object_notify (G_OBJECT (chat), "id");
3160 g_object_notify (G_OBJECT (chat), "account");
3162 /* This is a noop when tp-chat is set at object construction time and causes
3163 * the pending messages to be show when it's set on the object after it has
3165 show_pending_messages (chat);
3167 /* check if a password is needed */
3168 chat_password_needed_changed_cb (chat);
3172 empathy_chat_get_account (EmpathyChat *chat)
3174 EmpathyChatPriv *priv = GET_PRIV (chat);
3176 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3178 return priv->account;
3182 empathy_chat_get_id (EmpathyChat *chat)
3184 EmpathyChatPriv *priv = GET_PRIV (chat);
3186 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3192 empathy_chat_get_name (EmpathyChat *chat)
3194 EmpathyChatPriv *priv = GET_PRIV (chat);
3197 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3200 if (!ret && priv->remote_contact) {
3201 ret = empathy_contact_get_name (priv->remote_contact);
3207 return ret ? ret : _("Conversation");
3211 empathy_chat_get_subject (EmpathyChat *chat)
3213 EmpathyChatPriv *priv = GET_PRIV (chat);
3215 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3217 return priv->subject;
3221 empathy_chat_get_remote_contact (EmpathyChat *chat)
3223 EmpathyChatPriv *priv = GET_PRIV (chat);
3225 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3227 return priv->remote_contact;
3231 empathy_chat_get_contact_menu (EmpathyChat *chat)
3233 EmpathyChatPriv *priv = GET_PRIV (chat);
3234 GtkWidget *menu = NULL;
3236 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3238 if (priv->remote_contact) {
3239 menu = empathy_contact_menu_new (priv->remote_contact,
3240 EMPATHY_CONTACT_FEATURE_CALL |
3241 EMPATHY_CONTACT_FEATURE_LOG |
3242 EMPATHY_CONTACT_FEATURE_INFO);
3244 else if (priv->contact_list_view) {
3245 EmpathyContactListView *view;
3247 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
3248 menu = empathy_contact_list_view_get_contact_menu (view);
3255 empathy_chat_clear (EmpathyChat *chat)
3257 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3259 empathy_chat_view_clear (chat->view);
3263 empathy_chat_scroll_down (EmpathyChat *chat)
3265 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3267 empathy_chat_view_scroll_down (chat->view);
3271 empathy_chat_cut (EmpathyChat *chat)
3273 GtkTextBuffer *buffer;
3275 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3277 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3278 if (gtk_text_buffer_get_has_selection (buffer)) {
3279 GtkClipboard *clipboard;
3281 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3283 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
3288 empathy_chat_copy (EmpathyChat *chat)
3290 GtkTextBuffer *buffer;
3292 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3294 if (empathy_chat_view_get_has_selection (chat->view)) {
3295 empathy_chat_view_copy_clipboard (chat->view);
3299 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3300 if (gtk_text_buffer_get_has_selection (buffer)) {
3301 GtkClipboard *clipboard;
3303 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3305 gtk_text_buffer_copy_clipboard (buffer, clipboard);
3310 empathy_chat_paste (EmpathyChat *chat)
3312 GtkTextBuffer *buffer;
3313 GtkClipboard *clipboard;
3314 EmpathyChatPriv *priv;
3316 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3318 priv = GET_PRIV (chat);
3320 if (priv->tp_chat == NULL ||
3321 !gtk_widget_is_sensitive (chat->input_text_view))
3324 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3325 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
3327 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
3331 empathy_chat_find (EmpathyChat *chat)
3333 EmpathyChatPriv *priv;
3335 g_return_if_fail (EMPATHY_IS_CHAT (chat));
3337 priv = GET_PRIV (chat);
3339 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
3343 empathy_chat_correct_word (EmpathyChat *chat,
3346 const gchar *new_word)
3348 GtkTextBuffer *buffer;
3350 g_return_if_fail (chat != NULL);
3351 g_return_if_fail (new_word != NULL);
3353 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3355 gtk_text_buffer_delete (buffer, start, end);
3356 gtk_text_buffer_insert (buffer, start,
3362 empathy_chat_is_room (EmpathyChat *chat)
3364 EmpathyChatPriv *priv = GET_PRIV (chat);
3366 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
3368 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
3372 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
3374 EmpathyChatPriv *priv = GET_PRIV (self);
3376 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
3378 return priv->unread_messages;
3381 /* called when the messages have been read by user */
3383 empathy_chat_messages_read (EmpathyChat *self)
3385 EmpathyChatPriv *priv = GET_PRIV (self);
3387 g_return_if_fail (EMPATHY_IS_CHAT (self));
3389 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
3390 * definition. If we are still retrieving the backlogs, do not ACK */
3391 if (priv->retrieving_backlogs)
3394 if (priv->tp_chat != NULL ) {
3395 empathy_tp_chat_acknowledge_all_messages (priv->tp_chat);
3397 priv->unread_messages = 0;