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-2008 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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 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-lib.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-account-manager.h>
40 #include <libempathy/empathy-log-manager.h>
41 #include <libempathy/empathy-contact-list.h>
42 #include <libempathy/empathy-utils.h>
43 #include <libempathy/empathy-dispatcher.h>
45 #include "empathy-chat.h"
46 #include "empathy-conf.h"
47 #include "empathy-spell.h"
48 #include "empathy-contact-list-store.h"
49 #include "empathy-contact-list-view.h"
50 #include "empathy-contact-menu.h"
51 #include "empathy-theme-manager.h"
52 #include "empathy-smiley-manager.h"
53 #include "empathy-ui-utils.h"
55 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
56 #include <libempathy/empathy-debug.h>
58 #define CHAT_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
59 #define CHAT_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
60 #define IS_ENTER(v) (v == GDK_Return || v == GDK_ISO_Enter || v == GDK_KP_Enter)
61 #define MAX_INPUT_HEIGHT 150
62 #define COMPOSING_STOP_TIMEOUT 5
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
66 EmpathyTpChat *tp_chat;
67 EmpathyAccount *account;
71 EmpathyContact *remote_contact;
72 gboolean show_contacts;
74 EmpathyLogManager *log_manager;
75 EmpathyAccountManager *account_manager;
77 GList *input_history_current;
79 GCompletion *completion;
80 guint composing_stop_timeout_id;
81 guint block_events_timeout_id;
82 TpHandleType handle_type;
84 gboolean has_input_vscroll;
89 GtkWidget *scrolled_window_chat;
90 GtkWidget *scrolled_window_input;
91 GtkWidget *scrolled_window_contacts;
92 GtkWidget *hbox_topic;
93 GtkWidget *label_topic;
94 GtkWidget *contact_list_view;
98 gchar *text; /* Original message that was specified
99 * upon entry creation. */
100 gchar *modified_text; /* Message that was modified by user.
101 * When no modifications were made, it is NULL */
121 static guint signals[LAST_SIGNAL] = { 0 };
123 G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BIN);
126 chat_get_property (GObject *object,
131 EmpathyChat *chat = EMPATHY_CHAT (object);
132 EmpathyChatPriv *priv = GET_PRIV (object);
136 g_value_set_object (value, priv->tp_chat);
139 g_value_set_object (value, priv->account);
142 g_value_set_string (value, empathy_chat_get_name (chat));
145 g_value_set_string (value, priv->id);
148 g_value_set_string (value, priv->subject);
150 case PROP_REMOTE_CONTACT:
151 g_value_set_object (value, priv->remote_contact);
153 case PROP_SHOW_CONTACTS:
154 g_value_set_boolean (value, priv->show_contacts);
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
163 chat_set_property (GObject *object,
168 EmpathyChat *chat = EMPATHY_CHAT (object);
172 empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
174 case PROP_SHOW_CONTACTS:
175 empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
184 chat_connect_channel_reconnected (EmpathyDispatchOperation *dispatch,
188 EmpathyChat *chat = EMPATHY_CHAT (user_data);
189 EmpathyTpChat *tpchat;
192 empathy_chat_view_append_event (chat->view,
193 _("Failed to reconnect this chat"));
197 tpchat = EMPATHY_TP_CHAT (
198 empathy_dispatch_operation_get_channel_wrapper (dispatch));
200 if (empathy_dispatch_operation_claim (dispatch)) {
201 empathy_chat_set_tp_chat (chat, tpchat);
206 chat_new_connection_cb (EmpathyAccountManager *manager,
207 TpConnection *connection,
210 EmpathyChatPriv *priv = GET_PRIV (chat);
211 EmpathyAccount *account;
213 account = empathy_account_manager_get_account_for_connection (manager,
215 if (!priv->tp_chat && account == priv->account &&
216 priv->handle_type != TP_HANDLE_TYPE_NONE &&
217 !EMP_STR_EMPTY (priv->id)) {
219 DEBUG ("Account reconnected, request a new Text channel");
221 switch (priv->handle_type) {
222 case TP_HANDLE_TYPE_CONTACT:
223 empathy_dispatcher_chat_with_contact_id (
224 connection, priv->id,
225 chat_connect_channel_reconnected,
228 case TP_HANDLE_TYPE_ROOM:
229 empathy_dispatcher_join_muc (connection,
231 chat_connect_channel_reconnected,
235 g_assert_not_reached ();
242 chat_composing_remove_timeout (EmpathyChat *chat)
244 EmpathyChatPriv *priv;
246 priv = GET_PRIV (chat);
248 if (priv->composing_stop_timeout_id) {
249 g_source_remove (priv->composing_stop_timeout_id);
250 priv->composing_stop_timeout_id = 0;
255 chat_composing_stop_timeout_cb (EmpathyChat *chat)
257 EmpathyChatPriv *priv;
259 priv = GET_PRIV (chat);
261 priv->composing_stop_timeout_id = 0;
262 empathy_tp_chat_set_state (priv->tp_chat,
263 TP_CHANNEL_CHAT_STATE_PAUSED);
269 chat_composing_start (EmpathyChat *chat)
271 EmpathyChatPriv *priv;
273 priv = GET_PRIV (chat);
275 if (priv->composing_stop_timeout_id) {
276 /* Just restart the timeout */
277 chat_composing_remove_timeout (chat);
279 empathy_tp_chat_set_state (priv->tp_chat,
280 TP_CHANNEL_CHAT_STATE_COMPOSING);
283 priv->composing_stop_timeout_id = g_timeout_add_seconds (
284 COMPOSING_STOP_TIMEOUT,
285 (GSourceFunc) chat_composing_stop_timeout_cb,
290 chat_composing_stop (EmpathyChat *chat)
292 EmpathyChatPriv *priv;
294 priv = GET_PRIV (chat);
296 chat_composing_remove_timeout (chat);
297 empathy_tp_chat_set_state (priv->tp_chat,
298 TP_CHANNEL_CHAT_STATE_ACTIVE);
302 chat_input_history_entry_cmp (InputHistoryEntry *entry,
305 if (!tp_strdiff (entry->text, text)) {
306 if (entry->modified_text != NULL) {
307 /* Modified entry and single string cannot be equal. */
315 static InputHistoryEntry *
316 chat_input_history_entry_new_with_text (const gchar *text)
318 InputHistoryEntry *entry;
319 entry = g_slice_new0 (InputHistoryEntry);
320 entry->text = g_strdup (text);
326 chat_input_history_entry_free (InputHistoryEntry *entry)
328 g_free (entry->text);
329 g_free (entry->modified_text);
330 g_slice_free (InputHistoryEntry, entry);
334 chat_input_history_entry_revert (InputHistoryEntry *entry)
336 g_free (entry->modified_text);
337 entry->modified_text = NULL;
341 chat_input_history_entry_update_text (InputHistoryEntry *entry,
346 if (!tp_strdiff (text, entry->text)) {
347 g_free (entry->modified_text);
348 entry->modified_text = NULL;
352 old = entry->modified_text;
353 entry->modified_text = g_strdup (text);
358 chat_input_history_entry_get_text (InputHistoryEntry *entry)
364 if (entry->modified_text != NULL) {
365 return entry->modified_text;
371 chat_input_history_remove_item (GList *list,
374 list = g_list_remove_link (list, item);
375 chat_input_history_entry_free (item->data);
376 g_list_free_1 (item);
381 chat_input_history_revert (EmpathyChat *chat)
383 EmpathyChatPriv *priv;
387 InputHistoryEntry *entry;
389 priv = GET_PRIV (chat);
390 list = priv->input_history;
393 DEBUG ("No input history");
397 /* Delete temporary entry */
398 if (priv->input_history_current != NULL) {
400 list = chat_input_history_remove_item (list, item1);
401 if (priv->input_history_current == item1) {
402 /* Removed temporary entry was current entry */
403 priv->input_history = list;
404 priv->input_history_current = NULL;
409 /* There is no entry to revert */
413 /* Restore the current history entry to original value */
414 item1 = priv->input_history_current;
416 chat_input_history_entry_revert (entry);
418 /* Remove restored entry if there is other occurance before this entry */
419 item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
420 (GCompareFunc) chat_input_history_entry_cmp);
421 if (item2 != item1) {
422 list = chat_input_history_remove_item (list, item1);
425 /* Remove other occurance of the restored entry */
426 item2 = g_list_find_custom (item1->next,
427 chat_input_history_entry_get_text (entry),
428 (GCompareFunc) chat_input_history_entry_cmp);
430 list = chat_input_history_remove_item (list, item2);
434 priv->input_history_current = NULL;
435 priv->input_history = list;
439 chat_input_history_add (EmpathyChat *chat,
443 EmpathyChatPriv *priv;
446 InputHistoryEntry *entry;
448 priv = GET_PRIV (chat);
450 list = priv->input_history;
452 /* Remove any other occurances of this entry, if not temporary */
454 while ((item = g_list_find_custom (list, str,
455 (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
456 list = chat_input_history_remove_item (list, item);
459 /* Trim the list to the last 10 items */
460 while (g_list_length (list) > 10) {
461 item = g_list_last (list);
463 list = chat_input_history_remove_item (list, item);
471 entry = chat_input_history_entry_new_with_text (str);
472 list = g_list_prepend (list, entry);
474 /* Set the list and the current item pointer */
475 priv->input_history = list;
477 priv->input_history_current = list;
480 priv->input_history_current = NULL;
485 chat_input_history_get_next (EmpathyChat *chat)
487 EmpathyChatPriv *priv;
491 priv = GET_PRIV (chat);
493 if (priv->input_history == NULL) {
494 DEBUG ("No input history, next entry is NULL");
497 g_assert (priv->input_history_current != NULL);
499 if ((item = g_list_next (priv->input_history_current)) == NULL)
501 item = priv->input_history_current;
504 msg = chat_input_history_entry_get_text (item->data);
506 DEBUG ("Returning next entry: '%s'", msg);
508 priv->input_history_current = item;
514 chat_input_history_get_prev (EmpathyChat *chat)
516 EmpathyChatPriv *priv;
520 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
522 priv = GET_PRIV (chat);
524 if (priv->input_history == NULL) {
525 DEBUG ("No input history, previous entry is NULL");
529 if (priv->input_history_current == NULL)
533 else if ((item = g_list_previous (priv->input_history_current)) == NULL)
535 item = priv->input_history_current;
538 msg = chat_input_history_entry_get_text (item->data);
540 DEBUG ("Returning previous entry: '%s'", msg);
542 priv->input_history_current = item;
548 chat_input_history_update (EmpathyChat *chat,
549 GtkTextBuffer *buffer)
551 EmpathyChatPriv *priv;
552 GtkTextIter start, end;
554 InputHistoryEntry *entry;
556 priv = GET_PRIV (chat);
558 gtk_text_buffer_get_bounds (buffer, &start, &end);
559 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
561 if (priv->input_history_current == NULL) {
562 /* Add the current text temporarily to the history */
563 chat_input_history_add (chat, text, TRUE);
568 /* Save the changes in the history */
569 entry = priv->input_history_current->data;
570 if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
571 chat_input_history_entry_update_text (entry, text);
578 chat_send (EmpathyChat *chat,
581 EmpathyChatPriv *priv;
582 EmpathyMessage *message;
584 if (EMP_STR_EMPTY (msg)) {
588 priv = GET_PRIV (chat);
590 chat_input_history_add (chat, msg, FALSE);
592 if (strcmp (msg, "/clear") == 0) {
593 empathy_chat_view_clear (chat->view);
597 message = empathy_message_new_from_entry (msg);
599 if (message == NULL) {
600 empathy_chat_view_append_event (chat->view,
601 _("Unsupported command"));
603 empathy_tp_chat_send (priv->tp_chat, message);
604 g_object_unref (message);
609 chat_input_text_view_send (EmpathyChat *chat)
611 EmpathyChatPriv *priv;
612 GtkTextBuffer *buffer;
613 GtkTextIter start, end;
616 priv = GET_PRIV (chat);
618 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
620 gtk_text_buffer_get_bounds (buffer, &start, &end);
621 msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
623 /* clear the input field */
624 gtk_text_buffer_set_text (buffer, "", -1);
625 /* delete input history modifications */
626 chat_input_history_revert (chat);
628 chat_send (chat, msg);
633 chat_state_changed_cb (EmpathyTpChat *tp_chat,
634 EmpathyContact *contact,
635 TpChannelChatState state,
638 EmpathyChatPriv *priv;
640 gboolean was_composing;
642 priv = GET_PRIV (chat);
644 if (empathy_contact_is_user (contact)) {
645 /* We don't care about our own chat state */
649 was_composing = (priv->compositors != NULL);
651 /* Find the contact in the list. After that l is the list elem or NULL */
652 for (l = priv->compositors; l; l = l->next) {
653 if (contact == l->data) {
659 case TP_CHANNEL_CHAT_STATE_GONE:
660 case TP_CHANNEL_CHAT_STATE_INACTIVE:
661 case TP_CHANNEL_CHAT_STATE_PAUSED:
662 case TP_CHANNEL_CHAT_STATE_ACTIVE:
663 /* Contact is not composing */
665 priv->compositors = g_list_remove_link (priv->compositors, l);
666 g_object_unref (l->data);
670 case TP_CHANNEL_CHAT_STATE_COMPOSING:
671 /* Contact is composing */
673 priv->compositors = g_list_prepend (priv->compositors,
674 g_object_ref (contact));
678 g_assert_not_reached ();
681 DEBUG ("Was composing: %s now composing: %s",
682 was_composing ? "yes" : "no",
683 priv->compositors ? "yes" : "no");
685 if ((was_composing && !priv->compositors) ||
686 (!was_composing && priv->compositors)) {
687 /* Composing state changed */
688 g_signal_emit (chat, signals[COMPOSING], 0,
689 priv->compositors != NULL);
694 chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
696 EmpathyChatPriv *priv = GET_PRIV (chat);
697 EmpathyContact *sender;
699 sender = empathy_message_get_sender (message);
701 DEBUG ("Appending new message from %s (%d)",
702 empathy_contact_get_name (sender),
703 empathy_contact_get_handle (sender));
705 empathy_chat_view_append_message (chat->view, message);
707 /* We received a message so the contact is no longer composing */
708 chat_state_changed_cb (priv->tp_chat, sender,
709 TP_CHANNEL_CHAT_STATE_ACTIVE,
712 g_signal_emit (chat, signals[NEW_MESSAGE], 0, message);
716 chat_message_received_cb (EmpathyTpChat *tp_chat,
717 EmpathyMessage *message,
720 chat_message_received (chat, message);
721 empathy_tp_chat_acknowledge_message (tp_chat, message);
725 chat_send_error_cb (EmpathyTpChat *tp_chat,
726 const gchar *message_body,
727 TpChannelTextSendError error_code,
733 switch (error_code) {
734 case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
735 error = _("offline");
737 case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
738 error = _("invalid contact");
740 case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
741 error = _("permission denied");
743 case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
744 error = _("too long message");
746 case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
747 error = _("not implemented");
750 error = _("unknown");
754 str = g_strdup_printf (_("Error sending message '%s': %s"),
757 empathy_chat_view_append_event (chat->view, str);
762 chat_property_changed_cb (EmpathyTpChat *tp_chat,
767 EmpathyChatPriv *priv = GET_PRIV (chat);
769 if (!tp_strdiff (name, "subject")) {
770 g_free (priv->subject);
771 priv->subject = g_value_dup_string (value);
772 g_object_notify (G_OBJECT (chat), "subject");
774 if (EMP_STR_EMPTY (priv->subject)) {
775 gtk_widget_hide (priv->hbox_topic);
777 gtk_label_set_text (GTK_LABEL (priv->label_topic), priv->subject);
778 gtk_widget_show (priv->hbox_topic);
780 if (priv->block_events_timeout_id == 0) {
783 if (!EMP_STR_EMPTY (priv->subject)) {
784 str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
786 str = g_strdup (_("No topic defined"));
788 empathy_chat_view_append_event (EMPATHY_CHAT (chat)->view, str);
792 else if (!tp_strdiff (name, "name")) {
794 priv->name = g_value_dup_string (value);
795 g_object_notify (G_OBJECT (chat), "name");
800 chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
803 EmpathyChatPriv *priv;
804 GtkTextIter start, end;
806 gboolean spell_checker = FALSE;
808 priv = GET_PRIV (chat);
810 if (gtk_text_buffer_get_char_count (buffer) == 0) {
811 chat_composing_stop (chat);
813 chat_composing_start (chat);
816 empathy_conf_get_bool (empathy_conf_get (),
817 EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
820 gtk_text_buffer_get_start_iter (buffer, &start);
822 if (!spell_checker) {
823 gtk_text_buffer_get_end_iter (buffer, &end);
824 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
828 if (!empathy_spell_supported ()) {
832 /* NOTE: this is really inefficient, we shouldn't have to
833 reiterate the whole buffer each time and check each work
836 gboolean correct = FALSE;
839 if (gtk_text_iter_is_start (&start)) {
842 if (!gtk_text_iter_forward_word_end (&end)) {
843 /* no whole word yet */
847 if (!gtk_text_iter_forward_word_end (&end)) {
848 /* must be the end of the buffer */
853 gtk_text_iter_backward_word_start (&start);
856 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
858 /* spell check string if not a command */
860 correct = empathy_spell_check (str);
866 gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
868 gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
873 /* set start iter to the end iters position */
879 chat_input_key_press_event_cb (GtkWidget *widget,
883 EmpathyChatPriv *priv;
886 GtkWidget *text_view_sw;
888 priv = GET_PRIV (chat);
890 /* Catch ctrl+up/down so we can traverse messages we sent */
891 if ((event->state & GDK_CONTROL_MASK) &&
892 (event->keyval == GDK_Up ||
893 event->keyval == GDK_Down)) {
894 GtkTextBuffer *buffer;
897 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
898 chat_input_history_update (chat, buffer);
900 if (event->keyval == GDK_Up) {
901 str = chat_input_history_get_next (chat);
903 str = chat_input_history_get_prev (chat);
906 g_signal_handlers_block_by_func (buffer,
907 chat_input_text_buffer_changed_cb,
909 gtk_text_buffer_set_text (buffer, str ? str : "", -1);
910 g_signal_handlers_unblock_by_func (buffer,
911 chat_input_text_buffer_changed_cb,
917 /* Catch enter but not ctrl/shift-enter */
918 if (IS_ENTER (event->keyval) &&
919 !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
922 /* This is to make sure that kinput2 gets the enter. And if
923 * it's handled there we shouldn't send on it. This is because
924 * kinput2 uses Enter to commit letters. See:
925 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
928 view = GTK_TEXT_VIEW (chat->input_text_view);
929 if (gtk_im_context_filter_keypress (view->im_context, event)) {
930 GTK_TEXT_VIEW (chat->input_text_view)->need_im_reset = TRUE;
934 chat_input_text_view_send (chat);
938 text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));
940 if (IS_ENTER (event->keyval) &&
941 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
942 /* Newline for shift/control-enter. */
945 if (!(event->state & GDK_CONTROL_MASK) &&
946 event->keyval == GDK_Page_Up) {
947 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
948 gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
951 if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
952 event->keyval == GDK_Page_Down) {
953 adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
954 val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
955 gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
956 gtk_adjustment_set_value (adj, val);
959 if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
960 event->keyval == GDK_Tab) {
961 GtkTextBuffer *buffer;
962 GtkTextIter start, current;
963 gchar *nick, *completed;
964 GList *list, *completed_list;
965 gboolean is_start_of_buffer;
967 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
968 gtk_text_buffer_get_iter_at_mark (buffer, ¤t, gtk_text_buffer_get_insert (buffer));
970 /* Get the start of the nick to complete. */
971 gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
972 gtk_text_iter_backward_word_start (&start);
973 is_start_of_buffer = gtk_text_iter_is_start (&start);
975 list = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (priv->tp_chat));
976 g_completion_add_items (priv->completion, list);
978 nick = gtk_text_buffer_get_text (buffer, &start, ¤t, FALSE);
979 completed_list = g_completion_complete (priv->completion,
988 gchar *complete_char = NULL;
990 gtk_text_buffer_delete (buffer, &start, ¤t);
992 len = g_list_length (completed_list);
995 /* If we only have one hit, use that text
996 * instead of the text in completed since the
997 * completed text will use the typed string
998 * which might be cased all wrong.
1001 text = empathy_contact_get_name (completed_list->data);
1006 gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));
1008 if (len == 1 && is_start_of_buffer &&
1009 empathy_conf_get_string (empathy_conf_get (),
1010 EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR,
1012 complete_char != NULL) {
1013 gtk_text_buffer_insert_at_cursor (buffer,
1015 strlen (complete_char));
1016 gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
1017 g_free (complete_char);
1023 g_completion_clear_items (priv->completion);
1025 g_list_foreach (list, (GFunc) g_object_unref, NULL);
1035 chat_text_view_focus_in_event_cb (GtkWidget *widget,
1039 gtk_widget_grab_focus (chat->input_text_view);
1045 chat_input_set_size_request_idle (gpointer sw)
1047 gtk_widget_set_size_request (sw, -1, MAX_INPUT_HEIGHT);
1053 chat_input_size_request_cb (GtkWidget *widget,
1054 GtkRequisition *requisition,
1057 EmpathyChatPriv *priv = GET_PRIV (chat);
1060 sw = gtk_widget_get_parent (widget);
1061 if (requisition->height >= MAX_INPUT_HEIGHT && !priv->has_input_vscroll) {
1062 g_idle_add (chat_input_set_size_request_idle, sw);
1063 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1066 priv->has_input_vscroll = TRUE;
1069 if (requisition->height < MAX_INPUT_HEIGHT && priv->has_input_vscroll) {
1070 gtk_widget_set_size_request (sw, -1, -1);
1071 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
1074 priv->has_input_vscroll = FALSE;
1079 chat_input_realize_cb (GtkWidget *widget,
1082 DEBUG ("Setting focus to the input text view");
1083 gtk_widget_grab_focus (widget);
1087 chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
1088 EmpathySmiley *smiley,
1091 EmpathyChat *chat = EMPATHY_CHAT (user_data);
1092 GtkTextBuffer *buffer;
1095 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1097 gtk_text_buffer_get_end_iter (buffer, &iter);
1098 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
1100 gtk_text_buffer_get_end_iter (buffer, &iter);
1101 gtk_text_buffer_insert (buffer, &iter, " ", -1);
1112 static EmpathyChatSpell *
1113 chat_spell_new (EmpathyChat *chat,
1118 EmpathyChatSpell *chat_spell;
1120 chat_spell = g_slice_new0 (EmpathyChatSpell);
1122 chat_spell->chat = g_object_ref (chat);
1123 chat_spell->word = g_strdup (word);
1124 chat_spell->start = start;
1125 chat_spell->end = end;
1131 chat_spell_free (EmpathyChatSpell *chat_spell)
1133 g_object_unref (chat_spell->chat);
1134 g_free (chat_spell->word);
1135 g_slice_free (EmpathyChatSpell, chat_spell);
1139 chat_spelling_menu_activate_cb (GtkMenuItem *menu_item,
1140 EmpathyChatSpell *chat_spell)
1142 empathy_chat_correct_word (chat_spell->chat,
1143 &(chat_spell->start),
1145 gtk_menu_item_get_label (menu_item));
1149 chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
1151 GtkWidget *menu, *menu_item;
1152 GList *suggestions, *l;
1154 menu = gtk_menu_new ();
1155 suggestions = empathy_spell_get_suggestions (chat_spell->word);
1156 if (suggestions == NULL) {
1157 menu_item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
1158 gtk_widget_set_sensitive (menu_item, FALSE);
1159 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1161 for (l = suggestions; l; l = l->next) {
1162 menu_item = gtk_menu_item_new_with_label (l->data);
1163 g_signal_connect (G_OBJECT (menu_item),
1165 G_CALLBACK (chat_spelling_menu_activate_cb),
1167 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1170 empathy_spell_free_suggestions (suggestions);
1172 gtk_widget_show_all (menu);
1178 chat_text_send_cb (GtkMenuItem *menuitem,
1181 chat_input_text_view_send (chat);
1185 chat_input_populate_popup_cb (GtkTextView *view,
1189 EmpathyChatPriv *priv;
1190 GtkTextBuffer *buffer;
1191 GtkTextTagTable *table;
1194 GtkTextIter iter, start, end;
1197 EmpathyChatSpell *chat_spell;
1198 GtkWidget *spell_menu;
1199 EmpathySmileyManager *smiley_manager;
1200 GtkWidget *smiley_menu;
1203 priv = GET_PRIV (chat);
1204 buffer = gtk_text_view_get_buffer (view);
1206 /* Add the emoticon menu. */
1207 item = gtk_separator_menu_item_new ();
1208 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1209 gtk_widget_show (item);
1211 item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
1212 image = gtk_image_new_from_icon_name ("face-smile",
1213 GTK_ICON_SIZE_MENU);
1214 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1215 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1216 gtk_widget_show (item);
1218 smiley_manager = empathy_smiley_manager_dup_singleton ();
1219 smiley_menu = empathy_smiley_menu_new (smiley_manager,
1220 chat_insert_smiley_activate_cb,
1222 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
1223 g_object_unref (smiley_manager);
1225 /* Add the Send menu item. */
1226 gtk_text_buffer_get_bounds (buffer, &start, &end);
1227 str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1228 if (!EMP_STR_EMPTY (str)) {
1229 item = gtk_menu_item_new_with_mnemonic (_("_Send"));
1230 g_signal_connect (G_OBJECT (item), "activate",
1231 G_CALLBACK (chat_text_send_cb), chat);
1232 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1233 gtk_widget_show (item);
1237 /* Add the spell check menu item. */
1238 table = gtk_text_buffer_get_tag_table (buffer);
1239 tag = gtk_text_tag_table_lookup (table, "misspelled");
1240 gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
1241 gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
1242 GTK_TEXT_WINDOW_WIDGET,
1245 gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view), &iter, x, y);
1247 if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
1248 gtk_text_iter_forward_to_tag_toggle (&end, tag)) {
1250 str = gtk_text_buffer_get_text (buffer,
1251 &start, &end, FALSE);
1253 if (!EMP_STR_EMPTY (str)) {
1254 chat_spell = chat_spell_new (chat, str, start, end);
1255 g_object_set_data_full (G_OBJECT (menu),
1256 "chat_spell", chat_spell,
1257 (GDestroyNotify) chat_spell_free);
1259 item = gtk_separator_menu_item_new ();
1260 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1261 gtk_widget_show (item);
1263 item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
1264 image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
1265 GTK_ICON_SIZE_MENU);
1266 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
1268 spell_menu = chat_spelling_build_menu (chat_spell);
1269 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);
1271 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
1272 gtk_widget_show (item);
1277 chat_log_filter (EmpathyMessage *message,
1280 EmpathyChat *chat = (EmpathyChat *) user_data;
1281 EmpathyChatPriv *priv = GET_PRIV (chat);
1282 const GList *pending;
1284 pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1286 for (; pending; pending = g_list_next (pending)) {
1287 if (empathy_message_equal (message, pending->data)) {
1296 chat_add_logs (EmpathyChat *chat)
1298 EmpathyChatPriv *priv = GET_PRIV (chat);
1299 gboolean is_chatroom;
1300 GList *messages, *l;
1306 /* Turn off scrolling temporarily */
1307 empathy_chat_view_scroll (chat->view, FALSE);
1309 /* Add messages from last conversation */
1310 is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
1312 messages = empathy_log_manager_get_filtered_messages (priv->log_manager,
1320 for (l = messages; l; l = g_list_next (l)) {
1321 empathy_chat_view_append_message (chat->view, l->data);
1322 g_object_unref (l->data);
1325 g_list_free (messages);
1327 /* Turn back on scrolling */
1328 empathy_chat_view_scroll (chat->view, TRUE);
1332 chat_contacts_completion_func (const gchar *s1,
1336 gchar *tmp, *nick1, *nick2;
1343 return s1 ? -1 : +1;
1346 tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
1347 nick1 = g_utf8_casefold (tmp, -1);
1350 tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
1351 nick2 = g_utf8_casefold (tmp, -1);
1354 ret = strncmp (nick1, nick2, n);
1363 build_part_message (guint reason,
1365 EmpathyContact *actor,
1366 const gchar *message)
1368 GString *s = g_string_new ("");
1369 const gchar *actor_name = NULL;
1371 if (actor != NULL) {
1372 actor_name = empathy_contact_get_name (actor);
1375 /* Having an actor only really makes sense for a few actions... */
1377 case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
1378 g_string_append_printf (s, _("%s has disconnected"), name);
1380 case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
1381 if (actor_name != NULL) {
1382 /* translators: reverse the order of these arguments
1383 * if the kicked should come before the kicker in your locale.
1385 g_string_append_printf (s, _("%1$s was kicked by %2$s"),
1388 g_string_append_printf (s, _("%s was kicked"), name);
1391 case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
1392 if (actor_name != NULL) {
1393 /* translators: reverse the order of these arguments
1394 * if the banned should come before the banner in your locale.
1396 g_string_append_printf (s, _("%1$s was banned by %2$s"),
1399 g_string_append_printf (s, _("%s was banned"), name);
1403 g_string_append_printf (s, _("%s has left the room"), name);
1406 if (!EMP_STR_EMPTY (message)) {
1407 /* Note to translators: this string is appended to
1408 * notifications like "foo has left the room", with the message
1409 * given by the user living the room. If this poses a problem,
1410 * please let us know. :-)
1412 g_string_append_printf (s, _(" (%s)"), message);
1415 return g_string_free (s, FALSE);
1419 chat_members_changed_cb (EmpathyTpChat *tp_chat,
1420 EmpathyContact *contact,
1421 EmpathyContact *actor,
1427 EmpathyChatPriv *priv = GET_PRIV (chat);
1428 const gchar *name = empathy_contact_get_name (contact);
1431 if (priv->block_events_timeout_id != 0)
1435 str = g_strdup_printf (_("%s has joined the room"),
1438 str = build_part_message (reason, name, actor, message);
1441 empathy_chat_view_append_event (chat->view, str);
1446 chat_reset_size_request (gpointer widget)
1448 gtk_widget_set_size_request (widget, -1, -1);
1454 chat_update_contacts_visibility (EmpathyChat *chat)
1456 EmpathyChatPriv *priv = GET_PRIV (chat);
1459 show = priv->remote_contact == NULL && priv->show_contacts;
1461 if (!priv->scrolled_window_contacts) {
1465 if (show && priv->contact_list_view == NULL) {
1466 EmpathyContactListStore *store;
1469 /* We are adding the contact list to the chat, we don't want the
1470 * chat view to become too small. If the chat view is already
1471 * smaller than 250 make sure that size won't change. If the
1472 * chat view is bigger the contact list will take some space on
1473 * it but we make sure the chat view don't become smaller than
1474 * 250. Relax the size request once the resize is done */
1475 min_width = MIN (priv->vbox_left->allocation.width, 250);
1476 gtk_widget_set_size_request (priv->vbox_left, min_width, -1);
1477 g_idle_add (chat_reset_size_request, priv->vbox_left);
1479 if (priv->contacts_width > 0) {
1480 gtk_paned_set_position (GTK_PANED (priv->hpaned),
1481 priv->contacts_width);
1484 store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (priv->tp_chat));
1485 priv->contact_list_view = GTK_WIDGET (empathy_contact_list_view_new (store,
1486 EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP,
1487 EMPATHY_CONTACT_FEATURE_CHAT |
1488 EMPATHY_CONTACT_FEATURE_CALL |
1489 EMPATHY_CONTACT_FEATURE_LOG |
1490 EMPATHY_CONTACT_FEATURE_INFO));
1491 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
1492 priv->contact_list_view);
1493 gtk_widget_show (priv->contact_list_view);
1494 gtk_widget_show (priv->scrolled_window_contacts);
1495 g_object_unref (store);
1497 priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
1498 gtk_widget_hide (priv->scrolled_window_contacts);
1499 if (priv->contact_list_view != NULL) {
1500 gtk_widget_destroy (priv->contact_list_view);
1501 priv->contact_list_view = NULL;
1507 empathy_chat_set_show_contacts (EmpathyChat *chat,
1510 EmpathyChatPriv *priv = GET_PRIV (chat);
1512 priv->show_contacts = show;
1514 chat_update_contacts_visibility (chat);
1516 g_object_notify (G_OBJECT (chat), "show-contacts");
1520 chat_remote_contact_changed_cb (EmpathyChat *chat)
1522 EmpathyChatPriv *priv = GET_PRIV (chat);
1524 if (priv->remote_contact != NULL) {
1525 g_object_unref (priv->remote_contact);
1526 priv->remote_contact = NULL;
1531 priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
1532 priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
1533 if (priv->remote_contact != NULL) {
1534 g_object_ref (priv->remote_contact);
1535 priv->handle_type = TP_HANDLE_TYPE_CONTACT;
1537 else if (priv->tp_chat != NULL) {
1540 channel = empathy_tp_chat_get_channel (priv->tp_chat);
1541 g_object_get (channel, "handle-type", &priv->handle_type, NULL);
1544 chat_update_contacts_visibility (chat);
1546 g_object_notify (G_OBJECT (chat), "remote-contact");
1547 g_object_notify (G_OBJECT (chat), "id");
1551 chat_destroy_cb (EmpathyTpChat *tp_chat,
1554 EmpathyChatPriv *priv;
1556 priv = GET_PRIV (chat);
1558 if (!priv->tp_chat) {
1562 chat_composing_remove_timeout (chat);
1563 g_object_unref (priv->tp_chat);
1564 priv->tp_chat = NULL;
1565 g_object_notify (G_OBJECT (chat), "tp-chat");
1567 empathy_chat_view_append_event (chat->view, _("Disconnected"));
1568 gtk_widget_set_sensitive (chat->input_text_view, FALSE);
1569 empathy_chat_set_show_contacts (chat, FALSE);
1573 show_pending_messages (EmpathyChat *chat) {
1574 EmpathyChatPriv *priv = GET_PRIV (chat);
1575 const GList *messages, *l;
1577 if (chat->view == NULL || priv->tp_chat == NULL)
1580 messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);
1582 for (l = messages; l != NULL ; l = g_list_next (l)) {
1583 EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
1584 chat_message_received (chat, message);
1586 empathy_tp_chat_acknowledge_messages (priv->tp_chat, messages);
1590 chat_create_ui (EmpathyChat *chat)
1592 EmpathyChatPriv *priv = GET_PRIV (chat);
1596 GtkTextBuffer *buffer;
1598 filename = empathy_file_lookup ("empathy-chat.ui",
1600 gui = empathy_builder_get_file (filename,
1601 "chat_widget", &priv->widget,
1602 "hpaned", &priv->hpaned,
1603 "vbox_left", &priv->vbox_left,
1604 "scrolled_window_chat", &priv->scrolled_window_chat,
1605 "scrolled_window_input", &priv->scrolled_window_input,
1606 "hbox_topic", &priv->hbox_topic,
1607 "label_topic", &priv->label_topic,
1608 "scrolled_window_contacts", &priv->scrolled_window_contacts,
1612 /* Add message view. */
1613 chat->view = empathy_theme_manager_create_view (empathy_theme_manager_get ());
1614 g_signal_connect (chat->view, "focus_in_event",
1615 G_CALLBACK (chat_text_view_focus_in_event_cb),
1617 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
1618 GTK_WIDGET (chat->view));
1619 gtk_widget_show (GTK_WIDGET (chat->view));
1621 /* Add input GtkTextView */
1622 chat->input_text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
1623 "pixels-above-lines", 2,
1624 "pixels-below-lines", 2,
1625 "pixels-inside-wrap", 1,
1628 "wrap-mode", GTK_WRAP_WORD_CHAR,
1630 g_signal_connect (chat->input_text_view, "key-press-event",
1631 G_CALLBACK (chat_input_key_press_event_cb),
1633 g_signal_connect (chat->input_text_view, "size-request",
1634 G_CALLBACK (chat_input_size_request_cb),
1636 g_signal_connect (chat->input_text_view, "realize",
1637 G_CALLBACK (chat_input_realize_cb),
1639 g_signal_connect (chat->input_text_view, "populate-popup",
1640 G_CALLBACK (chat_input_populate_popup_cb),
1642 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
1643 g_signal_connect (buffer, "changed",
1644 G_CALLBACK (chat_input_text_buffer_changed_cb),
1646 gtk_text_buffer_create_tag (buffer, "misspelled",
1647 "underline", PANGO_UNDERLINE_ERROR,
1649 gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
1650 chat->input_text_view);
1651 gtk_widget_show (chat->input_text_view);
1653 /* Create contact list */
1654 chat_update_contacts_visibility (chat);
1656 /* Initialy hide the topic, will be shown if not empty */
1657 gtk_widget_hide (priv->hbox_topic);
1659 /* Set widget focus order */
1660 list = g_list_append (NULL, priv->scrolled_window_input);
1661 gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
1664 list = g_list_append (NULL, priv->vbox_left);
1665 list = g_list_append (list, priv->scrolled_window_contacts);
1666 gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
1669 list = g_list_append (NULL, priv->hpaned);
1670 list = g_list_append (list, priv->hbox_topic);
1671 gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
1674 /* Add the main widget in the chat widget */
1675 gtk_container_add (GTK_CONTAINER (chat), priv->widget);
1676 g_object_unref (gui);
1680 chat_size_request (GtkWidget *widget,
1681 GtkRequisition *requisition)
1683 GtkBin *bin = GTK_BIN (widget);
1686 requisition->width = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1687 requisition->height = gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2;
1689 child = gtk_bin_get_child (bin);
1691 if (child && GTK_WIDGET_VISIBLE (child))
1693 GtkRequisition child_requisition;
1695 gtk_widget_size_request (child, &child_requisition);
1697 requisition->width += child_requisition.width;
1698 requisition->height += child_requisition.height;
1703 chat_size_allocate (GtkWidget *widget,
1704 GtkAllocation *allocation)
1706 GtkBin *bin = GTK_BIN (widget);
1707 GtkAllocation child_allocation;
1710 widget->allocation = *allocation;
1712 child = gtk_bin_get_child (bin);
1714 if (child && GTK_WIDGET_VISIBLE (child))
1716 child_allocation.x = allocation->x + gtk_container_get_border_width (GTK_CONTAINER (widget));
1717 child_allocation.y = allocation->y + gtk_container_get_border_width (GTK_CONTAINER (widget));
1718 child_allocation.width = MAX (allocation->width - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1719 child_allocation.height = MAX (allocation->height - gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2, 0);
1721 gtk_widget_size_allocate (child, &child_allocation);
1726 chat_finalize (GObject *object)
1729 EmpathyChatPriv *priv;
1731 chat = EMPATHY_CHAT (object);
1732 priv = GET_PRIV (chat);
1734 DEBUG ("Finalized: %p", object);
1736 g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
1737 g_list_free (priv->input_history);
1739 g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
1740 g_list_free (priv->compositors);
1742 chat_composing_remove_timeout (chat);
1744 g_signal_handlers_disconnect_by_func (priv->account_manager,
1745 chat_new_connection_cb, object);
1747 g_object_unref (priv->account_manager);
1748 g_object_unref (priv->log_manager);
1750 if (priv->tp_chat) {
1751 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1752 chat_destroy_cb, chat);
1753 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1754 chat_message_received_cb, chat);
1755 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1756 chat_send_error_cb, chat);
1757 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1758 chat_state_changed_cb, chat);
1759 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1760 chat_property_changed_cb, chat);
1761 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1762 chat_members_changed_cb, chat);
1763 g_signal_handlers_disconnect_by_func (priv->tp_chat,
1764 chat_remote_contact_changed_cb, chat);
1765 empathy_tp_chat_close (priv->tp_chat);
1766 g_object_unref (priv->tp_chat);
1768 if (priv->account) {
1769 g_object_unref (priv->account);
1771 if (priv->remote_contact) {
1772 g_object_unref (priv->remote_contact);
1775 if (priv->block_events_timeout_id) {
1776 g_source_remove (priv->block_events_timeout_id);
1780 g_free (priv->name);
1781 g_free (priv->subject);
1782 g_completion_free (priv->completion);
1784 G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
1788 chat_constructed (GObject *object)
1790 EmpathyChat *chat = EMPATHY_CHAT (object);
1792 chat_create_ui (chat);
1793 chat_add_logs (chat);
1794 show_pending_messages (chat);
1798 empathy_chat_class_init (EmpathyChatClass *klass)
1800 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
1801 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1803 object_class->finalize = chat_finalize;
1804 object_class->get_property = chat_get_property;
1805 object_class->set_property = chat_set_property;
1806 object_class->constructed = chat_constructed;
1808 widget_class->size_request = chat_size_request;
1809 widget_class->size_allocate = chat_size_allocate;
1811 g_object_class_install_property (object_class,
1813 g_param_spec_object ("tp-chat",
1815 "The tp chat object",
1816 EMPATHY_TYPE_TP_CHAT,
1819 G_PARAM_STATIC_STRINGS));
1820 g_object_class_install_property (object_class,
1822 g_param_spec_object ("account",
1823 "Account of the chat",
1824 "The account of the chat",
1825 EMPATHY_TYPE_ACCOUNT,
1827 G_PARAM_STATIC_STRINGS));
1828 g_object_class_install_property (object_class,
1830 g_param_spec_string ("id",
1832 "The id of the chat",
1835 G_PARAM_STATIC_STRINGS));
1836 g_object_class_install_property (object_class,
1838 g_param_spec_string ("name",
1840 "The name of the chat",
1843 G_PARAM_STATIC_STRINGS));
1844 g_object_class_install_property (object_class,
1846 g_param_spec_string ("subject",
1848 "The subject or topic of the chat",
1851 G_PARAM_STATIC_STRINGS));
1852 g_object_class_install_property (object_class,
1853 PROP_REMOTE_CONTACT,
1854 g_param_spec_object ("remote-contact",
1855 "The remote contact",
1856 "The remote contact is any",
1857 EMPATHY_TYPE_CONTACT,
1859 G_PARAM_STATIC_STRINGS));
1860 g_object_class_install_property (object_class,
1862 g_param_spec_boolean ("show-contacts",
1863 "Contacts' visibility",
1864 "The visibility of the contacts' list",
1867 G_PARAM_STATIC_STRINGS));
1869 signals[COMPOSING] =
1870 g_signal_new ("composing",
1871 G_OBJECT_CLASS_TYPE (object_class),
1875 g_cclosure_marshal_VOID__BOOLEAN,
1879 signals[NEW_MESSAGE] =
1880 g_signal_new ("new-message",
1881 G_OBJECT_CLASS_TYPE (object_class),
1885 g_cclosure_marshal_VOID__OBJECT,
1887 1, EMPATHY_TYPE_MESSAGE);
1889 g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
1893 chat_block_events_timeout_cb (gpointer data)
1895 EmpathyChatPriv *priv = GET_PRIV (data);
1897 priv->block_events_timeout_id = 0;
1903 empathy_chat_init (EmpathyChat *chat)
1905 EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
1906 EMPATHY_TYPE_CHAT, EmpathyChatPriv);
1909 priv->log_manager = empathy_log_manager_dup_singleton ();
1910 priv->contacts_width = -1;
1911 priv->input_history = NULL;
1912 priv->input_history_current = NULL;
1913 priv->account_manager = empathy_account_manager_dup_singleton ();
1915 g_signal_connect (priv->account_manager,
1917 G_CALLBACK (chat_new_connection_cb),
1920 empathy_conf_get_bool (empathy_conf_get (),
1921 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1922 &priv->show_contacts);
1924 /* Block events for some time to avoid having "has come online" or
1925 * "joined" messages. */
1926 priv->block_events_timeout_id =
1927 g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);
1929 /* Add nick name completion */
1930 priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_name);
1931 g_completion_set_compare (priv->completion, chat_contacts_completion_func);
1935 empathy_chat_new (EmpathyTpChat *tp_chat)
1937 return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
1941 empathy_chat_get_tp_chat (EmpathyChat *chat)
1943 EmpathyChatPriv *priv = GET_PRIV (chat);
1945 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
1947 return priv->tp_chat;
1951 empathy_chat_set_tp_chat (EmpathyChat *chat,
1952 EmpathyTpChat *tp_chat)
1954 EmpathyChatPriv *priv = GET_PRIV (chat);
1955 TpConnection *connection;
1957 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1958 g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));
1959 g_return_if_fail (empathy_tp_chat_is_ready (tp_chat));
1961 if (priv->tp_chat) {
1965 if (priv->account) {
1966 g_object_unref (priv->account);
1969 priv->tp_chat = g_object_ref (tp_chat);
1970 connection = empathy_tp_chat_get_connection (priv->tp_chat);
1971 priv->account = empathy_account_manager_get_account_for_connection (
1972 priv->account_manager,
1974 g_object_ref (priv->account);
1976 g_signal_connect (tp_chat, "destroy",
1977 G_CALLBACK (chat_destroy_cb),
1979 g_signal_connect (tp_chat, "message-received",
1980 G_CALLBACK (chat_message_received_cb),
1982 g_signal_connect (tp_chat, "send-error",
1983 G_CALLBACK (chat_send_error_cb),
1985 g_signal_connect (tp_chat, "chat-state-changed",
1986 G_CALLBACK (chat_state_changed_cb),
1988 g_signal_connect (tp_chat, "property-changed",
1989 G_CALLBACK (chat_property_changed_cb),
1991 g_signal_connect (tp_chat, "members-changed",
1992 G_CALLBACK (chat_members_changed_cb),
1994 g_signal_connect_swapped (tp_chat, "notify::remote-contact",
1995 G_CALLBACK (chat_remote_contact_changed_cb),
1998 chat_remote_contact_changed_cb (chat);
2000 if (chat->input_text_view) {
2001 gtk_widget_set_sensitive (chat->input_text_view, TRUE);
2002 if (priv->block_events_timeout_id == 0) {
2003 empathy_chat_view_append_event (chat->view, _("Connected"));
2007 g_object_notify (G_OBJECT (chat), "tp-chat");
2008 g_object_notify (G_OBJECT (chat), "id");
2009 g_object_notify (G_OBJECT (chat), "account");
2011 /* This is a noop when tp-chat is set at object construction time and causes
2012 * the pending messages to be show when it's set on the object after it has
2014 show_pending_messages (chat);
2018 empathy_chat_get_account (EmpathyChat *chat)
2020 EmpathyChatPriv *priv = GET_PRIV (chat);
2022 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2024 return priv->account;
2028 empathy_chat_get_id (EmpathyChat *chat)
2030 EmpathyChatPriv *priv = GET_PRIV (chat);
2032 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2038 empathy_chat_get_name (EmpathyChat *chat)
2040 EmpathyChatPriv *priv = GET_PRIV (chat);
2043 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2046 if (!ret && priv->remote_contact) {
2047 ret = empathy_contact_get_name (priv->remote_contact);
2053 return ret ? ret : _("Conversation");
2057 empathy_chat_get_subject (EmpathyChat *chat)
2059 EmpathyChatPriv *priv = GET_PRIV (chat);
2061 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2063 return priv->subject;
2067 empathy_chat_get_remote_contact (EmpathyChat *chat)
2069 EmpathyChatPriv *priv = GET_PRIV (chat);
2071 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2073 return priv->remote_contact;
2077 empathy_chat_get_contact_menu (EmpathyChat *chat)
2079 EmpathyChatPriv *priv = GET_PRIV (chat);
2080 GtkWidget *menu = NULL;
2082 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);
2084 if (priv->remote_contact) {
2085 menu = empathy_contact_menu_new (priv->remote_contact,
2086 EMPATHY_CONTACT_FEATURE_CALL |
2087 EMPATHY_CONTACT_FEATURE_LOG |
2088 EMPATHY_CONTACT_FEATURE_INFO);
2090 else if (priv->contact_list_view) {
2091 EmpathyContactListView *view;
2093 view = EMPATHY_CONTACT_LIST_VIEW (priv->contact_list_view);
2094 menu = empathy_contact_list_view_get_contact_menu (view);
2101 empathy_chat_clear (EmpathyChat *chat)
2103 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2105 empathy_chat_view_clear (chat->view);
2109 empathy_chat_scroll_down (EmpathyChat *chat)
2111 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2113 empathy_chat_view_scroll_down (chat->view);
2117 empathy_chat_cut (EmpathyChat *chat)
2119 GtkTextBuffer *buffer;
2121 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2123 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2124 if (gtk_text_buffer_get_has_selection (buffer)) {
2125 GtkClipboard *clipboard;
2127 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2129 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
2134 empathy_chat_copy (EmpathyChat *chat)
2136 GtkTextBuffer *buffer;
2138 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2140 if (empathy_chat_view_get_has_selection (chat->view)) {
2141 empathy_chat_view_copy_clipboard (chat->view);
2145 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2146 if (gtk_text_buffer_get_has_selection (buffer)) {
2147 GtkClipboard *clipboard;
2149 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2151 gtk_text_buffer_copy_clipboard (buffer, clipboard);
2156 empathy_chat_paste (EmpathyChat *chat)
2158 GtkTextBuffer *buffer;
2159 GtkClipboard *clipboard;
2160 EmpathyChatPriv *priv;
2162 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2164 priv = GET_PRIV (chat);
2166 if (priv->tp_chat == NULL ||
2167 !GTK_WIDGET_IS_SENSITIVE (chat->input_text_view))
2170 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2171 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
2173 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
2177 empathy_chat_correct_word (EmpathyChat *chat,
2180 const gchar *new_word)
2182 GtkTextBuffer *buffer;
2184 g_return_if_fail (chat != NULL);
2185 g_return_if_fail (new_word != NULL);
2187 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
2189 gtk_text_buffer_delete (buffer, start, end);
2190 gtk_text_buffer_insert (buffer, start,
2196 empathy_chat_is_room (EmpathyChat *chat)
2198 EmpathyChatPriv *priv = GET_PRIV (chat);
2200 g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);
2202 return (priv->handle_type == TP_HANDLE_TYPE_ROOM);