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 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.h>
36 #include <glade/glade.h>
38 #include <libempathy/empathy-session.h>
39 #include <libempathy/empathy-contact-manager.h>
40 #include <libempathy/gossip-debug.h>
41 #include <libempathy/gossip-utils.h>
42 #include <libempathy/gossip-conf.h>
44 #include "gossip-chat.h"
45 #include "gossip-chat-window.h"
46 #include "gossip-geometry.h"
47 #include "gossip-preferences.h"
48 #include "gossip-spell.h"
49 //#include "gossip-spell-dialog.h"
50 #include "gossip-ui-utils.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
54 #define DEBUG_DOMAIN "Chat"
56 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
57 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
59 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
63 #define COMPOSING_STOP_TIMEOUT 5
65 struct _GossipChatPriv {
66 EmpathyTpChat *tp_chat;
67 GossipChatWindow *window;
69 GtkTooltips *tooltips;
70 guint composing_stop_timeout_id;
73 /* Used to automatically shrink a window that has temporarily
74 * grown due to long input.
77 gint default_window_height;
78 gint last_input_height;
79 gboolean vscroll_visible;
90 static void gossip_chat_class_init (GossipChatClass *klass);
91 static void gossip_chat_init (GossipChat *chat);
92 static void chat_finalize (GObject *object);
93 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
95 static void chat_send (GossipChat *chat,
97 static void chat_input_text_view_send (GossipChat *chat);
98 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
99 GossipMessage *message,
101 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
104 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
106 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
109 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
111 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
112 GtkAllocation *allocation,
114 static void chat_text_view_realize_cb (GtkWidget *widget,
116 static void chat_text_populate_popup_cb (GtkTextView *view,
119 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
120 GossipChatSpell *chat_spell);
121 static GossipChatSpell *chat_spell_new (GossipChat *chat,
125 static void chat_spell_free (GossipChatSpell *chat_spell);
126 static void chat_composing_start (GossipChat *chat);
127 static void chat_composing_stop (GossipChat *chat);
128 static void chat_composing_remove_timeout (GossipChat *chat);
129 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
139 static guint chat_signals[LAST_SIGNAL] = { 0 };
141 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
144 gossip_chat_class_init (GossipChatClass *klass)
146 GObjectClass *object_class;
148 object_class = G_OBJECT_CLASS (klass);
150 object_class->finalize = chat_finalize;
152 chat_signals[COMPOSING] =
153 g_signal_new ("composing",
154 G_OBJECT_CLASS_TYPE (object_class),
158 g_cclosure_marshal_VOID__BOOLEAN,
162 chat_signals[NEW_MESSAGE] =
163 g_signal_new ("new-message",
164 G_OBJECT_CLASS_TYPE (object_class),
168 g_cclosure_marshal_VOID__OBJECT,
170 1, GOSSIP_TYPE_MESSAGE);
172 chat_signals[NAME_CHANGED] =
173 g_signal_new ("name-changed",
174 G_OBJECT_CLASS_TYPE (object_class),
178 g_cclosure_marshal_VOID__POINTER,
182 chat_signals[STATUS_CHANGED] =
183 g_signal_new ("status-changed",
184 G_OBJECT_CLASS_TYPE (object_class),
188 g_cclosure_marshal_VOID__VOID,
192 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
196 gossip_chat_init (GossipChat *chat)
198 GossipChatPriv *priv;
199 GtkTextBuffer *buffer;
201 chat->view = gossip_chat_view_new ();
202 chat->input_text_view = gtk_text_view_new ();
204 chat->is_first_char = TRUE;
206 g_object_set (chat->input_text_view,
207 "pixels-above-lines", 2,
208 "pixels-below-lines", 2,
209 "pixels-inside-wrap", 1,
212 "wrap-mode", GTK_WRAP_WORD_CHAR,
215 priv = GET_PRIV (chat);
217 priv->tooltips = gtk_tooltips_new ();
219 priv->default_window_height = -1;
220 priv->vscroll_visible = FALSE;
221 priv->sensitive = TRUE;
223 g_signal_connect (chat->input_text_view,
225 G_CALLBACK (chat_input_key_press_event_cb),
228 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
229 g_signal_connect (buffer,
231 G_CALLBACK (chat_input_text_buffer_changed_cb),
233 g_signal_connect (GOSSIP_CHAT (chat)->view,
235 G_CALLBACK (chat_text_view_focus_in_event_cb),
238 g_signal_connect (chat->input_text_view,
240 G_CALLBACK (chat_text_view_size_allocate_cb),
243 g_signal_connect (chat->input_text_view,
245 G_CALLBACK (chat_text_view_realize_cb),
248 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
250 G_CALLBACK (chat_text_populate_popup_cb),
253 /* create misspelt words identification tag */
254 gtk_text_buffer_create_tag (buffer,
256 "underline", PANGO_UNDERLINE_ERROR,
261 chat_finalize (GObject *object)
264 GossipChatPriv *priv;
266 chat = GOSSIP_CHAT (object);
267 priv = GET_PRIV (chat);
269 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
271 chat_composing_remove_timeout (chat);
272 g_object_unref (GOSSIP_CHAT (object)->account);
275 g_object_unref (priv->tp_chat);
280 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
284 chat_destroy_cb (EmpathyTpChat *tp_chat,
287 GossipChatPriv *priv;
290 priv = GET_PRIV (chat);
293 g_object_unref (priv->tp_chat);
294 priv->tp_chat = NULL;
297 gossip_chat_view_append_event (chat->view, _("Disconnected"));
299 widget = gossip_chat_get_widget (chat);
300 gtk_widget_set_sensitive (widget, FALSE);
301 priv->sensitive = FALSE;
305 chat_send (GossipChat *chat,
308 GossipChatPriv *priv;
309 //GossipLogManager *log_manager;
310 GossipMessage *message;
311 GossipContact *own_contact;
313 priv = GET_PRIV (chat);
315 if (msg == NULL || msg[0] == '\0') {
319 if (g_str_has_prefix (msg, "/clear")) {
320 gossip_chat_view_clear (chat->view);
324 /* FIXME: gossip_app_set_not_away ();*/
326 own_contact = gossip_chat_get_own_contact (chat);
327 message = gossip_message_new (msg);
328 gossip_message_set_sender (message, own_contact);
330 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
331 //gossip_log_message_for_contact (log_manager, message, FALSE);
333 empathy_tp_chat_send (priv->tp_chat, message);
335 g_object_unref (message);
339 chat_input_text_view_send (GossipChat *chat)
341 GossipChatPriv *priv;
342 GtkTextBuffer *buffer;
343 GtkTextIter start, end;
346 priv = GET_PRIV (chat);
348 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
350 gtk_text_buffer_get_bounds (buffer, &start, &end);
351 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
353 /* clear the input field */
354 gtk_text_buffer_set_text (buffer, "", -1);
356 chat_send (chat, msg);
360 chat->is_first_char = TRUE;
364 chat_message_received_cb (EmpathyTpChat *tp_chat,
365 GossipMessage *message,
368 GossipChatPriv *priv;
369 //GossipLogManager *log_manager;
370 GossipContact *sender;
372 priv = GET_PRIV (chat);
374 sender = gossip_message_get_sender (message);
375 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
376 gossip_contact_get_name (sender));
379 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
380 gossip_log_message_for_contact (log_manager, message, TRUE);
382 gossip_chat_view_append_message (chat->view, message);
384 if (gossip_chat_should_play_sound (chat)) {
385 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
388 g_signal_emit_by_name (chat, "new-message", message);
392 chat_input_key_press_event_cb (GtkWidget *widget,
396 GossipChatPriv *priv;
399 GtkWidget *text_view_sw;
401 priv = GET_PRIV (chat);
403 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
407 /* Catch enter but not ctrl/shift-enter */
408 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
411 /* This is to make sure that kinput2 gets the enter. And if
412 * it's handled there we shouldn't send on it. This is because
413 * kinput2 uses Enter to commit letters. See:
414 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
417 view = GTK_TEXT_VIEW (chat->input_text_view);
418 if (gtk_im_context_filter_keypress (view->im_context, event)) {
419 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
423 chat_input_text_view_send (chat);
427 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
428 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
429 /* Newline for shift-enter. */
432 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
433 event->keyval == GDK_Page_Up) {
434 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
435 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
439 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
440 event->keyval == GDK_Page_Down) {
441 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
442 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
443 gtk_adjustment_set_value (adj, val);
452 chat_text_view_focus_in_event_cb (GtkWidget *widget,
456 gtk_widget_grab_focus (chat->input_text_view);
462 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
465 GossipChatPriv *priv;
466 GtkTextIter start, end;
468 gboolean spell_checker = FALSE;
470 priv = GET_PRIV (chat);
472 if (gtk_text_buffer_get_char_count (buffer) == 0) {
473 chat_composing_stop (chat);
475 chat_composing_start (chat);
478 gossip_conf_get_bool (gossip_conf_get (),
479 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
482 if (chat->is_first_char) {
486 GtkAllocation *allocation;
488 /* Save the window's size */
489 dialog = gossip_chat_window_get_dialog (priv->window);
490 gtk_window_get_size (GTK_WINDOW (dialog),
491 NULL, &window_height);
493 gtk_widget_size_request (chat->input_text_view, &req);
495 allocation = >K_WIDGET (chat->view)->allocation;
497 priv->default_window_height = window_height;
498 priv->last_input_height = req.height;
499 priv->padding_height = window_height - req.height - allocation->height;
501 chat->is_first_char = FALSE;
504 gtk_text_buffer_get_start_iter (buffer, &start);
506 if (!spell_checker) {
507 gtk_text_buffer_get_end_iter (buffer, &end);
508 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
512 if (!gossip_spell_supported ()) {
516 /* NOTE: this is really inefficient, we shouldn't have to
517 reiterate the whole buffer each time and check each work
520 gboolean correct = FALSE;
523 if (gtk_text_iter_is_start (&start)) {
526 if (!gtk_text_iter_forward_word_end (&end)) {
527 /* no whole word yet */
531 if (!gtk_text_iter_forward_word_end (&end)) {
532 /* must be the end of the buffer */
537 gtk_text_iter_backward_word_start (&start);
540 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
542 /* spell check string */
543 if (!gossip_chat_get_is_command (str)) {
544 correct = gossip_spell_check (str);
550 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
552 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
557 /* set start iter to the end iters position */
569 chat_change_size_in_idle_cb (ChangeSizeData *data)
571 gtk_window_resize (GTK_WINDOW (data->window),
572 data->width, data->height);
578 chat_text_view_scroll_hide_cb (GtkWidget *widget,
581 GossipChatPriv *priv;
584 priv = GET_PRIV (chat);
586 priv->vscroll_visible = FALSE;
587 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
589 sw = gtk_widget_get_parent (chat->input_text_view);
590 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
593 g_object_set (sw, "height-request", -1, NULL);
597 chat_text_view_size_allocate_cb (GtkWidget *widget,
598 GtkAllocation *allocation,
601 GossipChatPriv *priv;
604 ChangeSizeData *data;
607 GtkAllocation *view_allocation;
612 priv = GET_PRIV (chat);
614 if (priv->default_window_height <= 0) {
618 sw = gtk_widget_get_parent (widget);
619 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
622 priv->vscroll_visible = TRUE;
623 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
624 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
626 GTK_POLICY_AUTOMATIC);
627 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
628 g_signal_connect (vscroll, "hide",
629 G_CALLBACK (chat_text_view_scroll_hide_cb),
633 if (priv->last_input_height <= allocation->height) {
634 priv->last_input_height = allocation->height;
638 diff = priv->last_input_height - allocation->height;
639 priv->last_input_height = allocation->height;
641 view_allocation = >K_WIDGET (chat->view)->allocation;
643 dialog = gossip_chat_window_get_dialog (priv->window);
644 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
646 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
648 if (new_height <= priv->default_window_height) {
649 window_height = priv->default_window_height;
651 window_height = new_height;
654 if (current_height <= window_height) {
658 /* Restore the window's size */
659 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
661 data = g_new0 (ChangeSizeData, 1);
662 data->window = dialog;
664 data->height = window_height;
666 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
667 (GSourceFunc) chat_change_size_in_idle_cb,
672 chat_text_view_realize_cb (GtkWidget *widget,
675 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
676 gtk_widget_grab_focus (widget);
680 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
683 GtkTextBuffer *buffer;
687 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
689 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
691 gtk_text_buffer_get_end_iter (buffer, &iter);
692 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
694 gtk_text_buffer_get_end_iter (buffer, &iter);
695 gtk_text_buffer_insert (buffer, &iter, " ", -1);
699 chat_text_populate_popup_cb (GtkTextView *view,
703 GossipChatPriv *priv;
704 GtkTextBuffer *buffer;
705 GtkTextTagTable *table;
708 GtkTextIter iter, start, end;
711 GossipChatSpell *chat_spell;
712 GtkWidget *smiley_menu;
714 priv = GET_PRIV (chat);
716 /* Add the emoticon menu. */
717 item = gtk_separator_menu_item_new ();
718 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
719 gtk_widget_show (item);
721 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
722 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
723 gtk_widget_show (item);
725 smiley_menu = gossip_chat_view_get_smiley_menu (
726 G_CALLBACK (chat_insert_smiley_activate_cb),
729 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
731 /* Add the spell check menu item. */
732 buffer = gtk_text_view_get_buffer (view);
733 table = gtk_text_buffer_get_tag_table (buffer);
735 tag = gtk_text_tag_table_lookup (table, "misspelled");
737 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
739 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
740 GTK_TEXT_WINDOW_WIDGET,
744 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
748 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
749 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
751 str = gtk_text_buffer_get_text (buffer,
752 &start, &end, FALSE);
755 if (G_STR_EMPTY (str)) {
759 chat_spell = chat_spell_new (chat, str, start, end);
761 g_object_set_data_full (G_OBJECT (menu),
762 "chat_spell", chat_spell,
763 (GDestroyNotify) chat_spell_free);
765 item = gtk_separator_menu_item_new ();
766 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
767 gtk_widget_show (item);
769 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
770 g_signal_connect (item,
772 G_CALLBACK (chat_text_check_word_spelling_cb),
774 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
775 gtk_widget_show (item);
779 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
780 GossipChatSpell *chat_spell)
782 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
788 static GossipChatSpell *
789 chat_spell_new (GossipChat *chat,
794 GossipChatSpell *chat_spell;
796 chat_spell = g_new0 (GossipChatSpell, 1);
798 chat_spell->chat = g_object_ref (chat);
799 chat_spell->word = g_strdup (word);
800 chat_spell->start = start;
801 chat_spell->end = end;
807 chat_spell_free (GossipChatSpell *chat_spell)
809 g_object_unref (chat_spell->chat);
810 g_free (chat_spell->word);
815 chat_composing_start (GossipChat *chat)
817 GossipChatPriv *priv;
819 priv = GET_PRIV (chat);
821 if (priv->composing_stop_timeout_id) {
822 /* Just restart the timeout */
823 chat_composing_remove_timeout (chat);
826 gossip_session_send_composing (gossip_app_get_session (),
827 priv->contact, TRUE);
831 priv->composing_stop_timeout_id = g_timeout_add (
832 1000 * COMPOSING_STOP_TIMEOUT,
833 (GSourceFunc) chat_composing_stop_timeout_cb,
838 chat_composing_stop (GossipChat *chat)
840 GossipChatPriv *priv;
842 priv = GET_PRIV (chat);
844 chat_composing_remove_timeout (chat);
846 gossip_session_send_composing (gossip_app_get_session (),
847 priv->contact, FALSE);*/
851 chat_composing_remove_timeout (GossipChat *chat)
853 GossipChatPriv *priv;
855 priv = GET_PRIV (chat);
857 if (priv->composing_stop_timeout_id) {
858 g_source_remove (priv->composing_stop_timeout_id);
859 priv->composing_stop_timeout_id = 0;
864 chat_composing_stop_timeout_cb (GossipChat *chat)
866 GossipChatPriv *priv;
868 priv = GET_PRIV (chat);
870 priv->composing_stop_timeout_id = 0;
872 gossip_session_send_composing (gossip_app_get_session (),
873 priv->contact, FALSE);*/
879 gossip_chat_get_is_command (const gchar *str)
881 g_return_val_if_fail (str != NULL, FALSE);
887 if (g_str_has_prefix (str, "/me")) {
890 else if (g_str_has_prefix (str, "/nick")) {
893 else if (g_str_has_prefix (str, "/topic")) {
901 gossip_chat_correct_word (GossipChat *chat,
904 const gchar *new_word)
906 GtkTextBuffer *buffer;
908 g_return_if_fail (chat != NULL);
909 g_return_if_fail (new_word != NULL);
911 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
913 gtk_text_buffer_delete (buffer, &start, &end);
914 gtk_text_buffer_insert (buffer, &start,
920 gossip_chat_get_name (GossipChat *chat)
922 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
924 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
925 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
932 gossip_chat_get_tooltip (GossipChat *chat)
934 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
936 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
937 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
944 gossip_chat_get_status_pixbuf (GossipChat *chat)
946 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
948 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
949 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
956 gossip_chat_get_contact (GossipChat *chat)
958 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
960 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
961 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
967 gossip_chat_get_own_contact (GossipChat *chat)
969 EmpathyContactManager *manager;
971 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
973 manager = empathy_session_get_contact_manager ();
975 return empathy_contact_manager_get_own (manager, chat->account);
979 gossip_chat_get_widget (GossipChat *chat)
981 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
983 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
984 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
991 gossip_chat_is_group_chat (GossipChat *chat)
993 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
995 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
996 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1003 gossip_chat_is_connected (GossipChat *chat)
1005 GossipChatPriv *priv;
1007 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1009 priv = GET_PRIV (chat);
1011 return (priv->tp_chat != NULL);
1015 gossip_chat_get_show_contacts (GossipChat *chat)
1017 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1019 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1020 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1027 gossip_chat_set_show_contacts (GossipChat *chat,
1030 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1032 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1033 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1038 gossip_chat_save_geometry (GossipChat *chat,
1044 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1048 gossip_chat_load_geometry (GossipChat *chat,
1054 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1058 gossip_chat_set_tp_chat (GossipChat *chat,
1059 EmpathyTpChat *tp_chat)
1061 GossipChatPriv *priv;
1064 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1065 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1067 priv = GET_PRIV (chat);
1069 if (tp_chat == priv->tp_chat) {
1073 if (priv->tp_chat) {
1074 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1075 chat_message_received_cb,
1077 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1080 g_object_unref (priv->tp_chat);
1084 priv->tp_chat = g_object_ref (tp_chat);
1085 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1087 g_signal_connect (tp_chat, "message-received",
1088 G_CALLBACK (chat_message_received_cb),
1090 g_signal_connect (tp_chat, "destroy",
1091 G_CALLBACK (chat_destroy_cb),
1094 empathy_tp_chat_request_pending (tp_chat);
1096 if (!priv->sensitive) {
1097 widget = gossip_chat_get_widget (chat);
1098 gtk_widget_set_sensitive (widget, TRUE);
1099 gossip_chat_view_append_event (chat->view, _("Connected"));
1100 priv->sensitive = TRUE;
1105 gossip_chat_get_id (GossipChat *chat)
1107 GossipChatPriv *priv;
1109 priv = GET_PRIV (chat);
1115 gossip_chat_clear (GossipChat *chat)
1117 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1119 gossip_chat_view_clear (chat->view);
1123 gossip_chat_set_window (GossipChat *chat,
1124 GossipChatWindow *window)
1126 GossipChatPriv *priv;
1128 priv = GET_PRIV (chat);
1129 priv->window = window;
1133 gossip_chat_get_window (GossipChat *chat)
1135 GossipChatPriv *priv;
1137 priv = GET_PRIV (chat);
1139 return priv->window;
1143 gossip_chat_scroll_down (GossipChat *chat)
1145 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1147 gossip_chat_view_scroll_down (chat->view);
1151 gossip_chat_cut (GossipChat *chat)
1153 GtkTextBuffer *buffer;
1155 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1157 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1158 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1159 GtkClipboard *clipboard;
1161 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1163 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1168 gossip_chat_copy (GossipChat *chat)
1170 GtkTextBuffer *buffer;
1172 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1174 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1175 gossip_chat_view_copy_clipboard (chat->view);
1179 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1180 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1181 GtkClipboard *clipboard;
1183 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1185 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1190 gossip_chat_paste (GossipChat *chat)
1192 GtkTextBuffer *buffer;
1193 GtkClipboard *clipboard;
1195 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1197 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1198 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1200 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1204 gossip_chat_present (GossipChat *chat)
1206 GossipChatPriv *priv;
1208 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1210 priv = GET_PRIV (chat);
1212 if (priv->window == NULL) {
1213 GossipChatWindow *window;
1215 window = gossip_chat_window_get_default ();
1217 window = gossip_chat_window_new ();
1220 gossip_chat_window_add_chat (window, chat);
1223 gossip_chat_window_switch_to_chat (priv->window, chat);
1224 gossip_window_present (
1225 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1228 gtk_widget_grab_focus (chat->input_text_view);
1232 gossip_chat_should_play_sound (GossipChat *chat)
1234 GossipChatWindow *window;
1235 gboolean play = TRUE;
1237 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1239 window = gossip_chat_get_window (GOSSIP_CHAT (chat));
1244 play = !gossip_chat_window_has_focus (window);
1250 gossip_chat_should_highlight_nick (GossipMessage *message)
1252 GossipContact *my_contact;
1253 const gchar *msg, *to;
1254 gchar *cf_msg, *cf_to;
1258 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1260 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1264 msg = gossip_message_get_body (message);
1269 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1270 to = gossip_contact_get_name (my_contact);
1275 cf_msg = g_utf8_casefold (msg, -1);
1276 cf_to = g_utf8_casefold (to, -1);
1278 ch = strstr (cf_msg, cf_to);
1284 /* Not first in the message */
1285 if ((*(ch - 1) != ' ') &&
1286 (*(ch - 1) != ',') &&
1287 (*(ch - 1) != '.')) {
1292 ch = ch + strlen (cf_to);
1293 if (ch >= cf_msg + strlen (cf_msg)) {