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-contact-manager.h>
39 #include <libempathy/gossip-debug.h>
40 #include <libempathy/gossip-utils.h>
41 #include <libempathy/gossip-conf.h>
43 #include "gossip-chat.h"
44 #include "gossip-chat-window.h"
45 #include "gossip-geometry.h"
46 #include "gossip-preferences.h"
47 #include "gossip-spell.h"
48 //#include "gossip-spell-dialog.h"
49 #include "gossip-ui-utils.h"
51 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHAT, GossipChatPriv))
53 #define DEBUG_DOMAIN "Chat"
55 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
56 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
58 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
60 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
64 struct _GossipChatPriv {
65 EmpathyContactManager *manager;
66 EmpathyTpChat *tp_chat;
67 GossipChatWindow *window;
69 GtkTooltips *tooltips;
70 guint composing_stop_timeout_id;
73 GSList *sent_messages;
74 gint sent_messages_index;
75 /* Used to automatically shrink a window that has temporarily
76 * grown due to long input.
79 gint default_window_height;
80 gint last_input_height;
81 gboolean vscroll_visible;
92 static void gossip_chat_class_init (GossipChatClass *klass);
93 static void gossip_chat_init (GossipChat *chat);
94 static void chat_finalize (GObject *object);
95 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
97 static void chat_send (GossipChat *chat,
99 static void chat_input_text_view_send (GossipChat *chat);
100 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
101 GossipMessage *message,
103 void chat_sent_message_add (GossipChat *chat,
105 const gchar * chat_sent_message_get_next (GossipChat *chat);
106 const gchar * chat_sent_message_get_last (GossipChat *chat);
107 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
110 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
112 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
115 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
117 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
118 GtkAllocation *allocation,
120 static void chat_text_view_realize_cb (GtkWidget *widget,
122 static void chat_text_populate_popup_cb (GtkTextView *view,
125 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
126 GossipChatSpell *chat_spell);
127 static GossipChatSpell *chat_spell_new (GossipChat *chat,
131 static void chat_spell_free (GossipChatSpell *chat_spell);
132 static void chat_composing_start (GossipChat *chat);
133 static void chat_composing_stop (GossipChat *chat);
134 static void chat_composing_remove_timeout (GossipChat *chat);
135 static gboolean chat_composing_stop_timeout_cb (GossipChat *chat);
145 static guint chat_signals[LAST_SIGNAL] = { 0 };
147 G_DEFINE_TYPE (GossipChat, gossip_chat, G_TYPE_OBJECT);
150 gossip_chat_class_init (GossipChatClass *klass)
152 GObjectClass *object_class;
154 object_class = G_OBJECT_CLASS (klass);
156 object_class->finalize = chat_finalize;
158 chat_signals[COMPOSING] =
159 g_signal_new ("composing",
160 G_OBJECT_CLASS_TYPE (object_class),
164 g_cclosure_marshal_VOID__BOOLEAN,
168 chat_signals[NEW_MESSAGE] =
169 g_signal_new ("new-message",
170 G_OBJECT_CLASS_TYPE (object_class),
174 g_cclosure_marshal_VOID__OBJECT,
176 1, GOSSIP_TYPE_MESSAGE);
178 chat_signals[NAME_CHANGED] =
179 g_signal_new ("name-changed",
180 G_OBJECT_CLASS_TYPE (object_class),
184 g_cclosure_marshal_VOID__POINTER,
188 chat_signals[STATUS_CHANGED] =
189 g_signal_new ("status-changed",
190 G_OBJECT_CLASS_TYPE (object_class),
194 g_cclosure_marshal_VOID__VOID,
198 g_type_class_add_private (object_class, sizeof (GossipChatPriv));
202 gossip_chat_init (GossipChat *chat)
204 GossipChatPriv *priv;
205 GtkTextBuffer *buffer;
207 chat->view = gossip_chat_view_new ();
208 chat->input_text_view = gtk_text_view_new ();
210 chat->is_first_char = TRUE;
212 g_object_set (chat->input_text_view,
213 "pixels-above-lines", 2,
214 "pixels-below-lines", 2,
215 "pixels-inside-wrap", 1,
218 "wrap-mode", GTK_WRAP_WORD_CHAR,
221 priv = GET_PRIV (chat);
223 priv->manager = empathy_contact_manager_new ();
224 priv->tooltips = gtk_tooltips_new ();
225 priv->default_window_height = -1;
226 priv->vscroll_visible = FALSE;
227 priv->sensitive = TRUE;
228 priv->sent_messages = NULL;
229 priv->sent_messages_index = -1;
231 g_signal_connect (chat->input_text_view,
233 G_CALLBACK (chat_input_key_press_event_cb),
236 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
237 g_signal_connect (buffer,
239 G_CALLBACK (chat_input_text_buffer_changed_cb),
241 g_signal_connect (chat->view,
243 G_CALLBACK (chat_text_view_focus_in_event_cb),
246 g_signal_connect (chat->input_text_view,
248 G_CALLBACK (chat_text_view_size_allocate_cb),
251 g_signal_connect (chat->input_text_view,
253 G_CALLBACK (chat_text_view_realize_cb),
256 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
258 G_CALLBACK (chat_text_populate_popup_cb),
261 /* create misspelt words identification tag */
262 gtk_text_buffer_create_tag (buffer,
264 "underline", PANGO_UNDERLINE_ERROR,
269 chat_finalize (GObject *object)
272 GossipChatPriv *priv;
274 chat = GOSSIP_CHAT (object);
275 priv = GET_PRIV (chat);
277 gossip_debug (DEBUG_DOMAIN, "Finalized: %p", object);
279 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
280 g_slist_free (priv->sent_messages);
282 chat_composing_remove_timeout (chat);
283 g_object_unref (chat->account);
284 g_object_unref (priv->manager);
287 g_object_unref (priv->tp_chat);
292 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
296 chat_destroy_cb (EmpathyTpChat *tp_chat,
299 GossipChatPriv *priv;
302 priv = GET_PRIV (chat);
305 g_object_unref (priv->tp_chat);
306 priv->tp_chat = NULL;
309 gossip_chat_view_append_event (chat->view, _("Disconnected"));
311 widget = gossip_chat_get_widget (chat);
312 gtk_widget_set_sensitive (widget, FALSE);
313 priv->sensitive = FALSE;
317 chat_send (GossipChat *chat,
320 GossipChatPriv *priv;
321 //GossipLogManager *log_manager;
322 GossipMessage *message;
323 GossipContact *own_contact;
325 priv = GET_PRIV (chat);
327 if (G_STR_EMPTY (msg)) {
331 chat_sent_message_add (chat, msg);
333 if (g_str_has_prefix (msg, "/clear")) {
334 gossip_chat_view_clear (chat->view);
338 /* FIXME: gossip_app_set_not_away ();*/
340 own_contact = gossip_chat_get_own_contact (chat);
341 message = gossip_message_new (msg);
342 gossip_message_set_sender (message, own_contact);
344 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
345 //gossip_log_message_for_contact (log_manager, message, FALSE);
347 empathy_tp_chat_send (priv->tp_chat, message);
349 g_object_unref (message);
353 chat_input_text_view_send (GossipChat *chat)
355 GossipChatPriv *priv;
356 GtkTextBuffer *buffer;
357 GtkTextIter start, end;
360 priv = GET_PRIV (chat);
362 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
364 gtk_text_buffer_get_bounds (buffer, &start, &end);
365 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
367 /* clear the input field */
368 gtk_text_buffer_set_text (buffer, "", -1);
370 chat_send (chat, msg);
374 chat->is_first_char = TRUE;
378 chat_message_received_cb (EmpathyTpChat *tp_chat,
379 GossipMessage *message,
382 GossipChatPriv *priv;
383 //GossipLogManager *log_manager;
384 GossipContact *sender;
386 priv = GET_PRIV (chat);
388 sender = gossip_message_get_sender (message);
389 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
390 gossip_contact_get_name (sender));
393 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
394 gossip_log_message_for_contact (log_manager, message, TRUE);
396 gossip_chat_view_append_message (chat->view, message);
398 if (gossip_chat_should_play_sound (chat)) {
399 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
402 g_signal_emit_by_name (chat, "new-message", message);
406 chat_sent_message_add (GossipChat *chat,
409 GossipChatPriv *priv;
413 priv = GET_PRIV (chat);
415 /* Save the sent message in our repeat buffer */
416 list = priv->sent_messages;
418 /* Remove any other occurances of this msg */
419 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
420 list = g_slist_remove_link (list, item);
422 g_slist_free1 (item);
425 /* Trim the list to the last 10 items */
426 while (g_slist_length (list) > 10) {
427 item = g_slist_last (list);
429 list = g_slist_remove_link (list, item);
431 g_slist_free1 (item);
435 /* Add new message */
436 list = g_slist_prepend (list, g_strdup (str));
438 /* Set list and reset the index */
439 priv->sent_messages = list;
440 priv->sent_messages_index = -1;
444 chat_sent_message_get_next (GossipChat *chat)
446 GossipChatPriv *priv;
449 priv = GET_PRIV (chat);
451 if (!priv->sent_messages) {
452 gossip_debug (DEBUG_DOMAIN,
453 "No sent messages, next message is NULL");
457 max = g_slist_length (priv->sent_messages) - 1;
459 if (priv->sent_messages_index < max) {
460 priv->sent_messages_index++;
463 gossip_debug (DEBUG_DOMAIN,
464 "Returning next message index:%d",
465 priv->sent_messages_index);
467 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
471 chat_sent_message_get_last (GossipChat *chat)
473 GossipChatPriv *priv;
475 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
477 priv = GET_PRIV (chat);
479 if (!priv->sent_messages) {
480 gossip_debug (DEBUG_DOMAIN,
481 "No sent messages, last message is NULL");
485 if (priv->sent_messages_index >= 0) {
486 priv->sent_messages_index--;
489 gossip_debug (DEBUG_DOMAIN,
490 "Returning last message index:%d",
491 priv->sent_messages_index);
493 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
497 chat_input_key_press_event_cb (GtkWidget *widget,
501 GossipChatPriv *priv;
504 GtkWidget *text_view_sw;
506 priv = GET_PRIV (chat);
508 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
512 /* Catch ctrl+up/down so we can traverse messages we sent */
513 if ((event->state & GDK_CONTROL_MASK) &&
514 (event->keyval == GDK_Up ||
515 event->keyval == GDK_Down)) {
516 GtkTextBuffer *buffer;
519 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
521 if (event->keyval == GDK_Up) {
522 str = chat_sent_message_get_next (chat);
524 str = chat_sent_message_get_last (chat);
527 g_signal_handlers_block_by_func (buffer,
528 chat_input_text_buffer_changed_cb,
530 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
531 g_signal_handlers_unblock_by_func (buffer,
532 chat_input_text_buffer_changed_cb,
538 /* Catch enter but not ctrl/shift-enter */
539 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
542 /* This is to make sure that kinput2 gets the enter. And if
543 * it's handled there we shouldn't send on it. This is because
544 * kinput2 uses Enter to commit letters. See:
545 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
548 view = GTK_TEXT_VIEW (chat->input_text_view);
549 if (gtk_im_context_filter_keypress (view->im_context, event)) {
550 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
554 chat_input_text_view_send (chat);
558 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
560 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
561 /* Newline for shift-enter. */
564 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
565 event->keyval == GDK_Page_Up) {
566 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
567 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
571 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
572 event->keyval == GDK_Page_Down) {
573 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
574 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
575 gtk_adjustment_set_value (adj, val);
584 chat_text_view_focus_in_event_cb (GtkWidget *widget,
588 gtk_widget_grab_focus (chat->input_text_view);
594 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
597 GossipChatPriv *priv;
598 GtkTextIter start, end;
600 gboolean spell_checker = FALSE;
602 priv = GET_PRIV (chat);
604 if (gtk_text_buffer_get_char_count (buffer) == 0) {
605 chat_composing_stop (chat);
607 chat_composing_start (chat);
610 gossip_conf_get_bool (gossip_conf_get (),
611 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
614 if (chat->is_first_char) {
618 GtkAllocation *allocation;
620 /* Save the window's size */
621 dialog = gossip_chat_window_get_dialog (priv->window);
622 gtk_window_get_size (GTK_WINDOW (dialog),
623 NULL, &window_height);
625 gtk_widget_size_request (chat->input_text_view, &req);
627 allocation = >K_WIDGET (chat->view)->allocation;
629 priv->default_window_height = window_height;
630 priv->last_input_height = req.height;
631 priv->padding_height = window_height - req.height - allocation->height;
633 chat->is_first_char = FALSE;
636 gtk_text_buffer_get_start_iter (buffer, &start);
638 if (!spell_checker) {
639 gtk_text_buffer_get_end_iter (buffer, &end);
640 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
644 if (!gossip_spell_supported ()) {
648 /* NOTE: this is really inefficient, we shouldn't have to
649 reiterate the whole buffer each time and check each work
652 gboolean correct = FALSE;
655 if (gtk_text_iter_is_start (&start)) {
658 if (!gtk_text_iter_forward_word_end (&end)) {
659 /* no whole word yet */
663 if (!gtk_text_iter_forward_word_end (&end)) {
664 /* must be the end of the buffer */
669 gtk_text_iter_backward_word_start (&start);
672 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
674 /* spell check string */
675 if (!gossip_chat_get_is_command (str)) {
676 correct = gossip_spell_check (str);
682 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
684 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
689 /* set start iter to the end iters position */
701 chat_change_size_in_idle_cb (ChangeSizeData *data)
703 gtk_window_resize (GTK_WINDOW (data->window),
704 data->width, data->height);
710 chat_text_view_scroll_hide_cb (GtkWidget *widget,
713 GossipChatPriv *priv;
716 priv = GET_PRIV (chat);
718 priv->vscroll_visible = FALSE;
719 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
721 sw = gtk_widget_get_parent (chat->input_text_view);
722 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
725 g_object_set (sw, "height-request", -1, NULL);
729 chat_text_view_size_allocate_cb (GtkWidget *widget,
730 GtkAllocation *allocation,
733 GossipChatPriv *priv;
736 ChangeSizeData *data;
739 GtkAllocation *view_allocation;
744 priv = GET_PRIV (chat);
746 if (priv->default_window_height <= 0) {
750 sw = gtk_widget_get_parent (widget);
751 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
754 priv->vscroll_visible = TRUE;
755 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
756 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
758 GTK_POLICY_AUTOMATIC);
759 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
760 g_signal_connect (vscroll, "hide",
761 G_CALLBACK (chat_text_view_scroll_hide_cb),
765 if (priv->last_input_height <= allocation->height) {
766 priv->last_input_height = allocation->height;
770 diff = priv->last_input_height - allocation->height;
771 priv->last_input_height = allocation->height;
773 view_allocation = >K_WIDGET (chat->view)->allocation;
775 dialog = gossip_chat_window_get_dialog (priv->window);
776 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
778 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
780 if (new_height <= priv->default_window_height) {
781 window_height = priv->default_window_height;
783 window_height = new_height;
786 if (current_height <= window_height) {
790 /* Restore the window's size */
791 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
793 data = g_new0 (ChangeSizeData, 1);
794 data->window = dialog;
796 data->height = window_height;
798 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
799 (GSourceFunc) chat_change_size_in_idle_cb,
804 chat_text_view_realize_cb (GtkWidget *widget,
807 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
808 gtk_widget_grab_focus (widget);
812 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
815 GtkTextBuffer *buffer;
819 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
821 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
823 gtk_text_buffer_get_end_iter (buffer, &iter);
824 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
826 gtk_text_buffer_get_end_iter (buffer, &iter);
827 gtk_text_buffer_insert (buffer, &iter, " ", -1);
831 chat_text_populate_popup_cb (GtkTextView *view,
835 GossipChatPriv *priv;
836 GtkTextBuffer *buffer;
837 GtkTextTagTable *table;
840 GtkTextIter iter, start, end;
843 GossipChatSpell *chat_spell;
844 GtkWidget *smiley_menu;
846 priv = GET_PRIV (chat);
848 /* Add the emoticon menu. */
849 item = gtk_separator_menu_item_new ();
850 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
851 gtk_widget_show (item);
853 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
854 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
855 gtk_widget_show (item);
857 smiley_menu = gossip_chat_view_get_smiley_menu (
858 G_CALLBACK (chat_insert_smiley_activate_cb),
861 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
863 /* Add the spell check menu item. */
864 buffer = gtk_text_view_get_buffer (view);
865 table = gtk_text_buffer_get_tag_table (buffer);
867 tag = gtk_text_tag_table_lookup (table, "misspelled");
869 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
871 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
872 GTK_TEXT_WINDOW_WIDGET,
876 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
880 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
881 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
883 str = gtk_text_buffer_get_text (buffer,
884 &start, &end, FALSE);
887 if (G_STR_EMPTY (str)) {
891 chat_spell = chat_spell_new (chat, str, start, end);
893 g_object_set_data_full (G_OBJECT (menu),
894 "chat_spell", chat_spell,
895 (GDestroyNotify) chat_spell_free);
897 item = gtk_separator_menu_item_new ();
898 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
899 gtk_widget_show (item);
901 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
902 g_signal_connect (item,
904 G_CALLBACK (chat_text_check_word_spelling_cb),
906 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
907 gtk_widget_show (item);
911 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
912 GossipChatSpell *chat_spell)
914 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
920 static GossipChatSpell *
921 chat_spell_new (GossipChat *chat,
926 GossipChatSpell *chat_spell;
928 chat_spell = g_new0 (GossipChatSpell, 1);
930 chat_spell->chat = g_object_ref (chat);
931 chat_spell->word = g_strdup (word);
932 chat_spell->start = start;
933 chat_spell->end = end;
939 chat_spell_free (GossipChatSpell *chat_spell)
941 g_object_unref (chat_spell->chat);
942 g_free (chat_spell->word);
947 chat_composing_start (GossipChat *chat)
949 GossipChatPriv *priv;
951 priv = GET_PRIV (chat);
953 if (priv->composing_stop_timeout_id) {
954 /* Just restart the timeout */
955 chat_composing_remove_timeout (chat);
958 gossip_session_send_composing (gossip_app_get_session (),
959 priv->contact, TRUE);
963 priv->composing_stop_timeout_id = g_timeout_add (
964 1000 * COMPOSING_STOP_TIMEOUT,
965 (GSourceFunc) chat_composing_stop_timeout_cb,
970 chat_composing_stop (GossipChat *chat)
972 GossipChatPriv *priv;
974 priv = GET_PRIV (chat);
976 chat_composing_remove_timeout (chat);
978 gossip_session_send_composing (gossip_app_get_session (),
979 priv->contact, FALSE);*/
983 chat_composing_remove_timeout (GossipChat *chat)
985 GossipChatPriv *priv;
987 priv = GET_PRIV (chat);
989 if (priv->composing_stop_timeout_id) {
990 g_source_remove (priv->composing_stop_timeout_id);
991 priv->composing_stop_timeout_id = 0;
996 chat_composing_stop_timeout_cb (GossipChat *chat)
998 GossipChatPriv *priv;
1000 priv = GET_PRIV (chat);
1002 priv->composing_stop_timeout_id = 0;
1004 gossip_session_send_composing (gossip_app_get_session (),
1005 priv->contact, FALSE);*/
1011 gossip_chat_get_is_command (const gchar *str)
1013 g_return_val_if_fail (str != NULL, FALSE);
1015 if (str[0] != '/') {
1019 if (g_str_has_prefix (str, "/me")) {
1022 else if (g_str_has_prefix (str, "/nick")) {
1025 else if (g_str_has_prefix (str, "/topic")) {
1033 gossip_chat_correct_word (GossipChat *chat,
1036 const gchar *new_word)
1038 GtkTextBuffer *buffer;
1040 g_return_if_fail (chat != NULL);
1041 g_return_if_fail (new_word != NULL);
1043 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1045 gtk_text_buffer_delete (buffer, &start, &end);
1046 gtk_text_buffer_insert (buffer, &start,
1052 gossip_chat_get_name (GossipChat *chat)
1054 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1056 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1057 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1064 gossip_chat_get_tooltip (GossipChat *chat)
1066 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1068 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1069 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1076 gossip_chat_get_status_icon_name (GossipChat *chat)
1078 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1080 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1081 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1088 gossip_chat_get_contact (GossipChat *chat)
1090 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1092 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1093 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1099 gossip_chat_get_own_contact (GossipChat *chat)
1101 GossipChatPriv *priv;
1103 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1105 priv = GET_PRIV (chat);
1107 return empathy_contact_manager_get_own (priv->manager, chat->account);
1111 gossip_chat_get_widget (GossipChat *chat)
1113 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1115 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1116 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1123 gossip_chat_is_group_chat (GossipChat *chat)
1125 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1127 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1128 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1135 gossip_chat_is_connected (GossipChat *chat)
1137 GossipChatPriv *priv;
1139 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1141 priv = GET_PRIV (chat);
1143 return (priv->tp_chat != NULL);
1147 gossip_chat_get_show_contacts (GossipChat *chat)
1149 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1151 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1152 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1159 gossip_chat_set_show_contacts (GossipChat *chat,
1162 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1164 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1165 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1170 gossip_chat_save_geometry (GossipChat *chat,
1176 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1180 gossip_chat_load_geometry (GossipChat *chat,
1186 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1190 gossip_chat_set_tp_chat (GossipChat *chat,
1191 EmpathyTpChat *tp_chat)
1193 GossipChatPriv *priv;
1196 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1197 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1199 priv = GET_PRIV (chat);
1201 if (tp_chat == priv->tp_chat) {
1205 if (priv->tp_chat) {
1206 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1207 chat_message_received_cb,
1209 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1212 g_object_unref (priv->tp_chat);
1216 priv->tp_chat = g_object_ref (tp_chat);
1217 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1219 g_signal_connect (tp_chat, "message-received",
1220 G_CALLBACK (chat_message_received_cb),
1222 g_signal_connect (tp_chat, "destroy",
1223 G_CALLBACK (chat_destroy_cb),
1226 empathy_tp_chat_request_pending (tp_chat);
1228 if (!priv->sensitive) {
1229 widget = gossip_chat_get_widget (chat);
1230 gtk_widget_set_sensitive (widget, TRUE);
1231 gossip_chat_view_append_event (chat->view, _("Connected"));
1232 priv->sensitive = TRUE;
1237 gossip_chat_get_id (GossipChat *chat)
1239 GossipChatPriv *priv;
1241 priv = GET_PRIV (chat);
1247 gossip_chat_clear (GossipChat *chat)
1249 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1251 gossip_chat_view_clear (chat->view);
1255 gossip_chat_set_window (GossipChat *chat,
1256 GossipChatWindow *window)
1258 GossipChatPriv *priv;
1260 priv = GET_PRIV (chat);
1261 priv->window = window;
1265 gossip_chat_get_window (GossipChat *chat)
1267 GossipChatPriv *priv;
1269 priv = GET_PRIV (chat);
1271 return priv->window;
1275 gossip_chat_scroll_down (GossipChat *chat)
1277 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1279 gossip_chat_view_scroll_down (chat->view);
1283 gossip_chat_cut (GossipChat *chat)
1285 GtkTextBuffer *buffer;
1287 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1289 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1290 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1291 GtkClipboard *clipboard;
1293 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1295 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1300 gossip_chat_copy (GossipChat *chat)
1302 GtkTextBuffer *buffer;
1304 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1306 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1307 gossip_chat_view_copy_clipboard (chat->view);
1311 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1312 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1313 GtkClipboard *clipboard;
1315 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1317 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1322 gossip_chat_paste (GossipChat *chat)
1324 GtkTextBuffer *buffer;
1325 GtkClipboard *clipboard;
1327 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1329 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1330 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1332 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1336 gossip_chat_present (GossipChat *chat)
1338 GossipChatPriv *priv;
1340 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1342 priv = GET_PRIV (chat);
1344 if (priv->window == NULL) {
1345 GossipChatWindow *window;
1347 window = gossip_chat_window_get_default ();
1349 window = gossip_chat_window_new ();
1352 gossip_chat_window_add_chat (window, chat);
1355 gossip_chat_window_switch_to_chat (priv->window, chat);
1356 gossip_window_present (
1357 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1360 gtk_widget_grab_focus (chat->input_text_view);
1364 gossip_chat_should_play_sound (GossipChat *chat)
1366 GossipChatWindow *window;
1367 gboolean play = TRUE;
1369 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1371 window = gossip_chat_get_window (chat);
1376 play = !gossip_chat_window_has_focus (window);
1382 gossip_chat_should_highlight_nick (GossipMessage *message)
1384 GossipContact *my_contact;
1385 const gchar *msg, *to;
1386 gchar *cf_msg, *cf_to;
1390 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1392 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1396 msg = gossip_message_get_body (message);
1401 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1402 to = gossip_contact_get_name (my_contact);
1407 cf_msg = g_utf8_casefold (msg, -1);
1408 cf_to = g_utf8_casefold (to, -1);
1410 ch = strstr (cf_msg, cf_to);
1416 /* Not first in the message */
1417 if ((*(ch - 1) != ' ') &&
1418 (*(ch - 1) != ',') &&
1419 (*(ch - 1) != '.')) {
1424 ch = ch + strlen (cf_to);
1425 if (ch >= cf_msg + strlen (cf_msg)) {