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,
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;
555 chat_input_text_view_send (chat);
560 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
562 if (IS_ENTER (event->keyval) && (event->state & GDK_SHIFT_MASK)) {
563 /* Newline for shift-enter. */
566 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
567 event->keyval == GDK_Page_Up) {
568 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
569 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
573 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
574 event->keyval == GDK_Page_Down) {
575 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
576 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
577 gtk_adjustment_set_value (adj, val);
587 chat_text_view_focus_in_event_cb (GtkWidget *widget,
591 gtk_widget_grab_focus (chat->input_text_view);
597 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
600 GossipChatPriv *priv;
601 GtkTextIter start, end;
603 gboolean spell_checker = FALSE;
605 priv = GET_PRIV (chat);
607 if (gtk_text_buffer_get_char_count (buffer) == 0) {
608 chat_composing_stop (chat);
610 chat_composing_start (chat);
613 gossip_conf_get_bool (gossip_conf_get (),
614 GOSSIP_PREFS_CHAT_SPELL_CHECKER_ENABLED,
617 if (chat->is_first_char) {
621 GtkAllocation *allocation;
623 /* Save the window's size */
624 dialog = gossip_chat_window_get_dialog (priv->window);
625 gtk_window_get_size (GTK_WINDOW (dialog),
626 NULL, &window_height);
628 gtk_widget_size_request (chat->input_text_view, &req);
630 allocation = >K_WIDGET (chat->view)->allocation;
632 priv->default_window_height = window_height;
633 priv->last_input_height = req.height;
634 priv->padding_height = window_height - req.height - allocation->height;
636 chat->is_first_char = FALSE;
639 gtk_text_buffer_get_start_iter (buffer, &start);
641 if (!spell_checker) {
642 gtk_text_buffer_get_end_iter (buffer, &end);
643 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
647 if (!gossip_spell_supported ()) {
651 /* NOTE: this is really inefficient, we shouldn't have to
652 reiterate the whole buffer each time and check each work
655 gboolean correct = FALSE;
658 if (gtk_text_iter_is_start (&start)) {
661 if (!gtk_text_iter_forward_word_end (&end)) {
662 /* no whole word yet */
666 if (!gtk_text_iter_forward_word_end (&end)) {
667 /* must be the end of the buffer */
672 gtk_text_iter_backward_word_start (&start);
675 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
677 /* spell check string */
678 if (!gossip_chat_get_is_command (str)) {
679 correct = gossip_spell_check (str);
685 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
687 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
692 /* set start iter to the end iters position */
704 chat_change_size_in_idle_cb (ChangeSizeData *data)
706 gtk_window_resize (GTK_WINDOW (data->window),
707 data->width, data->height);
713 chat_text_view_scroll_hide_cb (GtkWidget *widget,
716 GossipChatPriv *priv;
719 priv = GET_PRIV (chat);
721 priv->vscroll_visible = FALSE;
722 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
724 sw = gtk_widget_get_parent (chat->input_text_view);
725 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
728 g_object_set (sw, "height-request", -1, NULL);
732 chat_text_view_size_allocate_cb (GtkWidget *widget,
733 GtkAllocation *allocation,
736 GossipChatPriv *priv;
739 ChangeSizeData *data;
742 GtkAllocation *view_allocation;
747 priv = GET_PRIV (chat);
749 if (priv->default_window_height <= 0) {
753 sw = gtk_widget_get_parent (widget);
754 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
757 priv->vscroll_visible = TRUE;
758 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
759 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
761 GTK_POLICY_AUTOMATIC);
762 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
763 g_signal_connect (vscroll, "hide",
764 G_CALLBACK (chat_text_view_scroll_hide_cb),
768 if (priv->last_input_height <= allocation->height) {
769 priv->last_input_height = allocation->height;
773 diff = priv->last_input_height - allocation->height;
774 priv->last_input_height = allocation->height;
776 view_allocation = >K_WIDGET (chat->view)->allocation;
778 dialog = gossip_chat_window_get_dialog (priv->window);
779 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
781 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
783 if (new_height <= priv->default_window_height) {
784 window_height = priv->default_window_height;
786 window_height = new_height;
789 if (current_height <= window_height) {
793 /* Restore the window's size */
794 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
796 data = g_new0 (ChangeSizeData, 1);
797 data->window = dialog;
799 data->height = window_height;
801 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
802 (GSourceFunc) chat_change_size_in_idle_cb,
807 chat_text_view_realize_cb (GtkWidget *widget,
810 gossip_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
811 gtk_widget_grab_focus (widget);
815 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
818 GtkTextBuffer *buffer;
822 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
824 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
826 gtk_text_buffer_get_end_iter (buffer, &iter);
827 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
829 gtk_text_buffer_get_end_iter (buffer, &iter);
830 gtk_text_buffer_insert (buffer, &iter, " ", -1);
834 chat_text_populate_popup_cb (GtkTextView *view,
838 GossipChatPriv *priv;
839 GtkTextBuffer *buffer;
840 GtkTextTagTable *table;
843 GtkTextIter iter, start, end;
846 GossipChatSpell *chat_spell;
847 GtkWidget *smiley_menu;
849 priv = GET_PRIV (chat);
851 /* Add the emoticon menu. */
852 item = gtk_separator_menu_item_new ();
853 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
854 gtk_widget_show (item);
856 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
857 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
858 gtk_widget_show (item);
860 smiley_menu = gossip_chat_view_get_smiley_menu (
861 G_CALLBACK (chat_insert_smiley_activate_cb),
864 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
866 /* Add the spell check menu item. */
867 buffer = gtk_text_view_get_buffer (view);
868 table = gtk_text_buffer_get_tag_table (buffer);
870 tag = gtk_text_tag_table_lookup (table, "misspelled");
872 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
874 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
875 GTK_TEXT_WINDOW_WIDGET,
879 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
883 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
884 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
886 str = gtk_text_buffer_get_text (buffer,
887 &start, &end, FALSE);
890 if (G_STR_EMPTY (str)) {
894 chat_spell = chat_spell_new (chat, str, start, end);
896 g_object_set_data_full (G_OBJECT (menu),
897 "chat_spell", chat_spell,
898 (GDestroyNotify) chat_spell_free);
900 item = gtk_separator_menu_item_new ();
901 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
902 gtk_widget_show (item);
904 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
905 g_signal_connect (item,
907 G_CALLBACK (chat_text_check_word_spelling_cb),
909 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
910 gtk_widget_show (item);
914 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
915 GossipChatSpell *chat_spell)
917 /*FIXME: gossip_spell_dialog_show (chat_spell->chat,
923 static GossipChatSpell *
924 chat_spell_new (GossipChat *chat,
929 GossipChatSpell *chat_spell;
931 chat_spell = g_new0 (GossipChatSpell, 1);
933 chat_spell->chat = g_object_ref (chat);
934 chat_spell->word = g_strdup (word);
935 chat_spell->start = start;
936 chat_spell->end = end;
942 chat_spell_free (GossipChatSpell *chat_spell)
944 g_object_unref (chat_spell->chat);
945 g_free (chat_spell->word);
950 chat_composing_start (GossipChat *chat)
952 GossipChatPriv *priv;
954 priv = GET_PRIV (chat);
956 if (priv->composing_stop_timeout_id) {
957 /* Just restart the timeout */
958 chat_composing_remove_timeout (chat);
961 gossip_session_send_composing (gossip_app_get_session (),
962 priv->contact, TRUE);
966 priv->composing_stop_timeout_id = g_timeout_add (
967 1000 * COMPOSING_STOP_TIMEOUT,
968 (GSourceFunc) chat_composing_stop_timeout_cb,
973 chat_composing_stop (GossipChat *chat)
975 GossipChatPriv *priv;
977 priv = GET_PRIV (chat);
979 chat_composing_remove_timeout (chat);
981 gossip_session_send_composing (gossip_app_get_session (),
982 priv->contact, FALSE);*/
986 chat_composing_remove_timeout (GossipChat *chat)
988 GossipChatPriv *priv;
990 priv = GET_PRIV (chat);
992 if (priv->composing_stop_timeout_id) {
993 g_source_remove (priv->composing_stop_timeout_id);
994 priv->composing_stop_timeout_id = 0;
999 chat_composing_stop_timeout_cb (GossipChat *chat)
1001 GossipChatPriv *priv;
1003 priv = GET_PRIV (chat);
1005 priv->composing_stop_timeout_id = 0;
1007 gossip_session_send_composing (gossip_app_get_session (),
1008 priv->contact, FALSE);*/
1014 gossip_chat_get_is_command (const gchar *str)
1016 g_return_val_if_fail (str != NULL, FALSE);
1018 if (str[0] != '/') {
1022 if (g_str_has_prefix (str, "/me")) {
1025 else if (g_str_has_prefix (str, "/nick")) {
1028 else if (g_str_has_prefix (str, "/topic")) {
1036 gossip_chat_correct_word (GossipChat *chat,
1039 const gchar *new_word)
1041 GtkTextBuffer *buffer;
1043 g_return_if_fail (chat != NULL);
1044 g_return_if_fail (new_word != NULL);
1046 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1048 gtk_text_buffer_delete (buffer, &start, &end);
1049 gtk_text_buffer_insert (buffer, &start,
1055 gossip_chat_get_name (GossipChat *chat)
1057 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1059 if (GOSSIP_CHAT_GET_CLASS (chat)->get_name) {
1060 return GOSSIP_CHAT_GET_CLASS (chat)->get_name (chat);
1067 gossip_chat_get_tooltip (GossipChat *chat)
1069 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1071 if (GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip) {
1072 return GOSSIP_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1079 gossip_chat_get_status_pixbuf (GossipChat *chat)
1081 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1083 if (GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf) {
1084 return GOSSIP_CHAT_GET_CLASS (chat)->get_status_pixbuf (chat);
1091 gossip_chat_get_contact (GossipChat *chat)
1093 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1095 if (GOSSIP_CHAT_GET_CLASS (chat)->get_contact) {
1096 return GOSSIP_CHAT_GET_CLASS (chat)->get_contact (chat);
1102 gossip_chat_get_own_contact (GossipChat *chat)
1104 EmpathyContactManager *manager;
1106 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1108 manager = empathy_session_get_contact_manager ();
1110 return empathy_contact_manager_get_own (manager, chat->account);
1114 gossip_chat_get_widget (GossipChat *chat)
1116 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), NULL);
1118 if (GOSSIP_CHAT_GET_CLASS (chat)->get_widget) {
1119 return GOSSIP_CHAT_GET_CLASS (chat)->get_widget (chat);
1126 gossip_chat_is_group_chat (GossipChat *chat)
1128 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1130 if (GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat) {
1131 return GOSSIP_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1138 gossip_chat_is_connected (GossipChat *chat)
1140 GossipChatPriv *priv;
1142 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1144 priv = GET_PRIV (chat);
1146 return (priv->tp_chat != NULL);
1150 gossip_chat_get_show_contacts (GossipChat *chat)
1152 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1154 if (GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts) {
1155 return GOSSIP_CHAT_GET_CLASS (chat)->get_show_contacts (chat);
1162 gossip_chat_set_show_contacts (GossipChat *chat,
1165 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1167 if (GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts) {
1168 GOSSIP_CHAT_GET_CLASS (chat)->set_show_contacts (chat, show);
1173 gossip_chat_save_geometry (GossipChat *chat,
1179 gossip_geometry_save (gossip_chat_get_id (chat), x, y, w, h);
1183 gossip_chat_load_geometry (GossipChat *chat,
1189 gossip_geometry_load (gossip_chat_get_id (chat), x, y, w, h);
1193 gossip_chat_set_tp_chat (GossipChat *chat,
1194 EmpathyTpChat *tp_chat)
1196 GossipChatPriv *priv;
1199 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1200 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1202 priv = GET_PRIV (chat);
1204 if (tp_chat == priv->tp_chat) {
1208 if (priv->tp_chat) {
1209 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1210 chat_message_received_cb,
1212 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1215 g_object_unref (priv->tp_chat);
1219 priv->tp_chat = g_object_ref (tp_chat);
1220 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1222 g_signal_connect (tp_chat, "message-received",
1223 G_CALLBACK (chat_message_received_cb),
1225 g_signal_connect (tp_chat, "destroy",
1226 G_CALLBACK (chat_destroy_cb),
1229 empathy_tp_chat_request_pending (tp_chat);
1231 if (!priv->sensitive) {
1232 widget = gossip_chat_get_widget (chat);
1233 gtk_widget_set_sensitive (widget, TRUE);
1234 gossip_chat_view_append_event (chat->view, _("Connected"));
1235 priv->sensitive = TRUE;
1240 gossip_chat_get_id (GossipChat *chat)
1242 GossipChatPriv *priv;
1244 priv = GET_PRIV (chat);
1250 gossip_chat_clear (GossipChat *chat)
1252 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1254 gossip_chat_view_clear (chat->view);
1258 gossip_chat_set_window (GossipChat *chat,
1259 GossipChatWindow *window)
1261 GossipChatPriv *priv;
1263 priv = GET_PRIV (chat);
1264 priv->window = window;
1268 gossip_chat_get_window (GossipChat *chat)
1270 GossipChatPriv *priv;
1272 priv = GET_PRIV (chat);
1274 return priv->window;
1278 gossip_chat_scroll_down (GossipChat *chat)
1280 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1282 gossip_chat_view_scroll_down (chat->view);
1286 gossip_chat_cut (GossipChat *chat)
1288 GtkTextBuffer *buffer;
1290 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1292 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1293 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1294 GtkClipboard *clipboard;
1296 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1298 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1303 gossip_chat_copy (GossipChat *chat)
1305 GtkTextBuffer *buffer;
1307 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1309 if (gossip_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1310 gossip_chat_view_copy_clipboard (chat->view);
1314 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1315 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1316 GtkClipboard *clipboard;
1318 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1320 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1325 gossip_chat_paste (GossipChat *chat)
1327 GtkTextBuffer *buffer;
1328 GtkClipboard *clipboard;
1330 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1332 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1333 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1335 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1339 gossip_chat_present (GossipChat *chat)
1341 GossipChatPriv *priv;
1343 g_return_if_fail (GOSSIP_IS_CHAT (chat));
1345 priv = GET_PRIV (chat);
1347 if (priv->window == NULL) {
1348 GossipChatWindow *window;
1350 window = gossip_chat_window_get_default ();
1352 window = gossip_chat_window_new ();
1355 gossip_chat_window_add_chat (window, chat);
1358 gossip_chat_window_switch_to_chat (priv->window, chat);
1359 gossip_window_present (
1360 GTK_WINDOW (gossip_chat_window_get_dialog (priv->window)),
1363 gtk_widget_grab_focus (chat->input_text_view);
1367 gossip_chat_should_play_sound (GossipChat *chat)
1369 GossipChatWindow *window;
1370 gboolean play = TRUE;
1372 g_return_val_if_fail (GOSSIP_IS_CHAT (chat), FALSE);
1374 window = gossip_chat_get_window (GOSSIP_CHAT (chat));
1379 play = !gossip_chat_window_has_focus (window);
1385 gossip_chat_should_highlight_nick (GossipMessage *message)
1387 GossipContact *my_contact;
1388 const gchar *msg, *to;
1389 gchar *cf_msg, *cf_to;
1393 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), FALSE);
1395 gossip_debug (DEBUG_DOMAIN, "Highlighting nickname");
1399 msg = gossip_message_get_body (message);
1404 my_contact = gossip_get_own_contact_from_contact (gossip_message_get_sender (message));
1405 to = gossip_contact_get_name (my_contact);
1410 cf_msg = g_utf8_casefold (msg, -1);
1411 cf_to = g_utf8_casefold (to, -1);
1413 ch = strstr (cf_msg, cf_to);
1419 /* Not first in the message */
1420 if ((*(ch - 1) != ' ') &&
1421 (*(ch - 1) != ',') &&
1422 (*(ch - 1) != '.')) {
1427 ch = ch + strlen (cf_to);
1428 if (ch >= cf_msg + strlen (cf_msg)) {