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);
278 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
282 chat_destroy_cb (EmpathyTpChat *tp_chat,
285 GossipChatPriv *priv;
288 priv = GET_PRIV (chat);
291 g_object_unref (priv->tp_chat);
292 priv->tp_chat = NULL;
295 gossip_chat_view_append_event (chat->view, _("Disconnected"));
297 widget = gossip_chat_get_widget (chat);
298 gtk_widget_set_sensitive (widget, FALSE);
299 priv->sensitive = FALSE;
303 chat_send (GossipChat *chat,
306 GossipChatPriv *priv;
307 //GossipLogManager *log_manager;
308 GossipMessage *message;
309 GossipContact *own_contact;
311 priv = GET_PRIV (chat);
313 if (msg == NULL || msg[0] == '\0') {
317 if (g_str_has_prefix (msg, "/clear")) {
318 gossip_chat_view_clear (chat->view);
322 /* FIXME: gossip_app_set_not_away ();*/
324 own_contact = gossip_chat_get_own_contact (chat);
325 message = gossip_message_new (msg);
326 gossip_message_set_sender (message, own_contact);
328 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
329 //gossip_log_message_for_contact (log_manager, message, FALSE);
331 empathy_tp_chat_send (priv->tp_chat, message);
333 g_object_unref (message);
337 chat_input_text_view_send (GossipChat *chat)
339 GossipChatPriv *priv;
340 GtkTextBuffer *buffer;
341 GtkTextIter start, end;
344 priv = GET_PRIV (chat);
346 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
348 gtk_text_buffer_get_bounds (buffer, &start, &end);
349 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
351 /* clear the input field */
352 gtk_text_buffer_set_text (buffer, "", -1);
354 chat_send (chat, msg);
358 chat->is_first_char = TRUE;
362 chat_message_received_cb (EmpathyTpChat *tp_chat,
363 GossipMessage *message,
366 GossipChatPriv *priv;
367 //GossipLogManager *log_manager;
368 GossipContact *sender;
370 priv = GET_PRIV (chat);
372 sender = gossip_message_get_sender (message);
373 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
374 gossip_contact_get_name (sender));
377 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
378 gossip_log_message_for_contact (log_manager, message, TRUE);
380 gossip_chat_view_append_message (chat->view, message);
382 if (gossip_chat_should_play_sound (chat)) {
383 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
386 g_signal_emit_by_name (chat, "new-message", message);
390 chat_input_key_press_event_cb (GtkWidget *widget,
394 GossipChatPriv *priv;
397 GtkWidget *text_view_sw;
399 priv = GET_PRIV (chat);
401 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
405 /* Catch enter but not ctrl/shift-enter */
406 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
409 /* This is to make sure that kinput2 gets the enter. And if
410 * it's handled there we shouldn't send on it. This is because
411 * kinput2 uses Enter to commit letters. See:
412 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
415 view = GTK_TEXT_VIEW (chat->input_text_view);
416 if (gtk_im_context_filter_keypress (view->im_context, event)) {
417 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
421 chat_input_text_view_send (chat);
425 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
426 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
427 /* Newline for shift-enter. */
430 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
431 event->keyval == GDK_Page_Up) {
432 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
433 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
437 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
438 event->keyval == GDK_Page_Down) {
439 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
440 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
441 gtk_adjustment_set_value (adj, val);
450 chat_text_view_focus_in_event_cb (GtkWidget *widget,
454 gtk_widget_grab_focus (chat->input_text_view);
460 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
463 GossipChatPriv *priv;
464 GtkTextIter start, end;
466 gboolean spell_checker = FALSE;
468 priv = GET_PRIV (chat);
470 if (gtk_text_buffer_get_char_count (buffer) == 0) {
471 chat_composing_stop (chat);
473 chat_composing_start (chat);
476 gossip_conf_get_bool (gossip_conf_get (),
477 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
480 if (chat->is_first_char) {
484 GtkAllocation *allocation;
486 /* Save the window's size */
487 dialog = gossip_chat_window_get_dialog (priv->window);
488 gtk_window_get_size (GTK_WINDOW (dialog),
489 NULL, &window_height);
491 gtk_widget_size_request (chat->input_text_view, &req);
493 allocation = >K_WIDGET (chat->view)->allocation;
495 priv->default_window_height = window_height;
496 priv->last_input_height = req.height;
497 priv->padding_height = window_height - req.height - allocation->height;
499 chat->is_first_char = FALSE;
502 gtk_text_buffer_get_start_iter (buffer, &start);
504 if (!spell_checker) {
505 gtk_text_buffer_get_end_iter (buffer, &end);
506 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
510 if (!gossip_spell_supported ()) {
514 /* NOTE: this is really inefficient, we shouldn't have to
515 reiterate the whole buffer each time and check each work
518 gboolean correct = FALSE;
521 if (gtk_text_iter_is_start (&start)) {
524 if (!gtk_text_iter_forward_word_end (&end)) {
525 /* no whole word yet */
529 if (!gtk_text_iter_forward_word_end (&end)) {
530 /* must be the end of the buffer */
535 gtk_text_iter_backward_word_start (&start);
538 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
540 /* spell check string */
541 if (!gossip_chat_get_is_command (str)) {
542 correct = gossip_spell_check (str);
548 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
550 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
555 /* set start iter to the end iters position */
567 chat_change_size_in_idle_cb (ChangeSizeData *data)
569 gtk_window_resize (GTK_WINDOW (data->window),
570 data->width, data->height);
576 chat_text_view_scroll_hide_cb (GtkWidget *widget,
579 GossipChatPriv *priv;
582 priv = GET_PRIV (chat);
584 priv->vscroll_visible = FALSE;
585 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
587 sw = gtk_widget_get_parent (chat->input_text_view);
588 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
591 g_object_set (sw, "height-request", -1, NULL);
595 chat_text_view_size_allocate_cb (GtkWidget *widget,
596 GtkAllocation *allocation,
599 GossipChatPriv *priv;
602 ChangeSizeData *data;
605 GtkAllocation *view_allocation;
610 priv = GET_PRIV (chat);
612 if (priv->default_window_height <= 0) {
616 sw = gtk_widget_get_parent (widget);
617 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
620 priv->vscroll_visible = TRUE;
621 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
622 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
624 GTK_POLICY_AUTOMATIC);
625 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
626 g_signal_connect (vscroll, "hide",
627 G_CALLBACK (chat_text_view_scroll_hide_cb),
631 if (priv->last_input_height <= allocation->height) {
632 priv->last_input_height = allocation->height;
636 diff = priv->last_input_height - allocation->height;
637 priv->last_input_height = allocation->height;
639 view_allocation = >K_WIDGET (chat->view)->allocation;
641 dialog = gossip_chat_window_get_dialog (priv->window);
642 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
644 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
646 if (new_height <= priv->default_window_height) {
647 window_height = priv->default_window_height;
649 window_height = new_height;
652 if (current_height <= window_height) {
656 /* Restore the window's size */
657 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
659 data = g_new0 (ChangeSizeData, 1);
660 data->window = dialog;
662 data->height = window_height;
664 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
665 (GSourceFunc) chat_change_size_in_idle_cb,
670 chat_text_view_realize_cb (GtkWidget *widget,
673 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
674 gtk_widget_grab_focus (widget);
678 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
681 GtkTextBuffer *buffer;
685 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
687 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
689 gtk_text_buffer_get_end_iter (buffer, &iter);
690 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
692 gtk_text_buffer_get_end_iter (buffer, &iter);
693 gtk_text_buffer_insert (buffer, &iter, " ", -1);
697 chat_text_populate_popup_cb (GtkTextView *view,
701 GossipChatPriv *priv;
702 GtkTextBuffer *buffer;
703 GtkTextTagTable *table;
706 GtkTextIter iter, start, end;
709 GossipChatSpell *chat_spell;
710 GtkWidget *smiley_menu;
712 priv = GET_PRIV (chat);
714 /* Add the emoticon menu. */
715 item = gtk_separator_menu_item_new ();
716 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
717 gtk_widget_show (item);
719 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
720 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
721 gtk_widget_show (item);
723 smiley_menu = gossip_chat_view_get_smiley_menu (
724 G_CALLBACK (chat_insert_smiley_activate_cb),
727 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
729 /* Add the spell check menu item. */
730 buffer = gtk_text_view_get_buffer (view);
731 table = gtk_text_buffer_get_tag_table (buffer);
733 tag = gtk_text_tag_table_lookup (table, "misspelled");
735 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
737 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
738 GTK_TEXT_WINDOW_WIDGET,
742 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
746 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
747 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
749 str = gtk_text_buffer_get_text (buffer,
750 &start, &end, FALSE);
753 if (G_STR_EMPTY (str)) {
757 chat_spell = chat_spell_new (chat, str, start, end);
759 g_object_set_data_full (G_OBJECT (menu),
760 "chat_spell", chat_spell,
761 (GDestroyNotify) chat_spell_free);
763 item = gtk_separator_menu_item_new ();
764 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
765 gtk_widget_show (item);
767 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
768 g_signal_connect (item,
770 G_CALLBACK (chat_text_check_word_spelling_cb),
772 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
773 gtk_widget_show (item);
777 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
778 GossipChatSpell *chat_spell)
780 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
786 static GossipChatSpell *
787 chat_spell_new (GossipChat *chat,
792 GossipChatSpell *chat_spell;
794 chat_spell = g_new0 (GossipChatSpell, 1);
796 chat_spell->chat = g_object_ref (chat);
797 chat_spell->word = g_strdup (word);
798 chat_spell->start = start;
799 chat_spell->end = end;
805 chat_spell_free (GossipChatSpell *chat_spell)
807 g_object_unref (chat_spell->chat);
808 g_free (chat_spell->word);
813 chat_composing_start (GossipChat *chat)
815 GossipChatPriv *priv;
817 priv = GET_PRIV (chat);
819 if (priv->composing_stop_timeout_id) {
820 /* Just restart the timeout */
821 chat_composing_remove_timeout (chat);
824 gossip_session_send_composing (gossip_app_get_session (),
825 priv->contact, TRUE);
829 priv->composing_stop_timeout_id = g_timeout_add (
830 1000 * COMPOSING_STOP_TIMEOUT,
831 (GSourceFunc) chat_composing_stop_timeout_cb,
836 chat_composing_stop (GossipChat *chat)
838 GossipChatPriv *priv;
840 priv = GET_PRIV (chat);
842 chat_composing_remove_timeout (chat);
844 gossip_session_send_composing (gossip_app_get_session (),
845 priv->contact, FALSE);*/
849 chat_composing_remove_timeout (GossipChat *chat)
851 GossipChatPriv *priv;
853 priv = GET_PRIV (chat);
855 if (priv->composing_stop_timeout_id) {
856 g_source_remove (priv->composing_stop_timeout_id);
857 priv->composing_stop_timeout_id = 0;
862 chat_composing_stop_timeout_cb (GossipChat *chat)
864 GossipChatPriv *priv;
866 priv = GET_PRIV (chat);
868 priv->composing_stop_timeout_id = 0;
870 gossip_session_send_composing (gossip_app_get_session (),
871 priv->contact, FALSE);*/
877 gossip_chat_get_is_command (const gchar *str)
879 g_return_val_if_fail (str != NULL, FALSE);
885 if (g_str_has_prefix (str, "/me")) {
888 else if (g_str_has_prefix (str, "/nick")) {
891 else if (g_str_has_prefix (str, "/topic")) {
899 gossip_chat_correct_word (GossipChat *chat,
902 const gchar *new_word)
904 GtkTextBuffer *buffer;
906 g_return_if_fail (chat != NULL);
907 g_return_if_fail (new_word != NULL);
909 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
911 gtk_text_buffer_delete (buffer, &start, &end);
912 gtk_text_buffer_insert (buffer, &start,
918 gossip_chat_get_name (GossipChat *chat)
920 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
922 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
923 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
930 gossip_chat_get_tooltip (GossipChat *chat)
932 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
934 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
935 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
942 gossip_chat_get_status_pixbuf (GossipChat *chat)
944 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
946 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
947 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
954 gossip_chat_get_contact (GossipChat *chat)
956 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
958 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
959 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
965 gossip_chat_get_own_contact (GossipChat *chat)
967 EmpathyContactManager *manager;
969 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
971 manager = empathy_session_get_contact_manager ();
973 return empathy_contact_manager_get_own (manager, chat->account);
977 gossip_chat_get_widget (GossipChat *chat)
979 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
981 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
982 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
989 gossip_chat_is_group_chat (GossipChat *chat)
991 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
993 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
994 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1001 gossip_chat_is_connected (GossipChat *chat)
1003 GossipChatPriv *priv;
1005 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1007 priv = GET_PRIV (chat);
1009 return (priv->tp_chat != NULL);
1013 gossip_chat_get_show_contacts (GossipChat *chat)
1015 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1017 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1018 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1025 gossip_chat_set_show_contacts (GossipChat *chat,
1028 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1030 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1031 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1036 gossip_chat_save_geometry (GossipChat *chat,
1042 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1046 gossip_chat_load_geometry (GossipChat *chat,
1052 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1056 gossip_chat_set_tp_chat (GossipChat *chat,
1057 EmpathyTpChat *tp_chat)
1059 GossipChatPriv *priv;
1062 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1063 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1065 priv = GET_PRIV (chat);
1067 if (tp_chat == priv->tp_chat) {
1071 if (priv->tp_chat) {
1072 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1073 chat_message_received_cb,
1075 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1078 g_object_unref (priv->tp_chat);
1082 priv->tp_chat = g_object_ref (tp_chat);
1083 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1085 g_signal_connect (tp_chat, "message-received",
1086 G_CALLBACK (chat_message_received_cb),
1088 g_signal_connect (tp_chat, "destroy",
1089 G_CALLBACK (chat_destroy_cb),
1092 empathy_tp_chat_request_pending (tp_chat);
1094 if (!priv->sensitive) {
1095 widget = gossip_chat_get_widget (chat);
1096 gtk_widget_set_sensitive (widget, TRUE);
1097 gossip_chat_view_append_event (chat->view, _("Connected"));
1098 priv->sensitive = TRUE;
1103 gossip_chat_get_id (GossipChat *chat)
1105 GossipChatPriv *priv;
1107 priv = GET_PRIV (chat);
1113 gossip_chat_clear (GossipChat *chat)
1115 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1117 gossip_chat_view_clear (chat->view);
1121 gossip_chat_set_window (GossipChat *chat,
1122 GossipChatWindow *window)
1124 GossipChatPriv *priv;
1126 priv = GET_PRIV (chat);
1127 priv->window = window;
1131 gossip_chat_get_window (GossipChat *chat)
1133 GossipChatPriv *priv;
1135 priv = GET_PRIV (chat);
1137 return priv->window;
1141 gossip_chat_scroll_down (GossipChat *chat)
1143 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1145 gossip_chat_view_scroll_down (chat->view);
1149 gossip_chat_cut (GossipChat *chat)
1151 GtkTextBuffer *buffer;
1153 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1155 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1156 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1157 GtkClipboard *clipboard;
1159 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1161 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1166 gossip_chat_copy (GossipChat *chat)
1168 GtkTextBuffer *buffer;
1170 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1172 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1173 gossip_chat_view_copy_clipboard (chat->view);
1177 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1178 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1179 GtkClipboard *clipboard;
1181 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1183 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1188 gossip_chat_paste (GossipChat *chat)
1190 GtkTextBuffer *buffer;
1191 GtkClipboard *clipboard;
1193 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1195 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1196 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1198 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1202 gossip_chat_present (GossipChat *chat)
1204 GossipChatPriv *priv;
1206 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1208 priv = GET_PRIV (chat);
1210 if (priv->window == NULL) {
1211 GossipChatWindow *window;
1213 window = gossip_chat_window_get_default ();
1215 window = gossip_chat_window_new ();
1218 gossip_chat_window_add_chat (window, chat);
1221 gossip_chat_window_switch_to_chat (priv->window, chat);
1222 gossip_window_present (
1223 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1226 gtk_widget_grab_focus (chat->input_text_view);
1230 gossip_chat_should_play_sound (GossipChat *chat)
1232 GossipChatWindow *window;
1233 gboolean play = TRUE;
1235 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1237 window = gossip_chat_get_window (GOSSIP_CHAT (chat));
1242 play = !gossip_chat_window_has_focus (window);
1248 gossip_chat_should_highlight_nick (GossipMessage *message)
1250 GossipContact *my_contact;
1251 const gchar *msg, *to;
1252 gchar *cf_msg, *cf_to;
1256 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1258 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1262 msg = gossip_message_get_body (message);
1267 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1268 to = gossip_contact_get_name (my_contact);
1273 cf_msg = g_utf8_casefold (msg, -1);
1274 cf_to = g_utf8_casefold (to, -1);
1276 ch = strstr (cf_msg, cf_to);
1282 /* Not first in the message */
1283 if ((*(ch - 1) != ' ') &&
1284 (*(ch - 1) != ',') &&
1285 (*(ch - 1) != '.')) {
1290 ch = ch + strlen (cf_to);
1291 if (ch >= cf_msg + strlen (cf_msg)) {