Updated Spanish translation
[empathy-mirror.git] / libempathy-gtk / empathy-chat.c
blob8f05b8f5cae4f7b2f9d48f0b0b4f1eb2607b6966
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
5 * Copyright (C) 2012 Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301 USA
22 * Authors: Mikael Hallendal <micke@imendio.com>
23 * Richard Hult <richard@imendio.com>
24 * Martyn Russell <martyn@imendio.com>
25 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
26 * Xavier Claessens <xclaesse@gmail.com>
29 /* for GCompletion */
30 #define GLIB_DISABLE_DEPRECATION_WARNINGS 1
32 #include "config.h"
33 #include "empathy-chat.h"
35 #include <glib/gi18n-lib.h>
36 #include <tp-account-widgets/tpaw-keyring.h>
37 #include <tp-account-widgets/tpaw-builder.h>
38 #include <tp-account-widgets/tpaw-utils.h>
39 #include <telepathy-glib/telepathy-glib-dbus.h>
41 #include "empathy-client-factory.h"
42 #include "empathy-gsettings.h"
43 #include "empathy-individual-information-dialog.h"
44 #include "empathy-individual-store-channel.h"
45 #include "empathy-individual-view.h"
46 #include "empathy-input-text-view.h"
47 #include "empathy-request-util.h"
48 #include "empathy-search-bar.h"
49 #include "empathy-spell.h"
50 #include "empathy-string-parser.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-ui-utils.h"
53 #include "empathy-utils.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include "empathy-debug.h"
58 #define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
59 #define COMPOSING_STOP_TIMEOUT 5
61 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
62 struct _EmpathyChatPriv {
63 EmpathyTpChat *tp_chat;
64 TpAccount *account;
65 gchar *id;
66 gchar *name;
67 gchar *subject;
68 EmpathyContact *self_contact;
69 EmpathyContact *remote_contact;
70 gboolean show_contacts;
72 GSettings *gsettings_chat;
73 GSettings *gsettings_ui;
75 TplLogManager *log_manager;
76 TplLogWalker *log_walker;
77 /* Are we watching for scrolling movements? */
78 gboolean watch_scroll;
79 /* Maximum page size of the chat->view. */
80 guint max_page_size;
81 /* The offset from the lower edge of the chat->view before it
82 * expanded to fit in the newly fetched logs. This is to
83 * restore the chat->view to the page it was on before the
84 * latest batch of logs were inserted. */
85 guint scroll_offset;
87 TpAccountManager *account_manager;
88 GList *input_history;
89 GList *input_history_current;
90 GList *compositors;
91 GCompletion *completion;
92 guint composing_stop_timeout_id;
93 guint block_events_timeout_id;
94 TpHandleType handle_type;
95 gint contacts_width;
96 gboolean has_input_vscroll;
98 /* TRUE if spell checking is enabled, FALSE otherwise.
99 * This is to keep track of the last state of spell checking
100 * when it changes. */
101 gboolean spell_checking_enabled;
103 /* These store the signal handler ids for the enclosed text entry. */
104 gulong insert_text_id;
105 gulong delete_range_id;
106 gulong notify_cursor_position_id;
108 /* Source func ID for update_misspelled_words () */
109 guint update_misspelled_words_id;
110 /* Source func ID for save_paned_pos_timeout () */
111 guint save_paned_pos_id;
112 /* Source func ID for chat_contacts_visible_timeout_cb () */
113 guint contacts_visible_id;
115 GtkWidget *widget;
116 GtkWidget *hpaned;
117 GtkWidget *vbox_left;
118 GtkWidget *scrolled_window_chat;
119 GtkWidget *scrolled_window_input;
120 GtkWidget *scrolled_window_contacts;
121 GtkWidget *hbox_topic;
122 GtkWidget *expander_topic;
123 GtkWidget *label_topic;
124 GtkWidget *contact_list_view;
125 GtkWidget *info_bar_vbox;
126 GtkWidget *search_bar;
128 guint unread_messages;
129 guint unread_messages_when_offline;
131 /* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
132 * be differently handled since it introduces another race condition, which
133 * is really hard to occur, but still possible.
135 * With the current workaround (which has the race above), we need to be
136 * sure to ACK any pending messages only when the retrieval of backlogs is
137 * finished, that's why using retrieving_backlogs flag.
138 * empathy_chat_messages_read () will check this variable and not ACK
139 * anything when TRUE. It will be set TRUE at chat_constructed () and set
140 * back to FALSE when the backlog has been retrieved and the pending
141 * messages actually showed to the user.
143 * Race condition introduced with this workaround:
144 * Scenario: a message is pending, the user is notified and selects the tab.
145 * the tab with a pending message is focused before the messages are properly
146 * shown (since the preparation of the window is slower AND async WRT the
147 * tab showing), which means the user won't see any new messages (rare but
148 * possible), if he/she will change tab focus before the messages are
149 * properly shown, the tab will be set as 'seen' and the user won't be
150 * notified again about the already notified pending messages when the
151 * messages in tab will be properly shown */
152 gboolean retrieving_backlogs;
153 gboolean sms_channel;
155 /* we need to know whether populate-popup happened in response to
156 * the keyboard or the mouse. We can't ask GTK for the most recent
157 * event, because it will be a notify event. Instead we track it here */
158 GdkEventType most_recent_event_type;
160 /* A regex matching our own current nickname in the room, or %NULL if
161 * !empathy_chat_is_room (). */
162 GRegex *highlight_regex;
164 /* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
165 * Cleared by empathy_chat_messages_read (). */
166 gboolean highlighted;
169 typedef struct {
170 gchar *text; /* Original message that was specified
171 * upon entry creation. */
172 gchar *modified_text; /* Message that was modified by user.
173 * When no modifications were made, it is NULL */
174 } InputHistoryEntry;
176 enum {
177 COMPOSING,
178 NEW_MESSAGE,
179 PART_COMMAND_ENTERED,
180 LAST_SIGNAL
183 enum {
184 PROP_0,
185 PROP_TP_CHAT,
186 PROP_ACCOUNT,
187 PROP_ID,
188 PROP_NAME,
189 PROP_SUBJECT,
190 PROP_REMOTE_CONTACT,
191 PROP_SHOW_CONTACTS,
192 PROP_SMS_CHANNEL,
193 PROP_N_MESSAGES_SENDING,
194 PROP_NB_UNREAD_MESSAGES,
197 static guint signals[LAST_SIGNAL] = { 0 };
199 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);
201 #if 0
202 /* FIXME: We need to figure out how to fill backlog automatically with WebKit2 */
203 static gboolean chat_scrollable_connect (gpointer user_data);
204 #endif
205 static gboolean update_misspelled_words (gpointer data);
207 static void
208 chat_get_property (GObject *object,
209 guint param_id,
210 GValue *value,
211 GParamSpec *pspec)
213 EmpathyChat *chat = EMPATHY_CHAT (object);
214 EmpathyChatPriv *priv = GET_PRIV (object);
216 switch (param_id) {
217 case PROP_TP_CHAT:
218 g_value_set_object (value, priv->tp_chat);
219 break;
220 case PROP_ACCOUNT:
221 g_value_set_object (value, priv->account);
222 break;
223 case PROP_NAME:
224 g_value_take_string (value, empathy_chat_dup_name (chat));
225 break;
226 case PROP_ID:
227 g_value_set_string (value, priv->id);
228 break;
229 case PROP_SUBJECT:
230 g_value_set_string (value, priv->subject);
231 break;
232 case PROP_REMOTE_CONTACT:
233 g_value_set_object (value, priv->remote_contact);
234 break;
235 case PROP_SHOW_CONTACTS:
236 g_value_set_boolean (value, priv->show_contacts);
237 break;
238 case PROP_SMS_CHANNEL:
239 g_value_set_boolean (value, priv->sms_channel);
240 break;
241 case PROP_N_MESSAGES_SENDING:
242 g_value_set_uint (value,
243 empathy_chat_get_n_messages_sending (chat));
244 break;
245 case PROP_NB_UNREAD_MESSAGES:
246 g_value_set_uint (value,
247 empathy_chat_get_nb_unread_messages (chat));
248 break;
249 default:
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
251 break;
255 static void
256 chat_set_property (GObject *object,
257 guint param_id,
258 const GValue *value,
259 GParamSpec *pspec)
261 EmpathyChat *chat = EMPATHY_CHAT (object);
263 switch (param_id) {
264 case PROP_TP_CHAT:
265 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
266 break;
267 case PROP_SHOW_CONTACTS:
268 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
269 break;
270 default:
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
272 break;
276 static void
277 account_reconnected (EmpathyChat *chat,
278 TpAccount *account)
280 EmpathyChatPriv *priv = GET_PRIV (chat);
282 DEBUG ("Account reconnected, request a new Text channel");
284 /* FIXME: Ideally we should ask to handle ourself the channel so we can
285 * report the error if any but this is blocked by
286 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
287 switch (priv->handle_type) {
288 case TP_HANDLE_TYPE_CONTACT:
289 if (priv->sms_channel)
290 empathy_sms_contact_id (
291 account, priv->id,
292 TP_USER_ACTION_TIME_NOT_USER_ACTION,
293 NULL, NULL);
294 else
295 empathy_chat_with_contact_id (
296 account, priv->id,
297 TP_USER_ACTION_TIME_NOT_USER_ACTION,
298 NULL, NULL);
299 break;
300 case TP_HANDLE_TYPE_ROOM:
301 empathy_join_muc (account, priv->id,
302 TP_USER_ACTION_TIME_NOT_USER_ACTION);
303 break;
304 case TP_HANDLE_TYPE_NONE:
305 case TP_HANDLE_TYPE_LIST:
306 case TP_HANDLE_TYPE_GROUP:
307 default:
308 g_assert_not_reached ();
309 break;
312 g_object_unref (chat);
315 static void
316 chat_new_connection_cb (TpAccount *account,
317 guint old_status,
318 guint new_status,
319 guint reason,
320 gchar *dbus_error_name,
321 GHashTable *details,
322 EmpathyChat *chat)
324 EmpathyChatPriv *priv = GET_PRIV (chat);
326 if (new_status != TP_CONNECTION_STATUS_CONNECTED)
327 return;
329 if (priv->tp_chat != NULL || account != priv->account ||
330 priv->handle_type == TP_HANDLE_TYPE_NONE ||
331 TPAW_STR_EMPTY (priv->id))
332 return;
334 g_object_ref (chat);
336 account_reconnected (chat, account);
339 static void
340 chat_composing_remove_timeout (EmpathyChat *chat)
342 EmpathyChatPriv *priv;
344 priv = GET_PRIV (chat);
346 if (priv->composing_stop_timeout_id) {
347 g_source_remove (priv->composing_stop_timeout_id);
348 priv->composing_stop_timeout_id = 0;
352 static void
353 set_chate_state_cb (GObject *source,
354 GAsyncResult *result,
355 gpointer user_data)
357 GError *error = NULL;
359 if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
360 &error)) {
361 DEBUG ("Failed to set chat state: %s", error->message);
362 g_error_free (error);
366 static void
367 set_chat_state (EmpathyChat *self,
368 TpChannelChatState state)
370 EmpathyChatPriv *priv = GET_PRIV (self);
372 if (!tp_proxy_has_interface_by_id (priv->tp_chat,
373 TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE))
374 return;
376 tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
377 set_chate_state_cb, self);
380 static gboolean
381 chat_composing_stop_timeout_cb (EmpathyChat *chat)
383 EmpathyChatPriv *priv;
384 gboolean send_chat_states;
386 priv = GET_PRIV (chat);
388 priv->composing_stop_timeout_id = 0;
389 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
390 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
391 if (!send_chat_states) {
392 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
393 } else {
394 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
397 return FALSE;
400 static void
401 chat_composing_start (EmpathyChat *chat)
403 EmpathyChatPriv *priv;
404 gboolean send_chat_states;
406 priv = GET_PRIV (chat);
408 send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
409 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
410 if (!send_chat_states) {
411 return;
414 if (priv->composing_stop_timeout_id) {
415 /* Just restart the timeout */
416 chat_composing_remove_timeout (chat);
417 } else {
418 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
421 priv->composing_stop_timeout_id = g_timeout_add_seconds (
422 COMPOSING_STOP_TIMEOUT,
423 (GSourceFunc) chat_composing_stop_timeout_cb,
424 chat);
427 static void
428 chat_composing_stop (EmpathyChat *chat)
430 chat_composing_remove_timeout (chat);
431 set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
434 static gint
435 chat_input_history_entry_cmp (InputHistoryEntry *entry,
436 const gchar *text)
438 if (!tp_strdiff (entry->text, text)) {
439 if (entry->modified_text != NULL) {
440 /* Modified entry and single string cannot be equal. */
441 return 1;
443 return 0;
445 return 1;
448 static InputHistoryEntry *
449 chat_input_history_entry_new_with_text (const gchar *text)
451 InputHistoryEntry *entry;
452 entry = g_slice_new0 (InputHistoryEntry);
453 entry->text = g_strdup (text);
455 return entry;
458 static void
459 chat_input_history_entry_free (InputHistoryEntry *entry)
461 g_free (entry->text);
462 g_free (entry->modified_text);
463 g_slice_free (InputHistoryEntry, entry);
466 static void
467 chat_input_history_entry_revert (InputHistoryEntry *entry)
469 g_free (entry->modified_text);
470 entry->modified_text = NULL;
473 static void
474 chat_input_history_entry_update_text (InputHistoryEntry *entry,
475 const gchar *text)
477 gchar *old;
479 if (!tp_strdiff (text, entry->text)) {
480 g_free (entry->modified_text);
481 entry->modified_text = NULL;
482 return;
485 old = entry->modified_text;
486 entry->modified_text = g_strdup (text);
487 g_free (old);
490 static const gchar *
491 chat_input_history_entry_get_text (InputHistoryEntry *entry)
493 if (entry == NULL) {
494 return NULL;
497 if (entry->modified_text != NULL) {
498 return entry->modified_text;
500 return entry->text;
503 static GList *
504 chat_input_history_remove_item (GList *list,
505 GList *item)
507 list = g_list_remove_link (list, item);
508 chat_input_history_entry_free (item->data);
509 g_list_free_1 (item);
510 return list;
513 static void
514 chat_input_history_revert (EmpathyChat *chat)
516 EmpathyChatPriv *priv;
517 GList *list;
518 GList *item1;
519 GList *item2;
520 InputHistoryEntry *entry;
522 priv = GET_PRIV (chat);
523 list = priv->input_history;
525 if (list == NULL) {
526 DEBUG ("No input history");
527 return;
530 /* Delete temporary entry */
531 if (priv->input_history_current != NULL) {
532 item1 = list;
533 list = chat_input_history_remove_item (list, item1);
534 if (priv->input_history_current == item1) {
535 /* Removed temporary entry was current entry */
536 priv->input_history = list;
537 priv->input_history_current = NULL;
538 return;
541 else {
542 /* There is no entry to revert */
543 return;
546 /* Restore the current history entry to original value */
547 item1 = priv->input_history_current;
548 entry = item1->data;
549 chat_input_history_entry_revert (entry);
551 /* Remove restored entry if there is other occurance before this entry */
552 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
553 (GCompareFunc) chat_input_history_entry_cmp);
554 if (item2 != item1) {
555 list = chat_input_history_remove_item (list, item1);
557 else {
558 /* Remove other occurance of the restored entry */
559 item2 = g_list_find_custom (item1->next,
560 chat_input_history_entry_get_text (entry),
561 (GCompareFunc) chat_input_history_entry_cmp);
562 if (item2 != NULL) {
563 list = chat_input_history_remove_item (list, item2);
567 priv->input_history_current = NULL;
568 priv->input_history = list;
571 static void
572 chat_input_history_add (EmpathyChat *chat,
573 const gchar *str,
574 gboolean temporary)
576 EmpathyChatPriv *priv;
577 GList *list;
578 GList *item;
579 InputHistoryEntry *entry;
581 priv = GET_PRIV (chat);
583 list = priv->input_history;
585 /* Remove any other occurances of this entry, if not temporary */
586 if (!temporary) {
587 while ((item = g_list_find_custom (list, str,
588 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
589 list = chat_input_history_remove_item (list, item);
592 /* Trim the list to the last 10 items */
593 while (g_list_length (list) > 10) {
594 item = g_list_last (list);
595 if (item != NULL) {
596 list = chat_input_history_remove_item (list, item);
603 /* Add new entry */
604 entry = chat_input_history_entry_new_with_text (str);
605 list = g_list_prepend (list, entry);
607 /* Set the list and the current item pointer */
608 priv->input_history = list;
609 if (temporary) {
610 priv->input_history_current = list;
612 else {
613 priv->input_history_current = NULL;
617 static const gchar *
618 chat_input_history_get_next (EmpathyChat *chat)
620 EmpathyChatPriv *priv;
621 GList *item;
622 const gchar *msg;
624 priv = GET_PRIV (chat);
626 if (priv->input_history == NULL) {
627 DEBUG ("No input history, next entry is NULL");
628 return NULL;
630 g_assert (priv->input_history_current != NULL);
632 if ((item = g_list_next (priv->input_history_current)) == NULL)
634 item = priv->input_history_current;
637 msg = chat_input_history_entry_get_text (item->data);
639 DEBUG ("Returning next entry: '%s'", msg);
641 priv->input_history_current = item;
643 return msg;
646 static const gchar *
647 chat_input_history_get_prev (EmpathyChat *chat)
649 EmpathyChatPriv *priv;
650 GList *item;
651 const gchar *msg;
653 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
655 priv = GET_PRIV (chat);
657 if (priv->input_history == NULL) {
658 DEBUG ("No input history, previous entry is NULL");
659 return NULL;
662 if (priv->input_history_current == NULL)
664 return NULL;
666 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
668 item = priv->input_history_current;
671 msg = chat_input_history_entry_get_text (item->data);
673 DEBUG ("Returning previous entry: '%s'", msg);
675 priv->input_history_current = item;
677 return msg;
680 static void
681 chat_input_history_update (EmpathyChat *chat,
682 GtkTextBuffer *buffer)
684 EmpathyChatPriv *priv;
685 GtkTextIter start, end;
686 gchar *text;
687 InputHistoryEntry *entry;
689 priv = GET_PRIV (chat);
691 gtk_text_buffer_get_bounds (buffer, &start, &end);
692 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
694 if (priv->input_history_current == NULL) {
695 /* Add the current text temporarily to the history */
696 chat_input_history_add (chat, text, TRUE);
697 g_free (text);
698 return;
701 /* Save the changes in the history */
702 entry = priv->input_history_current->data;
703 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
704 chat_input_history_entry_update_text (entry, text);
707 g_free (text);
710 typedef struct {
711 EmpathyChat *chat;
712 gchar *message;
713 } ChatCommandMsgData;
715 static void
716 chat_command_msg_cb (GObject *source,
717 GAsyncResult *result,
718 gpointer user_data)
720 ChatCommandMsgData *data = user_data;
721 GError *error = NULL;
722 TpChannel *channel;
724 channel = tp_account_channel_request_ensure_and_observe_channel_finish (
725 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);
727 if (channel == NULL) {
728 DEBUG ("Failed to get channel: %s", error->message);
729 g_error_free (error);
731 empathy_theme_adium_append_event (data->chat->view,
732 _("Failed to open private chat"));
733 goto OUT;
736 if (!TPAW_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
737 TpTextChannel *text = (TpTextChannel *) channel;
738 TpMessage *msg;
740 msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
741 data->message);
743 tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);
745 g_object_unref (msg);
748 g_object_unref (channel);
750 OUT:
751 g_free (data->message);
752 g_slice_free (ChatCommandMsgData, data);
755 static gboolean
756 nick_command_supported (EmpathyChat *chat)
758 EmpathyChatPriv * priv = GET_PRIV (chat);
759 TpConnection *connection;
761 connection = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
762 return tp_proxy_has_interface_by_id (connection,
763 TP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING);
766 static gboolean
767 part_command_supported (EmpathyChat *chat)
769 EmpathyChatPriv * priv = GET_PRIV (chat);
771 return tp_proxy_has_interface_by_id (priv->tp_chat,
772 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP);
775 static void
776 chat_command_clear (EmpathyChat *chat,
777 GStrv strv)
779 empathy_theme_adium_clear (chat->view);
782 static void
783 chat_command_topic (EmpathyChat *chat,
784 GStrv strv)
786 EmpathyChatPriv *priv = GET_PRIV (chat);
788 if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
789 empathy_theme_adium_append_event (chat->view,
790 _("Topic not supported on this conversation"));
791 return;
794 if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
795 empathy_theme_adium_append_event (chat->view,
796 _("You are not allowed to change the topic"));
797 return;
800 empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
803 void
804 empathy_chat_join_muc (EmpathyChat *chat,
805 const gchar *room)
807 EmpathyChatPriv *priv = GET_PRIV (chat);
809 empathy_join_muc (priv->account, room,
810 empathy_get_current_action_time ());
813 static void
814 chat_command_join (EmpathyChat *chat,
815 GStrv strv)
817 guint i = 0;
819 GStrv rooms = g_strsplit_set (strv[1], ", ", -1);
821 /* FIXME: Ideally we should ask to handle ourself the channel so we can
822 * report the error if any but this is blocked by
823 * https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
824 while (rooms[i] != NULL) {
825 /* ignore empty strings */
826 if (!TPAW_STR_EMPTY (rooms[i])) {
827 empathy_chat_join_muc (chat, rooms[i]);
829 i++;
831 g_strfreev (rooms);
834 static void
835 chat_command_part (EmpathyChat *chat,
836 GStrv strv)
838 g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
841 static void
842 chat_command_msg_internal (EmpathyChat *chat,
843 const gchar *contact_id,
844 const gchar *message)
846 EmpathyChatPriv *priv = GET_PRIV (chat);
847 ChatCommandMsgData *data;
848 TpAccountChannelRequest *req;
850 req = tp_account_channel_request_new_text (priv->account,
851 empathy_get_current_action_time ());
853 tp_account_channel_request_set_target_id (req, TP_HANDLE_TYPE_CONTACT,
854 contact_id);
856 /* FIXME: We should probably search in members alias. But this
857 * is enough for IRC */
858 data = g_slice_new (ChatCommandMsgData);
859 data->chat = chat;
860 data->message = g_strdup (message);
862 tp_account_channel_request_ensure_and_observe_channel_async (req,
863 EMPATHY_CHAT_TP_BUS_NAME, NULL, chat_command_msg_cb, data);
865 g_object_unref (req);
868 static void
869 chat_command_query (EmpathyChat *chat,
870 GStrv strv)
872 /* If <message> part is not defined,
873 * strv[2] will be the terminal NULL */
874 chat_command_msg_internal (chat, strv[1], strv[2]);
877 static void
878 chat_command_msg (EmpathyChat *chat,
879 GStrv strv)
881 chat_command_msg_internal (chat, strv[1], strv[2]);
884 static void
885 callback_for_request_rename (TpConnection *conn,
886 const GError *error,
887 gpointer user_data,
888 GObject *weak_object)
890 if (error != NULL) {
891 DEBUG ("Call to RequestRename method failed: %s",error->message);
895 static void
896 chat_command_nick (EmpathyChat *chat,
897 GStrv strv)
899 EmpathyChatPriv *priv = GET_PRIV (chat);
900 TpConnection *conn;
902 conn = tp_account_get_connection (priv->account);
904 tp_cli_connection_interface_renaming_call_request_rename (conn, -1,
905 strv[1], callback_for_request_rename, NULL, NULL, NULL);
908 static void
909 chat_command_me (EmpathyChat *chat,
910 GStrv strv)
912 EmpathyChatPriv *priv = GET_PRIV (chat);
913 TpMessage *message;
914 TpTextChannel *channel;
916 channel = (TpTextChannel *) (priv->tp_chat);
918 if (!tp_text_channel_supports_message_type (channel,
919 TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
920 /* Action message are not supported, 'simulate' the action */
921 gchar *tmp;
923 /* The TpChat can't be ready if it doesn't have the self contact */
924 g_assert (priv->self_contact != NULL);
926 tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
927 strv[1]);
928 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
929 tmp);
930 g_free (tmp);
932 else {
933 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
934 strv[1]);
937 empathy_tp_chat_send (priv->tp_chat, message);
938 g_object_unref (message);
941 static void
942 chat_command_say (EmpathyChat *chat,
943 GStrv strv)
945 EmpathyChatPriv *priv = GET_PRIV (chat);
946 TpMessage *message;
948 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
949 strv[1]);
950 empathy_tp_chat_send (priv->tp_chat, message);
951 g_object_unref (message);
954 static void
955 whois_got_contact_cb (GObject *source,
956 GAsyncResult *result,
957 gpointer user_data)
959 EmpathyChat *chat = user_data;
960 EmpathyContact *contact;
961 FolksIndividual *individual;
963 contact = empathy_client_factory_dup_contact_by_id_finish (
964 EMPATHY_CLIENT_FACTORY (source), result, NULL);
966 if (contact == NULL) {
967 empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
968 goto out;
971 individual = empathy_ensure_individual_from_tp_contact (
972 empathy_contact_get_tp_contact (contact));
973 empathy_display_individual_info (individual);
975 g_object_unref (individual);
976 g_object_unref (contact);
978 out:
979 g_object_unref (chat);
982 static void
983 chat_command_whois (EmpathyChat *chat,
984 GStrv strv)
986 EmpathyChatPriv *priv = GET_PRIV (chat);
987 TpConnection *conn;
988 EmpathyClientFactory *factory;
990 conn = tp_channel_get_connection ((TpChannel *) priv->tp_chat);
991 factory = empathy_client_factory_dup ();
993 /* Element 0 of 'strv' is "whois"; element 1 is the contact ID
994 * entered by the user (including spaces, if any). */
995 empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
996 whois_got_contact_cb, g_object_ref (chat));
998 g_object_unref (factory);
1001 static void
1002 chat_command_whale (EmpathyChat *chat,
1003 GStrv strv)
1005 EmpathyChatPriv *priv = GET_PRIV (chat);
1006 TpMessage *message;
1008 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1009 "\n\n\n"
1010 "•_______________•");
1011 empathy_tp_chat_send (priv->tp_chat, message);
1012 g_object_unref (message);
1015 static void
1016 chat_command_babywhale (EmpathyChat *chat,
1017 GStrv strv)
1019 EmpathyChatPriv *priv = GET_PRIV (chat);
1020 TpMessage *message;
1022 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1023 "\n"
1024 "•_____•");
1025 empathy_tp_chat_send (priv->tp_chat, message);
1026 g_object_unref (message);
1029 static void
1030 chat_command_inspector (EmpathyChat *chat,
1031 GStrv strv)
1033 if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
1034 empathy_theme_adium_show_inspector (
1035 EMPATHY_THEME_ADIUM (chat->view));
1039 static void chat_command_help (EmpathyChat *chat, GStrv strv);
1041 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
1043 typedef struct {
1044 const gchar *prefix;
1045 guint min_parts;
1046 guint max_parts;
1047 ChatCommandFunc func;
1048 gboolean (*is_supported)(EmpathyChat *chat);
1049 const gchar *help;
1050 } ChatCommandItem;
1052 static ChatCommandItem commands[] = {
1053 {"clear", 1, 1, chat_command_clear, NULL,
1054 N_("/clear: clear all messages from the current conversation")},
1056 {"topic", 2, 2, chat_command_topic, NULL,
1057 N_("/topic <topic>: set the topic of the current conversation")},
1059 {"join", 2, 2, chat_command_join, NULL,
1060 N_("/join <chat room ID>: join a new chat room")},
1062 {"j", 2, 2, chat_command_join, NULL,
1063 N_("/j <chat room ID>: join a new chat room")},
1065 /* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
1066 {"part", 1, 3, chat_command_part, part_command_supported,
1067 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
1068 "by default the current one")},
1070 {"query", 2, 3, chat_command_query, NULL,
1071 N_("/query <contact ID> [<message>]: open a private chat")},
1073 {"msg", 3, 3, chat_command_msg, NULL,
1074 N_("/msg <contact ID> <message>: open a private chat")},
1076 {"nick", 2, 2, chat_command_nick, nick_command_supported,
1077 N_("/nick <nickname>: change your nickname on the current server")},
1079 {"me", 2, 2, chat_command_me, NULL,
1080 N_("/me <message>: send an ACTION message to the current conversation")},
1082 {"say", 2, 2, chat_command_say, NULL,
1083 N_("/say <message>: send <message> to the current conversation. "
1084 "This is used to send a message starting with a “/”. For example: "
1085 "“/say /join is used to join a new chat room”")},
1087 {"whois", 2, 2, chat_command_whois, NULL,
1088 N_("/whois <contact ID>: display information about a contact")},
1090 {"help", 1, 2, chat_command_help, NULL,
1091 N_("/help [<command>]: show all supported commands. "
1092 "If <command> is defined, show its usage.")},
1094 {"inspector", 1, 1, chat_command_inspector, NULL, NULL},
1096 {"whale", 1, 1, chat_command_whale, NULL, NULL},
1097 {"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
1100 static void
1101 chat_command_show_help (EmpathyChat *chat,
1102 ChatCommandItem *item)
1104 gchar *str;
1106 if (item->help == NULL) {
1107 return;
1110 str = g_strdup_printf (_("Usage: %s"), _(item->help));
1111 empathy_theme_adium_append_event (chat->view, str);
1112 g_free (str);
1115 static void
1116 chat_command_help (EmpathyChat *chat,
1117 GStrv strv)
1119 guint i;
1121 /* If <command> part is not defined,
1122 * strv[1] will be the terminal NULL */
1123 if (strv[1] == NULL) {
1124 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1125 if (commands[i].is_supported != NULL) {
1126 if (!commands[i].is_supported (chat)) {
1127 continue;
1130 if (commands[i].help == NULL) {
1131 continue;
1133 empathy_theme_adium_append_event (chat->view,
1134 _(commands[i].help));
1136 return;
1139 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1140 if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
1141 if (commands[i].is_supported != NULL) {
1142 if (!commands[i].is_supported (chat)) {
1143 break;
1146 if (commands[i].help == NULL) {
1147 break;
1149 chat_command_show_help (chat, &commands[i]);
1150 return;
1154 empathy_theme_adium_append_event (chat->view,
1155 _("Unknown command"));
1158 static GStrv
1159 chat_command_parse (const gchar *text, guint max_parts)
1161 GPtrArray *array;
1162 gchar *item;
1164 DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);
1166 array = g_ptr_array_sized_new (max_parts + 1);
1167 while (max_parts > 1) {
1168 const gchar *end;
1170 /* Skip white spaces */
1171 while (g_ascii_isspace (*text)) {
1172 text++;
1175 /* Search the end of this part, until first space. */
1176 for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
1177 /* Do nothing */;
1178 if (*end == '\0') {
1179 break;
1182 item = g_strndup (text, end - text);
1183 g_ptr_array_add (array, item);
1184 DEBUG ("\tITEM: \"%s\"", item);
1186 text = end;
1187 max_parts--;
1190 /* Append last part if not empty */
1191 item = g_strstrip (g_strdup (text));
1192 if (!TPAW_STR_EMPTY (item)) {
1193 g_ptr_array_add (array, item);
1194 DEBUG ("\tITEM: \"%s\"", item);
1195 } else {
1196 g_free (item);
1199 /* Make the array NULL-terminated */
1200 g_ptr_array_add (array, NULL);
1202 return (GStrv) g_ptr_array_free (array, FALSE);
1205 static gboolean
1206 has_prefix_case (const gchar *s,
1207 const gchar *prefix)
1209 return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
1212 static void
1213 chat_send (EmpathyChat *chat,
1214 const gchar *msg)
1216 EmpathyChatPriv *priv;
1217 TpMessage *message;
1218 guint i;
1220 if (TPAW_STR_EMPTY (msg)) {
1221 return;
1224 priv = GET_PRIV (chat);
1226 chat_input_history_add (chat, msg, FALSE);
1228 if (msg[0] == '/') {
1229 gboolean second_slash = FALSE;
1230 const gchar *iter = msg + 1;
1232 for (i = 0; i < G_N_ELEMENTS (commands); i++) {
1233 GStrv strv;
1234 guint strv_len;
1235 gchar c;
1237 if (!has_prefix_case (msg + 1, commands[i].prefix)) {
1238 continue;
1240 c = *(msg + 1 + strlen (commands[i].prefix));
1241 if (c != '\0' && !g_ascii_isspace (c)) {
1242 continue;
1244 if (commands[i].is_supported != NULL) {
1245 if (!commands[i].is_supported (chat)) {
1246 continue;
1250 /* We can't use g_strsplit here because it does
1251 * not deal correctly if we have more than one space
1252 * between args */
1253 strv = chat_command_parse (msg + 1, commands[i].max_parts);
1255 strv_len = g_strv_length (strv);
1256 if (strv_len < commands[i].min_parts ||
1257 strv_len > commands[i].max_parts) {
1258 chat_command_show_help (chat, &commands[i]);
1259 g_strfreev (strv);
1260 return;
1263 commands[i].func (chat, strv);
1264 g_strfreev (strv);
1265 return;
1268 /* Also allow messages with two slashes before the
1269 * first space, so it is possible to send a /unix/path.
1270 * This heuristic is kind of crap. */
1271 while (*iter != '\0' && !g_ascii_isspace (*iter)) {
1272 if (*iter == '/') {
1273 second_slash = TRUE;
1274 break;
1276 iter++;
1279 if (!second_slash) {
1280 empathy_theme_adium_append_event (chat->view,
1281 _("Unknown command; see /help for the available"
1282 " commands"));
1283 return;
1287 message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
1288 msg);
1289 empathy_tp_chat_send (priv->tp_chat, message);
1290 g_object_unref (message);
1293 static void
1294 chat_input_text_view_send (EmpathyChat *chat)
1296 GtkTextBuffer *buffer;
1297 GtkTextIter start, end;
1298 gchar *msg;
1300 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1302 gtk_text_buffer_get_bounds (buffer, &start, &end);
1303 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1305 /* clear the input field */
1306 gtk_text_buffer_set_text (buffer, "", -1);
1307 /* delete input history modifications */
1308 chat_input_history_revert (chat);
1310 chat_send (chat, msg);
1311 g_free (msg);
1314 static void
1315 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1316 TpContact *tp_contact,
1317 TpChannelChatState state,
1318 EmpathyChat *chat)
1320 EmpathyChatPriv *priv;
1321 GList *l;
1322 gboolean was_composing;
1323 EmpathyContact *contact;
1325 priv = GET_PRIV (chat);
1327 contact = empathy_contact_dup_from_tp_contact (tp_contact);
1329 if (empathy_contact_is_user (contact)) {
1330 /* We don't care about our own chat state */
1331 goto out;
1334 was_composing = (priv->compositors != NULL);
1336 /* Find the contact in the list. After that l is the list elem or NULL */
1337 for (l = priv->compositors; l; l = l->next) {
1338 if (contact == l->data) {
1339 break;
1343 switch (state) {
1344 case TP_CHANNEL_CHAT_STATE_GONE:
1345 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1346 case TP_CHANNEL_CHAT_STATE_PAUSED:
1347 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1348 /* Contact is not composing */
1349 if (l) {
1350 priv->compositors = g_list_remove_link (priv->compositors, l);
1351 g_object_unref (l->data);
1352 g_list_free1 (l);
1354 break;
1355 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1356 /* Contact is composing */
1357 if (!l) {
1358 priv->compositors = g_list_prepend (priv->compositors,
1359 g_object_ref (contact));
1361 break;
1362 default:
1363 g_assert_not_reached ();
1366 DEBUG ("Was composing: %s now composing: %s",
1367 was_composing ? "yes" : "no",
1368 priv->compositors ? "yes" : "no");
1370 if ((was_composing && !priv->compositors) ||
1371 (!was_composing && priv->compositors)) {
1372 /* Composing state changed */
1373 g_signal_emit (chat, signals[COMPOSING], 0,
1374 priv->compositors != NULL);
1377 out:
1378 g_object_unref (contact);
1381 static GRegex *
1382 get_highlight_regex_for (const gchar *name)
1384 GRegex *regex;
1385 gchar *name_esc, *pattern;
1386 GError *error = NULL;
1388 name_esc = g_regex_escape_string (name, -1);
1389 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
1390 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
1391 &error);
1393 if (regex == NULL) {
1394 DEBUG ("couldn't compile regex /%s/: %s", pattern,
1395 error->message);
1397 g_error_free (error);
1400 g_free (pattern);
1401 g_free (name_esc);
1403 return regex;
1406 /* Called when priv->self_contact changes, or priv->self_contact:alias changes.
1407 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
1409 static void
1410 chat_self_contact_alias_changed_cb (EmpathyChat *chat)
1412 EmpathyChatPriv *priv = GET_PRIV (chat);
1414 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
1416 if (priv->self_contact != NULL) {
1417 const gchar *alias = empathy_contact_get_alias (priv->self_contact);
1419 g_return_if_fail (alias != NULL);
1420 priv->highlight_regex = get_highlight_regex_for (alias);
1424 static gboolean
1425 chat_should_highlight (EmpathyChat *chat,
1426 EmpathyMessage *message)
1428 EmpathyChatPriv *priv = GET_PRIV (chat);
1429 const gchar *msg;
1431 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1433 if (!empathy_chat_is_room (chat)) {
1434 return FALSE;
1437 if (!empathy_message_is_incoming (message)) {
1438 return FALSE;
1441 msg = empathy_message_get_body (message);
1442 if (!msg) {
1443 return FALSE;
1446 if (empathy_message_is_backlog (message)) {
1447 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
1448 * have already been received by the user before (and so are in the logs) */
1449 return FALSE;
1452 if (priv->highlight_regex == NULL) {
1453 return FALSE;
1456 return g_regex_match (priv->highlight_regex, msg, 0, NULL);
1459 static void
1460 chat_message_received (EmpathyChat *chat,
1461 EmpathyMessage *message,
1462 gboolean pending)
1464 EmpathyChatPriv *priv = GET_PRIV (chat);
1465 EmpathyContact *sender;
1467 sender = empathy_message_get_sender (message);
1469 if (empathy_message_is_edit (message)) {
1470 DEBUG ("Editing message '%s' to '%s'",
1471 empathy_message_get_supersedes (message),
1472 empathy_message_get_body (message));
1474 empathy_theme_adium_edit_message (chat->view, message);
1475 } else {
1476 gboolean should_highlight = chat_should_highlight (chat, message);
1478 if (should_highlight) {
1479 priv->highlighted = TRUE;
1482 DEBUG ("Appending new message '%s' from %s (%d)",
1483 empathy_message_get_token (message),
1484 empathy_contact_get_alias (sender),
1485 empathy_contact_get_handle (sender));
1487 empathy_theme_adium_append_message (chat->view, message, should_highlight);
1489 if (empathy_message_is_incoming (message)) {
1490 priv->unread_messages++;
1491 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1494 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
1495 should_highlight);
1498 /* We received a message so the contact is no longer
1499 * composing */
1500 chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
1501 TP_CHANNEL_CHAT_STATE_ACTIVE,
1502 chat);
1505 static void
1506 chat_message_received_cb (EmpathyTpChat *tp_chat,
1507 EmpathyMessage *message,
1508 EmpathyChat *chat)
1510 chat_message_received (chat, message, FALSE);
1513 static void
1514 chat_message_acknowledged_cb (EmpathyTpChat *tp_chat,
1515 EmpathyMessage *message,
1516 EmpathyChat *chat)
1518 EmpathyChatPriv *priv = GET_PRIV (chat);
1520 empathy_theme_adium_message_acknowledged (chat->view,
1521 message);
1523 if (!empathy_message_is_edit (message)) {
1524 priv->unread_messages--;
1525 g_object_notify (G_OBJECT (chat), "nb-unread-messages");
1529 static void
1530 append_balance_error (EmpathyChat *chat,
1531 const gchar *message_body)
1533 EmpathyChatPriv *priv = GET_PRIV (chat);
1534 TpConnection *conn = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
1535 const gchar *uri = tp_connection_get_balance_uri (conn);
1536 const gchar *error = _("insufficient balance to send message");
1537 gchar *str, *str_markup = NULL;
1539 if (message_body != NULL) {
1540 str = g_strdup_printf (_("Error sending message “%s”: %s"), message_body, error);
1541 } else {
1542 str = g_strdup_printf (_("Error sending message: %s"), error);
1545 if (!tp_str_empty (uri)) {
1546 /* translators: error used when user doesn't have enough credit on his
1547 * account to send the message. */
1548 gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
1549 " <a href='%s'>Top up</a>."), uri);
1551 if (message_body != NULL) {
1552 gchar *escaped_body = g_markup_escape_text (message_body, -1);
1554 str_markup = g_strdup_printf (_("Error sending message “%s”: %s"),
1555 escaped_body, markup_error);
1557 g_free (escaped_body);
1558 } else {
1559 str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
1562 g_free (markup_error);
1565 if (str_markup != NULL)
1566 empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
1567 else
1568 empathy_theme_adium_append_event (chat->view, str);
1570 g_free (str);
1571 g_free (str_markup);
1574 static void
1575 chat_send_error_cb (EmpathyTpChat *tp_chat,
1576 const gchar *message_body,
1577 TpChannelTextSendError error_code,
1578 const gchar *dbus_error,
1579 EmpathyChat *chat)
1581 const gchar *error = NULL;
1582 gchar *str;
1584 if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
1585 append_balance_error (chat, message_body);
1586 return;
1587 } else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
1588 error = _("not capable");
1591 if (error == NULL) {
1592 /* if we didn't find a dbus-error, try the old error */
1593 switch (error_code) {
1594 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
1595 error = _("offline");
1596 break;
1597 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
1598 error = _("invalid contact");
1599 break;
1600 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
1601 error = _("permission denied");
1602 break;
1603 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
1604 error = _("too long message");
1605 break;
1606 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
1607 error = _("not implemented");
1608 break;
1609 case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
1610 default:
1611 error = _("unknown");
1612 break;
1616 if (message_body != NULL) {
1617 str = g_strdup_printf (_("Error sending message “%s”: %s"),
1618 message_body, error);
1620 else {
1621 str = g_strdup_printf (_("Error sending message: %s"), error);
1624 empathy_theme_adium_append_event (chat->view, str);
1625 g_free (str);
1628 static void
1629 chat_topic_label_size_allocate_cb (GtkLabel *label,
1630 GtkAllocation *allocation,
1631 EmpathyChat *chat)
1633 EmpathyChatPriv *priv = GET_PRIV (chat);
1635 if (!gtk_label_get_line_wrap (label)) {
1636 if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
1637 gtk_widget_show (priv->expander_topic);
1638 else
1639 gtk_widget_hide (priv->expander_topic);
1641 return;
1645 static void
1646 chat_topic_expander_activate_cb (GtkExpander *expander,
1647 GParamSpec *param_spec,
1648 EmpathyChat *chat)
1650 EmpathyChatPriv *priv = GET_PRIV (chat);
1652 if (gtk_expander_get_expanded (expander)) {
1653 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
1654 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
1655 } else {
1656 gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
1657 gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
1661 static void
1662 chat_subject_changed_cb (EmpathyChat *chat)
1664 EmpathyChatPriv *priv = GET_PRIV (chat);
1666 g_free (priv->subject);
1667 priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
1668 g_object_notify (G_OBJECT (chat), "subject");
1670 if (TPAW_STR_EMPTY (priv->subject)) {
1671 gtk_widget_hide (priv->hbox_topic);
1672 } else {
1673 gchar *markup_topic;
1674 gchar *markup_text;
1676 markup_topic = tpaw_add_link_markup (priv->subject);
1677 markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
1678 _("Topic:"), markup_topic);
1680 gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
1681 g_free (markup_text);
1682 g_free (markup_topic);
1684 gtk_widget_show (priv->hbox_topic);
1686 if (priv->block_events_timeout_id == 0) {
1687 gchar *str = NULL;
1689 if (!TPAW_STR_EMPTY (priv->subject)) {
1690 const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);
1692 if (tp_str_empty (actor)) {
1693 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
1694 } else {
1695 str = g_strdup_printf (_("Topic set by %s to: %s"),
1696 actor, priv->subject);
1698 } else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
1699 /* No need to display this 'event' is no topic can be defined anyway */
1700 str = g_strdup (_("No topic defined"));
1703 if (str != NULL) {
1704 empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
1705 g_free (str);
1710 static void
1711 chat_title_changed_cb (EmpathyChat *chat)
1713 EmpathyChatPriv *priv = GET_PRIV (chat);
1715 g_free (priv->name);
1716 priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
1717 g_object_notify (G_OBJECT (chat), "name");
1720 static gboolean
1721 chat_input_text_get_word_from_iter (GtkTextIter *iter,
1722 GtkTextIter *start,
1723 GtkTextIter *end)
1725 GtkTextIter word_start = *iter;
1726 GtkTextIter word_end = *iter;
1727 GtkTextIter tmp;
1729 if (gtk_text_iter_inside_word (&word_end) &&
1730 !gtk_text_iter_ends_word (&word_end)) {
1731 gtk_text_iter_forward_word_end (&word_end);
1734 tmp = word_end;
1736 if (gtk_text_iter_get_char (&tmp) == '\'') {
1737 gtk_text_iter_forward_char (&tmp);
1739 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1740 gtk_text_iter_forward_word_end (&word_end);
1745 if (gtk_text_iter_inside_word (&word_start) ||
1746 gtk_text_iter_ends_word (&word_start)) {
1747 if (!gtk_text_iter_starts_word (&word_start) ||
1748 gtk_text_iter_equal (&word_start, &word_end)) {
1749 gtk_text_iter_backward_word_start (&word_start);
1752 tmp = word_start;
1753 gtk_text_iter_backward_char (&tmp);
1755 if (gtk_text_iter_get_char (&tmp) == '\'') {
1756 gtk_text_iter_backward_char (&tmp);
1758 if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
1759 gtk_text_iter_backward_word_start (&word_start);
1764 *start = word_start;
1765 *end = word_end;
1766 return TRUE;
1769 static void
1770 chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
1771 GtkTextIter *location,
1772 gchar *text,
1773 gint len,
1774 EmpathyChat *chat)
1776 GtkTextIter iter, pos;
1778 /* Remove all misspelled tags in the inserted text.
1779 * This happens when text is inserted within a misspelled word. */
1780 gtk_text_buffer_get_iter_at_offset (buffer, &iter,
1781 gtk_text_iter_get_offset (location) - len);
1782 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1783 &iter, location);
1785 gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));
1787 do {
1788 GtkTextIter start, end;
1789 gchar *str;
1791 if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
1792 continue;
1794 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1796 if (gtk_text_iter_in_range (&pos, &start, &end) ||
1797 gtk_text_iter_equal (&pos, &end) ||
1798 empathy_spell_check (str)) {
1799 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
1800 } else {
1801 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
1804 g_free (str);
1806 } while (gtk_text_iter_forward_word_end (&iter) &&
1807 gtk_text_iter_compare (&iter, location) <= 0);
1810 static void
1811 chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
1812 GtkTextIter *start,
1813 GtkTextIter *end,
1814 EmpathyChat *chat)
1816 GtkTextIter word_start, word_end;
1818 if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
1819 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
1820 &word_start, &word_end);
1824 static void
1825 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
1826 EmpathyChat *chat)
1828 if (gtk_text_buffer_get_char_count (buffer) == 0) {
1829 chat_composing_stop (chat);
1830 } else {
1831 chat_composing_start (chat);
1835 static void
1836 chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
1837 GParamSpec *pspec,
1838 EmpathyChat *chat)
1840 GtkTextIter pos;
1841 GtkTextIter prev_pos;
1842 GtkTextIter word_start;
1843 GtkTextIter word_end;
1844 GtkTextMark *mark;
1845 gchar *str;
1847 mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");
1849 gtk_text_buffer_get_iter_at_mark (buffer, &pos,
1850 gtk_text_buffer_get_insert (buffer));
1851 gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);
1853 if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
1854 goto out;
1856 if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
1857 !gtk_text_iter_equal (&pos, &word_end)) {
1858 str = gtk_text_buffer_get_text (buffer,
1859 &word_start, &word_end, FALSE);
1861 if (!empathy_spell_check (str)) {
1862 gtk_text_buffer_apply_tag_by_name (buffer,
1863 "misspelled", &word_start, &word_end);
1864 } else {
1865 gtk_text_buffer_remove_tag_by_name (buffer,
1866 "misspelled", &word_start, &word_end);
1869 g_free (str);
1872 out:
1873 gtk_text_buffer_move_mark (buffer, mark, &pos);
1876 static gboolean
1877 empathy_isspace_cb (gunichar c,
1878 gpointer data)
1880 return g_unichar_isspace (c);
1883 static gboolean
1884 chat_input_key_press_event_cb (GtkWidget *widget,
1885 GdkEventKey *event,
1886 EmpathyChat *chat)
1888 EmpathyChatPriv *priv;
1889 GtkAdjustment *adj;
1890 gdouble val;
1891 GtkWidget *text_view_sw;
1893 priv = GET_PRIV (chat);
1895 priv->most_recent_event_type = event->type;
1897 /* Catch ctrl+up/down so we can traverse messages we sent */
1898 if ((event->state & GDK_CONTROL_MASK) &&
1899 (event->keyval == GDK_KEY_Up ||
1900 event->keyval == GDK_KEY_Down)) {
1901 GtkTextBuffer *buffer;
1902 const gchar *str;
1904 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1905 chat_input_history_update (chat, buffer);
1907 if (event->keyval == GDK_KEY_Up) {
1908 str = chat_input_history_get_next (chat);
1909 } else {
1910 str = chat_input_history_get_prev (chat);
1913 g_signal_handlers_block_by_func (buffer,
1914 chat_input_text_buffer_changed_cb,
1915 chat);
1916 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
1917 g_signal_handlers_unblock_by_func (buffer,
1918 chat_input_text_buffer_changed_cb,
1919 chat);
1921 return TRUE;
1924 /* Catch enter but not ctrl/shift-enter */
1925 if (IS_ENTER (event->keyval) &&
1926 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1927 GtkTextView *view;
1929 /* This is to make sure that kinput2 gets the enter. And if
1930 * it's handled there we shouldn't send on it. This is because
1931 * kinput2 uses Enter to commit letters. See:
1932 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
1935 view = GTK_TEXT_VIEW (chat->input_text_view);
1936 if (gtk_text_view_im_context_filter_keypress (view, event)) {
1937 gtk_text_view_reset_im_context (view);
1938 return TRUE;
1941 chat_input_text_view_send (chat);
1942 return TRUE;
1945 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
1947 if (IS_ENTER (event->keyval) &&
1948 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
1949 /* Newline for shift/control-enter. */
1950 return FALSE;
1952 if (!(event->state & GDK_CONTROL_MASK) &&
1953 event->keyval == GDK_KEY_Page_Up) {
1954 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1955 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
1956 return TRUE;
1958 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
1959 event->keyval == GDK_KEY_Page_Down) {
1960 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
1961 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
1962 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
1963 gtk_adjustment_set_value (adj, val);
1964 return TRUE;
1966 if (event->keyval == GDK_KEY_Escape) {
1967 empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
1969 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
1970 event->keyval == GDK_KEY_Tab) {
1971 GtkTextBuffer *buffer;
1972 GtkTextIter start, current;
1973 gchar *nick, *completed;
1974 GList *list, *completed_list;
1975 gboolean is_start_of_buffer;
1977 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
1978 gtk_text_buffer_get_iter_at_mark (buffer, &current, gtk_text_buffer_get_insert (buffer));
1980 /* Get the start of the nick to complete. */
1981 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
1982 if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
1983 gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
1985 is_start_of_buffer = gtk_text_iter_is_start (&start);
1987 list = empathy_tp_chat_get_members (priv->tp_chat);
1988 g_completion_add_items (priv->completion, list);
1990 nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
1991 completed_list = g_completion_complete (priv->completion,
1992 nick,
1993 &completed);
1995 g_free (nick);
1997 if (completed) {
1998 guint len;
1999 const gchar *text;
2000 GString *message = NULL;
2001 GList *l;
2003 gtk_text_buffer_delete (buffer, &start, &current);
2005 len = g_list_length (completed_list);
2007 if (len == 1) {
2008 /* If we only have one hit, use that text
2009 * instead of the text in completed since the
2010 * completed text will use the typed string
2011 * which might be cased all wrong.
2012 * Fixes #120876
2013 * */
2014 text = empathy_contact_get_alias (completed_list->data);
2015 } else {
2016 text = completed;
2018 /* Print all hits to the scrollback view, so the
2019 * user knows what possibilities he has.
2020 * Fixes #599779
2021 * */
2022 message = g_string_new ("");
2023 for (l = completed_list; l != NULL; l = l->next) {
2024 g_string_append (message, empathy_contact_get_alias (l->data));
2025 g_string_append (message, " - ");
2027 empathy_theme_adium_append_event (chat->view, message->str);
2028 g_string_free (message, TRUE);
2031 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
2033 if (len == 1 && is_start_of_buffer) {
2034 gchar *complete_char;
2036 complete_char = g_settings_get_string (
2037 priv->gsettings_chat,
2038 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);
2040 if (complete_char != NULL) {
2041 gtk_text_buffer_insert_at_cursor (buffer,
2042 complete_char,
2043 strlen (complete_char));
2044 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
2045 g_free (complete_char);
2049 g_free (completed);
2052 g_completion_clear_items (priv->completion);
2054 g_list_foreach (list, (GFunc) g_object_unref, NULL);
2055 g_list_free (list);
2057 return TRUE;
2060 return FALSE;
2063 static gboolean
2064 chat_text_view_focus_in_event_cb (GtkWidget *widget,
2065 GdkEvent *event,
2066 EmpathyChat *chat)
2068 gtk_widget_grab_focus (chat->input_text_view);
2070 return TRUE;
2073 static void
2074 chat_input_realize_cb (GtkWidget *widget,
2075 EmpathyChat *chat)
2077 DEBUG ("Setting focus to the input text view");
2078 if (gtk_widget_is_sensitive (widget)) {
2079 gtk_widget_grab_focus (widget);
2083 static void
2084 chat_input_has_focus_notify_cb (GtkWidget *widget,
2085 GParamSpec *pspec,
2086 EmpathyChat *chat)
2088 empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
2091 void
2092 empathy_chat_insert_smiley (GtkTextBuffer *buffer,
2093 EmpathySmiley *smiley)
2095 gtk_text_buffer_insert_at_cursor (buffer, smiley->str, -1);
2098 static void
2099 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
2100 EmpathySmiley *smiley,
2101 gpointer user_data)
2103 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2104 GtkTextBuffer *buffer;
2106 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2108 empathy_chat_insert_smiley (buffer, smiley);
2111 typedef struct {
2112 EmpathyChat *chat;
2113 gchar *word;
2115 GtkTextIter start;
2116 GtkTextIter end;
2117 } EmpathyChatSpell;
2119 static EmpathyChatSpell *
2120 chat_spell_new (EmpathyChat *chat,
2121 const gchar *word,
2122 GtkTextIter start,
2123 GtkTextIter end)
2125 EmpathyChatSpell *chat_spell;
2127 chat_spell = g_slice_new0 (EmpathyChatSpell);
2129 chat_spell->chat = g_object_ref (chat);
2130 chat_spell->word = g_strdup (word);
2131 chat_spell->start = start;
2132 chat_spell->end = end;
2134 return chat_spell;
2137 static void
2138 chat_spell_free (EmpathyChatSpell *chat_spell)
2140 g_object_unref (chat_spell->chat);
2141 g_free (chat_spell->word);
2142 g_slice_free (EmpathyChatSpell, chat_spell);
2145 static void
2146 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
2147 EmpathyChatSpell *chat_spell)
2149 empathy_chat_correct_word (chat_spell->chat,
2150 &(chat_spell->start),
2151 &(chat_spell->end),
2152 gtk_menu_item_get_label (menu_item));
2156 static GtkWidget *
2157 chat_spelling_build_suggestions_menu (const gchar *code,
2158 EmpathyChatSpell *chat_spell)
2160 GList *suggestions, *l;
2161 GtkWidget *menu, *menu_item;
2163 suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
2164 if (suggestions == NULL)
2165 return NULL;
2167 menu = gtk_menu_new ();
2168 for (l = suggestions; l; l = l->next) {
2169 menu_item = gtk_menu_item_new_with_label (l->data);
2170 g_signal_connect (G_OBJECT (menu_item), "activate",
2171 G_CALLBACK (chat_spelling_menu_activate_cb),
2172 chat_spell);
2173 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
2175 empathy_spell_free_suggestions (suggestions);
2177 gtk_widget_show_all (menu);
2179 return menu;
2182 static GtkWidget *
2183 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
2185 GtkWidget *menu, *submenu, *item;
2186 GList *codes, *l;
2188 codes = empathy_spell_get_enabled_language_codes ();
2189 g_assert (codes != NULL);
2191 if (g_list_length (codes) > 1) {
2192 menu = gtk_menu_new ();
2194 for (l = codes; l; l = l->next) {
2195 const gchar *code, *name;
2197 code = l->data;
2198 name = empathy_spell_get_language_name (code);
2199 if (!name)
2200 continue;
2202 item = gtk_image_menu_item_new_with_label (name);
2204 submenu = chat_spelling_build_suggestions_menu (
2205 code, chat_spell);
2206 if (submenu == NULL)
2207 gtk_widget_set_sensitive (item, FALSE);
2208 else
2209 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
2210 submenu);
2211 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2213 } else {
2214 menu = chat_spelling_build_suggestions_menu (codes->data,
2215 chat_spell);
2216 if (menu == NULL) {
2217 menu = gtk_menu_new ();
2218 item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
2219 gtk_widget_set_sensitive (item, FALSE);
2220 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
2223 g_list_free (codes);
2225 gtk_widget_show_all (menu);
2227 return menu;
2230 typedef struct {
2231 EmpathyChat *chat;
2232 gchar *word;
2233 gchar *code;
2234 } EmpathyChatWord;
2236 static EmpathyChatWord *
2237 chat_word_new (EmpathyChat *chat,
2238 const gchar *word,
2239 const gchar *code)
2241 EmpathyChatWord *chat_word;
2243 chat_word = g_slice_new0 (EmpathyChatWord);
2245 chat_word->chat = g_object_ref (chat);
2246 chat_word->word = g_strdup (word);
2247 chat_word->code = g_strdup (code);
2249 return chat_word;
2252 static void
2253 chat_word_free (EmpathyChatWord *chat_word)
2255 g_object_unref (chat_word->chat);
2256 g_free (chat_word->word);
2257 g_free (chat_word->code);
2258 g_slice_free (EmpathyChatWord, chat_word);
2261 static void
2262 chat_add_to_dictionary_activate_cb (GtkMenuItem *menu_item,
2263 EmpathyChatWord *chat_word)
2265 EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);
2267 empathy_spell_add_to_dictionary (chat_word->code,
2268 chat_word->word);
2269 priv->update_misspelled_words_id =
2270 g_idle_add (update_misspelled_words, chat_word->chat);
2273 static GtkWidget *
2274 chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
2276 GtkWidget *menu, *item, *lang_item, *image;
2277 GList *codes, *l;
2278 gchar *label;
2279 const gchar *code, *name;
2280 EmpathyChatWord *chat_word;
2282 codes = empathy_spell_get_enabled_language_codes ();
2283 g_assert (codes != NULL);
2284 if (g_list_length (codes) > 1) {
2285 /* translators: %s is the selected word */
2286 label = g_strdup_printf (_("Add “%s” to Dictionary"),
2287 chat_spell->word);
2288 item = gtk_image_menu_item_new_with_mnemonic (label);
2289 g_free (label);
2290 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2291 GTK_ICON_SIZE_MENU);
2292 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2293 image);
2295 menu = gtk_menu_new ();
2297 for (l = codes; l; l = l->next) {
2298 code = l->data;
2299 name = empathy_spell_get_language_name (code);
2300 if (name == NULL)
2301 continue;
2303 lang_item = gtk_image_menu_item_new_with_label (name);
2305 chat_word= chat_word_new (chat_spell->chat,
2306 chat_spell->word, code);
2307 g_object_set_data_full (G_OBJECT (lang_item),
2308 "chat-word", chat_word,
2309 (GDestroyNotify) chat_word_free);
2311 g_signal_connect (G_OBJECT (lang_item), "activate",
2312 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2313 chat_word);
2314 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
2316 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
2317 } else {
2318 code = codes->data;
2319 name = empathy_spell_get_language_name (code);
2320 g_assert (name != NULL);
2321 /* translators: first %s is the selected word,
2322 * second %s is the language name of the target dictionary */
2323 label = g_strdup_printf (_("Add “%s” to %s Dictionary"),
2324 chat_spell->word, name);
2325 item = gtk_image_menu_item_new_with_mnemonic (label);
2326 g_free (label);
2327 image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
2328 GTK_ICON_SIZE_MENU);
2329 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2331 chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
2332 code);
2333 g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
2334 (GDestroyNotify) chat_word_free);
2336 g_signal_connect (G_OBJECT (item), "activate",
2337 G_CALLBACK (chat_add_to_dictionary_activate_cb),
2338 chat_word);
2340 g_list_free (codes);
2342 gtk_widget_show_all (item);
2344 return item;
2347 static void
2348 chat_text_send_cb (GtkMenuItem *menuitem,
2349 EmpathyChat *chat)
2351 chat_input_text_view_send (chat);
2354 static gboolean
2355 chat_input_button_press_event_cb (GtkTextView *view,
2356 GdkEventButton *event,
2357 EmpathyChat *chat)
2359 EmpathyChatPriv *priv = GET_PRIV (chat);
2361 priv->most_recent_event_type = event->type;
2363 return FALSE;
2366 static void
2367 chat_input_populate_popup_cb (GtkTextView *view,
2368 GtkMenu *menu,
2369 EmpathyChat *chat)
2371 EmpathyChatPriv *priv = GET_PRIV (chat);
2372 GtkTextBuffer *buffer;
2373 GtkTextTagTable *table;
2374 GtkTextTag *tag;
2375 gint x, y;
2376 GtkTextIter iter, start, end;
2377 GtkWidget *item;
2378 gchar *str = NULL;
2379 EmpathyChatSpell *chat_spell;
2380 GtkWidget *spell_menu;
2381 GtkWidget *spell_item;
2382 EmpathySmileyManager *smiley_manager;
2383 GtkWidget *smiley_menu;
2384 GtkWidget *image;
2386 buffer = gtk_text_view_get_buffer (view);
2388 /* Add the emoticon menu. */
2389 item = gtk_separator_menu_item_new ();
2390 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2391 gtk_widget_show (item);
2393 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
2394 image = gtk_image_new_from_icon_name ("face-smile",
2395 GTK_ICON_SIZE_MENU);
2396 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2397 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2398 gtk_widget_show (item);
2400 smiley_manager = empathy_smiley_manager_dup_singleton ();
2401 smiley_menu = empathy_smiley_menu_new (smiley_manager,
2402 chat_insert_smiley_activate_cb,
2403 chat);
2404 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
2405 g_object_unref (smiley_manager);
2407 /* Add the Send menu item. */
2408 gtk_text_buffer_get_bounds (buffer, &start, &end);
2409 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
2410 if (!TPAW_STR_EMPTY (str)) {
2411 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
2412 g_signal_connect (G_OBJECT (item), "activate",
2413 G_CALLBACK (chat_text_send_cb), chat);
2414 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2415 gtk_widget_show (item);
2417 str = NULL;
2419 /* Add the spell check menu item. */
2420 table = gtk_text_buffer_get_tag_table (buffer);
2421 tag = gtk_text_tag_table_lookup (table, "misspelled");
2423 switch (priv->most_recent_event_type) {
2424 case GDK_BUTTON_PRESS:
2425 /* get the location from the pointer */
2426 gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
2427 gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
2428 gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
2430 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
2431 GTK_TEXT_WINDOW_WIDGET,
2432 x, y,
2433 &x, &y);
2434 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
2435 &iter, x, y);
2436 break;
2438 default:
2439 g_warn_if_reached ();
2440 /* assume the KEY_PRESS case */
2442 case GDK_KEY_PRESS:
2443 /* get the location from the cursor */
2444 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
2445 gtk_text_buffer_get_insert (buffer));
2446 break;
2450 start = end = iter;
2451 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
2452 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
2454 str = gtk_text_buffer_get_text (buffer,
2455 &start, &end, FALSE);
2457 if (!TPAW_STR_EMPTY (str)) {
2458 chat_spell = chat_spell_new (chat, str, start, end);
2459 g_object_set_data_full (G_OBJECT (menu),
2460 "chat-spell", chat_spell,
2461 (GDestroyNotify) chat_spell_free);
2463 item = gtk_separator_menu_item_new ();
2464 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2465 gtk_widget_show (item);
2467 /* Spelling suggestions */
2468 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
2469 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
2470 GTK_ICON_SIZE_MENU);
2471 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
2473 spell_menu = chat_spelling_build_menu (chat_spell);
2474 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
2477 spell_item = gtk_separator_menu_item_new ();
2478 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2479 gtk_widget_show (spell_item);
2481 /* Add to dictionary */
2482 spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);
2484 gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
2485 gtk_widget_show (spell_item);
2487 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
2488 gtk_widget_show (item);
2493 static gboolean
2494 chat_log_filter (TplEvent *event,
2495 gpointer user_data)
2497 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2498 EmpathyChatPriv *priv = GET_PRIV (chat);
2499 EmpathyMessage *message;
2500 const GList *pending;
2501 bool retval = FALSE;
2503 g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
2504 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2506 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2507 message = empathy_message_from_tpl_log_event (event);
2509 for (; pending; pending = g_list_next (pending)) {
2510 if (empathy_message_equal (message, pending->data))
2511 goto out;
2514 retval = TRUE;
2516 out:
2517 g_object_unref (message);
2518 return retval;
2521 static void
2522 show_pending_messages (EmpathyChat *chat) {
2523 EmpathyChatPriv *priv = GET_PRIV (chat);
2524 const GList *messages, *l;
2526 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2527 g_return_if_fail (chat->view != NULL);
2528 g_return_if_fail (priv->tp_chat != NULL);
2530 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
2532 for (l = messages; l != NULL ; l = g_list_next (l)) {
2533 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
2534 chat_message_received (chat, message, TRUE);
2538 #if 0
2539 /* FIXME: We need to figure out how to fill backlog automatically with WebKit2 */
2540 static gboolean
2541 chat_scrollable_set_value (gpointer user_data)
2543 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2544 EmpathyChatPriv *priv = GET_PRIV (chat);
2545 GtkAdjustment *adjustment;
2546 guint upper;
2548 adjustment = gtk_scrollable_get_vadjustment (
2549 GTK_SCROLLABLE (chat->view));
2551 /* Set the chat->view's adjustment back to the value it had
2552 * before it grew as a result of new logs being inserted.
2554 upper = (guint) gtk_adjustment_get_upper (adjustment);
2555 gtk_adjustment_set_value (adjustment, upper - priv->scroll_offset);
2557 return G_SOURCE_REMOVE;
2559 #endif
2561 static void
2562 got_filtered_messages_cb (GObject *walker,
2563 GAsyncResult *result,
2564 gpointer user_data)
2566 GList *l;
2567 GList *messages;
2568 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2569 EmpathyChatPriv *priv = GET_PRIV (chat);
2570 GError *error = NULL;
2572 if (!tpl_log_walker_get_events_finish (TPL_LOG_WALKER (walker),
2573 result, &messages, &error)) {
2574 DEBUG ("%s. Aborting.", error->message);
2575 empathy_theme_adium_append_event (chat->view,
2576 _("Failed to retrieve recent logs"));
2577 g_error_free (error);
2578 goto out;
2581 for (l = g_list_last (messages); l; l = g_list_previous (l)) {
2582 EmpathyMessage *message;
2584 g_assert (TPL_IS_EVENT (l->data));
2586 message = empathy_message_from_tpl_log_event (l->data);
2587 g_object_unref (l->data);
2589 if (empathy_message_is_edit (message)) {
2590 /* this is an edited message, create a synthetic event
2591 * using the supersedes token and
2592 * original-message-sent timestamp, that we can then
2593 * replace */
2594 EmpathyMessage *syn_msg = g_object_new (
2595 EMPATHY_TYPE_MESSAGE,
2596 "body", "",
2597 "token", empathy_message_get_supersedes (message),
2598 "type", empathy_message_get_tptype (message),
2599 "timestamp", empathy_message_get_original_timestamp (message),
2600 "incoming", empathy_message_is_incoming (message),
2601 "is-backlog", TRUE,
2602 "receiver", empathy_message_get_receiver (message),
2603 "sender", empathy_message_get_sender (message),
2604 NULL);
2606 empathy_theme_adium_prepend_message (chat->view, syn_msg,
2607 chat_should_highlight (chat, syn_msg));
2608 empathy_theme_adium_edit_message (chat->view, message);
2610 g_object_unref (syn_msg);
2611 } else {
2612 /* append the latest message */
2613 empathy_theme_adium_prepend_message (chat->view, message,
2614 chat_should_highlight (chat, message));
2617 g_object_unref (message);
2619 g_list_free (messages);
2621 out:
2622 /* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
2623 * about it in EmpathyChatPriv definition */
2624 priv->retrieving_backlogs = FALSE;
2625 empathy_chat_messages_read (chat);
2627 /* Turn back on scrolling */
2628 empathy_theme_adium_scroll (chat->view, TRUE);
2630 #if 0
2631 /* FIXME: We need to figure out how to fill backlog automatically with WebKit2 */
2632 /* We start watching the scrolling movements only after the first
2633 * batch of logs have been fetched. Otherwise, if the
2634 * chat->view's page size is too small the scrollbar might hit
2635 * the upper edge and trigger another batch of logs to be
2636 * fetched.
2638 if (G_UNLIKELY (!priv->watch_scroll &&
2639 !tpl_log_walker_is_end (priv->log_walker))) {
2640 priv->watch_scroll = TRUE;
2641 g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_connect,
2642 g_object_ref (chat), g_object_unref);
2644 else {
2645 GtkAdjustment *adjustment;
2646 guint upper;
2647 guint value;
2649 /* The chat->view's adjustment won't change unless we
2650 * return to the main loop. Save the current offset
2651 * from the lower edge (or the upper value of the
2652 * adjustment) so that we can restore it later once the
2653 * adjustment grows.
2655 adjustment = gtk_scrollable_get_vadjustment (
2656 GTK_SCROLLABLE (chat->view));
2657 upper = (guint) gtk_adjustment_get_upper (adjustment);
2658 value = (guint) gtk_adjustment_get_value (adjustment);
2659 priv->scroll_offset = upper - value;
2661 g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_set_value,
2662 g_object_ref (chat), g_object_unref);
2664 #endif
2665 g_object_unref (chat);
2668 static gboolean
2669 chat_add_logs (EmpathyChat *chat)
2671 EmpathyChatPriv *priv = GET_PRIV (chat);
2673 if (!priv->id) {
2674 return G_SOURCE_REMOVE;
2677 /* Turn off scrolling temporarily */
2678 empathy_theme_adium_scroll (chat->view, FALSE);
2680 tpl_log_walker_get_events_async (priv->log_walker, 5,
2681 got_filtered_messages_cb, g_object_ref (chat));
2683 return G_SOURCE_REMOVE;
2686 #if 0
2687 /* FIXME: We need to figure out how to fill backlog automatically with WebKit2 */
2688 static void
2689 chat_schedule_logs (EmpathyChat *chat)
2691 EmpathyChatPriv *priv = GET_PRIV (chat);
2693 if (priv->retrieving_backlogs)
2694 return;
2696 priv->retrieving_backlogs = TRUE;
2697 g_timeout_add_full (G_PRIORITY_LOW, 500, /* ms */
2698 (GSourceFunc) chat_add_logs, g_object_ref (chat), g_object_unref);
2701 static void
2702 chat_view_adjustment_changed_cb (GtkAdjustment *adjustment,
2703 gpointer user_data)
2705 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2706 EmpathyChatPriv *priv = GET_PRIV (chat);
2707 guint page_size;
2709 if (tpl_log_walker_is_end (priv->log_walker)) {
2710 g_signal_handlers_disconnect_by_func (adjustment,
2711 chat_view_adjustment_changed_cb, user_data);
2712 return;
2715 page_size = (guint) gtk_adjustment_get_page_size (adjustment);
2716 if (page_size <= priv->max_page_size)
2717 return;
2719 /* We need to fetch more logs if the page size of the view
2720 * increases, so that there is no empty space at the top.
2722 if (G_LIKELY (priv->max_page_size != 0))
2723 chat_schedule_logs (chat);
2725 priv->max_page_size = page_size;
2728 static void
2729 chat_view_adjustment_value_changed_cb (GtkAdjustment *adjustment,
2730 gpointer user_data)
2732 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2733 EmpathyChatPriv *priv = GET_PRIV (chat);
2734 guint lower;
2735 guint value;
2737 if (tpl_log_walker_is_end (priv->log_walker)) {
2738 g_signal_handlers_disconnect_by_func (adjustment,
2739 chat_view_adjustment_value_changed_cb, user_data);
2740 return;
2743 lower = (guint) gtk_adjustment_get_lower (adjustment);
2744 value = (guint) gtk_adjustment_get_value (adjustment);
2745 if (value != lower)
2746 return;
2748 /* Request for more logs to be fetched if the user hit the
2749 * upper edge of the chat->view.
2751 chat_schedule_logs (chat);
2754 static gboolean
2755 chat_scrollable_connect (gpointer user_data)
2757 EmpathyChat *chat = EMPATHY_CHAT (user_data);
2758 GtkAdjustment *adjustment;
2760 adjustment = gtk_scrollable_get_vadjustment (
2761 GTK_SCROLLABLE (chat->view));
2763 g_signal_connect (adjustment, "changed",
2764 G_CALLBACK (chat_view_adjustment_changed_cb), chat);
2765 g_signal_connect (adjustment, "value-changed",
2766 G_CALLBACK (chat_view_adjustment_value_changed_cb), chat);
2768 return G_SOURCE_REMOVE;
2770 #endif
2772 static gint
2773 chat_contacts_completion_func (const gchar *s1,
2774 const gchar *s2,
2775 gsize n)
2777 gchar *tmp, *nick1, *nick2;
2778 gint ret;
2780 if (s1 == s2) {
2781 return 0;
2783 if (!s1 || !s2) {
2784 return s1 ? -1 : +1;
2787 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
2788 nick1 = g_utf8_casefold (tmp, -1);
2789 g_free (tmp);
2791 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
2792 nick2 = g_utf8_casefold (tmp, -1);
2793 g_free (tmp);
2795 ret = strncmp (nick1, nick2, n);
2797 g_free (nick1);
2798 g_free (nick2);
2800 return ret;
2803 static gchar *
2804 build_part_message (guint reason,
2805 const gchar *name,
2806 EmpathyContact *actor,
2807 const gchar *message)
2809 GString *s = g_string_new ("");
2810 const gchar *actor_name = NULL;
2812 if (actor != NULL) {
2813 actor_name = empathy_contact_get_alias (actor);
2816 /* Having an actor only really makes sense for a few actions... */
2817 switch (reason) {
2818 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
2819 g_string_append_printf (s, _("%s has disconnected"), name);
2820 break;
2821 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
2822 if (actor_name != NULL) {
2823 /* translators: reverse the order of these arguments
2824 * if the kicked should come before the kicker in your locale.
2826 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
2827 name, actor_name);
2828 } else {
2829 g_string_append_printf (s, _("%s was kicked"), name);
2831 break;
2832 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
2833 if (actor_name != NULL) {
2834 /* translators: reverse the order of these arguments
2835 * if the banned should come before the banner in your locale.
2837 g_string_append_printf (s, _("%1$s was banned by %2$s"),
2838 name, actor_name);
2839 } else {
2840 g_string_append_printf (s, _("%s was banned"), name);
2842 break;
2843 default:
2844 g_string_append_printf (s, _("%s has left the room"), name);
2847 if (!TPAW_STR_EMPTY (message)) {
2848 /* Note to translators: this string is appended to
2849 * notifications like "foo has left the room", with the message
2850 * given by the user living the room. If this poses a problem,
2851 * please let us know. :-)
2853 g_string_append_printf (s, _(" (%s)"), message);
2856 return g_string_free (s, FALSE);
2859 static void
2860 chat_members_changed_cb (EmpathyTpChat *tp_chat,
2861 EmpathyContact *contact,
2862 EmpathyContact *actor,
2863 guint reason,
2864 gchar *message,
2865 gboolean is_member,
2866 EmpathyChat *chat)
2868 EmpathyChatPriv *priv = GET_PRIV (chat);
2869 const gchar *name = empathy_contact_get_alias (contact);
2870 gchar *str;
2872 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);
2874 if (priv->block_events_timeout_id != 0)
2875 return;
2877 if (is_member) {
2878 str = g_strdup_printf (_("%s has joined the room"),
2879 name);
2880 } else {
2881 str = build_part_message (reason, name, actor, message);
2884 empathy_theme_adium_append_event (chat->view, str);
2885 g_free (str);
2888 static void
2889 chat_member_renamed_cb (EmpathyTpChat *tp_chat,
2890 EmpathyContact *old_contact,
2891 EmpathyContact *new_contact,
2892 guint reason,
2893 gchar *message,
2894 EmpathyChat *chat)
2896 EmpathyChatPriv *priv = GET_PRIV (chat);
2898 g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);
2900 if (priv->block_events_timeout_id == 0) {
2901 gchar *str;
2903 str = g_strdup_printf (_("%s is now known as %s"),
2904 empathy_contact_get_alias (old_contact),
2905 empathy_contact_get_alias (new_contact));
2906 empathy_theme_adium_append_event (chat->view, str);
2907 g_free (str);
2912 static gboolean
2913 chat_contacts_visible_timeout_cb (gpointer chat)
2915 EmpathyChatPriv *priv = GET_PRIV (chat);
2917 /* Relax the size request */
2918 gtk_widget_set_size_request (priv->vbox_left, -1, -1);
2920 /* Set the position of the slider. This must be done here because
2921 * GtkPaned need to know its size allocation and it will be settled only
2922 * after the gtk_window_resize () tough effect. */
2923 if (priv->contacts_width > 0) {
2924 gtk_paned_set_position (GTK_PANED (priv->hpaned),
2925 priv->contacts_width);
2928 priv->contacts_visible_id = 0;
2930 return FALSE;
2933 static void
2934 chat_update_contacts_visibility (EmpathyChat *chat,
2935 gboolean show)
2937 EmpathyChatPriv *priv = GET_PRIV (chat);
2939 if (!priv->scrolled_window_contacts) {
2940 return;
2943 if (priv->remote_contact != NULL) {
2944 show = FALSE;
2947 if (show && priv->contact_list_view == NULL) {
2948 EmpathyIndividualStore *store;
2949 gint min_width;
2950 GtkAllocation allocation;
2952 /* We are adding the contact list to the chat, we don't want the
2953 * chat view to become too small. If the chat view is already
2954 * smaller than 250 make sure that size won't change. If the
2955 * chat view is bigger the contact list will take some space on
2956 * it but we make sure the chat view don't become smaller than
2957 * 250. Relax the size request once the resize is done */
2958 gtk_widget_get_allocation (priv->vbox_left, &allocation);
2959 min_width = MIN (allocation.width, 250);
2960 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
2962 /* There is no way to know when the window resize will happen
2963 * since it is WM's decision. Let's hope it won't be longer. */
2964 if (priv->contacts_visible_id != 0)
2965 g_source_remove (priv->contacts_visible_id);
2966 priv->contacts_visible_id = g_timeout_add (500,
2967 chat_contacts_visible_timeout_cb, chat);
2969 store = EMPATHY_INDIVIDUAL_STORE (
2970 empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));
2972 empathy_individual_store_set_show_groups (store, FALSE);
2974 priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
2975 EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
2976 EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
2977 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
2978 EMPATHY_INDIVIDUAL_FEATURE_CALL |
2979 EMPATHY_INDIVIDUAL_FEATURE_LOG |
2980 EMPATHY_INDIVIDUAL_FEATURE_INFO));
2982 empathy_individual_view_set_show_offline (
2983 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2984 empathy_individual_view_set_show_uninteresting (
2985 EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
2987 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
2988 priv->contact_list_view);
2990 gtk_widget_show (priv->contact_list_view);
2991 gtk_widget_show (priv->scrolled_window_contacts);
2992 g_object_unref (store);
2993 } else if (!show) {
2994 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
2995 gtk_widget_hide (priv->scrolled_window_contacts);
2996 if (priv->contact_list_view != NULL) {
2997 gtk_widget_destroy (priv->contact_list_view);
2998 priv->contact_list_view = NULL;
3003 void
3004 empathy_chat_set_show_contacts (EmpathyChat *chat,
3005 gboolean show)
3007 EmpathyChatPriv *priv = GET_PRIV (chat);
3009 priv->show_contacts = show;
3011 chat_update_contacts_visibility (chat, show);
3013 g_object_notify (G_OBJECT (chat), "show-contacts");
3016 static void
3017 chat_self_contact_changed_cb (EmpathyChat *chat)
3019 EmpathyChatPriv *priv = GET_PRIV (chat);
3021 if (priv->self_contact != NULL) {
3022 g_signal_handlers_disconnect_by_func (priv->self_contact,
3023 chat_self_contact_alias_changed_cb,
3024 chat);
3026 g_clear_object (&priv->self_contact);
3028 priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
3029 if (priv->self_contact != NULL) {
3030 g_object_ref (priv->self_contact);
3032 if (empathy_chat_is_room (chat)) {
3033 g_signal_connect_swapped (priv->self_contact, "notify::alias",
3034 G_CALLBACK (chat_self_contact_alias_changed_cb),
3035 chat);
3039 chat_self_contact_alias_changed_cb (chat);
3042 static void
3043 chat_remote_contact_changed_cb (EmpathyChat *chat)
3045 EmpathyChatPriv *priv = GET_PRIV (chat);
3047 if (priv->remote_contact != NULL) {
3048 g_object_unref (priv->remote_contact);
3049 priv->remote_contact = NULL;
3052 g_free (priv->id);
3054 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
3055 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
3056 if (priv->remote_contact != NULL) {
3057 g_object_ref (priv->remote_contact);
3058 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
3060 else if (priv->tp_chat != NULL) {
3061 tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
3064 chat_update_contacts_visibility (chat, priv->show_contacts);
3066 g_object_notify (G_OBJECT (chat), "remote-contact");
3067 g_object_notify (G_OBJECT (chat), "id");
3070 static void
3071 chat_invalidated_cb (EmpathyTpChat *tp_chat,
3072 guint domain,
3073 gint code,
3074 gchar *message,
3075 EmpathyChat *chat)
3077 EmpathyChatPriv *priv;
3079 priv = GET_PRIV (chat);
3081 if (!priv->tp_chat) {
3082 return;
3085 chat_composing_remove_timeout (chat);
3086 g_object_unref (priv->tp_chat);
3087 priv->tp_chat = NULL;
3088 g_object_notify (G_OBJECT (chat), "tp-chat");
3090 empathy_theme_adium_append_event (chat->view, _("Disconnected"));
3091 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
3093 chat_update_contacts_visibility (chat, FALSE);
3095 priv->unread_messages_when_offline = priv->unread_messages;
3098 static gboolean
3099 update_misspelled_words (gpointer data)
3101 EmpathyChat *chat = EMPATHY_CHAT (data);
3102 EmpathyChatPriv *priv = GET_PRIV (chat);
3103 GtkTextBuffer *buffer;
3104 GtkTextIter iter;
3105 gint length;
3107 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3109 gtk_text_buffer_get_end_iter (buffer, &iter);
3110 length = gtk_text_iter_get_offset (&iter);
3111 chat_input_text_buffer_insert_text_cb (buffer, &iter,
3112 NULL, length, chat);
3114 priv->update_misspelled_words_id = 0;
3116 return FALSE;
3119 static void
3120 conf_spell_checking_cb (GSettings *gsettings_chat,
3121 const gchar *key,
3122 gpointer user_data)
3124 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3125 EmpathyChatPriv *priv = GET_PRIV (chat);
3126 gboolean spell_checker;
3127 GtkTextBuffer *buffer;
3129 if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
3130 return;
3132 spell_checker = g_settings_get_boolean (gsettings_chat,
3133 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);
3135 if (!empathy_spell_supported ()) {
3136 spell_checker = FALSE;
3139 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3141 if (spell_checker == priv->spell_checking_enabled) {
3142 if (spell_checker) {
3143 /* Possibly changed dictionaries,
3144 * update misspelled words. Need to do so in idle
3145 * so the spell checker is updated. */
3146 priv->update_misspelled_words_id =
3147 g_idle_add (update_misspelled_words, chat);
3150 return;
3153 if (spell_checker) {
3154 GtkTextIter iter;
3156 priv->notify_cursor_position_id = tp_g_signal_connect_object (
3157 buffer, "notify::cursor-position",
3158 G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
3159 chat, 0);
3160 priv->insert_text_id = tp_g_signal_connect_object (
3161 buffer, "insert-text",
3162 G_CALLBACK (chat_input_text_buffer_insert_text_cb),
3163 chat, G_CONNECT_AFTER);
3164 priv->delete_range_id = tp_g_signal_connect_object (
3165 buffer, "delete-range",
3166 G_CALLBACK (chat_input_text_buffer_delete_range_cb),
3167 chat, G_CONNECT_AFTER);
3169 gtk_text_buffer_create_tag (buffer, "misspelled",
3170 "underline", PANGO_UNDERLINE_ERROR,
3171 NULL);
3173 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
3174 gtk_text_buffer_get_insert (buffer));
3175 gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
3176 &iter, TRUE);
3178 /* Mark misspelled words in the existing buffer.
3179 * Need to do so in idle so the spell checker is updated. */
3180 priv->update_misspelled_words_id =
3181 g_idle_add (update_misspelled_words, chat);
3182 } else {
3183 GtkTextTagTable *table;
3184 GtkTextTag *tag;
3186 g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
3187 priv->notify_cursor_position_id = 0;
3188 g_signal_handler_disconnect (buffer, priv->insert_text_id);
3189 priv->insert_text_id = 0;
3190 g_signal_handler_disconnect (buffer, priv->delete_range_id);
3191 priv->delete_range_id = 0;
3193 table = gtk_text_buffer_get_tag_table (buffer);
3194 tag = gtk_text_tag_table_lookup (table, "misspelled");
3195 gtk_text_tag_table_remove (table, tag);
3197 gtk_text_buffer_delete_mark_by_name (buffer,
3198 "previous-cursor-position");
3201 priv->spell_checking_enabled = spell_checker;
3204 static gboolean
3205 save_paned_pos_timeout (gpointer data)
3207 EmpathyChat *self = data;
3208 gint hpaned_pos;
3210 self->priv->save_paned_pos_id = 0;
3212 hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));
3214 g_settings_set_int (self->priv->gsettings_ui,
3215 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
3216 hpaned_pos);
3218 return FALSE;
3221 static gboolean
3222 chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
3223 GParamSpec *spec,
3224 gpointer user_data)
3226 EmpathyChat *chat = EMPATHY_CHAT (user_data);
3228 if (chat->priv->save_paned_pos_id != 0)
3229 g_source_remove (chat->priv->save_paned_pos_id);
3231 chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
3232 save_paned_pos_timeout, chat);
3234 return TRUE;
3237 static void
3238 chat_create_ui (EmpathyChat *chat)
3240 EmpathyChatPriv *priv = GET_PRIV (chat);
3241 GtkBuilder *gui;
3242 GList *list = NULL;
3243 gchar *filename;
3244 GtkTextBuffer *buffer;
3245 EmpathyThemeManager *theme_mgr;
3247 filename = empathy_file_lookup ("empathy-chat.ui",
3248 "libempathy-gtk");
3249 gui = tpaw_builder_get_file (filename,
3250 "chat_widget", &priv->widget,
3251 "hpaned", &priv->hpaned,
3252 "vbox_left", &priv->vbox_left,
3253 "scrolled_window_chat", &priv->scrolled_window_chat,
3254 "scrolled_window_input", &priv->scrolled_window_input,
3255 "hbox_topic", &priv->hbox_topic,
3256 "expander_topic", &priv->expander_topic,
3257 "label_topic", &priv->label_topic,
3258 "scrolled_window_contacts", &priv->scrolled_window_contacts,
3259 "info_bar_vbox", &priv->info_bar_vbox,
3260 NULL);
3262 tpaw_builder_connect (gui, chat,
3263 "expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
3264 "label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
3265 NULL);
3267 g_free (filename);
3269 /* Add message view. */
3270 theme_mgr = empathy_theme_manager_dup_singleton ();
3271 chat->view = empathy_theme_manager_create_view (theme_mgr);
3272 g_object_unref (theme_mgr);
3273 /* If this is a GtkTextView, it's set as a drag destination for text/plain
3274 and other types, even though it's non-editable and doesn't accept any
3275 drags. This steals drag motion for anything inside the scrollbars,
3276 making drag destinations on chat windows far less useful.
3278 gtk_drag_dest_unset (GTK_WIDGET (chat->view));
3279 g_signal_connect (chat->view, "focus_in_event",
3280 G_CALLBACK (chat_text_view_focus_in_event_cb),
3281 chat);
3282 if (GTK_IS_SCROLLABLE (chat->view))
3284 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
3285 GTK_WIDGET (chat->view));
3287 else
3289 gtk_widget_hide (priv->scrolled_window_chat);
3290 gtk_box_pack_start (GTK_BOX (priv->vbox_left),
3291 GTK_WIDGET (chat->view),
3292 TRUE, TRUE, 0);
3293 gtk_box_reorder_child (GTK_BOX (priv->vbox_left),
3294 GTK_WIDGET (chat->view), 0);
3296 gtk_widget_show (GTK_WIDGET (chat->view));
3298 /* Add input GtkTextView */
3299 chat->input_text_view = empathy_input_text_view_new ();
3300 g_signal_connect (chat->input_text_view, "notify::has-focus",
3301 G_CALLBACK (chat_input_has_focus_notify_cb),
3302 chat);
3303 g_signal_connect (chat->input_text_view, "key-press-event",
3304 G_CALLBACK (chat_input_key_press_event_cb),
3305 chat);
3306 g_signal_connect (chat->input_text_view, "realize",
3307 G_CALLBACK (chat_input_realize_cb),
3308 chat);
3309 g_signal_connect (chat->input_text_view, "button-press-event",
3310 G_CALLBACK (chat_input_button_press_event_cb),
3311 chat);
3312 g_signal_connect (chat->input_text_view, "populate-popup",
3313 G_CALLBACK (chat_input_populate_popup_cb),
3314 chat);
3315 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
3316 tp_g_signal_connect_object (buffer, "changed",
3317 G_CALLBACK (chat_input_text_buffer_changed_cb),
3318 chat, 0);
3319 tp_g_signal_connect_object (priv->gsettings_chat,
3320 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
3321 G_CALLBACK (conf_spell_checking_cb), chat, 0);
3322 conf_spell_checking_cb (priv->gsettings_chat,
3323 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
3324 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
3325 chat->input_text_view);
3326 gtk_widget_show (chat->input_text_view);
3328 /* Add the (invisible) search bar */
3329 priv->search_bar = empathy_search_bar_new (chat->view);
3330 gtk_box_pack_start (GTK_BOX(priv->vbox_left),
3331 priv->search_bar,
3332 FALSE, FALSE, 0);
3333 gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);
3335 /* Initialy hide the topic, will be shown if not empty */
3336 gtk_widget_hide (priv->hbox_topic);
3338 g_signal_connect (priv->hpaned, "notify::position",
3339 G_CALLBACK (chat_hpaned_pos_changed_cb),
3340 chat);
3342 /* Set widget focus order */
3343 list = g_list_append (NULL, priv->search_bar);
3344 list = g_list_append (list, priv->scrolled_window_input);
3345 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
3346 g_list_free (list);
3348 list = g_list_append (NULL, priv->vbox_left);
3349 list = g_list_append (list, priv->scrolled_window_contacts);
3350 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
3351 g_list_free (list);
3353 list = g_list_append (NULL, priv->hpaned);
3354 list = g_list_append (list, priv->hbox_topic);
3355 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
3356 g_list_free (list);
3358 /* Add the main widget in the chat widget */
3359 gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
3360 g_object_unref (gui);
3363 static void
3364 chat_finalize (GObject *object)
3366 EmpathyChat *chat;
3367 EmpathyChatPriv *priv;
3369 chat = EMPATHY_CHAT (object);
3370 priv = GET_PRIV (chat);
3372 DEBUG ("Finalized: %p", object);
3374 if (priv->update_misspelled_words_id != 0)
3375 g_source_remove (priv->update_misspelled_words_id);
3377 if (priv->save_paned_pos_id != 0)
3378 g_source_remove (priv->save_paned_pos_id);
3380 if (priv->contacts_visible_id != 0)
3381 g_source_remove (priv->contacts_visible_id);
3383 g_object_unref (priv->gsettings_chat);
3384 g_object_unref (priv->gsettings_ui);
3386 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
3387 g_list_free (priv->input_history);
3389 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
3390 g_list_free (priv->compositors);
3392 chat_composing_remove_timeout (chat);
3394 g_object_unref (priv->account_manager);
3395 g_object_unref (priv->log_manager);
3396 g_object_unref (priv->log_walker);
3398 if (priv->tp_chat) {
3399 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3400 chat_invalidated_cb, chat);
3401 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3402 chat_message_received_cb, chat);
3403 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3404 chat_message_acknowledged_cb, chat);
3405 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3406 chat_send_error_cb, chat);
3407 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3408 chat_state_changed_cb, chat);
3409 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3410 chat_members_changed_cb, chat);
3411 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3412 chat_self_contact_changed_cb, chat);
3413 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3414 chat_remote_contact_changed_cb, chat);
3415 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3416 chat_title_changed_cb, chat);
3417 g_signal_handlers_disconnect_by_func (priv->tp_chat,
3418 chat_subject_changed_cb, chat);
3419 empathy_tp_chat_leave (priv->tp_chat, "");
3420 g_object_unref (priv->tp_chat);
3422 if (priv->account) {
3423 g_object_unref (priv->account);
3425 if (priv->self_contact) {
3426 g_signal_handlers_disconnect_by_func (priv->self_contact,
3427 chat_self_contact_alias_changed_cb,
3428 chat);
3429 g_object_unref (priv->self_contact);
3431 if (priv->remote_contact) {
3432 g_object_unref (priv->remote_contact);
3435 if (priv->block_events_timeout_id) {
3436 g_source_remove (priv->block_events_timeout_id);
3439 g_free (priv->id);
3440 g_free (priv->name);
3441 g_free (priv->subject);
3442 g_completion_free (priv->completion);
3444 tp_clear_pointer (&priv->highlight_regex, g_regex_unref);
3446 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
3449 static void
3450 chat_constructed (GObject *object)
3452 EmpathyChat *chat = EMPATHY_CHAT (object);
3453 EmpathyChatPriv *priv = GET_PRIV (chat);
3454 TplEntity *target;
3456 if (priv->tp_chat != NULL) {
3457 TpChannel *channel = TP_CHANNEL (priv->tp_chat);
3458 TpConnection *conn = tp_channel_get_connection (channel);
3459 gboolean supports_avatars =
3460 tp_proxy_has_interface_by_id (conn,
3461 TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS);
3463 empathy_theme_adium_set_show_avatars (chat->view,
3464 supports_avatars);
3467 /* Add messages from last conversations. Backlog messages are always
3468 * prepended and pending messages are appended, so we can do both
3469 * independently. Hacks like we previously had for bug #603980 are no
3470 * longer needed. Pending messages are handled within
3471 * empathy_chat_set_tp_chat() so we don't have to care about them here.
3473 if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
3474 target = tpl_entity_new_from_room_id (priv->id);
3475 else
3476 target = tpl_entity_new (priv->id, TPL_ENTITY_CONTACT, NULL, NULL);
3478 priv->log_walker = tpl_log_manager_walk_filtered_events (priv->log_manager, priv->account, target,
3479 TPL_EVENT_MASK_TEXT, chat_log_filter, chat);
3480 g_object_unref (target);
3482 if (priv->handle_type != TP_HANDLE_TYPE_ROOM) {
3483 chat_add_logs (chat);
3487 static void
3488 empathy_chat_class_init (EmpathyChatClass *klass)
3490 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3492 object_class->finalize = chat_finalize;
3493 object_class->get_property = chat_get_property;
3494 object_class->set_property = chat_set_property;
3495 object_class->constructed = chat_constructed;
3497 g_object_class_install_property (object_class,
3498 PROP_TP_CHAT,
3499 g_param_spec_object ("tp-chat",
3500 "Empathy tp chat",
3501 "The tp chat object",
3502 EMPATHY_TYPE_TP_CHAT,
3503 G_PARAM_CONSTRUCT |
3504 G_PARAM_READWRITE |
3505 G_PARAM_STATIC_STRINGS));
3506 g_object_class_install_property (object_class,
3507 PROP_ACCOUNT,
3508 g_param_spec_object ("account",
3509 "Account of the chat",
3510 "The account of the chat",
3511 TP_TYPE_ACCOUNT,
3512 G_PARAM_READABLE |
3513 G_PARAM_STATIC_STRINGS));
3514 g_object_class_install_property (object_class,
3515 PROP_ID,
3516 g_param_spec_string ("id",
3517 "Chat's id",
3518 "The id of the chat",
3519 NULL,
3520 G_PARAM_READABLE |
3521 G_PARAM_STATIC_STRINGS));
3522 g_object_class_install_property (object_class,
3523 PROP_NAME,
3524 g_param_spec_string ("name",
3525 "Chat's name",
3526 "The name of the chat",
3527 NULL,
3528 G_PARAM_READABLE |
3529 G_PARAM_STATIC_STRINGS));
3530 g_object_class_install_property (object_class,
3531 PROP_SUBJECT,
3532 g_param_spec_string ("subject",
3533 "Chat's subject",
3534 "The subject or topic of the chat",
3535 NULL,
3536 G_PARAM_READABLE |
3537 G_PARAM_STATIC_STRINGS));
3538 g_object_class_install_property (object_class,
3539 PROP_REMOTE_CONTACT,
3540 g_param_spec_object ("remote-contact",
3541 "The remote contact",
3542 "The remote contact is any",
3543 EMPATHY_TYPE_CONTACT,
3544 G_PARAM_READABLE |
3545 G_PARAM_STATIC_STRINGS));
3546 g_object_class_install_property (object_class,
3547 PROP_SHOW_CONTACTS,
3548 g_param_spec_boolean ("show-contacts",
3549 "Contacts' visibility",
3550 "The visibility of the contacts' list",
3551 TRUE,
3552 G_PARAM_READWRITE |
3553 G_PARAM_STATIC_STRINGS));
3555 g_object_class_install_property (object_class,
3556 PROP_SMS_CHANNEL,
3557 g_param_spec_boolean ("sms-channel",
3558 "SMS Channel",
3559 "TRUE if this channel is for sending SMSes",
3560 FALSE,
3561 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3563 g_object_class_install_property (object_class,
3564 PROP_N_MESSAGES_SENDING,
3565 g_param_spec_uint ("n-messages-sending",
3566 "Num Messages Sending",
3567 "The number of messages being sent",
3568 0, G_MAXUINT, 0,
3569 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3571 g_object_class_install_property (object_class,
3572 PROP_NB_UNREAD_MESSAGES,
3573 g_param_spec_uint ("nb-unread-messages",
3574 "Num Unread Messages",
3575 "The number of unread messages",
3576 0, G_MAXUINT, 0,
3577 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
3579 signals[COMPOSING] =
3580 g_signal_new ("composing",
3581 G_OBJECT_CLASS_TYPE (object_class),
3582 G_SIGNAL_RUN_LAST,
3584 NULL, NULL,
3585 g_cclosure_marshal_generic,
3586 G_TYPE_NONE,
3587 1, G_TYPE_BOOLEAN);
3590 * EmpathyChat::new-message:
3591 * @self: the #EmpathyChat
3592 * @message: the new message
3593 * @pending: whether the message was in the pending queue when @self
3594 * was created
3595 * @should_highlight: %TRUE if the message mentions the local user
3597 signals[NEW_MESSAGE] =
3598 g_signal_new ("new-message",
3599 G_OBJECT_CLASS_TYPE (object_class),
3600 G_SIGNAL_RUN_LAST,
3602 NULL, NULL,
3603 g_cclosure_marshal_generic,
3604 G_TYPE_NONE,
3605 3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
3607 signals[PART_COMMAND_ENTERED] =
3608 g_signal_new ("part-command-entered",
3609 G_OBJECT_CLASS_TYPE (object_class),
3610 G_SIGNAL_RUN_LAST,
3612 NULL, NULL,
3613 g_cclosure_marshal_generic,
3614 G_TYPE_NONE,
3615 1, G_TYPE_STRV);
3617 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
3620 static gboolean
3621 chat_block_events_timeout_cb (gpointer data)
3623 EmpathyChatPriv *priv = GET_PRIV (data);
3625 priv->block_events_timeout_id = 0;
3627 return FALSE;
3630 static void
3631 account_manager_prepared_cb (GObject *source_object,
3632 GAsyncResult *result,
3633 gpointer user_data)
3635 GList *accounts, *l;
3636 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
3637 EmpathyChat *chat = user_data;
3638 GError *error = NULL;
3640 if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
3641 DEBUG ("Failed to prepare the account manager: %s", error->message);
3642 g_error_free (error);
3643 return;
3646 accounts = tp_account_manager_dup_valid_accounts (account_manager);
3648 for (l = accounts; l != NULL; l = l->next) {
3649 TpAccount *account = l->data;
3650 tp_g_signal_connect_object (account, "status-changed",
3651 G_CALLBACK (chat_new_connection_cb),
3652 chat, 0);
3655 g_list_free_full (accounts, g_object_unref);
3658 static void
3659 empathy_chat_init (EmpathyChat *chat)
3661 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
3662 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
3664 chat->priv = priv;
3665 priv->log_manager = tpl_log_manager_dup_singleton ();
3666 priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
3667 priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);
3669 priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
3670 EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
3671 priv->input_history = NULL;
3672 priv->input_history_current = NULL;
3673 priv->account_manager = tp_account_manager_dup ();
3675 tp_proxy_prepare_async (priv->account_manager, NULL,
3676 account_manager_prepared_cb, chat);
3678 priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
3679 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);
3681 /* Block events for some time to avoid having "has come online" or
3682 * "joined" messages. */
3683 priv->block_events_timeout_id =
3684 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
3686 /* Add nick name completion */
3687 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
3688 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
3690 /* Create UI early so by the time empathy_chat_set_tp_chat() is called
3691 * (construct property) the view will already exists to receive pending
3692 * messages. */
3693 chat_create_ui (chat);
3696 EmpathyChat *
3697 empathy_chat_new (EmpathyTpChat *tp_chat)
3699 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
3702 EmpathyTpChat *
3703 empathy_chat_get_tp_chat (EmpathyChat *chat)
3705 EmpathyChatPriv *priv = GET_PRIV (chat);
3707 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
3709 return priv->tp_chat;
3712 typedef struct
3714 EmpathyChat *self;
3715 GtkWidget *info_bar;
3716 gulong response_id;
3717 GtkWidget *button;
3718 GtkWidget *label;
3719 GtkWidget *entry;
3720 GtkWidget *spinner;
3721 gchar *password;
3722 } PasswordData;
3724 static void
3725 passwd_remember_button_cb (GtkButton *button,
3726 PasswordData *data)
3728 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3731 static void
3732 passwd_not_now_button_cb (GtkButton *button,
3733 PasswordData *data)
3735 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
3738 static void
3739 remember_password_infobar_response_cb (GtkWidget *info_bar,
3740 gint response_id,
3741 PasswordData *data)
3743 EmpathyChatPriv *priv = GET_PRIV (data->self);
3745 if (response_id == GTK_RESPONSE_OK) {
3746 DEBUG ("Saving room password");
3747 tpaw_keyring_set_room_password_async (priv->account,
3748 empathy_tp_chat_get_id (priv->tp_chat),
3749 data->password,
3750 NULL, NULL);
3753 gtk_widget_destroy (info_bar);
3754 g_free (data->password);
3755 g_slice_free (PasswordData, data);
3758 static void
3759 chat_prompt_to_save_password (EmpathyChat *self,
3760 PasswordData *data)
3762 GtkWidget *content_area;
3763 GtkWidget *hbox;
3764 GtkWidget *image;
3765 GtkWidget *label;
3766 GtkWidget *alig;
3767 GtkWidget *button;
3769 /* save the password in case it needs to be saved */
3770 data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));
3772 /* Remove all previous widgets */
3773 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
3774 gtk_container_forall (GTK_CONTAINER (content_area),
3775 (GtkCallback) gtk_widget_destroy, NULL);
3776 data->button = NULL;
3777 data->label = NULL;
3778 data->entry = NULL;
3779 data->spinner = NULL;
3781 gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
3782 GTK_MESSAGE_QUESTION);
3784 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3785 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3787 /* Add image */
3788 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3789 GTK_ICON_SIZE_DIALOG);
3790 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3792 /* Add message */
3793 label = gtk_label_new (_("Would you like to store this password?"));
3794 gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
3796 /* Add 'Remember' button */
3797 alig = gtk_alignment_new (0, 0.5, 1, 0);
3799 button = gtk_button_new_with_label (_("Remember"));
3800 gtk_container_add (GTK_CONTAINER (alig), button);
3801 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3803 g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
3804 data);
3806 /* Add 'Not now' button */
3807 alig = gtk_alignment_new (0, 0.5, 1, 0);
3809 button = gtk_button_new_with_label (_("Not now"));
3810 gtk_container_add (GTK_CONTAINER (alig), button);
3811 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
3813 g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
3814 data);
3816 /* go! */
3817 g_signal_handler_disconnect (data->info_bar, data->response_id);
3818 g_signal_connect (data->info_bar, "response",
3819 G_CALLBACK (remember_password_infobar_response_cb), data);
3821 gtk_widget_show_all (data->info_bar);
3824 static void
3825 provide_password_cb (GObject *tp_chat,
3826 GAsyncResult *res,
3827 gpointer user_data)
3829 PasswordData *data = user_data;
3830 EmpathyChat *self = data->self;
3831 EmpathyChatPriv *priv = GET_PRIV (self);
3832 GError *error = NULL;
3834 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
3835 &error)) {
3836 DEBUG ("error: %s", error->message);
3837 /* FIXME: what should we do if that's another error? Close the channel?
3838 * Display the raw D-Bus error to the user isn't very useful */
3839 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
3840 /* entry */
3841 gtk_entry_set_text (GTK_ENTRY (data->entry), "");
3842 gtk_widget_set_sensitive (data->entry, TRUE);
3843 gtk_widget_grab_focus (data->entry);
3845 /* info bar */
3846 gtk_info_bar_set_message_type (
3847 GTK_INFO_BAR (data->info_bar),
3848 GTK_MESSAGE_ERROR);
3850 /* button */
3851 gtk_widget_set_sensitive (data->button, TRUE);
3852 gtk_button_set_label (GTK_BUTTON (data->button),
3853 _("Retry"));
3855 /* label */
3856 gtk_label_set_text (GTK_LABEL (data->label),
3857 _("Wrong password; please try again:"));
3859 /* spinner */
3860 gtk_spinner_stop (GTK_SPINNER (data->spinner));
3861 gtk_widget_hide (data->spinner);
3863 g_error_free (error);
3864 return;
3867 /* ask whether they want to save the password */
3868 chat_prompt_to_save_password (self, data);
3870 /* Room joined */
3871 gtk_widget_set_sensitive (priv->hpaned, TRUE);
3872 gtk_widget_set_sensitive (self->input_text_view, TRUE);
3873 gtk_widget_grab_focus (self->input_text_view);
3876 static void
3877 password_infobar_response_cb (GtkWidget *info_bar,
3878 gint response_id,
3879 PasswordData *data)
3881 EmpathyChatPriv *priv = GET_PRIV (data->self);
3882 const gchar *password;
3884 if (response_id != GTK_RESPONSE_OK) {
3885 gtk_widget_destroy (info_bar);
3886 g_slice_free (PasswordData, data);
3887 return;
3890 password = gtk_entry_get_text (GTK_ENTRY (data->entry));
3892 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
3893 provide_password_cb, data);
3895 gtk_widget_set_sensitive (data->button, FALSE);
3896 gtk_widget_set_sensitive (data->entry, FALSE);
3898 gtk_spinner_start (GTK_SPINNER (data->spinner));
3899 gtk_widget_show (data->spinner);
3902 static void
3903 password_entry_activate_cb (GtkWidget *entry,
3904 PasswordData *data)
3906 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3909 static void
3910 passwd_join_button_cb (GtkButton *button,
3911 PasswordData *data)
3913 gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
3916 static void
3917 clear_icon_released_cb (GtkEntry *entry,
3918 GtkEntryIconPosition icon_pos,
3919 GdkEvent *event,
3920 PasswordData *data)
3922 gtk_entry_set_text (entry, "");
3925 static void
3926 password_entry_changed_cb (GtkEditable *entry,
3927 PasswordData *data)
3929 const gchar *str;
3931 str = gtk_entry_get_text (GTK_ENTRY (entry));
3933 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3934 GTK_ENTRY_ICON_SECONDARY, !TPAW_STR_EMPTY (str));
3937 static void
3938 infobar_chat_invalidated_cb (TpProxy *proxy,
3939 guint domain,
3940 gint code,
3941 gchar *message,
3942 gpointer password_infobar)
3944 /* Destroy the password infobar whenever a channel is invalidated
3945 * so we don't have multiple infobars when the MUC is rejoined */
3946 gtk_widget_destroy (GTK_WIDGET (password_infobar));
3949 static void
3950 display_password_info_bar (EmpathyChat *self)
3952 EmpathyChatPriv *priv = GET_PRIV (self);
3953 GtkWidget *info_bar;
3954 GtkWidget *content_area;
3955 GtkWidget *hbox;
3956 GtkWidget *image;
3957 GtkWidget *label;
3958 GtkWidget *entry;
3959 GtkWidget *alig;
3960 GtkWidget *button;
3961 GtkWidget *spinner;
3962 PasswordData *data;
3964 data = g_slice_new0 (PasswordData);
3966 info_bar = gtk_info_bar_new ();
3967 gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
3968 GTK_MESSAGE_QUESTION);
3970 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
3972 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
3973 gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
3975 /* Add image */
3976 image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
3977 GTK_ICON_SIZE_DIALOG);
3978 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
3980 /* Add message */
3981 label = gtk_label_new (_("This room is protected by a password:"));
3982 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3984 /* Add password entry */
3985 entry = gtk_entry_new ();
3986 gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
3987 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3989 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
3990 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
3991 gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
3992 GTK_ENTRY_ICON_SECONDARY, FALSE);
3994 g_signal_connect (entry, "icon-release",
3995 G_CALLBACK (clear_icon_released_cb), data);
3996 g_signal_connect (entry, "changed",
3997 G_CALLBACK (password_entry_changed_cb), data);
3999 g_signal_connect (entry, "activate",
4000 G_CALLBACK (password_entry_activate_cb), data);
4002 /* Focus the password entry once it's realized */
4003 g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);
4005 /* Add 'Join' button */
4006 alig = gtk_alignment_new (0, 0.5, 1, 0);
4008 button = gtk_button_new_with_label (_("Join"));
4009 gtk_container_add (GTK_CONTAINER (alig), button);
4010 gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);
4012 g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
4013 data);
4015 /* Add spinner */
4016 spinner = gtk_spinner_new ();
4017 gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
4019 /* Save some data for messing around with later */
4020 data->self = self;
4021 data->info_bar = info_bar;
4022 data->button = button;
4023 data->label = label;
4024 data->entry = entry;
4025 data->spinner = spinner;
4027 gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
4028 TRUE, TRUE, 3);
4029 gtk_widget_show_all (hbox);
4031 tp_g_signal_connect_object (priv->tp_chat,
4032 "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
4033 info_bar, 0);
4035 data->response_id = g_signal_connect (info_bar, "response",
4036 G_CALLBACK (password_infobar_response_cb), data);
4038 gtk_widget_show_all (info_bar);
4039 /* ... but hide the spinner */
4040 gtk_widget_hide (spinner);
4042 /* prevent the user from typing anything */
4043 gtk_widget_set_sensitive (self->input_text_view, FALSE);
4046 static void
4047 provide_saved_password_cb (GObject *tp_chat,
4048 GAsyncResult *res,
4049 gpointer user_data)
4051 EmpathyChat *self = user_data;
4052 EmpathyChatPriv *priv = GET_PRIV (self);
4053 GError *error = NULL;
4055 if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
4056 &error)) {
4057 DEBUG ("error: %s", error->message);
4058 /* FIXME: what should we do if that's another error? Close the channel?
4059 * Display the raw D-Bus error to the user isn't very useful */
4060 if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
4061 display_password_info_bar (self);
4062 gtk_widget_set_sensitive (priv->hpaned, FALSE);
4064 g_error_free (error);
4065 return;
4068 /* Room joined */
4069 gtk_widget_set_sensitive (priv->hpaned, TRUE);
4070 gtk_widget_grab_focus (self->input_text_view);
4073 static void
4074 chat_room_got_password_cb (GObject *source,
4075 GAsyncResult *result,
4076 gpointer user_data)
4078 EmpathyChat *self = user_data;
4079 EmpathyChatPriv *priv = GET_PRIV (self);
4080 const gchar *password;
4081 GError *error = NULL;
4083 password = tpaw_keyring_get_room_password_finish (priv->account,
4084 result, &error);
4086 if (error != NULL) {
4087 DEBUG ("Couldn't get room password: %s\n", error->message);
4088 g_clear_error (&error);
4090 display_password_info_bar (self);
4091 gtk_widget_set_sensitive (priv->hpaned, FALSE);
4092 return;
4095 tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
4096 provide_saved_password_cb, self);
4099 static void
4100 chat_password_needed_changed_cb (EmpathyChat *self)
4102 EmpathyChatPriv *priv = GET_PRIV (self);
4104 if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
4105 tpaw_keyring_get_room_password_async (priv->account,
4106 empathy_tp_chat_get_id (priv->tp_chat),
4107 chat_room_got_password_cb, self);
4111 static void
4112 chat_sms_channel_changed_cb (EmpathyChat *self)
4114 EmpathyChatPriv *priv = GET_PRIV (self);
4116 priv->sms_channel = tp_text_channel_is_sms_channel (
4117 (TpTextChannel *) priv->tp_chat);
4118 g_object_notify (G_OBJECT (self), "sms-channel");
4121 static void
4122 chat_n_messages_sending_changed_cb (EmpathyChat *self)
4124 g_object_notify (G_OBJECT (self), "n-messages-sending");
4127 void
4128 empathy_chat_set_tp_chat (EmpathyChat *chat,
4129 EmpathyTpChat *tp_chat)
4131 EmpathyChatPriv *priv = GET_PRIV (chat);
4133 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4134 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
4136 if (priv->tp_chat) {
4137 return;
4140 if (priv->account) {
4141 g_object_unref (priv->account);
4144 priv->tp_chat = g_object_ref (tp_chat);
4145 priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));
4147 g_signal_connect (tp_chat, "invalidated",
4148 G_CALLBACK (chat_invalidated_cb),
4149 chat);
4150 g_signal_connect (tp_chat, "message-received-empathy",
4151 G_CALLBACK (chat_message_received_cb),
4152 chat);
4153 g_signal_connect (tp_chat, "message_acknowledged",
4154 G_CALLBACK (chat_message_acknowledged_cb),
4155 chat);
4156 g_signal_connect (tp_chat, "send-error",
4157 G_CALLBACK (chat_send_error_cb),
4158 chat);
4159 g_signal_connect (tp_chat, "contact-chat-state-changed",
4160 G_CALLBACK (chat_state_changed_cb),
4161 chat);
4162 g_signal_connect (tp_chat, "members-changed",
4163 G_CALLBACK (chat_members_changed_cb),
4164 chat);
4165 g_signal_connect (tp_chat, "member-renamed",
4166 G_CALLBACK (chat_member_renamed_cb),
4167 chat);
4168 g_signal_connect_swapped (tp_chat, "notify::self-contact",
4169 G_CALLBACK (chat_self_contact_changed_cb),
4170 chat);
4171 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
4172 G_CALLBACK (chat_remote_contact_changed_cb),
4173 chat);
4174 g_signal_connect_swapped (tp_chat, "notify::password-needed",
4175 G_CALLBACK (chat_password_needed_changed_cb),
4176 chat);
4177 g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
4178 G_CALLBACK (chat_sms_channel_changed_cb),
4179 chat);
4180 g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
4181 G_CALLBACK (chat_n_messages_sending_changed_cb),
4182 chat);
4183 g_signal_connect_swapped (tp_chat, "notify::title",
4184 G_CALLBACK (chat_title_changed_cb),
4185 chat);
4186 g_signal_connect_swapped (tp_chat, "notify::subject",
4187 G_CALLBACK (chat_subject_changed_cb),
4188 chat);
4190 /* Get initial value of properties */
4191 chat_sms_channel_changed_cb (chat);
4192 chat_self_contact_changed_cb (chat);
4193 chat_remote_contact_changed_cb (chat);
4194 chat_title_changed_cb (chat);
4195 chat_subject_changed_cb (chat);
4197 if (chat->input_text_view) {
4198 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
4199 if (priv->block_events_timeout_id == 0) {
4200 empathy_theme_adium_append_event (chat->view, _("Connected"));
4204 g_object_notify (G_OBJECT (chat), "tp-chat");
4205 g_object_notify (G_OBJECT (chat), "id");
4206 g_object_notify (G_OBJECT (chat), "account");
4208 show_pending_messages (chat);
4210 /* check if a password is needed */
4211 chat_password_needed_changed_cb (chat);
4214 TpAccount *
4215 empathy_chat_get_account (EmpathyChat *chat)
4217 EmpathyChatPriv *priv = GET_PRIV (chat);
4219 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4221 return priv->account;
4224 const gchar *
4225 empathy_chat_get_id (EmpathyChat *chat)
4227 EmpathyChatPriv *priv = GET_PRIV (chat);
4229 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4231 return priv->id;
4234 gchar *
4235 empathy_chat_dup_name (EmpathyChat *chat)
4237 EmpathyChatPriv *priv = GET_PRIV (chat);
4238 const gchar *ret;
4240 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4242 ret = priv->name;
4244 if (!ret && priv->remote_contact) {
4245 ret = empathy_contact_get_alias (priv->remote_contact);
4248 if (!ret)
4249 ret = priv->id;
4251 if (!ret)
4252 ret = _("Conversation");
4254 if (priv->sms_channel)
4255 /* Translators: this string is a something like
4256 * "Escher Cat (SMS)" */
4257 return g_strdup_printf (_("%s (SMS)"), ret);
4258 else
4259 return g_strdup (ret);
4262 const gchar *
4263 empathy_chat_get_subject (EmpathyChat *chat)
4265 EmpathyChatPriv *priv = GET_PRIV (chat);
4267 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4269 return priv->subject;
4272 EmpathyContact *
4273 empathy_chat_get_remote_contact (EmpathyChat *chat)
4275 EmpathyChatPriv *priv = GET_PRIV (chat);
4277 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4279 return priv->remote_contact;
4282 GtkWidget *
4283 empathy_chat_get_contact_menu (EmpathyChat *chat)
4285 EmpathyChatPriv *priv = GET_PRIV (chat);
4286 GtkWidget *menu = NULL;
4287 FolksIndividual *individual;
4288 TpContact *contact;
4290 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
4292 if (priv->remote_contact == NULL)
4293 return NULL;
4295 contact = empathy_contact_get_tp_contact (priv->remote_contact);
4296 if (contact == NULL)
4297 return NULL;
4299 individual = empathy_ensure_individual_from_tp_contact (contact);
4301 if (individual == NULL)
4302 return NULL;
4304 menu = empathy_individual_menu_new (individual, NULL,
4305 EMPATHY_INDIVIDUAL_FEATURE_CALL |
4306 EMPATHY_INDIVIDUAL_FEATURE_LOG |
4307 EMPATHY_INDIVIDUAL_FEATURE_INFO |
4308 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);
4310 g_object_unref (individual);
4312 return menu;
4315 void
4316 empathy_chat_clear (EmpathyChat *chat)
4318 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4320 empathy_theme_adium_clear (chat->view);
4323 void
4324 empathy_chat_scroll_down (EmpathyChat *chat)
4326 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4328 empathy_theme_adium_scroll_down (chat->view);
4331 void
4332 empathy_chat_cut (EmpathyChat *chat)
4334 GtkTextBuffer *buffer;
4336 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4338 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4339 if (gtk_text_buffer_get_has_selection (buffer)) {
4340 GtkClipboard *clipboard;
4342 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4344 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
4348 static gboolean
4349 copy_from_input (EmpathyChat *chat)
4351 GtkTextBuffer *buffer;
4352 GtkClipboard *clipboard;
4354 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4355 if (!gtk_text_buffer_get_has_selection (buffer))
4356 return FALSE;
4358 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4359 gtk_text_buffer_copy_clipboard (buffer, clipboard);
4360 return TRUE;
4363 static gboolean
4364 copy_from_topic (EmpathyChat *chat)
4366 EmpathyChatPriv *priv = GET_PRIV (chat);
4367 gint start_offset;
4368 gint end_offset;
4369 gchar *start;
4370 gchar *end;
4371 gchar *selection;
4372 const gchar *topic;
4373 GtkClipboard *clipboard;
4375 if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
4376 &start_offset,
4377 &end_offset))
4378 return FALSE;
4380 topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
4381 start = g_utf8_offset_to_pointer (topic, start_offset);
4382 end = g_utf8_offset_to_pointer (topic, end_offset);
4383 selection = g_strndup (start, end - start);
4385 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4386 gtk_clipboard_set_text (clipboard, selection, -1);
4388 g_free (selection);
4389 return TRUE;
4392 static void
4393 theme_adium_can_copy_cb (EmpathyThemeAdium *view,
4394 GAsyncResult *result,
4395 EmpathyChat *chat)
4397 if (empathy_theme_adium_can_copy_finish (view, result, NULL)) {
4398 empathy_theme_adium_copy_clipboard (chat->view);
4399 return;
4402 if (copy_from_input (chat))
4403 return;
4405 copy_from_topic (chat);
4408 void
4409 empathy_chat_copy (EmpathyChat *chat)
4411 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4413 empathy_theme_adium_can_copy (chat->view, NULL,
4414 (GAsyncReadyCallback)theme_adium_can_copy_cb,
4415 chat);
4418 void
4419 empathy_chat_paste (EmpathyChat *chat)
4421 GtkTextBuffer *buffer;
4422 GtkClipboard *clipboard;
4423 EmpathyChatPriv *priv;
4425 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4427 priv = GET_PRIV (chat);
4429 if (gtk_widget_get_visible (priv->search_bar)) {
4430 empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
4431 return;
4434 if (priv->tp_chat == NULL ||
4435 !gtk_widget_is_sensitive (chat->input_text_view))
4436 return;
4438 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4439 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
4441 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
4444 void
4445 empathy_chat_find (EmpathyChat *chat)
4447 EmpathyChatPriv *priv;
4449 g_return_if_fail (EMPATHY_IS_CHAT (chat));
4451 priv = GET_PRIV (chat);
4453 empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
4456 void
4457 empathy_chat_correct_word (EmpathyChat *chat,
4458 GtkTextIter *start,
4459 GtkTextIter *end,
4460 const gchar *new_word)
4462 GtkTextBuffer *buffer;
4464 g_return_if_fail (chat != NULL);
4465 g_return_if_fail (new_word != NULL);
4467 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
4469 gtk_text_buffer_delete (buffer, start, end);
4470 gtk_text_buffer_insert (buffer, start,
4471 new_word,
4472 -1);
4475 gboolean
4476 empathy_chat_is_room (EmpathyChat *chat)
4478 EmpathyChatPriv *priv = GET_PRIV (chat);
4480 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4482 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);
4485 gboolean
4486 empathy_chat_is_highlighted (EmpathyChat *chat)
4488 EmpathyChatPriv *priv = GET_PRIV (chat);
4490 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
4492 return priv->highlighted;
4495 guint
4496 empathy_chat_get_nb_unread_messages (EmpathyChat *self)
4498 EmpathyChatPriv *priv = GET_PRIV (self);
4500 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4502 return priv->unread_messages;
4505 /* called when the messages have been read by user */
4506 void
4507 empathy_chat_messages_read (EmpathyChat *self)
4509 EmpathyChatPriv *priv = GET_PRIV (self);
4511 g_return_if_fail (EMPATHY_IS_CHAT (self));
4513 /* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
4514 * definition. If we are still retrieving the backlogs, do not ACK */
4515 if (priv->retrieving_backlogs)
4516 return;
4518 if (priv->tp_chat != NULL) {
4519 tp_text_channel_ack_all_pending_messages_async (
4520 TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
4523 priv->highlighted = FALSE;
4525 if (priv->unread_messages_when_offline > 0) {
4526 /* We can't ack those as the connection has gone away so just consider
4527 * them as read. */
4528 priv->unread_messages -= priv->unread_messages_when_offline;
4529 g_object_notify (G_OBJECT (self), "nb-unread-messages");
4530 priv->unread_messages_when_offline = 0;
4534 /* Return TRUE if on of the contacts in this chat is composing */
4535 gboolean
4536 empathy_chat_is_composing (EmpathyChat *chat)
4538 return chat->priv->compositors != NULL;
4541 gboolean
4542 empathy_chat_is_sms_channel (EmpathyChat *self)
4544 EmpathyChatPriv *priv = GET_PRIV (self);
4546 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4548 return priv->sms_channel;
4551 guint
4552 empathy_chat_get_n_messages_sending (EmpathyChat *self)
4554 EmpathyChatPriv *priv;
4556 g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);
4558 priv = GET_PRIV (self);
4560 if (priv->tp_chat == NULL) {
4561 return 0;
4562 } else {
4563 guint n_messages;
4565 g_object_get (priv->tp_chat,
4566 "n-messages-sending", &n_messages,
4567 NULL);
4569 return n_messages;
4573 gchar *
4574 empathy_chat_dup_text (EmpathyChat *self)
4576 GtkTextBuffer *buffer;
4577 GtkTextIter start, end;
4579 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4581 gtk_text_buffer_get_bounds (buffer, &start, &end);
4582 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
4585 void
4586 empathy_chat_set_text (EmpathyChat *self,
4587 const gchar *text)
4589 GtkTextBuffer *buffer;
4591 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));
4593 gtk_text_buffer_set_text (buffer, text, -1);