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>
37 #include <libempathy/empathy-contact-manager.h>
38 #include <libempathy/empathy-log-manager.h>
39 #include <libempathy/empathy-debug.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-conf.h>
42 #include <libempathy/empathy-marshal.h>
44 #include "empathy-chat.h"
45 #include "empathy-chat-window.h"
46 #include "empathy-geometry.h"
47 #include "empathy-preferences.h"
48 #include "empathy-spell.h"
49 #include "empathy-spell-dialog.h"
50 #include "empathy-ui-utils.h"
52 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT, EmpathyChatPriv))
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 _EmpathyChatPriv {
66 EmpathyContactManager *manager;
67 EmpathyLogManager *log_manager;
68 EmpathyTpChat *tp_chat;
69 EmpathyChatWindow *window;
70 guint composing_stop_timeout_id;
73 GSList *sent_messages;
74 gint sent_messages_index;
77 gboolean first_tp_chat;
78 time_t last_log_timestamp;
79 /* Used to automatically shrink a window that has temporarily
80 * grown due to long input.
83 gint default_window_height;
84 gint last_input_height;
85 gboolean vscroll_visible;
96 static void empathy_chat_class_init (EmpathyChatClass *klass);
97 static void empathy_chat_init (EmpathyChat *chat);
98 static void chat_finalize (GObject *object);
99 static void chat_destroy_cb (EmpathyTpChat *tp_chat,
101 static void chat_send (EmpathyChat *chat,
103 static void chat_input_text_view_send (EmpathyChat *chat);
104 static void chat_message_received_cb (EmpathyTpChat *tp_chat,
105 EmpathyMessage *message,
107 static void chat_send_error_cb (EmpathyTpChat *tp_chat,
108 EmpathyMessage *message,
109 TelepathyChannelTextSendError error_code,
111 void chat_sent_message_add (EmpathyChat *chat,
113 const gchar * chat_sent_message_get_next (EmpathyChat *chat);
114 const gchar * chat_sent_message_get_last (EmpathyChat *chat);
115 static gboolean chat_input_key_press_event_cb (GtkWidget *widget,
118 static void chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
120 static gboolean chat_text_view_focus_in_event_cb (GtkWidget *widget,
123 static void chat_text_view_scroll_hide_cb (GtkWidget *widget,
125 static void chat_text_view_size_allocate_cb (GtkWidget *widget,
126 GtkAllocation *allocation,
128 static void chat_text_view_realize_cb (GtkWidget *widget,
130 static void chat_text_populate_popup_cb (GtkTextView *view,
133 static void chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
134 EmpathyChatSpell *chat_spell);
135 static EmpathyChatSpell *chat_spell_new (EmpathyChat *chat,
139 static void chat_spell_free (EmpathyChatSpell *chat_spell);
140 static void chat_composing_start (EmpathyChat *chat);
141 static void chat_composing_stop (EmpathyChat *chat);
142 static void chat_composing_remove_timeout (EmpathyChat *chat);
143 static gboolean chat_composing_stop_timeout_cb (EmpathyChat *chat);
144 static void chat_state_changed_cb (EmpathyTpChat *tp_chat,
145 EmpathyContact *contact,
146 TelepathyChannelChatState state,
148 static void chat_add_logs (EmpathyChat *chat);
149 static gboolean chat_scroll_down_idle_func (EmpathyChat *chat);
159 static guint signals[LAST_SIGNAL] = { 0 };
161 G_DEFINE_TYPE (EmpathyChat, empathy_chat, G_TYPE_OBJECT);
164 empathy_chat_class_init (EmpathyChatClass *klass)
166 GObjectClass *object_class;
168 object_class = G_OBJECT_CLASS (klass);
170 object_class->finalize = chat_finalize;
173 g_signal_new ("composing",
174 G_OBJECT_CLASS_TYPE (object_class),
178 g_cclosure_marshal_VOID__BOOLEAN,
182 signals[NEW_MESSAGE] =
183 g_signal_new ("new-message",
184 G_OBJECT_CLASS_TYPE (object_class),
188 empathy_marshal_VOID__OBJECT_BOOLEAN,
190 2, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN);
192 signals[NAME_CHANGED] =
193 g_signal_new ("name-changed",
194 G_OBJECT_CLASS_TYPE (object_class),
198 g_cclosure_marshal_VOID__POINTER,
202 signals[STATUS_CHANGED] =
203 g_signal_new ("status-changed",
204 G_OBJECT_CLASS_TYPE (object_class),
208 g_cclosure_marshal_VOID__VOID,
212 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
216 empathy_chat_init (EmpathyChat *chat)
218 EmpathyChatPriv *priv;
219 GtkTextBuffer *buffer;
221 chat->view = empathy_chat_view_new ();
222 chat->input_text_view = gtk_text_view_new ();
224 chat->is_first_char = TRUE;
226 g_object_set (chat->input_text_view,
227 "pixels-above-lines", 2,
228 "pixels-below-lines", 2,
229 "pixels-inside-wrap", 1,
232 "wrap-mode", GTK_WRAP_WORD_CHAR,
235 priv = GET_PRIV (chat);
237 priv->manager = empathy_contact_manager_new ();
238 priv->log_manager = empathy_log_manager_new ();
239 priv->default_window_height = -1;
240 priv->vscroll_visible = FALSE;
241 priv->sensitive = TRUE;
242 priv->sent_messages = NULL;
243 priv->sent_messages_index = -1;
244 priv->first_tp_chat = TRUE;
246 g_signal_connect (chat->input_text_view,
248 G_CALLBACK (chat_input_key_press_event_cb),
251 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
252 g_signal_connect (buffer,
254 G_CALLBACK (chat_input_text_buffer_changed_cb),
256 g_signal_connect (chat->view,
258 G_CALLBACK (chat_text_view_focus_in_event_cb),
261 g_signal_connect (chat->input_text_view,
263 G_CALLBACK (chat_text_view_size_allocate_cb),
266 g_signal_connect (chat->input_text_view,
268 G_CALLBACK (chat_text_view_realize_cb),
271 g_signal_connect (GTK_TEXT_VIEW (chat->input_text_view),
273 G_CALLBACK (chat_text_populate_popup_cb),
276 /* create misspelt words identification tag */
277 gtk_text_buffer_create_tag (buffer,
279 "underline", PANGO_UNDERLINE_ERROR,
284 chat_finalize (GObject *object)
287 EmpathyChatPriv *priv;
289 chat = EMPATHY_CHAT (object);
290 priv = GET_PRIV (chat);
292 empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
294 g_slist_foreach (priv->sent_messages, (GFunc) g_free, NULL);
295 g_slist_free (priv->sent_messages);
297 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
298 g_list_free (priv->compositors);
300 chat_composing_remove_timeout (chat);
301 g_object_unref (chat->account);
302 g_object_unref (priv->manager);
303 g_object_unref (priv->log_manager);
306 g_object_unref (priv->tp_chat);
309 if (priv->scroll_idle_id) {
310 g_source_remove (priv->scroll_idle_id);
315 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
319 chat_destroy_cb (EmpathyTpChat *tp_chat,
322 EmpathyChatPriv *priv;
324 priv = GET_PRIV (chat);
327 g_object_unref (priv->tp_chat);
328 priv->tp_chat = NULL;
330 priv->sensitive = FALSE;
332 empathy_chat_view_append_event (chat->view, _("Disconnected"));
333 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
335 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
336 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, NULL);
341 chat_send (EmpathyChat *chat,
344 EmpathyChatPriv *priv;
345 EmpathyMessage *message;
347 priv = GET_PRIV (chat);
349 if (G_STR_EMPTY (msg)) {
353 chat_sent_message_add (chat, msg);
355 if (g_str_has_prefix (msg, "/clear")) {
356 empathy_chat_view_clear (chat->view);
360 /* FIXME: add here something to let group/privrate chat handle
361 * some special messages */
363 message = empathy_message_new (msg);
365 empathy_tp_chat_send (priv->tp_chat, message);
367 g_object_unref (message);
371 chat_input_text_view_send (EmpathyChat *chat)
373 EmpathyChatPriv *priv;
374 GtkTextBuffer *buffer;
375 GtkTextIter start, end;
378 priv = GET_PRIV (chat);
380 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
382 gtk_text_buffer_get_bounds (buffer, &start, &end);
383 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
385 /* clear the input field */
386 gtk_text_buffer_set_text (buffer, "", -1);
388 chat_send (chat, msg);
392 chat->is_first_char = TRUE;
396 chat_message_received_cb (EmpathyTpChat *tp_chat,
397 EmpathyMessage *message,
400 EmpathyChatPriv *priv;
401 EmpathyContact *sender;
404 priv = GET_PRIV (chat);
406 timestamp = empathy_message_get_timestamp (message);
407 if (timestamp <= priv->last_log_timestamp) {
408 /* Do not take care of messages anterior of the last
409 * logged message. Some Jabber chatroom sends messages
410 * received before we joined the room, this avoid
411 * displaying those messages if we already logged them
412 * last time we joined that room. */
413 empathy_debug (DEBUG_DOMAIN, "Skipping message because it is "
414 "anterior of last logged message.");
418 sender = empathy_message_get_sender (message);
419 empathy_debug (DEBUG_DOMAIN, "Appending message ('%s')",
420 empathy_contact_get_name (sender));
422 empathy_log_manager_add_message (priv->log_manager,
423 empathy_chat_get_id (chat),
424 empathy_chat_is_group_chat (chat),
427 empathy_chat_view_append_message (chat->view, message);
429 if (empathy_chat_should_play_sound (chat)) {
430 // FIXME: empathy_sound_play (EMPATHY_SOUND_CHAT);
433 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, FALSE);
437 chat_send_error_cb (EmpathyTpChat *tp_chat,
438 EmpathyMessage *message,
439 TelepathyChannelTextSendError error_code,
445 switch (error_code) {
446 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
447 error = _("offline");
449 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
450 error = _("invalid contact");
452 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
453 error = _("permission denied");
455 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
456 error = _("too long message");
458 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
459 error = _("not implemented");
462 error = _("unknown");
466 str = g_strdup_printf (_("Error sending message '%s': %s"),
467 empathy_message_get_body (message),
469 empathy_chat_view_append_event (chat->view, str);
474 chat_sent_message_add (EmpathyChat *chat,
477 EmpathyChatPriv *priv;
481 priv = GET_PRIV (chat);
483 /* Save the sent message in our repeat buffer */
484 list = priv->sent_messages;
486 /* Remove any other occurances of this msg */
487 while ((item = g_slist_find_custom (list, str, (GCompareFunc) strcmp)) != NULL) {
488 list = g_slist_remove_link (list, item);
490 g_slist_free1 (item);
493 /* Trim the list to the last 10 items */
494 while (g_slist_length (list) > 10) {
495 item = g_slist_last (list);
497 list = g_slist_remove_link (list, item);
499 g_slist_free1 (item);
503 /* Add new message */
504 list = g_slist_prepend (list, g_strdup (str));
506 /* Set list and reset the index */
507 priv->sent_messages = list;
508 priv->sent_messages_index = -1;
512 chat_sent_message_get_next (EmpathyChat *chat)
514 EmpathyChatPriv *priv;
517 priv = GET_PRIV (chat);
519 if (!priv->sent_messages) {
520 empathy_debug (DEBUG_DOMAIN,
521 "No sent messages, next message is NULL");
525 max = g_slist_length (priv->sent_messages) - 1;
527 if (priv->sent_messages_index < max) {
528 priv->sent_messages_index++;
531 empathy_debug (DEBUG_DOMAIN,
532 "Returning next message index:%d",
533 priv->sent_messages_index);
535 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
539 chat_sent_message_get_last (EmpathyChat *chat)
541 EmpathyChatPriv *priv;
543 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
545 priv = GET_PRIV (chat);
547 if (!priv->sent_messages) {
548 empathy_debug (DEBUG_DOMAIN,
549 "No sent messages, last message is NULL");
553 if (priv->sent_messages_index >= 0) {
554 priv->sent_messages_index--;
557 empathy_debug (DEBUG_DOMAIN,
558 "Returning last message index:%d",
559 priv->sent_messages_index);
561 return g_slist_nth_data (priv->sent_messages, priv->sent_messages_index);
565 chat_input_key_press_event_cb (GtkWidget *widget,
569 EmpathyChatPriv *priv;
572 GtkWidget *text_view_sw;
574 priv = GET_PRIV (chat);
576 if (event->keyval == GDK_Tab && !(event->state & GDK_CONTROL_MASK)) {
580 /* Catch ctrl+up/down so we can traverse messages we sent */
581 if ((event->state & GDK_CONTROL_MASK) &&
582 (event->keyval == GDK_Up ||
583 event->keyval == GDK_Down)) {
584 GtkTextBuffer *buffer;
587 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
589 if (event->keyval == GDK_Up) {
590 str = chat_sent_message_get_next (chat);
592 str = chat_sent_message_get_last (chat);
595 g_signal_handlers_block_by_func (buffer,
596 chat_input_text_buffer_changed_cb,
598 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
599 g_signal_handlers_unblock_by_func (buffer,
600 chat_input_text_buffer_changed_cb,
606 /* Catch enter but not ctrl/shift-enter */
607 if (IS_ENTER (event->keyval) && !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
610 /* This is to make sure that kinput2 gets the enter. And if
611 * it's handled there we shouldn't send on it. This is because
612 * kinput2 uses Enter to commit letters. See:
613 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
616 view = GTK_TEXT_VIEW (chat->input_text_view);
617 if (gtk_im_context_filter_keypress (view->im_context, event)) {
618 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
622 chat_input_text_view_send (chat);
626 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
628 if (IS_ENTER (event->keyval) && (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
629 /* Newline for shift-enter. */
632 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
633 event->keyval == GDK_Page_Up) {
634 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
635 gtk_adjustment_set_value (adj, adj->value - adj->page_size);
639 else if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
640 event->keyval == GDK_Page_Down) {
641 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
642 val = MIN (adj->value + adj->page_size, adj->upper - adj->page_size);
643 gtk_adjustment_set_value (adj, val);
652 chat_text_view_focus_in_event_cb (GtkWidget *widget,
656 gtk_widget_grab_focus (chat->input_text_view);
662 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
665 EmpathyChatPriv *priv;
666 GtkTextIter start, end;
668 gboolean spell_checker = FALSE;
670 priv = GET_PRIV (chat);
672 if (gtk_text_buffer_get_char_count (buffer) == 0) {
673 chat_composing_stop (chat);
675 chat_composing_start (chat);
678 empathy_conf_get_bool (empathy_conf_get (),
679 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
682 if (chat->is_first_char) {
686 GtkAllocation *allocation;
688 /* Save the window's size */
689 dialog = empathy_chat_window_get_dialog (priv->window);
690 gtk_window_get_size (GTK_WINDOW (dialog),
691 NULL, &window_height);
693 gtk_widget_size_request (chat->input_text_view, &req);
695 allocation = >K_WIDGET (chat->view)->allocation;
697 priv->default_window_height = window_height;
698 priv->last_input_height = req.height;
699 priv->padding_height = window_height - req.height - allocation->height;
701 chat->is_first_char = FALSE;
704 gtk_text_buffer_get_start_iter (buffer, &start);
706 if (!spell_checker) {
707 gtk_text_buffer_get_end_iter (buffer, &end);
708 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
712 if (!empathy_spell_supported ()) {
716 /* NOTE: this is really inefficient, we shouldn't have to
717 reiterate the whole buffer each time and check each work
720 gboolean correct = FALSE;
723 if (gtk_text_iter_is_start (&start)) {
726 if (!gtk_text_iter_forward_word_end (&end)) {
727 /* no whole word yet */
731 if (!gtk_text_iter_forward_word_end (&end)) {
732 /* must be the end of the buffer */
737 gtk_text_iter_backward_word_start (&start);
740 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
742 /* spell check string */
743 if (!empathy_chat_get_is_command (str)) {
744 correct = empathy_spell_check (str);
750 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
752 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
757 /* set start iter to the end iters position */
769 chat_change_size_in_idle_cb (ChangeSizeData *data)
771 gtk_window_resize (GTK_WINDOW (data->window),
772 data->width, data->height);
778 chat_text_view_scroll_hide_cb (GtkWidget *widget,
781 EmpathyChatPriv *priv;
784 priv = GET_PRIV (chat);
786 priv->vscroll_visible = FALSE;
787 g_signal_handlers_disconnect_by_func (widget, chat_text_view_scroll_hide_cb, chat);
789 sw = gtk_widget_get_parent (chat->input_text_view);
790 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
793 g_object_set (sw, "height-request", -1, NULL);
797 chat_text_view_size_allocate_cb (GtkWidget *widget,
798 GtkAllocation *allocation,
801 EmpathyChatPriv *priv;
804 ChangeSizeData *data;
807 GtkAllocation *view_allocation;
812 priv = GET_PRIV (chat);
814 if (priv->default_window_height <= 0) {
818 sw = gtk_widget_get_parent (widget);
819 if (sw->allocation.height >= MAX_INPUT_HEIGHT && !priv->vscroll_visible) {
822 priv->vscroll_visible = TRUE;
823 gtk_widget_set_size_request (sw, sw->allocation.width, MAX_INPUT_HEIGHT);
824 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
826 GTK_POLICY_AUTOMATIC);
827 vscroll = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (sw));
828 g_signal_connect (vscroll, "hide",
829 G_CALLBACK (chat_text_view_scroll_hide_cb),
833 if (priv->last_input_height <= allocation->height) {
834 priv->last_input_height = allocation->height;
838 diff = priv->last_input_height - allocation->height;
839 priv->last_input_height = allocation->height;
841 view_allocation = >K_WIDGET (chat->view)->allocation;
843 dialog = empathy_chat_window_get_dialog (priv->window);
844 gtk_window_get_size (GTK_WINDOW (dialog), NULL, ¤t_height);
846 new_height = view_allocation->height + priv->padding_height + allocation->height - diff;
848 if (new_height <= priv->default_window_height) {
849 window_height = priv->default_window_height;
851 window_height = new_height;
854 if (current_height <= window_height) {
858 /* Restore the window's size */
859 gtk_window_get_size (GTK_WINDOW (dialog), &width, NULL);
861 data = g_new0 (ChangeSizeData, 1);
862 data->window = dialog;
864 data->height = window_height;
866 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
867 (GSourceFunc) chat_change_size_in_idle_cb,
872 chat_text_view_realize_cb (GtkWidget *widget,
875 empathy_debug (DEBUG_DOMAIN, "Setting focus to the input text view");
876 gtk_widget_grab_focus (widget);
880 chat_insert_smiley_activate_cb (GtkWidget *menuitem,
883 GtkTextBuffer *buffer;
887 smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
889 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
891 gtk_text_buffer_get_end_iter (buffer, &iter);
892 gtk_text_buffer_insert (buffer, &iter, smiley, -1);
894 gtk_text_buffer_get_end_iter (buffer, &iter);
895 gtk_text_buffer_insert (buffer, &iter, " ", -1);
899 chat_text_populate_popup_cb (GtkTextView *view,
903 EmpathyChatPriv *priv;
904 GtkTextBuffer *buffer;
905 GtkTextTagTable *table;
908 GtkTextIter iter, start, end;
911 EmpathyChatSpell *chat_spell;
912 GtkWidget *smiley_menu;
914 priv = GET_PRIV (chat);
916 /* Add the emoticon menu. */
917 item = gtk_separator_menu_item_new ();
918 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
919 gtk_widget_show (item);
921 item = gtk_menu_item_new_with_mnemonic (_("Insert Smiley"));
922 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
923 gtk_widget_show (item);
925 smiley_menu = empathy_chat_view_get_smiley_menu (
926 G_CALLBACK (chat_insert_smiley_activate_cb),
928 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
930 /* Add the spell check menu item. */
931 buffer = gtk_text_view_get_buffer (view);
932 table = gtk_text_buffer_get_tag_table (buffer);
934 tag = gtk_text_tag_table_lookup (table, "misspelled");
936 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
938 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
939 GTK_TEXT_WINDOW_WIDGET,
943 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
947 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
948 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
950 str = gtk_text_buffer_get_text (buffer,
951 &start, &end, FALSE);
954 if (G_STR_EMPTY (str)) {
958 chat_spell = chat_spell_new (chat, str, start, end);
960 g_object_set_data_full (G_OBJECT (menu),
961 "chat_spell", chat_spell,
962 (GDestroyNotify) chat_spell_free);
964 item = gtk_separator_menu_item_new ();
965 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
966 gtk_widget_show (item);
968 item = gtk_menu_item_new_with_mnemonic (_("_Check Word Spelling..."));
969 g_signal_connect (item,
971 G_CALLBACK (chat_text_check_word_spelling_cb),
973 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
974 gtk_widget_show (item);
978 chat_text_check_word_spelling_cb (GtkMenuItem *menuitem,
979 EmpathyChatSpell *chat_spell)
981 empathy_spell_dialog_show (chat_spell->chat,
987 static EmpathyChatSpell *
988 chat_spell_new (EmpathyChat *chat,
993 EmpathyChatSpell *chat_spell;
995 chat_spell = g_new0 (EmpathyChatSpell, 1);
997 chat_spell->chat = g_object_ref (chat);
998 chat_spell->word = g_strdup (word);
999 chat_spell->start = start;
1000 chat_spell->end = end;
1006 chat_spell_free (EmpathyChatSpell *chat_spell)
1008 g_object_unref (chat_spell->chat);
1009 g_free (chat_spell->word);
1010 g_free (chat_spell);
1014 chat_composing_start (EmpathyChat *chat)
1016 EmpathyChatPriv *priv;
1018 priv = GET_PRIV (chat);
1020 if (priv->composing_stop_timeout_id) {
1021 /* Just restart the timeout */
1022 chat_composing_remove_timeout (chat);
1024 empathy_tp_chat_set_state (priv->tp_chat,
1025 TP_CHANNEL_CHAT_STATE_COMPOSING);
1028 priv->composing_stop_timeout_id = g_timeout_add_seconds (
1029 COMPOSING_STOP_TIMEOUT,
1030 (GSourceFunc) chat_composing_stop_timeout_cb,
1035 chat_composing_stop (EmpathyChat *chat)
1037 EmpathyChatPriv *priv;
1039 priv = GET_PRIV (chat);
1041 chat_composing_remove_timeout (chat);
1042 empathy_tp_chat_set_state (priv->tp_chat,
1043 TP_CHANNEL_CHAT_STATE_ACTIVE);
1047 chat_composing_remove_timeout (EmpathyChat *chat)
1049 EmpathyChatPriv *priv;
1051 priv = GET_PRIV (chat);
1053 if (priv->composing_stop_timeout_id) {
1054 g_source_remove (priv->composing_stop_timeout_id);
1055 priv->composing_stop_timeout_id = 0;
1060 chat_composing_stop_timeout_cb (EmpathyChat *chat)
1062 EmpathyChatPriv *priv;
1064 priv = GET_PRIV (chat);
1066 priv->composing_stop_timeout_id = 0;
1067 empathy_tp_chat_set_state (priv->tp_chat,
1068 TP_CHANNEL_CHAT_STATE_PAUSED);
1074 chat_state_changed_cb (EmpathyTpChat *tp_chat,
1075 EmpathyContact *contact,
1076 TelepathyChannelChatState state,
1079 EmpathyChatPriv *priv;
1081 gboolean was_composing;
1083 priv = GET_PRIV (chat);
1085 if (empathy_contact_is_user (contact)) {
1086 /* We don't care about our own chat state */
1090 was_composing = (priv->compositors != NULL);
1092 /* Find the contact in the list. After that l is the list elem or NULL */
1093 for (l = priv->compositors; l; l = l->next) {
1094 if (empathy_contact_equal (contact, l->data)) {
1100 case TP_CHANNEL_CHAT_STATE_GONE:
1101 case TP_CHANNEL_CHAT_STATE_INACTIVE:
1102 case TP_CHANNEL_CHAT_STATE_PAUSED:
1103 case TP_CHANNEL_CHAT_STATE_ACTIVE:
1104 /* Contact is not composing */
1106 priv->compositors = g_list_remove_link (priv->compositors, l);
1107 g_object_unref (l->data);
1111 case TP_CHANNEL_CHAT_STATE_COMPOSING:
1112 /* Contact is composing */
1114 priv->compositors = g_list_prepend (priv->compositors,
1115 g_object_ref (contact));
1119 g_assert_not_reached ();
1122 empathy_debug (DEBUG_DOMAIN, "Was composing: %s now composing: %s",
1123 was_composing ? "yes" : "no",
1124 priv->compositors ? "yes" : "no");
1126 if ((was_composing && !priv->compositors) ||
1127 (!was_composing && priv->compositors)) {
1128 /* Composing state changed */
1129 g_signal_emit (chat, signals[COMPOSING], 0,
1130 priv->compositors != NULL);
1135 chat_add_logs (EmpathyChat *chat)
1137 EmpathyChatPriv *priv;
1138 GList *messages, *l;
1142 priv = GET_PRIV (chat);
1144 /* Turn off scrolling temporarily */
1145 empathy_chat_view_scroll (chat->view, FALSE);
1147 /* Add messages from last conversation */
1148 messages = empathy_log_manager_get_last_messages (priv->log_manager,
1150 empathy_chat_get_id (chat),
1151 empathy_chat_is_group_chat (chat));
1152 num_messages = g_list_length (messages);
1154 for (l = messages, i = 0; l; l = l->next, i++) {
1155 EmpathyMessage *message;
1159 /* Only add 10 last messages */
1160 if (num_messages - i > 10) {
1161 g_object_unref (message);
1165 priv->last_log_timestamp = empathy_message_get_timestamp (message);
1166 empathy_chat_view_append_message (chat->view, message);
1168 g_object_unref (message);
1170 g_list_free (messages);
1172 /* Turn back on scrolling */
1173 empathy_chat_view_scroll (chat->view, TRUE);
1175 /* Scroll to the most recent messages, we reference the chat
1176 * for the duration of the scroll func.
1178 priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func,
1179 g_object_ref (chat));
1182 /* Scroll down after the back-log has been received. */
1184 chat_scroll_down_idle_func (EmpathyChat *chat)
1186 EmpathyChatPriv *priv;
1188 priv = GET_PRIV (chat);
1190 empathy_chat_scroll_down (chat);
1191 g_object_unref (chat);
1193 priv->scroll_idle_id = 0;
1199 empathy_chat_get_is_command (const gchar *str)
1201 g_return_val_if_fail (str != NULL, FALSE);
1203 if (str[0] != '/') {
1207 if (g_str_has_prefix (str, "/me")) {
1210 else if (g_str_has_prefix (str, "/nick")) {
1213 else if (g_str_has_prefix (str, "/topic")) {
1221 empathy_chat_correct_word (EmpathyChat *chat,
1224 const gchar *new_word)
1226 GtkTextBuffer *buffer;
1228 g_return_if_fail (chat != NULL);
1229 g_return_if_fail (new_word != NULL);
1231 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1233 gtk_text_buffer_delete (buffer, &start, &end);
1234 gtk_text_buffer_insert (buffer, &start,
1240 empathy_chat_get_name (EmpathyChat *chat)
1242 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1244 if (EMPATHY_CHAT_GET_CLASS (chat)->get_name) {
1245 return EMPATHY_CHAT_GET_CLASS (chat)->get_name (chat);
1252 empathy_chat_get_tooltip (EmpathyChat *chat)
1254 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1256 if (EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip) {
1257 return EMPATHY_CHAT_GET_CLASS (chat)->get_tooltip (chat);
1264 empathy_chat_get_status_icon_name (EmpathyChat *chat)
1266 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1268 if (EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name) {
1269 return EMPATHY_CHAT_GET_CLASS (chat)->get_status_icon_name (chat);
1276 empathy_chat_get_widget (EmpathyChat *chat)
1278 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1280 if (EMPATHY_CHAT_GET_CLASS (chat)->get_widget) {
1281 return EMPATHY_CHAT_GET_CLASS (chat)->get_widget (chat);
1288 empathy_chat_is_group_chat (EmpathyChat *chat)
1290 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1292 if (EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat) {
1293 return EMPATHY_CHAT_GET_CLASS (chat)->is_group_chat (chat);
1300 empathy_chat_is_connected (EmpathyChat *chat)
1302 EmpathyChatPriv *priv;
1304 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1306 priv = GET_PRIV (chat);
1308 return (priv->tp_chat != NULL);
1312 empathy_chat_save_geometry (EmpathyChat *chat,
1318 empathy_geometry_save (empathy_chat_get_id (chat), x, y, w, h);
1322 empathy_chat_load_geometry (EmpathyChat *chat,
1328 empathy_geometry_load (empathy_chat_get_id (chat), x, y, w, h);
1332 empathy_chat_set_tp_chat (EmpathyChat *chat,
1333 EmpathyTpChat *tp_chat)
1335 EmpathyChatPriv *priv;
1336 GList *messages, *l;
1338 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1339 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1341 priv = GET_PRIV (chat);
1343 if (tp_chat == priv->tp_chat) {
1347 if (priv->tp_chat) {
1348 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1349 chat_message_received_cb,
1351 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1354 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1357 g_object_unref (priv->tp_chat);
1361 priv->tp_chat = g_object_ref (tp_chat);
1362 priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
1364 if (priv->first_tp_chat) {
1365 chat_add_logs (chat);
1366 priv->first_tp_chat = FALSE;
1369 g_signal_connect (tp_chat, "message-received",
1370 G_CALLBACK (chat_message_received_cb),
1372 g_signal_connect (tp_chat, "send-error",
1373 G_CALLBACK (chat_send_error_cb),
1375 g_signal_connect (tp_chat, "chat-state-changed",
1376 G_CALLBACK (chat_state_changed_cb),
1378 g_signal_connect (tp_chat, "destroy",
1379 G_CALLBACK (chat_destroy_cb),
1382 /* Get pending messages */
1383 empathy_tp_chat_set_acknowledge (tp_chat, TRUE);
1384 messages = empathy_tp_chat_get_pendings (tp_chat);
1385 for (l = messages; l; l = l->next) {
1386 chat_message_received_cb (tp_chat, l->data, chat);
1387 g_object_unref (l->data);
1389 g_list_free (messages);
1391 if (!priv->sensitive) {
1392 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
1393 empathy_chat_view_append_event (chat->view, _("Connected"));
1394 priv->sensitive = TRUE;
1397 if (EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat) {
1398 EMPATHY_CHAT_GET_CLASS (chat)->set_tp_chat (chat, tp_chat);
1404 empathy_chat_get_id (EmpathyChat *chat)
1406 EmpathyChatPriv *priv;
1408 priv = GET_PRIV (chat);
1414 empathy_chat_clear (EmpathyChat *chat)
1416 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1418 empathy_chat_view_clear (chat->view);
1422 empathy_chat_set_window (EmpathyChat *chat,
1423 EmpathyChatWindow *window)
1425 EmpathyChatPriv *priv;
1427 priv = GET_PRIV (chat);
1428 priv->window = window;
1432 empathy_chat_get_window (EmpathyChat *chat)
1434 EmpathyChatPriv *priv;
1436 priv = GET_PRIV (chat);
1438 return priv->window;
1442 empathy_chat_scroll_down (EmpathyChat *chat)
1444 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1446 empathy_chat_view_scroll_down (chat->view);
1450 empathy_chat_cut (EmpathyChat *chat)
1452 GtkTextBuffer *buffer;
1454 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1456 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1457 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1458 GtkClipboard *clipboard;
1460 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1462 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
1467 empathy_chat_copy (EmpathyChat *chat)
1469 GtkTextBuffer *buffer;
1471 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1473 if (empathy_chat_view_get_selection_bounds (chat->view, NULL, NULL)) {
1474 empathy_chat_view_copy_clipboard (chat->view);
1478 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1479 if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
1480 GtkClipboard *clipboard;
1482 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1484 gtk_text_buffer_copy_clipboard (buffer, clipboard);
1489 empathy_chat_paste (EmpathyChat *chat)
1491 GtkTextBuffer *buffer;
1492 GtkClipboard *clipboard;
1494 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1496 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1497 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1499 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
1503 empathy_chat_present (EmpathyChat *chat)
1505 EmpathyChatPriv *priv;
1507 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1509 priv = GET_PRIV (chat);
1511 if (priv->window == NULL) {
1512 EmpathyChatWindow *window;
1514 window = empathy_chat_window_get_default ();
1516 window = empathy_chat_window_new ();
1519 empathy_chat_window_add_chat (window, chat);
1522 empathy_chat_window_switch_to_chat (priv->window, chat);
1523 empathy_window_present (
1524 GTK_WINDOW (empathy_chat_window_get_dialog (priv->window)),
1527 gtk_widget_grab_focus (chat->input_text_view);
1531 empathy_chat_should_play_sound (EmpathyChat *chat)
1533 EmpathyChatWindow *window;
1534 gboolean play = TRUE;
1536 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
1538 window = empathy_chat_get_window (chat);
1543 play = !empathy_chat_window_has_focus (window);
1549 empathy_chat_should_highlight_nick (EmpathyMessage *message)
1551 EmpathyContact *contact;
1552 const gchar *msg, *to;
1553 gchar *cf_msg, *cf_to;
1557 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
1559 empathy_debug (DEBUG_DOMAIN, "Highlighting nickname");
1563 msg = empathy_message_get_body (message);
1568 contact = empathy_message_get_receiver (message);
1569 if (!contact || !empathy_contact_is_user (contact)) {
1573 to = empathy_contact_get_name (contact);
1578 cf_msg = g_utf8_casefold (msg, -1);
1579 cf_to = g_utf8_casefold (to, -1);
1581 ch = strstr (cf_msg, cf_to);
1587 /* Not first in the message */
1588 if ((*(ch - 1) != ' ') &&
1589 (*(ch - 1) != ',') &&
1590 (*(ch - 1) != '.')) {
1595 ch = ch + strlen (cf_to);
1596 if (ch >= cf_msg + strlen (cf_msg)) {