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;
68 gulong tp_chat_destroy_handler;
73 EmpathyContact *remote_contact;
75 EmpathyLogManager *log_manager;
76 EmpathyAccountManager *account_manager;
77 GSList *sent_messages;
78 gint sent_messages_index;
80 GCompletion *completion;
81 guint composing_stop_timeout_id;
82 guint block_events_timeout_id;
83 TpHandleType handle_type;
85 gboolean has_input_vscroll;
90 GtkWidget *scrolled_window_chat;
91 GtkWidget *scrolled_window_input;
92 GtkWidget *scrolled_window_contacts;
93 GtkWidget *hbox_topic;
94 GtkWidget *label_topic;
95 GtkWidget *contact_list_view;
114 static guint signals[LAST_SIGNAL] = { 0 };
116 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
119 chat_get_property (GObject *object,
124 EmpathyChat *chat = EMPATHY_CHAT (object);
125 EmpathyChatPriv *priv = GET_PRIV (object);
129 g_value_set_object (value, priv->tp_chat);
132 g_value_set_object (value, priv->account);
135 g_value_set_string (value, empathy_chat_get_name (chat));
138 g_value_set_string (value, priv->id);
141 g_value_set_string (value, priv->subject);
143 case PROP_REMOTE_CONTACT:
144 g_value_set_object (value, priv->remote_contact);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
153 chat_set_property (GObject *object,
158 EmpathyChat *chat = EMPATHY_CHAT (object);
162 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
171 chat_connection_changed_cb (EmpathyAccountManager *manager,
173 TpConnectionStatusReason reason,
174 TpConnectionStatus current,
175 TpConnectionStatus previous,
178 EmpathyChatPriv *priv = GET_PRIV (chat);
180 if (current == TP_CONNECTION_STATUS_CONNECTED && !priv->tp_chat &&
181 empathy_account_equal (account, priv->account) &&
182 priv->handle_type != TP_HANDLE_TYPE_NONE &&
183 !EMP_STR_EMPTY (priv->id)) {
185 DEBUG ("Account reconnected, request a new Text channel");
187 switch (priv->handle_type) {
188 case TP_HANDLE_TYPE_CONTACT:
189 empathy_dispatcher_chat_with_contact_id (account, priv->id,
192 case TP_HANDLE_TYPE_ROOM:
193 empathy_dispatcher_join_muc (account, priv->id, NULL, NULL);
196 g_assert_not_reached ();
203 chat_composing_remove_timeout (EmpathyChat *chat)
205 EmpathyChatPriv *priv;
207 priv = GET_PRIV (chat);
209 if (priv->composing_stop_timeout_id) {
210 g_source_remove (priv->composing_stop_timeout_id);
211 priv->composing_stop_timeout_id = 0;
216 chat_composing_stop_timeout_cb (EmpathyChat *chat)
218 EmpathyChatPriv *priv;
220 priv = GET_PRIV (chat);
222 priv->composing_stop_timeout_id = 0;
223 empathy_tp_chat_set_state (priv->tp_chat,
224 TP_CHANNEL_CHAT_STATE_PAUSED);
230 chat_composing_start (EmpathyChat *chat)
232 EmpathyChatPriv *priv;
234 priv = GET_PRIV (chat);
236 if (priv->composing_stop_timeout_id) {
237 /* Just restart the timeout */
238 chat_composing_remove_timeout (chat);
240 empathy_tp_chat_set_state (priv->tp_chat,
241 TP_CHANNEL_CHAT_STATE_COMPOSING);
244 priv->composing_stop_timeout_id = g_timeout_add_seconds (
245 COMPOSING_STOP_TIMEOUT,
246 (GSourceFunc) chat_composing_stop_timeout_cb,
251 chat_composing_stop (EmpathyChat *chat)
253 EmpathyChatPriv *priv;
255 priv = GET_PRIV (chat);
257 chat_composing_remove_timeout (chat);
258 empathy_tp_chat_set_state (priv->tp_chat,
259 TP_CHANNEL_CHAT_STATE_ACTIVE);
263 chat_sent_message_add (EmpathyChat *chat,
266 EmpathyChatPriv *priv;
270 priv = GET_PRIV (chat);
272 /* Save the sent message in our repeat buffer */
273 list = priv->sent_messages;
275 /* Remove any other occurances of this msg */
276 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
277 list = g_slist_remove_link (list, item);
279 g_slist_free1 (item);
282 /* Trim the list to the last 10 items */
283 while (g_slist_length (list) > 10) {
284 item = g_slist_last (list);
286 list = g_slist_remove_link (list, item);
288 g_slist_free1 (item);
292 /* Add new message */
293 list = g_slist_prepend (list, g_strdup (str));
295 /* Set list and reset the index */
296 priv->sent_messages = list;
297 priv->sent_messages_index = -1;
301 chat_sent_message_get_next (EmpathyChat *chat)
303 EmpathyChatPriv *priv;
306 priv = GET_PRIV (chat);
308 if (!priv->sent_messages) {
309 DEBUG ("No sent messages, next message is NULL");
313 max = g_slist_length (priv->sent_messages) - 1;
315 if (priv->sent_messages_index < max) {
316 priv->sent_messages_index++;
319 DEBUG ("Returning next message index:%d", priv->sent_messages_index);
321 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
325 chat_sent_message_get_last (EmpathyChat *chat)
327 EmpathyChatPriv *priv;
329 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
331 priv = GET_PRIV (chat);
333 if (!priv->sent_messages) {
334 DEBUG ("No sent messages, last message is NULL");
338 if (priv->sent_messages_index >= 0) {
339 priv->sent_messages_index--;
342 DEBUG ("Returning last message index:%d", priv->sent_messages_index);
344 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
348 chat_send (EmpathyChat *chat,
351 EmpathyChatPriv *priv;
353 if (EMP_STR_EMPTY (msg)) {
357 priv = GET_PRIV (chat);
359 chat_sent_message_add (chat, msg);
361 /* If this is not a command, send the message */
363 EmpathyMessage *message;
365 message = empathy_message_new (msg);
366 empathy_tp_chat_send (priv->tp_chat, message);
367 g_object_unref (message);
371 /* Check for all supported commands */
372 if (g_str_has_prefix (msg, "/clear")) {
373 empathy_chat_view_clear (chat->view);
377 /* This is an unknown command, display a message to the user */
378 empathy_chat_view_append_event (chat->view, _("Unsupported command"));
382 chat_input_text_view_send (EmpathyChat *chat)
384 EmpathyChatPriv *priv;
385 GtkTextBuffer *buffer;
386 GtkTextIter start, end;
389 priv = GET_PRIV (chat);
391 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
393 gtk_text_buffer_get_bounds (buffer, &start, &end);
394 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
396 /* clear the input field */
397 gtk_text_buffer_set_text (buffer, "", -1);
399 chat_send (chat, msg);
404 chat_state_changed_cb (EmpathyTpChat *tp_chat,
405 EmpathyContact *contact,
406 TpChannelChatState state,
409 EmpathyChatPriv *priv;
411 gboolean was_composing;
413 priv = GET_PRIV (chat);
415 if (empathy_contact_is_user (contact)) {
416 /* We don't care about our own chat state */
420 was_composing = (priv->compositors != NULL);
422 /* Find the contact in the list. After that l is the list elem or NULL */
423 for (l = priv->compositors; l; l = l->next) {
424 if (contact == l->data) {
430 case TP_CHANNEL_CHAT_STATE_GONE:
431 case TP_CHANNEL_CHAT_STATE_INACTIVE:
432 case TP_CHANNEL_CHAT_STATE_PAUSED:
433 case TP_CHANNEL_CHAT_STATE_ACTIVE:
434 /* Contact is not composing */
436 priv->compositors = g_list_remove_link (priv->compositors, l);
437 g_object_unref (l->data);
441 case TP_CHANNEL_CHAT_STATE_COMPOSING:
442 /* Contact is composing */
444 priv->compositors = g_list_prepend (priv->compositors,
445 g_object_ref (contact));
449 g_assert_not_reached ();
452 DEBUG ("Was composing: %s now composing: %s",
453 was_composing ? "yes" : "no",
454 priv->compositors ? "yes" : "no");
456 if ((was_composing && !priv->compositors) ||
457 (!was_composing && priv->compositors)) {
458 /* Composing state changed */
459 g_signal_emit (chat, signals[COMPOSING], 0,
460 priv->compositors != NULL);
465 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
467 EmpathyChatPriv *priv = GET_PRIV (chat);
468 EmpathyContact *sender;
470 sender = empathy_message_get_sender (message);
472 DEBUG ("Appending new message from %s (%d)",
473 empathy_contact_get_name (sender),
474 empathy_contact_get_handle (sender));
476 empathy_chat_view_append_message (chat->view, message);
478 /* We received a message so the contact is no longer composing */
479 chat_state_changed_cb (priv->tp_chat, sender,
480 TP_CHANNEL_CHAT_STATE_ACTIVE,
483 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
487 chat_message_received_cb (EmpathyTpChat *tp_chat,
488 EmpathyMessage *message,
491 chat_message_received (chat, message);
492 empathy_tp_chat_acknowledge_message (tp_chat, message);
496 chat_send_error_cb (EmpathyTpChat *tp_chat,
497 EmpathyMessage *message,
498 TpChannelTextSendError error_code,
504 switch (error_code) {
505 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
506 error = _("offline");
508 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
509 error = _("invalid contact");
511 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
512 error = _("permission denied");
514 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
515 error = _("too long message");
517 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
518 error = _("not implemented");
521 error = _("unknown");
525 str = g_strdup_printf (_("Error sending message '%s': %s"),
526 empathy_message_get_body (message),
528 empathy_chat_view_append_event (chat->view, str);
533 chat_property_changed_cb (EmpathyTpChat *tp_chat,
538 EmpathyChatPriv *priv = GET_PRIV (chat);
540 if (!tp_strdiff (name, "subject")) {
541 g_free (priv->subject);
542 priv->subject = g_value_dup_string (value);
543 g_object_notify (G_OBJECT (chat), "subject");
545 if (EMP_STR_EMPTY (priv->subject)) {
546 gtk_widget_hide (priv->hbox_topic);
548 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
549 gtk_widget_show (priv->hbox_topic);
551 if (priv->block_events_timeout_id == 0) {
554 if (!EMP_STR_EMPTY (priv->subject)) {
555 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
557 str = g_strdup (_("No topic defined"));
559 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
563 else if (!tp_strdiff (name, "name")) {
565 priv->name = g_value_dup_string (value);
566 g_object_notify (G_OBJECT (chat), "name");
571 chat_get_is_command (const gchar *str)
573 g_return_val_if_fail (str != NULL, FALSE);
579 if (g_str_has_prefix (str, "/me")) {
582 else if (g_str_has_prefix (str, "/nick")) {
585 else if (g_str_has_prefix (str, "/topic")) {
593 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
596 EmpathyChatPriv *priv;
597 GtkTextIter start, end;
599 gboolean spell_checker = FALSE;
601 priv = GET_PRIV (chat);
603 if (gtk_text_buffer_get_char_count (buffer) == 0) {
604 chat_composing_stop (chat);
606 chat_composing_start (chat);
609 empathy_conf_get_bool (empathy_conf_get (),
610 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
613 gtk_text_buffer_get_start_iter (buffer, &start);
615 if (!spell_checker) {
616 gtk_text_buffer_get_end_iter (buffer, &end);
617 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
621 if (!empathy_spell_supported ()) {
625 /* NOTE: this is really inefficient, we shouldn't have to
626 reiterate the whole buffer each time and check each work
629 gboolean correct = FALSE;
632 if (gtk_text_iter_is_start (&start)) {
635 if (!gtk_text_iter_forward_word_end (&end)) {
636 /* no whole word yet */
640 if (!gtk_text_iter_forward_word_end (&end)) {
641 /* must be the end of the buffer */
646 gtk_text_iter_backward_word_start (&start);
649 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
651 /* spell check string */
652 if (!chat_get_is_command (str)) {
653 correct = empathy_spell_check (str);
659 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
661 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
666 /* set start iter to the end iters position */
672 chat_input_key_press_event_cb (GtkWidget *widget,
676 EmpathyChatPriv *priv;
679 GtkWidget *text_view_sw;
681 priv = GET_PRIV (chat);
683 /* Catch ctrl+up/down so we can traverse messages we sent */
684 if ((event->state & GDK_CONTROL_MASK) &&
685 (event->keyval == GDK_Up ||
686 event->keyval == GDK_Down)) {
687 GtkTextBuffer *buffer;
690 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
692 if (event->keyval == GDK_Up) {
693 str = chat_sent_message_get_next (chat);
695 str = chat_sent_message_get_last (chat);
698 g_signal_handlers_block_by_func (buffer,
699 chat_input_text_buffer_changed_cb,
701 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
702 g_signal_handlers_unblock_by_func (buffer,
703 chat_input_text_buffer_changed_cb,
709 /* Catch enter but not ctrl/shift-enter */
710 if (IS_ENTER (event->keyval) &&
711 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
714 /* This is to make sure that kinput2 gets the enter. And if
715 * it's handled there we shouldn't send on it. This is because
716 * kinput2 uses Enter to commit letters. See:
717 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
720 view = GTK_TEXT_VIEW (chat->input_text_view);
721 if (gtk_im_context_filter_keypress (view->im_context, event)) {
722 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
726 chat_input_text_view_send (chat);
730 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
732 if (IS_ENTER (event->keyval) &&
733 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
734 /* Newline for shift/control-enter. */
737 if (!(event->state & GDK_CONTROL_MASK) &&
738 event->keyval == GDK_Page_Up) {
739 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
740 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
743 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
744 event->keyval == GDK_Page_Down) {
745 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
746 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
747 gtk_adjustment_set_value (adj, val);
750 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
751 event->keyval == GDK_Tab) {
752 GtkTextBuffer *buffer;
753 GtkTextIter start, current;
754 gchar *nick, *completed;
755 GList *list, *completed_list;
756 gboolean is_start_of_buffer;
758 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
759 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
761 /* Get the start of the nick to complete. */
762 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
763 gtk_text_iter_backward_word_start (&start);
764 is_start_of_buffer = gtk_text_iter_is_start (&start);
766 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
767 g_completion_add_items (priv->completion, list);
769 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
770 completed_list = g_completion_complete (priv->completion,
779 gchar *complete_char = NULL;
781 gtk_text_buffer_delete (buffer, &start, ¤t);
783 len = g_list_length (completed_list);
786 /* If we only have one hit, use that text
787 * instead of the text in completed since the
788 * completed text will use the typed string
789 * which might be cased all wrong.
792 text = empathy_contact_get_name (completed_list->data);
797 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
799 if (len == 1 && is_start_of_buffer &&
800 empathy_conf_get_string (empathy_conf_get (),
801 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
803 complete_char != NULL) {
804 gtk_text_buffer_insert_at_cursor (buffer,
806 strlen (complete_char));
807 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
808 g_free (complete_char);
814 g_completion_clear_items (priv->completion);
816 g_list_foreach (list, (GFunc) g_object_unref, NULL);
826 chat_text_view_focus_in_event_cb (GtkWidget *widget,
830 gtk_widget_grab_focus (chat->input_text_view);
836 chat_input_set_size_request_idle (gpointer sw)
838 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
844 chat_input_size_request_cb (GtkWidget *widget,
845 GtkRequisition *requisition,
848 EmpathyChatPriv *priv = GET_PRIV (chat);
851 sw = gtk_widget_get_parent (widget);
852 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
853 g_idle_add (chat_input_set_size_request_idle, sw);
854 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
857 priv->has_input_vscroll = TRUE;
860 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
861 gtk_widget_set_size_request (sw, -1, -1);
862 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
865 priv->has_input_vscroll = FALSE;
870 chat_input_realize_cb (GtkWidget *widget,
873 DEBUG ("Setting focus to the input text view");
874 gtk_widget_grab_focus (widget);
878 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
879 EmpathySmiley *smiley,
882 EmpathyChat *chat = EMPATHY_CHAT (user_data);
883 GtkTextBuffer *buffer;
886 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
888 gtk_text_buffer_get_end_iter (buffer, &iter);
889 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
891 gtk_text_buffer_get_end_iter (buffer, &iter);
892 gtk_text_buffer_insert (buffer, &iter, " ", -1);
903 static EmpathyChatSpell *
904 chat_spell_new (EmpathyChat *chat,
909 EmpathyChatSpell *chat_spell;
911 chat_spell = g_slice_new0 (EmpathyChatSpell);
913 chat_spell->chat = g_object_ref (chat);
914 chat_spell->word = g_strdup (word);
915 chat_spell->start = start;
916 chat_spell->end = end;
922 chat_spell_free (EmpathyChatSpell *chat_spell)
924 g_object_unref (chat_spell->chat);
925 g_free (chat_spell->word);
926 g_slice_free (EmpathyChatSpell, chat_spell);
930 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
931 EmpathyChatSpell *chat_spell)
933 empathy_spell_dialog_show (chat_spell->chat,
940 chat_text_send_cb (GtkMenuItem *menuitem,
943 chat_input_text_view_send (chat);
947 chat_input_populate_popup_cb (GtkTextView *view,
951 EmpathyChatPriv *priv;
952 GtkTextBuffer *buffer;
953 GtkTextTagTable *table;
956 GtkTextIter iter, start, end;
959 EmpathyChatSpell *chat_spell;
960 EmpathySmileyManager *smiley_manager;
961 GtkWidget *smiley_menu;
964 priv = GET_PRIV (chat);
965 buffer = gtk_text_view_get_buffer (view);
967 /* Add the emoticon menu. */
968 item = gtk_separator_menu_item_new ();
969 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
970 gtk_widget_show (item);
972 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
973 image = gtk_image_new_from_icon_name ("face-smile",
975 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
976 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
977 gtk_widget_show (item);
979 smiley_manager = empathy_smiley_manager_dup_singleton ();
980 smiley_menu = empathy_smiley_menu_new (smiley_manager,
981 chat_insert_smiley_activate_cb,
983 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
984 g_object_unref (smiley_manager);
986 /* Add the Send menu item. */
987 gtk_text_buffer_get_bounds (buffer, &start, &end);
988 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
989 if (!EMP_STR_EMPTY (str)) {
990 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
991 g_signal_connect (G_OBJECT (item), "activate",
992 G_CALLBACK (chat_text_send_cb), chat);
993 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
994 gtk_widget_show (item);
998 /* Add the spell check menu item. */
999 table = gtk_text_buffer_get_tag_table (buffer);
1000 tag = gtk_text_tag_table_lookup (table, "misspelled");
1001 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1002 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1003 GTK_TEXT_WINDOW_WIDGET,
1006 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1008 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1009 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1011 str = gtk_text_buffer_get_text (buffer,
1012 &start, &end, FALSE);
1014 if (!EMP_STR_EMPTY (str)) {
1015 chat_spell = chat_spell_new (chat, str, start, end);
1016 g_object_set_data_full (G_OBJECT (menu),
1017 "chat_spell", chat_spell,
1018 (GDestroyNotify) chat_spell_free);
1020 item = gtk_separator_menu_item_new ();
1021 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1022 gtk_widget_show (item);
1024 item = gtk_image_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
1025 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1026 GTK_ICON_SIZE_MENU);
1027 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1028 g_signal_connect (item,
1030 G_CALLBACK (chat_text_check_word_spelling_cb),
1032 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1033 gtk_widget_show (item);
1038 chat_log_filter (EmpathyMessage *message,
1041 EmpathyChat *chat = (EmpathyChat *) user_data;
1042 EmpathyChatPriv *priv = GET_PRIV (chat);
1043 const GList *pending;
1045 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1047 for (; pending; pending = g_list_next (pending)) {
1048 if (empathy_message_equal (message, pending->data)) {
1057 chat_add_logs (EmpathyChat *chat)
1059 EmpathyChatPriv *priv = GET_PRIV (chat);
1060 gboolean is_chatroom;
1061 GList *messages, *l;
1067 /* Turn off scrolling temporarily */
1068 empathy_chat_view_scroll (chat->view, FALSE);
1070 /* Add messages from last conversation */
1071 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1073 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1081 for (l = messages; l; l = g_list_next (l)) {
1082 empathy_chat_view_append_message (chat->view, l->data);
1083 g_object_unref (l->data);
1086 g_list_free (messages);
1088 /* Turn back on scrolling */
1089 empathy_chat_view_scroll (chat->view, TRUE);
1093 chat_contacts_completion_func (const gchar *s1,
1097 gchar *tmp, *nick1, *nick2;
1104 return s1 ? -1 : +1;
1107 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1108 nick1 = g_utf8_casefold (tmp, -1);
1111 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1112 nick2 = g_utf8_casefold (tmp, -1);
1115 ret = strncmp (nick1, nick2, n);
1124 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1125 EmpathyContact *contact,
1126 EmpathyContact *actor,
1132 EmpathyChatPriv *priv = GET_PRIV (chat);
1134 if (priv->block_events_timeout_id == 0) {
1137 empathy_contact_run_until_ready (contact,
1138 EMPATHY_CONTACT_READY_NAME,
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.glade",
1293 glade = empathy_glade_get_file (filename,
1296 "chat_widget", &priv->widget,
1297 "hpaned", &priv->hpaned,
1298 "vbox_left", &priv->vbox_left,
1299 "scrolled_window_chat", &priv->scrolled_window_chat,
1300 "scrolled_window_input", &priv->scrolled_window_input,
1301 "hbox_topic", &priv->hbox_topic,
1302 "label_topic", &priv->label_topic,
1303 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1306 g_object_unref (glade);
1308 /* Add message view. */
1309 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1310 g_signal_connect (chat->view, "focus_in_event",
1311 G_CALLBACK (chat_text_view_focus_in_event_cb),
1313 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1314 GTK_WIDGET (chat->view));
1315 gtk_widget_show (GTK_WIDGET (chat->view));
1317 /* Add input GtkTextView */
1318 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1319 "pixels-above-lines", 2,
1320 "pixels-below-lines", 2,
1321 "pixels-inside-wrap", 1,
1324 "wrap-mode", GTK_WRAP_WORD_CHAR,
1326 g_signal_connect (chat->input_text_view, "key-press-event",
1327 G_CALLBACK (chat_input_key_press_event_cb),
1329 g_signal_connect (chat->input_text_view, "size-request",
1330 G_CALLBACK (chat_input_size_request_cb),
1332 g_signal_connect (chat->input_text_view, "realize",
1333 G_CALLBACK (chat_input_realize_cb),
1335 g_signal_connect (chat->input_text_view, "populate-popup",
1336 G_CALLBACK (chat_input_populate_popup_cb),
1338 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1339 g_signal_connect (buffer, "changed",
1340 G_CALLBACK (chat_input_text_buffer_changed_cb),
1342 gtk_text_buffer_create_tag (buffer, "misspelled",
1343 "underline", PANGO_UNDERLINE_ERROR,
1345 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1346 chat->input_text_view);
1347 gtk_widget_show (chat->input_text_view);
1349 /* Create contact list */
1350 chat_set_show_contacts (chat, priv->remote_contact == NULL);
1352 /* Initialy hide the topic, will be shown if not empty */
1353 gtk_widget_hide (priv->hbox_topic);
1355 /* Set widget focus order */
1356 list = g_list_append (NULL, priv->scrolled_window_input);
1357 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1360 list = g_list_append (NULL, priv->vbox_left);
1361 list = g_list_append (list, priv->scrolled_window_contacts);
1362 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1365 list = g_list_append (NULL, priv->hpaned);
1366 list = g_list_append (list, priv->hbox_topic);
1367 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1370 /* Add the main widget in the chat widget */
1371 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1375 chat_size_request (GtkWidget *widget,
1376 GtkRequisition *requisition)
1378 GtkBin *bin = GTK_BIN (widget);
1380 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
1381 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
1383 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1385 GtkRequisition child_requisition;
1387 gtk_widget_size_request (bin->child, &child_requisition);
1389 requisition->width += child_requisition.width;
1390 requisition->height += child_requisition.height;
1395 chat_size_allocate (GtkWidget *widget,
1396 GtkAllocation *allocation)
1398 GtkBin *bin = GTK_BIN (widget);
1399 GtkAllocation child_allocation;
1401 widget->allocation = *allocation;
1403 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
1405 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
1406 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
1407 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
1408 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
1410 gtk_widget_size_allocate (bin->child, &child_allocation);
1415 chat_finalize (GObject *object)
1418 EmpathyChatPriv *priv;
1420 chat = EMPATHY_CHAT (object);
1421 priv = GET_PRIV (chat);
1423 DEBUG ("Finalized: %p", object);
1425 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
1426 g_slist_free (priv->sent_messages);
1428 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1429 g_list_free (priv->compositors);
1431 chat_composing_remove_timeout (chat);
1433 g_signal_handlers_disconnect_by_func (priv->account_manager,
1434 chat_connection_changed_cb, object);
1436 g_object_unref (priv->account_manager);
1437 g_object_unref (priv->log_manager);
1439 if (priv->tp_chat) {
1440 g_signal_handler_disconnect (priv->tp_chat, priv->tp_chat_destroy_handler);
1441 empathy_tp_chat_close (priv->tp_chat);
1442 g_object_unref (priv->tp_chat);
1444 if (priv->account) {
1445 g_object_unref (priv->account);
1447 if (priv->remote_contact) {
1448 g_object_unref (priv->remote_contact);
1451 if (priv->block_events_timeout_id) {
1452 g_source_remove (priv->block_events_timeout_id);
1456 g_free (priv->name);
1457 g_free (priv->subject);
1458 g_completion_free (priv->completion);
1460 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1464 chat_constructed (GObject *object)
1466 EmpathyChat *chat = EMPATHY_CHAT (object);
1468 chat_create_ui (chat);
1469 chat_add_logs (chat);
1470 show_pending_messages (chat);
1474 empathy_chat_class_init (EmpathyChatClass *klass)
1476 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1477 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1479 object_class->finalize = chat_finalize;
1480 object_class->get_property = chat_get_property;
1481 object_class->set_property = chat_set_property;
1482 object_class->constructed = chat_constructed;
1484 widget_class->size_request = chat_size_request;
1485 widget_class->size_allocate = chat_size_allocate;
1487 g_object_class_install_property (object_class,
1489 g_param_spec_object ("tp-chat",
1491 "The tp chat object",
1492 EMPATHY_TYPE_TP_CHAT,
1494 G_PARAM_READWRITE));
1495 g_object_class_install_property (object_class,
1497 g_param_spec_object ("account",
1498 "Account of the chat",
1499 "The account of the chat",
1502 g_object_class_install_property (object_class,
1504 g_param_spec_string ("id",
1506 "The id of the chat",
1509 g_object_class_install_property (object_class,
1511 g_param_spec_string ("name",
1513 "The name of the chat",
1516 g_object_class_install_property (object_class,
1518 g_param_spec_string ("subject",
1520 "The subject or topic of the chat",
1523 g_object_class_install_property (object_class,
1524 PROP_REMOTE_CONTACT,
1525 g_param_spec_object ("remote-contact",
1526 "The remote contact",
1527 "The remote contact is any",
1528 EMPATHY_TYPE_CONTACT,
1531 signals[COMPOSING] =
1532 g_signal_new ("composing",
1533 G_OBJECT_CLASS_TYPE (object_class),
1537 g_cclosure_marshal_VOID__BOOLEAN,
1541 signals[NEW_MESSAGE] =
1542 g_signal_new ("new-message",
1543 G_OBJECT_CLASS_TYPE (object_class),
1547 g_cclosure_marshal_VOID__OBJECT,
1549 1, EMPATHY_TYPE_MESSAGE);
1551 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1555 chat_block_events_timeout_cb (gpointer data)
1557 EmpathyChatPriv *priv = GET_PRIV (data);
1559 priv->block_events_timeout_id = 0;
1565 empathy_chat_init (EmpathyChat *chat)
1567 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1568 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1571 priv->log_manager = empathy_log_manager_dup_singleton ();
1572 priv->contacts_width = -1;
1573 priv->sent_messages = NULL;
1574 priv->sent_messages_index = -1;
1575 priv->account_manager = empathy_account_manager_dup_singleton ();
1577 g_signal_connect (priv->account_manager,
1578 "account-connection-changed",
1579 G_CALLBACK (chat_connection_changed_cb),
1582 /* Block events for some time to avoid having "has come online" or
1583 * "joined" messages. */
1584 priv->block_events_timeout_id =
1585 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1587 /* Add nick name completion */
1588 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1589 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1593 empathy_chat_new (EmpathyTpChat *tp_chat)
1595 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1599 empathy_chat_get_tp_chat (EmpathyChat *chat)
1601 EmpathyChatPriv *priv = GET_PRIV (chat);
1603 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1605 return priv->tp_chat;
1609 empathy_chat_set_tp_chat (EmpathyChat *chat,
1610 EmpathyTpChat *tp_chat)
1612 EmpathyChatPriv *priv = GET_PRIV (chat);
1614 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1615 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1616 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1618 if (priv->tp_chat) {
1622 if (priv->account) {
1623 g_object_unref (priv->account);
1626 priv->tp_chat = g_object_ref (tp_chat);
1627 priv->account = g_object_ref (empathy_tp_chat_get_account (tp_chat));
1629 g_signal_connect (tp_chat, "message-received",
1630 G_CALLBACK (chat_message_received_cb),
1632 g_signal_connect (tp_chat, "send-error",
1633 G_CALLBACK (chat_send_error_cb),
1635 g_signal_connect (tp_chat, "chat-state-changed",
1636 G_CALLBACK (chat_state_changed_cb),
1638 g_signal_connect (tp_chat, "property-changed",
1639 G_CALLBACK (chat_property_changed_cb),
1641 g_signal_connect (tp_chat, "members-changed",
1642 G_CALLBACK (chat_members_changed_cb),
1644 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1645 G_CALLBACK (chat_remote_contact_changed_cb),
1647 priv->tp_chat_destroy_handler =
1648 g_signal_connect (tp_chat, "destroy",
1649 G_CALLBACK (chat_destroy_cb),
1652 chat_remote_contact_changed_cb (chat);
1654 if (chat->input_text_view) {
1655 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1656 if (priv->block_events_timeout_id == 0) {
1657 empathy_chat_view_append_event (chat->view, _("Connected"));
1661 g_object_notify (G_OBJECT (chat), "tp-chat");
1662 g_object_notify (G_OBJECT (chat), "id");
1663 g_object_notify (G_OBJECT (chat), "account");
1665 /* This is a noop when tp-chat is set at object construction time and causes
1666 * the pending messages to be show when it's set on the object after it has
1668 show_pending_messages (chat);
1672 empathy_chat_get_account (EmpathyChat *chat)
1674 EmpathyChatPriv *priv = GET_PRIV (chat);
1676 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1678 return priv->account;
1682 empathy_chat_get_id (EmpathyChat *chat)
1684 EmpathyChatPriv *priv = GET_PRIV (chat);
1686 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1692 empathy_chat_get_name (EmpathyChat *chat)
1694 EmpathyChatPriv *priv = GET_PRIV (chat);
1697 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1700 if (!ret && priv->remote_contact) {
1701 ret = empathy_contact_get_name (priv->remote_contact);
1707 return ret ? ret : _("Conversation");
1711 empathy_chat_get_subject (EmpathyChat *chat)
1713 EmpathyChatPriv *priv = GET_PRIV (chat);
1715 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1717 return priv->subject;
1721 empathy_chat_get_remote_contact (EmpathyChat *chat)
1723 EmpathyChatPriv *priv = GET_PRIV (chat);
1725 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1727 return priv->remote_contact;
1731 empathy_chat_get_members_count (EmpathyChat *chat)
1733 EmpathyChatPriv *priv = GET_PRIV (chat);
1735 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), 0);
1737 if (priv->tp_chat) {
1738 return empathy_tp_chat_get_members_count (priv->tp_chat);
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);