1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
31 #include <gdk/gdkkeysyms.h>
32 #include <glib/gi18n.h>
34 #include <glade/glade.h>
36 #include <libempathy/empathy-session.h>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/gossip-debug.h>
39 #include <libempathy/gossip-utils.h>
40 #include <libempathy/gossip-conf.h>
42 #include "gossip-chat.h"
43 #include "gossip-chat-window.h"
44 //#include "gossip-geometry.h"
45 #include "gossip-preferences.h"
46 #include "gossip-spell.h"
47 //#include "gossip-spell-dialog.h"
48 #include "gossip-ui-utils.h"
50 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
52 #define DEBUG_DOMAIN "Chat"
54 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
55 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
57 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
59 #define MAX_INPUT_HEIGHT 150
61 #define COMPOSING_STOP_TIMEOUT 5
63 struct _GossipChatPriv {
64 EmpathyTpChat *tp_chat;
65 GossipChatWindow *window;
67 GtkTooltips *tooltips;
68 guint composing_stop_timeout_id;
70 /* Used to automatically shrink a window that has temporarily
71 * grown due to long input.
74 gint default_window_height;
75 gint last_input_height;
76 gboolean vscroll_visible;
87 static void gossip_chat_class_init (GossipChatClass *klass);
88 static void gossip_chat_init (GossipChat *chat);
89 static void chat_finalize (GObject *object);
90 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
92 static void chat_send (GossipChat *chat,
94 static void chat_input_text_view_send (GossipChat *chat);
95 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
96 GossipMessage *message,
98 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
101 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
103 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
106 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
108 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
109 GtkAllocation *allocation,
111 static void chat_text_view_realize_cb (GtkWidget *widget,
113 static void chat_text_populate_popup_cb (GtkTextView *view,
116 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
117 GossipChatSpell *chat_spell);
118 static GossipChatSpell *chat_spell_new (GossipChat *chat,
122 static void chat_spell_free (GossipChatSpell *chat_spell);
123 static void chat_composing_start (GossipChat *chat);
124 static void chat_composing_stop (GossipChat *chat);
125 static void chat_composing_remove_timeout (GossipChat *chat);
126 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
136 static guint chat_signals[LAST_SIGNAL] = { 0 };
138 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
141 gossip_chat_class_init (GossipChatClass *klass)
143 GObjectClass *object_class;
145 object_class = G_OBJECT_CLASS (klass);
147 object_class->finalize = chat_finalize;
149 chat_signals[COMPOSING] =
150 g_signal_new ("composing",
151 G_OBJECT_CLASS_TYPE (object_class),
155 g_cclosure_marshal_VOID__BOOLEAN,
159 chat_signals[NEW_MESSAGE] =
160 g_signal_new ("new-message",
161 G_OBJECT_CLASS_TYPE (object_class),
165 g_cclosure_marshal_VOID__OBJECT,
167 1, GOSSIP_TYPE_MESSAGE);
169 chat_signals[NAME_CHANGED] =
170 g_signal_new ("name-changed",
171 G_OBJECT_CLASS_TYPE (object_class),
175 g_cclosure_marshal_VOID__POINTER,
179 chat_signals[STATUS_CHANGED] =
180 g_signal_new ("status-changed",
181 G_OBJECT_CLASS_TYPE (object_class),
185 g_cclosure_marshal_VOID__VOID,
189 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
193 gossip_chat_init (GossipChat *chat)
195 GossipChatPriv *priv;
196 GtkTextBuffer *buffer;
198 chat->view = gossip_chat_view_new ();
199 chat->input_text_view = gtk_text_view_new ();
201 chat->is_first_char = TRUE;
203 g_object_set (chat->input_text_view,
204 "pixels-above-lines", 2,
205 "pixels-below-lines", 2,
206 "pixels-inside-wrap", 1,
209 "wrap-mode", GTK_WRAP_WORD_CHAR,
212 priv = GET_PRIV (chat);
214 priv->tooltips = gtk_tooltips_new ();
216 priv->default_window_height = -1;
217 priv->vscroll_visible = FALSE;
218 priv->sensitive = TRUE;
220 g_signal_connect (chat->input_text_view,
222 G_CALLBACK (chat_input_key_press_event_cb),
225 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
226 g_signal_connect (buffer,
228 G_CALLBACK (chat_input_text_buffer_changed_cb),
230 g_signal_connect (GOSSIP_CHAT (chat)->view,
232 G_CALLBACK (chat_text_view_focus_in_event_cb),
235 g_signal_connect (chat->input_text_view,
237 G_CALLBACK (chat_text_view_size_allocate_cb),
240 g_signal_connect (chat->input_text_view,
242 G_CALLBACK (chat_text_view_realize_cb),
245 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
247 G_CALLBACK (chat_text_populate_popup_cb),
250 /* create misspelt words identification tag */
251 gtk_text_buffer_create_tag (buffer,
253 "underline", PANGO_UNDERLINE_ERROR,
258 chat_finalize (GObject *object)
261 GossipChatPriv *priv;
263 chat = GOSSIP_CHAT (object);
264 priv = GET_PRIV (chat);
266 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
268 chat_composing_remove_timeout (chat);
269 g_object_unref (GOSSIP_CHAT (object)->account);
272 g_object_unref (priv->tp_chat);
275 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
279 chat_destroy_cb (EmpathyTpChat *tp_chat,
282 GossipChatPriv *priv;
285 priv = GET_PRIV (chat);
288 g_object_unref (priv->tp_chat);
289 priv->tp_chat = NULL;
292 gossip_chat_view_append_event (chat->view, _("Disconnected"));
294 widget = gossip_chat_get_widget (chat);
295 gtk_widget_set_sensitive (widget, FALSE);
296 priv->sensitive = FALSE;
300 chat_send (GossipChat *chat,
303 GossipChatPriv *priv;
304 //GossipLogManager *log_manager;
305 GossipMessage *message;
306 GossipContact *own_contact;
308 priv = GET_PRIV (chat);
310 if (msg == NULL || msg[0] == '\0') {
314 if (g_str_has_prefix (msg, "/clear")) {
315 gossip_chat_view_clear (chat->view);
319 /* FIXME: gossip_app_set_not_away ();*/
321 own_contact = gossip_chat_get_own_contact (chat);
322 message = gossip_message_new (msg);
323 gossip_message_set_sender (message, own_contact);
325 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
326 //gossip_log_message_for_contact (log_manager, message, FALSE);
328 empathy_tp_chat_send (priv->tp_chat, message);
330 g_object_unref (message);
334 chat_input_text_view_send (GossipChat *chat)
336 GossipChatPriv *priv;
337 GtkTextBuffer *buffer;
338 GtkTextIter start, end;
341 priv = GET_PRIV (chat);
343 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
345 gtk_text_buffer_get_bounds (buffer, &start, &end);
346 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
348 /* clear the input field */
349 gtk_text_buffer_set_text (buffer, "", -1);
351 chat_send (chat, msg);
355 chat->is_first_char = TRUE;
359 chat_message_received_cb (EmpathyTpChat *tp_chat,
360 GossipMessage *message,
363 GossipChatPriv *priv;
364 //GossipLogManager *log_manager;
365 GossipContact *sender;
367 priv = GET_PRIV (chat);
369 sender = gossip_message_get_sender (message);
370 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
371 gossip_contact_get_name (sender));
374 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
375 gossip_log_message_for_contact (log_manager, message, TRUE);
377 gossip_chat_view_append_message (chat->view, message);
379 if (gossip_chat_should_play_sound (chat)) {
380 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
383 g_signal_emit_by_name (chat, "new-message", message);
387 chat_input_key_press_event_cb (GtkWidget *widget,
391 GossipChatPriv *priv;
394 GtkWidget *text_view_sw;
396 priv = GET_PRIV (chat);
398 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
402 /* Catch enter but not ctrl/shift-enter */
403 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
406 /* This is to make sure that kinput2 gets the enter. And if
407 * it's handled there we shouldn't send on it. This is because
408 * kinput2 uses Enter to commit letters. See:
409 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
412 view = GTK_TEXT_VIEW (chat->input_text_view);
413 if (gtk_im_context_filter_keypress (view->im_context, event)) {
414 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
418 chat_input_text_view_send (chat);
422 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
423 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
424 /* Newline for shift-enter. */
427 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
428 event->keyval == GDK_Page_Up) {
429 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
430 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
434 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
435 event->keyval == GDK_Page_Down) {
436 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
437 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
438 gtk_adjustment_set_value (adj, val);
447 chat_text_view_focus_in_event_cb (GtkWidget *widget,
451 gtk_widget_grab_focus (chat->input_text_view);
457 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
460 GossipChatPriv *priv;
461 GtkTextIter start, end;
463 gboolean spell_checker = FALSE;
465 priv = GET_PRIV (chat);
467 if (gtk_text_buffer_get_char_count (buffer) == 0) {
468 chat_composing_stop (chat);
470 chat_composing_start (chat);
473 gossip_conf_get_bool (gossip_conf_get (),
474 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
477 if (chat->is_first_char) {
481 GtkAllocation *allocation;
483 /* Save the window's size */
484 dialog = gossip_chat_window_get_dialog (priv->window);
485 gtk_window_get_size (GTK_WINDOW (dialog),
486 NULL, &window_height);
488 gtk_widget_size_request (chat->input_text_view, &req);
490 allocation = >K_WIDGET (chat->view)->allocation;
492 priv->default_window_height = window_height;
493 priv->last_input_height = req.height;
494 priv->padding_height = window_height - req.height - allocation->height;
496 chat->is_first_char = FALSE;
499 gtk_text_buffer_get_start_iter (buffer, &start);
501 if (!spell_checker) {
502 gtk_text_buffer_get_end_iter (buffer, &end);
503 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
507 if (!gossip_spell_supported ()) {
511 /* NOTE: this is really inefficient, we shouldn't have to
512 reiterate the whole buffer each time and check each work
515 gboolean correct = FALSE;
518 if (gtk_text_iter_is_start (&start)) {
521 if (!gtk_text_iter_forward_word_end (&end)) {
522 /* no whole word yet */
526 if (!gtk_text_iter_forward_word_end (&end)) {
527 /* must be the end of the buffer */
532 gtk_text_iter_backward_word_start (&start);
535 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
537 /* spell check string */
538 if (!gossip_chat_get_is_command (str)) {
539 correct = gossip_spell_check (str);
545 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
547 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
552 /* set start iter to the end iters position */
564 chat_change_size_in_idle_cb (ChangeSizeData *data)
566 gtk_window_resize (GTK_WINDOW (data->window),
567 data->width, data->height);
573 chat_text_view_scroll_hide_cb (GtkWidget *widget,
576 GossipChatPriv *priv;
579 priv = GET_PRIV (chat);
581 priv->vscroll_visible = FALSE;
582 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
584 sw = gtk_widget_get_parent (chat->input_text_view);
585 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
588 g_object_set (sw, "height-request", -1, NULL);
592 chat_text_view_size_allocate_cb (GtkWidget *widget,
593 GtkAllocation *allocation,
596 GossipChatPriv *priv;
599 ChangeSizeData *data;
602 GtkAllocation *view_allocation;
607 priv = GET_PRIV (chat);
609 if (priv->default_window_height <= 0) {
613 sw = gtk_widget_get_parent (widget);
614 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
617 priv->vscroll_visible = TRUE;
618 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
619 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
621 GTK_POLICY_AUTOMATIC);
622 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
623 g_signal_connect (vscroll, "hide",
624 G_CALLBACK (chat_text_view_scroll_hide_cb),
628 if (priv->last_input_height <= allocation->height) {
629 priv->last_input_height = allocation->height;
633 diff = priv->last_input_height - allocation->height;
634 priv->last_input_height = allocation->height;
636 view_allocation = >K_WIDGET (chat->view)->allocation;
638 dialog = gossip_chat_window_get_dialog (priv->window);
639 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
641 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
643 if (new_height <= priv->default_window_height) {
644 window_height = priv->default_window_height;
646 window_height = new_height;
649 if (current_height <= window_height) {
653 /* Restore the window's size */
654 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
656 data = g_new0 (ChangeSizeData, 1);
657 data->window = dialog;
659 data->height = window_height;
661 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
662 (GSourceFunc) chat_change_size_in_idle_cb,
667 chat_text_view_realize_cb (GtkWidget *widget,
670 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
671 gtk_widget_grab_focus (widget);
675 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
678 GtkTextBuffer *buffer;
682 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
684 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
686 gtk_text_buffer_get_end_iter (buffer, &iter);
687 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
689 gtk_text_buffer_get_end_iter (buffer, &iter);
690 gtk_text_buffer_insert (buffer, &iter, " ", -1);
694 chat_text_populate_popup_cb (GtkTextView *view,
698 GossipChatPriv *priv;
699 GtkTextBuffer *buffer;
700 GtkTextTagTable *table;
703 GtkTextIter iter, start, end;
706 GossipChatSpell *chat_spell;
707 GtkWidget *smiley_menu;
709 priv = GET_PRIV (chat);
711 /* Add the emoticon menu. */
712 item = gtk_separator_menu_item_new ();
713 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
714 gtk_widget_show (item);
716 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
717 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
718 gtk_widget_show (item);
720 smiley_menu = gossip_chat_view_get_smiley_menu (
721 G_CALLBACK (chat_insert_smiley_activate_cb),
724 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
726 /* Add the spell check menu item. */
727 buffer = gtk_text_view_get_buffer (view);
728 table = gtk_text_buffer_get_tag_table (buffer);
730 tag = gtk_text_tag_table_lookup (table, "misspelled");
732 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
734 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
735 GTK_TEXT_WINDOW_WIDGET,
739 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
743 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
744 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
746 str = gtk_text_buffer_get_text (buffer,
747 &start, &end, FALSE);
750 if (G_STR_EMPTY (str)) {
754 chat_spell = chat_spell_new (chat, str, start, end);
756 g_object_set_data_full (G_OBJECT (menu),
757 "chat_spell", chat_spell,
758 (GDestroyNotify) chat_spell_free);
760 item = gtk_separator_menu_item_new ();
761 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
762 gtk_widget_show (item);
764 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
765 g_signal_connect (item,
767 G_CALLBACK (chat_text_check_word_spelling_cb),
769 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
770 gtk_widget_show (item);
774 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
775 GossipChatSpell *chat_spell)
777 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
783 static GossipChatSpell *
784 chat_spell_new (GossipChat *chat,
789 GossipChatSpell *chat_spell;
791 chat_spell = g_new0 (GossipChatSpell, 1);
793 chat_spell->chat = g_object_ref (chat);
794 chat_spell->word = g_strdup (word);
795 chat_spell->start = start;
796 chat_spell->end = end;
802 chat_spell_free (GossipChatSpell *chat_spell)
804 g_object_unref (chat_spell->chat);
805 g_free (chat_spell->word);
810 chat_composing_start (GossipChat *chat)
812 GossipChatPriv *priv;
814 priv = GET_PRIV (chat);
816 if (priv->composing_stop_timeout_id) {
817 /* Just restart the timeout */
818 chat_composing_remove_timeout (chat);
821 gossip_session_send_composing (gossip_app_get_session (),
822 priv->contact, TRUE);
826 priv->composing_stop_timeout_id = g_timeout_add (
827 1000 * COMPOSING_STOP_TIMEOUT,
828 (GSourceFunc) chat_composing_stop_timeout_cb,
833 chat_composing_stop (GossipChat *chat)
835 GossipChatPriv *priv;
837 priv = GET_PRIV (chat);
839 chat_composing_remove_timeout (chat);
841 gossip_session_send_composing (gossip_app_get_session (),
842 priv->contact, FALSE);*/
846 chat_composing_remove_timeout (GossipChat *chat)
848 GossipChatPriv *priv;
850 priv = GET_PRIV (chat);
852 if (priv->composing_stop_timeout_id) {
853 g_source_remove (priv->composing_stop_timeout_id);
854 priv->composing_stop_timeout_id = 0;
859 chat_composing_stop_timeout_cb (GossipChat *chat)
861 GossipChatPriv *priv;
863 priv = GET_PRIV (chat);
865 priv->composing_stop_timeout_id = 0;
867 gossip_session_send_composing (gossip_app_get_session (),
868 priv->contact, FALSE);*/
874 gossip_chat_get_is_command (const gchar *str)
876 g_return_val_if_fail (str != NULL, FALSE);
882 if (g_str_has_prefix (str, "/me")) {
885 else if (g_str_has_prefix (str, "/nick")) {
888 else if (g_str_has_prefix (str, "/topic")) {
896 gossip_chat_correct_word (GossipChat *chat,
899 const gchar *new_word)
901 GtkTextBuffer *buffer;
903 g_return_if_fail (chat != NULL);
904 g_return_if_fail (new_word != NULL);
906 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
908 gtk_text_buffer_delete (buffer, &start, &end);
909 gtk_text_buffer_insert (buffer, &start,
915 gossip_chat_get_name (GossipChat *chat)
917 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
919 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
920 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
927 gossip_chat_get_tooltip (GossipChat *chat)
929 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
931 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
932 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
939 gossip_chat_get_status_pixbuf (GossipChat *chat)
941 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
943 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
944 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
951 gossip_chat_get_contact (GossipChat *chat)
953 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
955 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
956 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
962 gossip_chat_get_own_contact (GossipChat *chat)
964 EmpathyContactManager *manager;
966 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
968 manager = empathy_session_get_contact_manager ();
970 return empathy_contact_manager_get_own (manager, chat->account);
974 gossip_chat_get_widget (GossipChat *chat)
976 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
978 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
979 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
986 gossip_chat_is_group_chat (GossipChat *chat)
988 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
990 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
991 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
998 gossip_chat_is_connected (GossipChat *chat)
1000 GossipChatPriv *priv;
1002 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1004 priv = GET_PRIV (chat);
1006 return (priv->tp_chat != NULL);
1010 gossip_chat_get_show_contacts (GossipChat *chat)
1012 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1014 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1015 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1022 gossip_chat_set_show_contacts (GossipChat *chat,
1025 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1027 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1028 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1033 gossip_chat_save_geometry (GossipChat *chat,
1039 //FIXME: gossip_geometry_save_for_chat (chat, x, y, w, h);
1043 gossip_chat_load_geometry (GossipChat *chat,
1049 //FIXME: gossip_geometry_load_for_chat (chat, x, y, w, h);
1053 gossip_chat_set_tp_chat (GossipChat *chat,
1054 EmpathyTpChat *tp_chat)
1056 GossipChatPriv *priv;
1059 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1060 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1062 priv = GET_PRIV (chat);
1064 if (tp_chat == priv->tp_chat) {
1068 if (priv->tp_chat) {
1069 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1070 chat_message_received_cb,
1072 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1075 g_object_unref (priv->tp_chat);
1078 priv->tp_chat = g_object_ref (tp_chat);
1080 g_signal_connect (tp_chat, "message-received",
1081 G_CALLBACK (chat_message_received_cb),
1083 g_signal_connect (tp_chat, "destroy",
1084 G_CALLBACK (chat_destroy_cb),
1087 empathy_tp_chat_request_pending (tp_chat);
1089 if (!priv->sensitive) {
1090 widget = gossip_chat_get_widget (chat);
1091 gtk_widget_set_sensitive (widget, TRUE);
1092 gossip_chat_view_append_event (chat->view, _("Connected"));
1093 priv->sensitive = TRUE;
1098 gossip_chat_clear (GossipChat *chat)
1100 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1102 gossip_chat_view_clear (chat->view);
1106 gossip_chat_set_window (GossipChat *chat,
1107 GossipChatWindow *window)
1109 GossipChatPriv *priv;
1111 priv = GET_PRIV (chat);
1112 priv->window = window;
1116 gossip_chat_get_window (GossipChat *chat)
1118 GossipChatPriv *priv;
1120 priv = GET_PRIV (chat);
1122 return priv->window;
1126 gossip_chat_scroll_down (GossipChat *chat)
1128 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1130 gossip_chat_view_scroll_down (chat->view);
1134 gossip_chat_cut (GossipChat *chat)
1136 GtkTextBuffer *buffer;
1138 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1140 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1141 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1142 GtkClipboard *clipboard;
1144 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1146 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1151 gossip_chat_copy (GossipChat *chat)
1153 GtkTextBuffer *buffer;
1155 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1157 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1158 gossip_chat_view_copy_clipboard (chat->view);
1162 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1163 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1164 GtkClipboard *clipboard;
1166 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1168 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1173 gossip_chat_paste (GossipChat *chat)
1175 GtkTextBuffer *buffer;
1176 GtkClipboard *clipboard;
1178 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1180 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1181 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1183 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1187 gossip_chat_present (GossipChat *chat)
1189 GossipChatPriv *priv;
1191 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1193 priv = GET_PRIV (chat);
1195 if (priv->window == NULL) {
1196 GossipChatWindow *window;
1198 window = gossip_chat_window_get_default ();
1200 window = gossip_chat_window_new ();
1203 gossip_chat_window_add_chat (window, chat);
1206 gossip_chat_window_switch_to_chat (priv->window, chat);
1207 gossip_window_present (
1208 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1211 gtk_widget_grab_focus (chat->input_text_view);
1215 gossip_chat_should_play_sound (GossipChat *chat)
1217 GossipChatWindow *window;
1218 gboolean play = TRUE;
1220 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1222 window = gossip_chat_get_window (GOSSIP_CHAT (chat));
1227 play = !gossip_chat_window_has_focus (window);
1233 gossip_chat_should_highlight_nick (GossipMessage *message)
1235 GossipContact *my_contact;
1236 const gchar *msg, *to;
1237 gchar *cf_msg, *cf_to;
1241 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1243 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1247 msg = gossip_message_get_body (message);
1252 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1253 to = gossip_contact_get_name (my_contact);
1258 cf_msg = g_utf8_casefold (msg, -1);
1259 cf_to = g_utf8_casefold (to, -1);
1261 ch = strstr (cf_msg, cf_to);
1267 /* Not first in the message */
1268 if ((*(ch - 1) != ' ') &&
1269 (*(ch - 1) != ',') &&
1270 (*(ch - 1) != '.')) {
1275 ch = ch + strlen (cf_to);
1276 if (ch >= cf_msg + strlen (cf_msg)) {