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 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->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 (GOSSIP_CHAT (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 (GOSSIP_CHAT (object)->account);
286 g_object_unref (priv->tp_chat);
291 G_OBJECT_CLASS (gossip_chat_parent_class)->finalize (object);
295 chat_destroy_cb (EmpathyTpChat *tp_chat,
298 GossipChatPriv *priv;
301 priv = GET_PRIV (chat);
304 g_object_unref (priv->tp_chat);
305 priv->tp_chat = NULL;
308 gossip_chat_view_append_event (chat->view, _("Disconnected"));
310 widget = gossip_chat_get_widget (chat);
311 gtk_widget_set_sensitive (widget, FALSE);
312 priv->sensitive = FALSE;
316 chat_send (GossipChat *chat,
319 GossipChatPriv *priv;
320 //GossipLogManager *log_manager;
321 GossipMessage *message;
322 GossipContact *own_contact;
324 priv = GET_PRIV (chat);
326 if (G_STR_EMPTY (msg)) {
330 chat_sent_message_add (chat, msg);
332 if (g_str_has_prefix (msg, "/clear")) {
333 gossip_chat_view_clear (chat->view);
337 /* FIXME: gossip_app_set_not_away ();*/
339 own_contact = gossip_chat_get_own_contact (chat);
340 message = gossip_message_new (msg);
341 gossip_message_set_sender (message, own_contact);
343 //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
344 //gossip_log_message_for_contact (log_manager, message, FALSE);
346 empathy_tp_chat_send (priv->tp_chat, message);
348 g_object_unref (message);
352 chat_input_text_view_send (GossipChat *chat)
354 GossipChatPriv *priv;
355 GtkTextBuffer *buffer;
356 GtkTextIter start, end;
359 priv = GET_PRIV (chat);
361 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
363 gtk_text_buffer_get_bounds (buffer, &start, &end);
364 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
366 /* clear the input field */
367 gtk_text_buffer_set_text (buffer, "", -1);
369 chat_send (chat, msg);
373 chat->is_first_char = TRUE;
377 chat_message_received_cb (EmpathyTpChat *tp_chat,
378 GossipMessage *message,
381 GossipChatPriv *priv;
382 //GossipLogManager *log_manager;
383 GossipContact *sender;
385 priv = GET_PRIV (chat);
387 sender = gossip_message_get_sender (message);
388 gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
389 gossip_contact_get_name (sender));
392 log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
393 gossip_log_message_for_contact (log_manager, message, TRUE);
395 gossip_chat_view_append_message (chat->view, message);
397 if (gossip_chat_should_play_sound (chat)) {
398 // FIXME: gossip_sound_play (GOSSIP_SOUND_CHAT);
401 g_signal_emit_by_name (chat, "new-message", message);
405 chat_sent_message_add (GossipChat *chat,
408 GossipChatPriv *priv;
412 priv = GET_PRIV (chat);
414 /* Save the sent message in our repeat buffer */
415 list = priv->sent_messages;
417 /* Remove any other occurances of this msg */
418 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
419 list = g_slist_remove_link (list, item);
421 g_slist_free1 (item);
424 /* Trim the list to the last 10 items */
425 while (g_slist_length (list) > 10) {
426 item = g_slist_last (list);
428 list = g_slist_remove_link (list, item);
430 g_slist_free1 (item);
434 /* Add new message */
435 list = g_slist_prepend (list, g_strdup (str));
437 /* Set list and reset the index */
438 priv->sent_messages = list;
439 priv->sent_messages_index = -1;
443 chat_sent_message_get_next (GossipChat *chat)
445 GossipChatPriv *priv;
448 priv = GET_PRIV (chat);
450 if (!priv->sent_messages) {
451 gossip_debug (DEBUG_DOMAIN,
452 "No sent messages, next message is NULL");
456 max = g_slist_length (priv->sent_messages) - 1;
458 if (priv->sent_messages_index < max) {
459 priv->sent_messages_index++;
462 gossip_debug (DEBUG_DOMAIN,
463 "Returning next message index:%d",
464 priv->sent_messages_index);
466 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
470 chat_sent_message_get_last (GossipChat *chat)
472 GossipChatPriv *priv;
474 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
476 priv = GET_PRIV (chat);
478 if (!priv->sent_messages) {
479 gossip_debug (DEBUG_DOMAIN,
480 "No sent messages, last message is NULL");
484 if (priv->sent_messages_index >= 0) {
485 priv->sent_messages_index--;
488 gossip_debug (DEBUG_DOMAIN,
489 "Returning last message index:%d",
490 priv->sent_messages_index);
492 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
496 chat_input_key_press_event_cb (GtkWidget *widget,
500 GossipChatPriv *priv;
503 GtkWidget *text_view_sw;
505 priv = GET_PRIV (chat);
507 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
511 /* Catch ctrl+up/down so we can traverse messages we sent */
512 if ((event->state & GDK_CONTROL_MASK) &&
513 (event->keyval == GDK_Up ||
514 event->keyval == GDK_Down)) {
515 GtkTextBuffer *buffer;
518 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
520 if (event->keyval == GDK_Up) {
521 str = chat_sent_message_get_next (chat);
523 str = chat_sent_message_get_last (chat);
526 g_signal_handlers_block_by_func (buffer,
527 chat_input_text_buffer_changed_cb,
529 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
530 g_signal_handlers_unblock_by_func (buffer,
531 chat_input_text_buffer_changed_cb,
537 /* Catch enter but not ctrl/shift-enter */
538 if (IS_ENTER (event->keyval) && !(event->state & GDK_SHIFT_MASK)) {
541 /* This is to make sure that kinput2 gets the enter. And if
542 * it's handled there we shouldn't send on it. This is because
543 * kinput2 uses Enter to commit letters. See:
544 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
547 view = GTK_TEXT_VIEW (chat->input_text_view);
548 if (gtk_im_context_filter_keypress (view->im_context, event)) {
549 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
553 chat_input_text_view_send (chat);
557 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
559 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
560 /* Newline for shift-enter. */
563 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
564 event->keyval == GDK_Page_Up) {
565 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
566 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
570 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
571 event->keyval == GDK_Page_Down) {
572 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
573 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
574 gtk_adjustment_set_value (adj, val);
583 chat_text_view_focus_in_event_cb (GtkWidget *widget,
587 gtk_widget_grab_focus (chat->input_text_view);
593 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
596 GossipChatPriv *priv;
597 GtkTextIter start, end;
599 gboolean spell_checker = FALSE;
601 priv = GET_PRIV (chat);
603 if (gtk_text_buffer_get_char_count (buffer) == 0) {
604 chat_composing_stop (chat);
606 chat_composing_start (chat);
609 gossip_conf_get_bool (gossip_conf_get (),
610 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
613 if (chat->is_first_char) {
617 GtkAllocation *allocation;
619 /* Save the window's size */
620 dialog = gossip_chat_window_get_dialog (priv->window);
621 gtk_window_get_size (GTK_WINDOW (dialog),
622 NULL, &window_height);
624 gtk_widget_size_request (chat->input_text_view, &req);
626 allocation = >K_WIDGET (chat->view)->allocation;
628 priv->default_window_height = window_height;
629 priv->last_input_height = req.height;
630 priv->padding_height = window_height - req.height - allocation->height;
632 chat->is_first_char = FALSE;
635 gtk_text_buffer_get_start_iter (buffer, &start);
637 if (!spell_checker) {
638 gtk_text_buffer_get_end_iter (buffer, &end);
639 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
643 if (!gossip_spell_supported ()) {
647 /* NOTE: this is really inefficient, we shouldn't have to
648 reiterate the whole buffer each time and check each work
651 gboolean correct = FALSE;
654 if (gtk_text_iter_is_start (&start)) {
657 if (!gtk_text_iter_forward_word_end (&end)) {
658 /* no whole word yet */
662 if (!gtk_text_iter_forward_word_end (&end)) {
663 /* must be the end of the buffer */
668 gtk_text_iter_backward_word_start (&start);
671 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
673 /* spell check string */
674 if (!gossip_chat_get_is_command (str)) {
675 correct = gossip_spell_check (str);
681 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
683 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
688 /* set start iter to the end iters position */
700 chat_change_size_in_idle_cb (ChangeSizeData *data)
702 gtk_window_resize (GTK_WINDOW (data->window),
703 data->width, data->height);
709 chat_text_view_scroll_hide_cb (GtkWidget *widget,
712 GossipChatPriv *priv;
715 priv = GET_PRIV (chat);
717 priv->vscroll_visible = FALSE;
718 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
720 sw = gtk_widget_get_parent (chat->input_text_view);
721 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
724 g_object_set (sw, "height-request", -1, NULL);
728 chat_text_view_size_allocate_cb (GtkWidget *widget,
729 GtkAllocation *allocation,
732 GossipChatPriv *priv;
735 ChangeSizeData *data;
738 GtkAllocation *view_allocation;
743 priv = GET_PRIV (chat);
745 if (priv->default_window_height <= 0) {
749 sw = gtk_widget_get_parent (widget);
750 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
753 priv->vscroll_visible = TRUE;
754 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
755 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
757 GTK_POLICY_AUTOMATIC);
758 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
759 g_signal_connect (vscroll, "hide",
760 G_CALLBACK (chat_text_view_scroll_hide_cb),
764 if (priv->last_input_height <= allocation->height) {
765 priv->last_input_height = allocation->height;
769 diff = priv->last_input_height - allocation->height;
770 priv->last_input_height = allocation->height;
772 view_allocation = >K_WIDGET (chat->view)->allocation;
774 dialog = gossip_chat_window_get_dialog (priv->window);
775 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
777 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
779 if (new_height <= priv->default_window_height) {
780 window_height = priv->default_window_height;
782 window_height = new_height;
785 if (current_height <= window_height) {
789 /* Restore the window's size */
790 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
792 data = g_new0 (ChangeSizeData, 1);
793 data->window = dialog;
795 data->height = window_height;
797 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
798 (GSourceFunc) chat_change_size_in_idle_cb,
803 chat_text_view_realize_cb (GtkWidget *widget,
806 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
807 gtk_widget_grab_focus (widget);
811 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
814 GtkTextBuffer *buffer;
818 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
820 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
822 gtk_text_buffer_get_end_iter (buffer, &iter);
823 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
825 gtk_text_buffer_get_end_iter (buffer, &iter);
826 gtk_text_buffer_insert (buffer, &iter, " ", -1);
830 chat_text_populate_popup_cb (GtkTextView *view,
834 GossipChatPriv *priv;
835 GtkTextBuffer *buffer;
836 GtkTextTagTable *table;
839 GtkTextIter iter, start, end;
842 GossipChatSpell *chat_spell;
843 GtkWidget *smiley_menu;
845 priv = GET_PRIV (chat);
847 /* Add the emoticon menu. */
848 item = gtk_separator_menu_item_new ();
849 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
850 gtk_widget_show (item);
852 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
853 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
854 gtk_widget_show (item);
856 smiley_menu = gossip_chat_view_get_smiley_menu (
857 G_CALLBACK (chat_insert_smiley_activate_cb),
860 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
862 /* Add the spell check menu item. */
863 buffer = gtk_text_view_get_buffer (view);
864 table = gtk_text_buffer_get_tag_table (buffer);
866 tag = gtk_text_tag_table_lookup (table, "misspelled");
868 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
870 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
871 GTK_TEXT_WINDOW_WIDGET,
875 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
879 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
880 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
882 str = gtk_text_buffer_get_text (buffer,
883 &start, &end, FALSE);
886 if (G_STR_EMPTY (str)) {
890 chat_spell = chat_spell_new (chat, str, start, end);
892 g_object_set_data_full (G_OBJECT (menu),
893 "chat_spell", chat_spell,
894 (GDestroyNotify) chat_spell_free);
896 item = gtk_separator_menu_item_new ();
897 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
898 gtk_widget_show (item);
900 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
901 g_signal_connect (item,
903 G_CALLBACK (chat_text_check_word_spelling_cb),
905 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
906 gtk_widget_show (item);
910 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
911 GossipChatSpell *chat_spell)
913 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
919 static GossipChatSpell *
920 chat_spell_new (GossipChat *chat,
925 GossipChatSpell *chat_spell;
927 chat_spell = g_new0 (GossipChatSpell, 1);
929 chat_spell->chat = g_object_ref (chat);
930 chat_spell->word = g_strdup (word);
931 chat_spell->start = start;
932 chat_spell->end = end;
938 chat_spell_free (GossipChatSpell *chat_spell)
940 g_object_unref (chat_spell->chat);
941 g_free (chat_spell->word);
946 chat_composing_start (GossipChat *chat)
948 GossipChatPriv *priv;
950 priv = GET_PRIV (chat);
952 if (priv->composing_stop_timeout_id) {
953 /* Just restart the timeout */
954 chat_composing_remove_timeout (chat);
957 gossip_session_send_composing (gossip_app_get_session (),
958 priv->contact, TRUE);
962 priv->composing_stop_timeout_id = g_timeout_add (
963 1000 * COMPOSING_STOP_TIMEOUT,
964 (GSourceFunc) chat_composing_stop_timeout_cb,
969 chat_composing_stop (GossipChat *chat)
971 GossipChatPriv *priv;
973 priv = GET_PRIV (chat);
975 chat_composing_remove_timeout (chat);
977 gossip_session_send_composing (gossip_app_get_session (),
978 priv->contact, FALSE);*/
982 chat_composing_remove_timeout (GossipChat *chat)
984 GossipChatPriv *priv;
986 priv = GET_PRIV (chat);
988 if (priv->composing_stop_timeout_id) {
989 g_source_remove (priv->composing_stop_timeout_id);
990 priv->composing_stop_timeout_id = 0;
995 chat_composing_stop_timeout_cb (GossipChat *chat)
997 GossipChatPriv *priv;
999 priv = GET_PRIV (chat);
1001 priv->composing_stop_timeout_id = 0;
1003 gossip_session_send_composing (gossip_app_get_session (),
1004 priv->contact, FALSE);*/
1010 gossip_chat_get_is_command (const gchar *str)
1012 g_return_val_if_fail (str != NULL, FALSE);
1014 if (str[0] != '/') {
1018 if (g_str_has_prefix (str, "/me")) {
1021 else if (g_str_has_prefix (str, "/nick")) {
1024 else if (g_str_has_prefix (str, "/topic")) {
1032 gossip_chat_correct_word (GossipChat *chat,
1035 const gchar *new_word)
1037 GtkTextBuffer *buffer;
1039 g_return_if_fail (chat != NULL);
1040 g_return_if_fail (new_word != NULL);
1042 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1044 gtk_text_buffer_delete (buffer, &start, &end);
1045 gtk_text_buffer_insert (buffer, &start,
1051 gossip_chat_get_name (GossipChat *chat)
1053 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1055 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1056 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1063 gossip_chat_get_tooltip (GossipChat *chat)
1065 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1067 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1068 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1075 gossip_chat_get_status_pixbuf (GossipChat *chat)
1077 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1079 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
1080 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
1087 gossip_chat_get_contact (GossipChat *chat)
1089 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1091 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1092 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1098 gossip_chat_get_own_contact (GossipChat *chat)
1100 EmpathyContactManager *manager;
1102 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1104 manager = empathy_session_get_contact_manager ();
1106 return empathy_contact_manager_get_own (manager, chat->account);
1110 gossip_chat_get_widget (GossipChat *chat)
1112 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1114 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1115 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1122 gossip_chat_is_group_chat (GossipChat *chat)
1124 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1126 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1127 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1134 gossip_chat_is_connected (GossipChat *chat)
1136 GossipChatPriv *priv;
1138 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1140 priv = GET_PRIV (chat);
1142 return (priv->tp_chat != NULL);
1146 gossip_chat_get_show_contacts (GossipChat *chat)
1148 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1150 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1151 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1158 gossip_chat_set_show_contacts (GossipChat *chat,
1161 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1163 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1164 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1169 gossip_chat_save_geometry (GossipChat *chat,
1175 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1179 gossip_chat_load_geometry (GossipChat *chat,
1185 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1189 gossip_chat_set_tp_chat (GossipChat *chat,
1190 EmpathyTpChat *tp_chat)
1192 GossipChatPriv *priv;
1195 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1196 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1198 priv = GET_PRIV (chat);
1200 if (tp_chat == priv->tp_chat) {
1204 if (priv->tp_chat) {
1205 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1206 chat_message_received_cb,
1208 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1211 g_object_unref (priv->tp_chat);
1215 priv->tp_chat = g_object_ref (tp_chat);
1216 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1218 g_signal_connect (tp_chat, "message-received",
1219 G_CALLBACK (chat_message_received_cb),
1221 g_signal_connect (tp_chat, "destroy",
1222 G_CALLBACK (chat_destroy_cb),
1225 empathy_tp_chat_request_pending (tp_chat);
1227 if (!priv->sensitive) {
1228 widget = gossip_chat_get_widget (chat);
1229 gtk_widget_set_sensitive (widget, TRUE);
1230 gossip_chat_view_append_event (chat->view, _("Connected"));
1231 priv->sensitive = TRUE;
1236 gossip_chat_get_id (GossipChat *chat)
1238 GossipChatPriv *priv;
1240 priv = GET_PRIV (chat);
1246 gossip_chat_clear (GossipChat *chat)
1248 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1250 gossip_chat_view_clear (chat->view);
1254 gossip_chat_set_window (GossipChat *chat,
1255 GossipChatWindow *window)
1257 GossipChatPriv *priv;
1259 priv = GET_PRIV (chat);
1260 priv->window = window;
1264 gossip_chat_get_window (GossipChat *chat)
1266 GossipChatPriv *priv;
1268 priv = GET_PRIV (chat);
1270 return priv->window;
1274 gossip_chat_scroll_down (GossipChat *chat)
1276 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1278 gossip_chat_view_scroll_down (chat->view);
1282 gossip_chat_cut (GossipChat *chat)
1284 GtkTextBuffer *buffer;
1286 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1288 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1289 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1290 GtkClipboard *clipboard;
1292 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1294 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1299 gossip_chat_copy (GossipChat *chat)
1301 GtkTextBuffer *buffer;
1303 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1305 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1306 gossip_chat_view_copy_clipboard (chat->view);
1310 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1311 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1312 GtkClipboard *clipboard;
1314 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1316 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1321 gossip_chat_paste (GossipChat *chat)
1323 GtkTextBuffer *buffer;
1324 GtkClipboard *clipboard;
1326 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1328 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1329 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1331 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1335 gossip_chat_present (GossipChat *chat)
1337 GossipChatPriv *priv;
1339 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1341 priv = GET_PRIV (chat);
1343 if (priv->window == NULL) {
1344 GossipChatWindow *window;
1346 window = gossip_chat_window_get_default ();
1348 window = gossip_chat_window_new ();
1351 gossip_chat_window_add_chat (window, chat);
1354 gossip_chat_window_switch_to_chat (priv->window, chat);
1355 gossip_window_present (
1356 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1359 gtk_widget_grab_focus (chat->input_text_view);
1363 gossip_chat_should_play_sound (GossipChat *chat)
1365 GossipChatWindow *window;
1366 gboolean play = TRUE;
1368 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1370 window = gossip_chat_get_window (GOSSIP_CHAT (chat));
1375 play = !gossip_chat_window_has_focus (window);
1381 gossip_chat_should_highlight_nick (GossipMessage *message)
1383 GossipContact *my_contact;
1384 const gchar *msg, *to;
1385 gchar *cf_msg, *cf_to;
1389 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1391 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1395 msg = gossip_message_get_body (message);
1400 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1401 to = gossip_contact_get_name (my_contact);
1406 cf_msg = g_utf8_casefold (msg, -1);
1407 cf_to = g_utf8_casefold (to, -1);
1409 ch = strstr (cf_msg, cf_to);
1415 /* Not first in the message */
1416 if ((*(ch - 1) != ' ') &&
1417 (*(ch - 1) != ',') &&
1418 (*(ch - 1) != '.')) {
1423 ch = ch + strlen (cf_to);
1424 if (ch >= cf_msg + strlen (cf_msg)) {