1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n-lib.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-account-manager.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-dispatcher.h>
45 #include "empathy-chat.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-spell-dialog.h"
49 #include "empathy-contact-list-store.h"
50 #include "empathy-contact-list-view.h"
51 #include "empathy-contact-menu.h"
52 #include "empathy-theme-manager.h"
53 #include "empathy-smiley-manager.h"
54 #include "empathy-ui-utils.h"
56 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
57 #include <libempathy/empathy-debug.h>
59 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
60 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
61 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
62 #define MAX_INPUT_HEIGHT 150
63 #define COMPOSING_STOP_TIMEOUT 5
65 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
67 EmpathyTpChat *tp_chat;
72 EmpathyContact *remote_contact;
74 EmpathyLogManager *log_manager;
75 EmpathyAccountManager *account_manager;
76 GSList *sent_messages;
77 gint sent_messages_index;
79 GCompletion *completion;
80 guint composing_stop_timeout_id;
81 guint block_events_timeout_id;
82 TpHandleType handle_type;
84 gboolean has_input_vscroll;
89 GtkWidget *scrolled_window_chat;
90 GtkWidget *scrolled_window_input;
91 GtkWidget *scrolled_window_contacts;
92 GtkWidget *hbox_topic;
93 GtkWidget *label_topic;
94 GtkWidget *contact_list_view;
113 static guint signals[LAST_SIGNAL] = { 0 };
115 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
118 chat_get_property (GObject *object,
123 EmpathyChat *chat = EMPATHY_CHAT (object);
124 EmpathyChatPriv *priv = GET_PRIV (object);
128 g_value_set_object (value, priv->tp_chat);
131 g_value_set_object (value, priv->account);
134 g_value_set_string (value, empathy_chat_get_name (chat));
137 g_value_set_string (value, priv->id);
140 g_value_set_string (value, priv->subject);
142 case PROP_REMOTE_CONTACT:
143 g_value_set_object (value, priv->remote_contact);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
152 chat_set_property (GObject *object,
157 EmpathyChat *chat = EMPATHY_CHAT (object);
161 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
170 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
174 EmpathyChat *chat = EMPATHY_CHAT (user_data);
175 EmpathyTpChat *tpchat;
178 empathy_chat_view_append_event (chat->view,
179 _("Failed to reconnect this chat"));
183 tpchat = EMPATHY_TP_CHAT (
184 empathy_dispatch_operation_get_channel_wrapper (dispatch));
186 if (empathy_dispatch_operation_claim (dispatch)) {
187 empathy_chat_set_tp_chat (chat, tpchat);
192 chat_new_connection_cb (EmpathyAccountManager *manager,
193 TpConnection *connection,
196 EmpathyChatPriv *priv = GET_PRIV (chat);
199 account = empathy_account_manager_get_account (manager, connection);
200 if (!priv->tp_chat && empathy_account_equal (account, priv->account) &&
201 priv->handle_type != TP_HANDLE_TYPE_NONE &&
202 !EMP_STR_EMPTY (priv->id)) {
204 DEBUG ("Account reconnected, request a new Text channel");
206 switch (priv->handle_type) {
207 case TP_HANDLE_TYPE_CONTACT:
208 empathy_dispatcher_chat_with_contact_id (
209 connection, priv->id,
210 chat_connect_channel_reconnected,
213 case TP_HANDLE_TYPE_ROOM:
214 empathy_dispatcher_join_muc (connection,
216 chat_connect_channel_reconnected,
220 g_assert_not_reached ();
227 chat_composing_remove_timeout (EmpathyChat *chat)
229 EmpathyChatPriv *priv;
231 priv = GET_PRIV (chat);
233 if (priv->composing_stop_timeout_id) {
234 g_source_remove (priv->composing_stop_timeout_id);
235 priv->composing_stop_timeout_id = 0;
240 chat_composing_stop_timeout_cb (EmpathyChat *chat)
242 EmpathyChatPriv *priv;
244 priv = GET_PRIV (chat);
246 priv->composing_stop_timeout_id = 0;
247 empathy_tp_chat_set_state (priv->tp_chat,
248 TP_CHANNEL_CHAT_STATE_PAUSED);
254 chat_composing_start (EmpathyChat *chat)
256 EmpathyChatPriv *priv;
258 priv = GET_PRIV (chat);
260 if (priv->composing_stop_timeout_id) {
261 /* Just restart the timeout */
262 chat_composing_remove_timeout (chat);
264 empathy_tp_chat_set_state (priv->tp_chat,
265 TP_CHANNEL_CHAT_STATE_COMPOSING);
268 priv->composing_stop_timeout_id = g_timeout_add_seconds (
269 COMPOSING_STOP_TIMEOUT,
270 (GSourceFunc) chat_composing_stop_timeout_cb,
275 chat_composing_stop (EmpathyChat *chat)
277 EmpathyChatPriv *priv;
279 priv = GET_PRIV (chat);
281 chat_composing_remove_timeout (chat);
282 empathy_tp_chat_set_state (priv->tp_chat,
283 TP_CHANNEL_CHAT_STATE_ACTIVE);
287 chat_sent_message_add (EmpathyChat *chat,
290 EmpathyChatPriv *priv;
294 priv = GET_PRIV (chat);
296 /* Save the sent message in our repeat buffer */
297 list = priv->sent_messages;
299 /* Remove any other occurances of this msg */
300 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
301 list = g_slist_remove_link (list, item);
303 g_slist_free1 (item);
306 /* Trim the list to the last 10 items */
307 while (g_slist_length (list) > 10) {
308 item = g_slist_last (list);
310 list = g_slist_remove_link (list, item);
312 g_slist_free1 (item);
316 /* Add new message */
317 list = g_slist_prepend (list, g_strdup (str));
319 /* Set list and reset the index */
320 priv->sent_messages = list;
321 priv->sent_messages_index = -1;
325 chat_sent_message_get_next (EmpathyChat *chat)
327 EmpathyChatPriv *priv;
330 priv = GET_PRIV (chat);
332 if (!priv->sent_messages) {
333 DEBUG ("No sent messages, next message is NULL");
337 max = g_slist_length (priv->sent_messages) - 1;
339 if (priv->sent_messages_index < max) {
340 priv->sent_messages_index++;
343 DEBUG ("Returning next message index:%d", priv->sent_messages_index);
345 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
349 chat_sent_message_get_last (EmpathyChat *chat)
351 EmpathyChatPriv *priv;
353 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
355 priv = GET_PRIV (chat);
357 if (!priv->sent_messages) {
358 DEBUG ("No sent messages, last message is NULL");
362 if (priv->sent_messages_index >= 0) {
363 priv->sent_messages_index--;
366 DEBUG ("Returning last message index:%d", priv->sent_messages_index);
368 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
372 chat_send (EmpathyChat *chat,
375 EmpathyChatPriv *priv;
376 EmpathyMessage *message;
378 if (EMP_STR_EMPTY (msg)) {
382 priv = GET_PRIV (chat);
384 chat_sent_message_add (chat, msg);
386 if (g_str_has_prefix (msg, "/clear")) {
387 empathy_chat_view_clear (chat->view);
391 /* Blacklist messages begining by '/', except for "/me" and "/say"
392 * because they are handled in EmpathyMessage */
394 !g_str_has_prefix (msg, "/me") &&
395 !g_str_has_prefix (msg, "/say")) {
396 empathy_chat_view_append_event (chat->view,
397 _("Unsupported command"));
401 /* We can send the message */
402 message = empathy_message_new (msg);
403 empathy_tp_chat_send (priv->tp_chat, message);
404 g_object_unref (message);
408 chat_input_text_view_send (EmpathyChat *chat)
410 EmpathyChatPriv *priv;
411 GtkTextBuffer *buffer;
412 GtkTextIter start, end;
415 priv = GET_PRIV (chat);
417 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
419 gtk_text_buffer_get_bounds (buffer, &start, &end);
420 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
422 /* clear the input field */
423 gtk_text_buffer_set_text (buffer, "", -1);
425 chat_send (chat, msg);
430 chat_state_changed_cb (EmpathyTpChat *tp_chat,
431 EmpathyContact *contact,
432 TpChannelChatState state,
435 EmpathyChatPriv *priv;
437 gboolean was_composing;
439 priv = GET_PRIV (chat);
441 if (empathy_contact_is_user (contact)) {
442 /* We don't care about our own chat state */
446 was_composing = (priv->compositors != NULL);
448 /* Find the contact in the list. After that l is the list elem or NULL */
449 for (l = priv->compositors; l; l = l->next) {
450 if (contact == l->data) {
456 case TP_CHANNEL_CHAT_STATE_GONE:
457 case TP_CHANNEL_CHAT_STATE_INACTIVE:
458 case TP_CHANNEL_CHAT_STATE_PAUSED:
459 case TP_CHANNEL_CHAT_STATE_ACTIVE:
460 /* Contact is not composing */
462 priv->compositors = g_list_remove_link (priv->compositors, l);
463 g_object_unref (l->data);
467 case TP_CHANNEL_CHAT_STATE_COMPOSING:
468 /* Contact is composing */
470 priv->compositors = g_list_prepend (priv->compositors,
471 g_object_ref (contact));
475 g_assert_not_reached ();
478 DEBUG ("Was composing: %s now composing: %s",
479 was_composing ? "yes" : "no",
480 priv->compositors ? "yes" : "no");
482 if ((was_composing && !priv->compositors) ||
483 (!was_composing && priv->compositors)) {
484 /* Composing state changed */
485 g_signal_emit (chat, signals[COMPOSING], 0,
486 priv->compositors != NULL);
491 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
493 EmpathyChatPriv *priv = GET_PRIV (chat);
494 EmpathyContact *sender;
496 sender = empathy_message_get_sender (message);
498 DEBUG ("Appending new message from %s (%d)",
499 empathy_contact_get_name (sender),
500 empathy_contact_get_handle (sender));
502 empathy_chat_view_append_message (chat->view, message);
504 /* We received a message so the contact is no longer composing */
505 chat_state_changed_cb (priv->tp_chat, sender,
506 TP_CHANNEL_CHAT_STATE_ACTIVE,
509 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
513 chat_message_received_cb (EmpathyTpChat *tp_chat,
514 EmpathyMessage *message,
517 chat_message_received (chat, message);
518 empathy_tp_chat_acknowledge_message (tp_chat, message);
522 chat_send_error_cb (EmpathyTpChat *tp_chat,
523 EmpathyMessage *message,
524 TpChannelTextSendError error_code,
530 switch (error_code) {
531 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
532 error = _("offline");
534 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
535 error = _("invalid contact");
537 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
538 error = _("permission denied");
540 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
541 error = _("too long message");
543 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
544 error = _("not implemented");
547 error = _("unknown");
551 str = g_strdup_printf (_("Error sending message '%s': %s"),
552 empathy_message_get_body (message),
554 empathy_chat_view_append_event (chat->view, str);
559 chat_property_changed_cb (EmpathyTpChat *tp_chat,
564 EmpathyChatPriv *priv = GET_PRIV (chat);
566 if (!tp_strdiff (name, "subject")) {
567 g_free (priv->subject);
568 priv->subject = g_value_dup_string (value);
569 g_object_notify (G_OBJECT (chat), "subject");
571 if (EMP_STR_EMPTY (priv->subject)) {
572 gtk_widget_hide (priv->hbox_topic);
574 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
575 gtk_widget_show (priv->hbox_topic);
577 if (priv->block_events_timeout_id == 0) {
580 if (!EMP_STR_EMPTY (priv->subject)) {
581 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
583 str = g_strdup (_("No topic defined"));
585 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
589 else if (!tp_strdiff (name, "name")) {
591 priv->name = g_value_dup_string (value);
592 g_object_notify (G_OBJECT (chat), "name");
597 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
600 EmpathyChatPriv *priv;
601 GtkTextIter start, end;
603 gboolean spell_checker = FALSE;
605 priv = GET_PRIV (chat);
607 if (gtk_text_buffer_get_char_count (buffer) == 0) {
608 chat_composing_stop (chat);
610 chat_composing_start (chat);
613 empathy_conf_get_bool (empathy_conf_get (),
614 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
617 gtk_text_buffer_get_start_iter (buffer, &start);
619 if (!spell_checker) {
620 gtk_text_buffer_get_end_iter (buffer, &end);
621 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
625 if (!empathy_spell_supported ()) {
629 /* NOTE: this is really inefficient, we shouldn't have to
630 reiterate the whole buffer each time and check each work
633 gboolean correct = FALSE;
636 if (gtk_text_iter_is_start (&start)) {
639 if (!gtk_text_iter_forward_word_end (&end)) {
640 /* no whole word yet */
644 if (!gtk_text_iter_forward_word_end (&end)) {
645 /* must be the end of the buffer */
650 gtk_text_iter_backward_word_start (&start);
653 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
655 /* spell check string if not a command */
657 correct = empathy_spell_check (str);
663 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
665 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
670 /* set start iter to the end iters position */
676 chat_input_key_press_event_cb (GtkWidget *widget,
680 EmpathyChatPriv *priv;
683 GtkWidget *text_view_sw;
685 priv = GET_PRIV (chat);
687 /* Catch ctrl+up/down so we can traverse messages we sent */
688 if ((event->state & GDK_CONTROL_MASK) &&
689 (event->keyval == GDK_Up ||
690 event->keyval == GDK_Down)) {
691 GtkTextBuffer *buffer;
694 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
696 if (event->keyval == GDK_Up) {
697 str = chat_sent_message_get_next (chat);
699 str = chat_sent_message_get_last (chat);
702 g_signal_handlers_block_by_func (buffer,
703 chat_input_text_buffer_changed_cb,
705 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
706 g_signal_handlers_unblock_by_func (buffer,
707 chat_input_text_buffer_changed_cb,
713 /* Catch enter but not ctrl/shift-enter */
714 if (IS_ENTER (event->keyval) &&
715 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
718 /* This is to make sure that kinput2 gets the enter. And if
719 * it's handled there we shouldn't send on it. This is because
720 * kinput2 uses Enter to commit letters. See:
721 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
724 view = GTK_TEXT_VIEW (chat->input_text_view);
725 if (gtk_im_context_filter_keypress (view->im_context, event)) {
726 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
730 chat_input_text_view_send (chat);
734 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
736 if (IS_ENTER (event->keyval) &&
737 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
738 /* Newline for shift/control-enter. */
741 if (!(event->state & GDK_CONTROL_MASK) &&
742 event->keyval == GDK_Page_Up) {
743 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
744 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
747 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
748 event->keyval == GDK_Page_Down) {
749 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
750 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
751 gtk_adjustment_set_value (adj, val);
754 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
755 event->keyval == GDK_Tab) {
756 GtkTextBuffer *buffer;
757 GtkTextIter start, current;
758 gchar *nick, *completed;
759 GList *list, *completed_list;
760 gboolean is_start_of_buffer;
762 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
763 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
765 /* Get the start of the nick to complete. */
766 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
767 gtk_text_iter_backward_word_start (&start);
768 is_start_of_buffer = gtk_text_iter_is_start (&start);
770 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
771 g_completion_add_items (priv->completion, list);
773 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
774 completed_list = g_completion_complete (priv->completion,
783 gchar *complete_char = NULL;
785 gtk_text_buffer_delete (buffer, &start, ¤t);
787 len = g_list_length (completed_list);
790 /* If we only have one hit, use that text
791 * instead of the text in completed since the
792 * completed text will use the typed string
793 * which might be cased all wrong.
796 text = empathy_contact_get_name (completed_list->data);
801 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
803 if (len == 1 && is_start_of_buffer &&
804 empathy_conf_get_string (empathy_conf_get (),
805 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
807 complete_char != NULL) {
808 gtk_text_buffer_insert_at_cursor (buffer,
810 strlen (complete_char));
811 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
812 g_free (complete_char);
818 g_completion_clear_items (priv->completion);
820 g_list_foreach (list, (GFunc) g_object_unref, NULL);
830 chat_text_view_focus_in_event_cb (GtkWidget *widget,
834 gtk_widget_grab_focus (chat->input_text_view);
840 chat_input_set_size_request_idle (gpointer sw)
842 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
848 chat_input_size_request_cb (GtkWidget *widget,
849 GtkRequisition *requisition,
852 EmpathyChatPriv *priv = GET_PRIV (chat);
855 sw = gtk_widget_get_parent (widget);
856 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
857 g_idle_add (chat_input_set_size_request_idle, sw);
858 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
861 priv->has_input_vscroll = TRUE;
864 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
865 gtk_widget_set_size_request (sw, -1, -1);
866 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
869 priv->has_input_vscroll = FALSE;
874 chat_input_realize_cb (GtkWidget *widget,
877 DEBUG ("Setting focus to the input text view");
878 gtk_widget_grab_focus (widget);
882 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
883 EmpathySmiley *smiley,
886 EmpathyChat *chat = EMPATHY_CHAT (user_data);
887 GtkTextBuffer *buffer;
890 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
892 gtk_text_buffer_get_end_iter (buffer, &iter);
893 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
895 gtk_text_buffer_get_end_iter (buffer, &iter);
896 gtk_text_buffer_insert (buffer, &iter, " ", -1);
907 static EmpathyChatSpell *
908 chat_spell_new (EmpathyChat *chat,
913 EmpathyChatSpell *chat_spell;
915 chat_spell = g_slice_new0 (EmpathyChatSpell);
917 chat_spell->chat = g_object_ref (chat);
918 chat_spell->word = g_strdup (word);
919 chat_spell->start = start;
920 chat_spell->end = end;
926 chat_spell_free (EmpathyChatSpell *chat_spell)
928 g_object_unref (chat_spell->chat);
929 g_free (chat_spell->word);
930 g_slice_free (EmpathyChatSpell, chat_spell);
934 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
935 EmpathyChatSpell *chat_spell)
937 empathy_spell_dialog_show (chat_spell->chat,
944 chat_text_send_cb (GtkMenuItem *menuitem,
947 chat_input_text_view_send (chat);
951 chat_input_populate_popup_cb (GtkTextView *view,
955 EmpathyChatPriv *priv;
956 GtkTextBuffer *buffer;
957 GtkTextTagTable *table;
960 GtkTextIter iter, start, end;
963 EmpathyChatSpell *chat_spell;
964 EmpathySmileyManager *smiley_manager;
965 GtkWidget *smiley_menu;
968 priv = GET_PRIV (chat);
969 buffer = gtk_text_view_get_buffer (view);
971 /* Add the emoticon menu. */
972 item = gtk_separator_menu_item_new ();
973 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
974 gtk_widget_show (item);
976 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
977 image = gtk_image_new_from_icon_name ("face-smile",
979 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
980 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
981 gtk_widget_show (item);
983 smiley_manager = empathy_smiley_manager_dup_singleton ();
984 smiley_menu = empathy_smiley_menu_new (smiley_manager,
985 chat_insert_smiley_activate_cb,
987 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
988 g_object_unref (smiley_manager);
990 /* Add the Send menu item. */
991 gtk_text_buffer_get_bounds (buffer, &start, &end);
992 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
993 if (!EMP_STR_EMPTY (str)) {
994 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
995 g_signal_connect (G_OBJECT (item), "activate",
996 G_CALLBACK (chat_text_send_cb), chat);
997 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
998 gtk_widget_show (item);
1002 /* Add the spell check menu item. */
1003 table = gtk_text_buffer_get_tag_table (buffer);
1004 tag = gtk_text_tag_table_lookup (table, "misspelled");
1005 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1006 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1007 GTK_TEXT_WINDOW_WIDGET,
1010 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1012 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1013 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1015 str = gtk_text_buffer_get_text (buffer,
1016 &start, &end, FALSE);
1018 if (!EMP_STR_EMPTY (str)) {
1019 chat_spell = chat_spell_new (chat, str, start, end);
1020 g_object_set_data_full (G_OBJECT (menu),
1021 "chat_spell", chat_spell,
1022 (GDestroyNotify) chat_spell_free);
1024 item = gtk_separator_menu_item_new ();
1025 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1026 gtk_widget_show (item);
1028 item = gtk_image_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1029 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1030 GTK_ICON_SIZE_MENU);
1031 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1032 g_signal_connect (item,
1034 G_CALLBACK (chat_text_check_word_spelling_cb),
1036 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1037 gtk_widget_show (item);
1042 chat_log_filter (EmpathyMessage *message,
1045 EmpathyChat *chat = (EmpathyChat *) user_data;
1046 EmpathyChatPriv *priv = GET_PRIV (chat);
1047 const GList *pending;
1049 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1051 for (; pending; pending = g_list_next (pending)) {
1052 if (empathy_message_equal (message, pending->data)) {
1061 chat_add_logs (EmpathyChat *chat)
1063 EmpathyChatPriv *priv = GET_PRIV (chat);
1064 gboolean is_chatroom;
1065 GList *messages, *l;
1071 /* Turn off scrolling temporarily */
1072 empathy_chat_view_scroll (chat->view, FALSE);
1074 /* Add messages from last conversation */
1075 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1077 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1085 for (l = messages; l; l = g_list_next (l)) {
1086 empathy_chat_view_append_message (chat->view, l->data);
1087 g_object_unref (l->data);
1090 g_list_free (messages);
1092 /* Turn back on scrolling */
1093 empathy_chat_view_scroll (chat->view, TRUE);
1097 chat_contacts_completion_func (const gchar *s1,
1101 gchar *tmp, *nick1, *nick2;
1108 return s1 ? -1 : +1;
1111 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1112 nick1 = g_utf8_casefold (tmp, -1);
1115 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1116 nick2 = g_utf8_casefold (tmp, -1);
1119 ret = strncmp (nick1, nick2, n);
1128 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1129 EmpathyContact *contact,
1130 EmpathyContact *actor,
1136 EmpathyChatPriv *priv = GET_PRIV (chat);
1138 if (priv->block_events_timeout_id == 0) {
1142 str = g_strdup_printf (_("%s has joined the room"),
1143 empathy_contact_get_name (contact));
1145 str = g_strdup_printf (_("%s has left the room"),
1146 empathy_contact_get_name (contact));
1148 empathy_chat_view_append_event (chat->view, str);
1154 chat_reset_size_request (gpointer widget)
1156 gtk_widget_set_size_request (widget, -1, -1);
1162 chat_set_show_contacts (EmpathyChat *chat, gboolean show)
1164 EmpathyChatPriv *priv = GET_PRIV (chat);
1166 if (!priv->scrolled_window_contacts) {
1171 EmpathyContactListStore *store;
1174 /* We are adding the contact list to the chat, we don't want the
1175 * chat view to become too small. If the chat view is already
1176 * smaller than 250 make sure that size won't change. If the
1177 * chat view is bigger the contact list will take some space on
1178 * it but we make sure the chat view don't become smaller than
1179 * 250. Relax the size request once the resize is done */
1180 min_width = MIN (priv->vbox_left->allocation.width, 250);
1181 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1182 g_idle_add (chat_reset_size_request, priv->vbox_left);
1184 if (priv->contacts_width > 0) {
1185 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1186 priv->contacts_width);
1189 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1190 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1191 EMPATHY_CONTACT_LIST_FEATURE_NONE,
1192 EMPATHY_CONTACT_FEATURE_CHAT |
1193 EMPATHY_CONTACT_FEATURE_CALL |
1194 EMPATHY_CONTACT_FEATURE_LOG |
1195 EMPATHY_CONTACT_FEATURE_INFO));
1196 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1197 priv->contact_list_view);
1198 gtk_widget_show (priv->contact_list_view);
1199 gtk_widget_show (priv->scrolled_window_contacts);
1200 g_object_unref (store);
1202 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1203 gtk_widget_hide (priv->scrolled_window_contacts);
1204 if (priv->contact_list_view) {
1205 gtk_widget_destroy (priv->contact_list_view);
1206 priv->contact_list_view = NULL;
1212 chat_remote_contact_changed_cb (EmpathyChat *chat)
1214 EmpathyChatPriv *priv = GET_PRIV (chat);
1216 if (priv->remote_contact) {
1217 g_object_unref (priv->remote_contact);
1218 priv->remote_contact = NULL;
1221 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1222 if (priv->remote_contact) {
1223 g_object_ref (priv->remote_contact);
1224 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1226 priv->id = g_strdup (empathy_contact_get_id (priv->remote_contact));
1228 else if (priv->tp_chat) {
1231 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1232 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1234 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1237 chat_set_show_contacts (chat, priv->remote_contact == NULL);
1239 g_object_notify (G_OBJECT (chat), "remote-contact");
1240 g_object_notify (G_OBJECT (chat), "id");
1244 chat_destroy_cb (EmpathyTpChat *tp_chat,
1247 EmpathyChatPriv *priv;
1249 priv = GET_PRIV (chat);
1251 if (!priv->tp_chat) {
1255 chat_composing_remove_timeout (chat);
1256 g_object_unref (priv->tp_chat);
1257 priv->tp_chat = NULL;
1258 g_object_notify (G_OBJECT (chat), "tp-chat");
1260 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1261 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1262 chat_set_show_contacts (chat, FALSE);
1266 show_pending_messages (EmpathyChat *chat) {
1267 EmpathyChatPriv *priv = GET_PRIV (chat);
1268 const GList *messages, *l;
1270 if (chat->view == NULL || priv->tp_chat == NULL)
1273 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1275 for (l = messages; l != NULL ; l = g_list_next (l)) {
1276 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1277 chat_message_received (chat, message);
1279 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1283 chat_create_ui (EmpathyChat *chat)
1285 EmpathyChatPriv *priv = GET_PRIV (chat);
1289 GtkTextBuffer *buffer;
1291 filename = empathy_file_lookup ("empathy-chat.ui",
1293 gui = empathy_builder_get_file (filename,
1294 "chat_widget", &priv->widget,
1295 "hpaned", &priv->hpaned,
1296 "vbox_left", &priv->vbox_left,
1297 "scrolled_window_chat", &priv->scrolled_window_chat,
1298 "scrolled_window_input", &priv->scrolled_window_input,
1299 "hbox_topic", &priv->hbox_topic,
1300 "label_topic", &priv->label_topic,
1301 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1305 /* Add message view. */
1306 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1307 g_signal_connect (chat->view, "focus_in_event",
1308 G_CALLBACK (chat_text_view_focus_in_event_cb),
1310 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1311 GTK_WIDGET (chat->view));
1312 gtk_widget_show (GTK_WIDGET (chat->view));
1314 /* Add input GtkTextView */
1315 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1316 "pixels-above-lines", 2,
1317 "pixels-below-lines", 2,
1318 "pixels-inside-wrap", 1,
1321 "wrap-mode", GTK_WRAP_WORD_CHAR,
1323 g_signal_connect (chat->input_text_view, "key-press-event",
1324 G_CALLBACK (chat_input_key_press_event_cb),
1326 g_signal_connect (chat->input_text_view, "size-request",
1327 G_CALLBACK (chat_input_size_request_cb),
1329 g_signal_connect (chat->input_text_view, "realize",
1330 G_CALLBACK (chat_input_realize_cb),
1332 g_signal_connect (chat->input_text_view, "populate-popup",
1333 G_CALLBACK (chat_input_populate_popup_cb),
1335 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1336 g_signal_connect (buffer, "changed",
1337 G_CALLBACK (chat_input_text_buffer_changed_cb),
1339 gtk_text_buffer_create_tag (buffer, "misspelled",
1340 "underline", PANGO_UNDERLINE_ERROR,
1342 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1343 chat->input_text_view);
1344 gtk_widget_show (chat->input_text_view);
1346 /* Create contact list */
1347 chat_set_show_contacts (chat, priv->remote_contact == NULL);
1349 /* Initialy hide the topic, will be shown if not empty */
1350 gtk_widget_hide (priv->hbox_topic);
1352 /* Set widget focus order */
1353 list = g_list_append (NULL, priv->scrolled_window_input);
1354 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1357 list = g_list_append (NULL, priv->vbox_left);
1358 list = g_list_append (list, priv->scrolled_window_contacts);
1359 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1362 list = g_list_append (NULL, priv->hpaned);
1363 list = g_list_append (list, priv->hbox_topic);
1364 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1367 /* Add the main widget in the chat widget */
1368 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1369 g_object_unref (gui);
1373 chat_size_request (GtkWidget *widget,
1374 GtkRequisition *requisition)
1376 GtkBin *bin = GTK_BIN (widget);
1378 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1379 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1381 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1383 GtkRequisition child_requisition;
1385 gtk_widget_size_request (bin->child, &child_requisition);
1387 requisition->width += child_requisition.width;
1388 requisition->height += child_requisition.height;
1393 chat_size_allocate (GtkWidget *widget,
1394 GtkAllocation *allocation)
1396 GtkBin *bin = GTK_BIN (widget);
1397 GtkAllocation child_allocation;
1399 widget->allocation = *allocation;
1401 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1403 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1404 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1405 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1406 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1408 gtk_widget_size_allocate (bin->child, &child_allocation);
1413 chat_finalize (GObject *object)
1416 EmpathyChatPriv *priv;
1418 chat = EMPATHY_CHAT (object);
1419 priv = GET_PRIV (chat);
1421 DEBUG ("Finalized: %p", object);
1423 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1424 g_slist_free (priv->sent_messages);
1426 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1427 g_list_free (priv->compositors);
1429 chat_composing_remove_timeout (chat);
1431 g_signal_handlers_disconnect_by_func (priv->account_manager,
1432 chat_new_connection_cb, object);
1434 g_object_unref (priv->account_manager);
1435 g_object_unref (priv->log_manager);
1437 if (priv->tp_chat) {
1438 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1439 chat_destroy_cb, chat);
1440 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1441 chat_message_received_cb, chat);
1442 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1443 chat_send_error_cb, chat);
1444 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1445 chat_state_changed_cb, chat);
1446 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1447 chat_property_changed_cb, chat);
1448 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1449 chat_members_changed_cb, chat);
1450 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1451 chat_remote_contact_changed_cb, chat);
1452 empathy_tp_chat_close (priv->tp_chat);
1453 g_object_unref (priv->tp_chat);
1455 if (priv->account) {
1456 g_object_unref (priv->account);
1458 if (priv->remote_contact) {
1459 g_object_unref (priv->remote_contact);
1462 if (priv->block_events_timeout_id) {
1463 g_source_remove (priv->block_events_timeout_id);
1467 g_free (priv->name);
1468 g_free (priv->subject);
1469 g_completion_free (priv->completion);
1471 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1475 chat_constructed (GObject *object)
1477 EmpathyChat *chat = EMPATHY_CHAT (object);
1479 chat_create_ui (chat);
1480 chat_add_logs (chat);
1481 show_pending_messages (chat);
1485 empathy_chat_class_init (EmpathyChatClass *klass)
1487 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1488 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1490 object_class->finalize = chat_finalize;
1491 object_class->get_property = chat_get_property;
1492 object_class->set_property = chat_set_property;
1493 object_class->constructed = chat_constructed;
1495 widget_class->size_request = chat_size_request;
1496 widget_class->size_allocate = chat_size_allocate;
1498 g_object_class_install_property (object_class,
1500 g_param_spec_object ("tp-chat",
1502 "The tp chat object",
1503 EMPATHY_TYPE_TP_CHAT,
1505 G_PARAM_READWRITE));
1506 g_object_class_install_property (object_class,
1508 g_param_spec_object ("account",
1509 "Account of the chat",
1510 "The account of the chat",
1513 g_object_class_install_property (object_class,
1515 g_param_spec_string ("id",
1517 "The id of the chat",
1520 g_object_class_install_property (object_class,
1522 g_param_spec_string ("name",
1524 "The name of the chat",
1527 g_object_class_install_property (object_class,
1529 g_param_spec_string ("subject",
1531 "The subject or topic of the chat",
1534 g_object_class_install_property (object_class,
1535 PROP_REMOTE_CONTACT,
1536 g_param_spec_object ("remote-contact",
1537 "The remote contact",
1538 "The remote contact is any",
1539 EMPATHY_TYPE_CONTACT,
1542 signals[COMPOSING] =
1543 g_signal_new ("composing",
1544 G_OBJECT_CLASS_TYPE (object_class),
1548 g_cclosure_marshal_VOID__BOOLEAN,
1552 signals[NEW_MESSAGE] =
1553 g_signal_new ("new-message",
1554 G_OBJECT_CLASS_TYPE (object_class),
1558 g_cclosure_marshal_VOID__OBJECT,
1560 1, EMPATHY_TYPE_MESSAGE);
1562 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1566 chat_block_events_timeout_cb (gpointer data)
1568 EmpathyChatPriv *priv = GET_PRIV (data);
1570 priv->block_events_timeout_id = 0;
1576 empathy_chat_init (EmpathyChat *chat)
1578 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1579 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1582 priv->log_manager = empathy_log_manager_dup_singleton ();
1583 priv->contacts_width = -1;
1584 priv->sent_messages = NULL;
1585 priv->sent_messages_index = -1;
1586 priv->account_manager = empathy_account_manager_dup_singleton ();
1588 g_signal_connect (priv->account_manager,
1590 G_CALLBACK (chat_new_connection_cb),
1593 /* Block events for some time to avoid having "has come online" or
1594 * "joined" messages. */
1595 priv->block_events_timeout_id =
1596 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1598 /* Add nick name completion */
1599 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1600 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1604 empathy_chat_new (EmpathyTpChat *tp_chat)
1606 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1610 empathy_chat_get_tp_chat (EmpathyChat *chat)
1612 EmpathyChatPriv *priv = GET_PRIV (chat);
1614 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1616 return priv->tp_chat;
1620 empathy_chat_set_tp_chat (EmpathyChat *chat,
1621 EmpathyTpChat *tp_chat)
1623 EmpathyChatPriv *priv = GET_PRIV (chat);
1624 TpConnection *connection;
1626 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1627 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1628 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1630 if (priv->tp_chat) {
1634 if (priv->account) {
1635 g_object_unref (priv->account);
1638 priv->tp_chat = g_object_ref (tp_chat);
1639 connection = empathy_tp_chat_get_connection (priv->tp_chat);
1640 priv->account = empathy_account_manager_get_account (priv->account_manager,
1642 g_object_ref (priv->account);
1644 g_signal_connect (tp_chat, "destroy",
1645 G_CALLBACK (chat_destroy_cb),
1647 g_signal_connect (tp_chat, "message-received",
1648 G_CALLBACK (chat_message_received_cb),
1650 g_signal_connect (tp_chat, "send-error",
1651 G_CALLBACK (chat_send_error_cb),
1653 g_signal_connect (tp_chat, "chat-state-changed",
1654 G_CALLBACK (chat_state_changed_cb),
1656 g_signal_connect (tp_chat, "property-changed",
1657 G_CALLBACK (chat_property_changed_cb),
1659 g_signal_connect (tp_chat, "members-changed",
1660 G_CALLBACK (chat_members_changed_cb),
1662 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1663 G_CALLBACK (chat_remote_contact_changed_cb),
1666 chat_remote_contact_changed_cb (chat);
1668 if (chat->input_text_view) {
1669 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1670 if (priv->block_events_timeout_id == 0) {
1671 empathy_chat_view_append_event (chat->view, _("Connected"));
1675 g_object_notify (G_OBJECT (chat), "tp-chat");
1676 g_object_notify (G_OBJECT (chat), "id");
1677 g_object_notify (G_OBJECT (chat), "account");
1679 /* This is a noop when tp-chat is set at object construction time and causes
1680 * the pending messages to be show when it's set on the object after it has
1682 show_pending_messages (chat);
1686 empathy_chat_get_account (EmpathyChat *chat)
1688 EmpathyChatPriv *priv = GET_PRIV (chat);
1690 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1692 return priv->account;
1696 empathy_chat_get_id (EmpathyChat *chat)
1698 EmpathyChatPriv *priv = GET_PRIV (chat);
1700 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1706 empathy_chat_get_name (EmpathyChat *chat)
1708 EmpathyChatPriv *priv = GET_PRIV (chat);
1711 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1714 if (!ret && priv->remote_contact) {
1715 ret = empathy_contact_get_name (priv->remote_contact);
1721 return ret ? ret : _("Conversation");
1725 empathy_chat_get_subject (EmpathyChat *chat)
1727 EmpathyChatPriv *priv = GET_PRIV (chat);
1729 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1731 return priv->subject;
1735 empathy_chat_get_remote_contact (EmpathyChat *chat)
1737 EmpathyChatPriv *priv = GET_PRIV (chat);
1739 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1741 return priv->remote_contact;
1745 empathy_chat_get_contact_menu (EmpathyChat *chat)
1747 EmpathyChatPriv *priv = GET_PRIV (chat);
1748 GtkWidget *menu = NULL;
1750 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1752 if (priv->remote_contact) {
1753 menu = empathy_contact_menu_new (priv->remote_contact,
1754 EMPATHY_CONTACT_FEATURE_CALL |
1755 EMPATHY_CONTACT_FEATURE_LOG |
1756 EMPATHY_CONTACT_FEATURE_INFO);
1758 else if (priv->contact_list_view) {
1759 EmpathyContactListView *view;
1761 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
1762 menu = empathy_contact_list_view_get_contact_menu (view);
1769 empathy_chat_clear (EmpathyChat *chat)
1771 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1773 empathy_chat_view_clear (chat->view);
1777 empathy_chat_scroll_down (EmpathyChat *chat)
1779 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1781 empathy_chat_view_scroll_down (chat->view);
1785 empathy_chat_cut (EmpathyChat *chat)
1787 GtkTextBuffer *buffer;
1789 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1791 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1792 if (gtk_text_buffer_get_has_selection (buffer)) {
1793 GtkClipboard *clipboard;
1795 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1797 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1802 empathy_chat_copy (EmpathyChat *chat)
1804 GtkTextBuffer *buffer;
1806 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1808 if (empathy_chat_view_get_has_selection (chat->view)) {
1809 empathy_chat_view_copy_clipboard (chat->view);
1813 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1814 if (gtk_text_buffer_get_has_selection (buffer)) {
1815 GtkClipboard *clipboard;
1817 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1819 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1824 empathy_chat_paste (EmpathyChat *chat)
1826 GtkTextBuffer *buffer;
1827 GtkClipboard *clipboard;
1829 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1831 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1832 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1834 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1838 empathy_chat_correct_word (EmpathyChat *chat,
1841 const gchar *new_word)
1843 GtkTextBuffer *buffer;
1845 g_return_if_fail (chat != NULL);
1846 g_return_if_fail (new_word != NULL);
1848 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1850 gtk_text_buffer_delete (buffer, start, end);
1851 gtk_text_buffer_insert (buffer, start,
1857 empathy_chat_is_room (EmpathyChat *chat)
1859 EmpathyChatPriv *priv = GET_PRIV (chat);
1861 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1863 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);