1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-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>
26 * RĂ´mulo Fernandes Machado <romulo@castorgroup.net>
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n.h>
36 #include <libnotify/notification.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
41 #include <libempathy/empathy-contact.h>
42 #include <libempathy/empathy-message.h>
43 #include <libempathy/empathy-dispatcher.h>
44 #include <libempathy/empathy-chatroom-manager.h>
45 #include <libempathy/empathy-utils.h>
47 #include <libempathy-gtk/empathy-images.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-sound.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
55 #include <libempathy-gtk/empathy-notify-manager.h>
57 #include "empathy-chat-window.h"
58 #include "empathy-about-dialog.h"
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
64 EmpathyChatWindow *window;
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
70 EmpathyChat *current_chat;
73 GList *chats_composing;
75 gboolean dnd_same_window;
76 EmpathyChatroomManager *chatroom_manager;
77 EmpathyNotifyManager *notify_mgr;
80 NotifyNotification *notification;
81 NotificationData *notification_data;
84 GtkUIManager *ui_manager;
85 GtkAction *menu_conv_insert_smiley;
86 GtkAction *menu_conv_favorite;
87 GtkAction *menu_conv_toggle_contacts;
89 GtkAction *menu_edit_cut;
90 GtkAction *menu_edit_copy;
91 GtkAction *menu_edit_paste;
93 GtkAction *menu_tabs_next;
94 GtkAction *menu_tabs_prev;
95 GtkAction *menu_tabs_left;
96 GtkAction *menu_tabs_right;
97 GtkAction *menu_tabs_detach;
98 } EmpathyChatWindowPriv;
100 static GList *chat_windows = NULL;
102 static const guint tab_accel_keys[] = {
103 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
104 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
108 DND_DRAG_TYPE_CONTACT_ID,
112 static const GtkTargetEntry drag_types_dest[] = {
113 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
114 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
117 static void chat_window_update (EmpathyChatWindow *window);
119 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
122 chat_window_accel_cb (GtkAccelGroup *accelgroup,
126 EmpathyChatWindow *window)
128 EmpathyChatWindowPriv *priv;
132 priv = GET_PRIV (window);
134 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
135 if (tab_accel_keys[i] == key) {
142 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
146 static EmpathyChatWindow *
147 chat_window_find_chat (EmpathyChat *chat)
149 EmpathyChatWindowPriv *priv;
152 for (l = chat_windows; l; l = l->next) {
153 priv = GET_PRIV (l->data);
154 ll = g_list_find (priv->chats, chat);
164 chat_window_close_clicked_cb (GtkAction *action,
167 EmpathyChatWindow *window;
169 window = chat_window_find_chat (chat);
170 empathy_chat_window_remove_chat (window, chat);
174 chat_tab_style_set_cb (GtkWidget *hbox,
175 GtkStyle *previous_style,
179 int char_width, h, w;
180 PangoContext *context;
181 PangoFontMetrics *metrics;
183 button = g_object_get_data (G_OBJECT (user_data),
184 "chat-window-tab-close-button");
185 context = gtk_widget_get_pango_context (hbox);
187 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
188 pango_context_get_language (context));
189 char_width = pango_font_metrics_get_approximate_char_width (metrics);
190 pango_font_metrics_unref (metrics);
192 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
193 GTK_ICON_SIZE_MENU, &w, &h);
195 /* Request at least about 12 chars width plus at least space for the status
196 * image and the close button */
197 gtk_widget_set_size_request (hbox,
198 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
200 gtk_widget_set_size_request (button, w, h);
204 chat_window_create_label (EmpathyChatWindow *window,
206 gboolean is_tab_label)
208 EmpathyChatWindowPriv *priv;
210 GtkWidget *name_label;
211 GtkWidget *status_image;
212 GtkWidget *close_button;
213 GtkWidget *close_image;
214 GtkWidget *event_box;
215 GtkWidget *event_box_hbox;
216 PangoAttrList *attr_list;
217 PangoAttribute *attr;
219 priv = GET_PRIV (window);
221 /* The spacing between the button and the label. */
222 hbox = gtk_hbox_new (FALSE, 0);
224 event_box = gtk_event_box_new ();
225 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
227 name_label = gtk_label_new (NULL);
229 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
231 attr_list = pango_attr_list_new ();
232 attr = pango_attr_scale_new (1/1.2);
233 attr->start_index = 0;
234 attr->end_index = -1;
235 pango_attr_list_insert (attr_list, attr);
236 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
237 pango_attr_list_unref (attr_list);
239 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
240 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
241 g_object_set_data (G_OBJECT (chat),
242 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
245 status_image = gtk_image_new ();
247 /* Spacing between the icon and label. */
248 event_box_hbox = gtk_hbox_new (FALSE, 0);
250 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
251 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
253 g_object_set_data (G_OBJECT (chat),
254 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
256 g_object_set_data (G_OBJECT (chat),
257 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
260 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
261 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
264 close_button = gtk_button_new ();
265 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
266 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
268 /* We don't want focus/keynav for the button to avoid clutter, and
269 * Ctrl-W works anyway.
271 gtk_widget_set_can_focus (close_button, FALSE);
272 gtk_widget_set_can_default (close_button, FALSE);
274 /* Set the name to make the special rc style match. */
275 gtk_widget_set_name (close_button, "empathy-close-button");
277 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
279 gtk_container_add (GTK_CONTAINER (close_button), close_image);
281 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
283 g_signal_connect (close_button,
285 G_CALLBACK (chat_window_close_clicked_cb),
288 /* React to theme changes and also setup the size correctly. */
289 g_signal_connect (hbox,
291 G_CALLBACK (chat_tab_style_set_cb),
295 gtk_widget_show_all (hbox);
301 _submenu_notify_visible_changed_cb (GObject *object,
305 g_signal_handlers_disconnect_by_func (object,
306 _submenu_notify_visible_changed_cb,
308 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
312 chat_window_menu_context_update (EmpathyChatWindowPriv *priv,
317 gboolean is_connected;
320 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
321 first_page = (page_num == 0);
322 last_page = (page_num == (num_pages - 1));
323 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
325 DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
327 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
328 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
329 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
330 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
331 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
332 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
336 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
337 EmpathyChatWindow *window)
339 GtkWidget *menu, *submenu, *orig_submenu;
341 menu = gtk_ui_manager_get_widget (priv->ui_manager,
342 "/chats_menubar/menu_contact");
343 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
345 DEBUG ("Update window : Contact Menu");
347 if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
348 submenu = empathy_chat_get_contact_menu (priv->current_chat);
349 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
350 gtk_widget_show (menu);
352 empathy_signal_connect_weak (orig_submenu,
354 (GCallback)_submenu_notify_visible_changed_cb,
360 get_all_unread_messages (EmpathyChatWindowPriv *priv)
365 for (l = priv->chats_new_msg; l != NULL; l = g_list_next (l)) {
366 EmpathyChat *chat = l->data;
368 nb += empathy_chat_get_nb_unread_messages (chat);
375 get_window_title_name (EmpathyChatWindowPriv *priv)
377 const gchar *active_name;
379 guint current_unread_msgs;
381 nb_chats = g_list_length (priv->chats);
382 g_assert (nb_chats > 0);
384 active_name = empathy_chat_get_name (priv->current_chat);
386 current_unread_msgs = empathy_chat_get_nb_unread_messages (
391 if (current_unread_msgs == 0)
392 return g_strdup (active_name);
394 return g_strdup_printf (ngettext (
396 "%s (%d unread)", current_unread_msgs),
397 active_name, current_unread_msgs);
399 guint nb_others = nb_chats - 1;
400 guint all_unread_msgs;
402 all_unread_msgs = get_all_unread_messages (priv);
404 if (all_unread_msgs == 0) {
405 /* no unread message */
406 return g_strdup_printf (ngettext (
408 "%s (and %u others)", nb_others),
409 active_name, nb_others);
412 else if (all_unread_msgs == current_unread_msgs) {
413 /* unread messages are in the current tab */
414 return g_strdup_printf (ngettext (
416 "%s (%d unread)", current_unread_msgs),
417 active_name, current_unread_msgs);
420 else if (current_unread_msgs == 0) {
421 /* unread messages are in others tab */
422 return g_strdup_printf (ngettext (
423 "%s (%d unread from others)",
424 "%s (%d unread from others)",
426 active_name, all_unread_msgs);
430 /* unread messages are in all the tabs */
431 return g_strdup_printf (ngettext (
432 "%s (%d unread from everyone)",
433 "%s (%d unread from everyone)",
435 active_name, all_unread_msgs);
441 chat_window_title_update (EmpathyChatWindowPriv *priv)
445 DEBUG ("Update window : Title");
447 name = get_window_title_name (priv);
448 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
453 chat_window_icon_update (EmpathyChatWindowPriv *priv)
456 EmpathyContact *remote_contact;
457 gboolean avatar_in_icon;
460 n_chats = g_list_length (priv->chats);
462 DEBUG ("Update window : Icon");
464 /* Update window icon */
465 if (priv->chats_new_msg) {
466 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
467 EMPATHY_IMAGE_MESSAGE);
469 empathy_conf_get_bool (empathy_conf_get (),
470 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
473 if (n_chats == 1 && avatar_in_icon) {
474 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
475 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
476 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
479 g_object_unref (icon);
482 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
488 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
492 GtkWidget *chat_close_button;
495 DEBUG ("Update window : Close Button");
497 if (num_pages == 1) {
498 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
499 chat_close_button = g_object_get_data (G_OBJECT (chat),
500 "chat-window-tab-close-button");
501 gtk_widget_hide (chat_close_button);
503 for (i=0; i<num_pages; i++) {
504 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
505 chat_close_button = g_object_get_data (G_OBJECT (chat),
506 "chat-window-tab-close-button");
507 gtk_widget_show (chat_close_button);
513 chat_window_update (EmpathyChatWindow *window)
515 EmpathyChatWindowPriv *priv = GET_PRIV (window);
518 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
520 DEBUG ("Update window");
522 /* Update Tab menu */
523 chat_window_menu_context_update (priv,
526 chat_window_contact_menu_update (priv,
529 chat_window_title_update (priv);
531 chat_window_icon_update (priv);
533 chat_window_close_button_update (priv,
538 append_markup_printf (GString *string,
545 va_start (args, format);
547 tmp = g_markup_vprintf_escaped (format, args);
548 g_string_append (string, tmp);
555 chat_window_update_chat_tab (EmpathyChat *chat)
557 EmpathyChatWindow *window;
558 EmpathyChatWindowPriv *priv;
559 EmpathyContact *remote_contact;
563 const gchar *subject;
564 const gchar *status = NULL;
568 const gchar *icon_name;
569 GtkWidget *tab_image;
570 GtkWidget *menu_image;
572 window = chat_window_find_chat (chat);
576 priv = GET_PRIV (window);
578 /* Get information */
579 name = empathy_chat_get_name (chat);
580 account = empathy_chat_get_account (chat);
581 subject = empathy_chat_get_subject (chat);
582 remote_contact = empathy_chat_get_remote_contact (chat);
584 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
585 name, tp_proxy_get_object_path (account), subject, remote_contact);
587 /* Update tab image */
588 if (empathy_chat_get_tp_chat (chat) == NULL) {
589 /* No TpChat, we are disconnected */
592 else if (g_list_find (priv->chats_new_msg, chat)) {
593 icon_name = EMPATHY_IMAGE_MESSAGE;
595 else if (g_list_find (priv->chats_composing, chat)) {
596 icon_name = EMPATHY_IMAGE_TYPING;
598 else if (remote_contact) {
599 icon_name = empathy_icon_name_for_contact (remote_contact);
601 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
604 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
605 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
606 if (icon_name != NULL) {
607 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
608 gtk_widget_show (tab_image);
609 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
610 gtk_widget_show (menu_image);
612 gtk_widget_hide (tab_image);
613 gtk_widget_hide (menu_image);
616 /* Update tab tooltip */
617 tooltip = g_string_new (NULL);
619 if (remote_contact) {
620 id = empathy_contact_get_id (remote_contact);
621 status = empathy_contact_get_presence_message (remote_contact);
626 append_markup_printf (tooltip,
627 "<b>%s</b><small> (%s)</small>",
629 tp_account_get_display_name (account));
631 if (!EMP_STR_EMPTY (status)) {
632 append_markup_printf (tooltip, "\n<i>%s</i>", status);
636 append_markup_printf (tooltip, "\n<b>%s</b> %s",
637 _("Topic:"), subject);
640 if (g_list_find (priv->chats_composing, chat)) {
641 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
644 markup = g_string_free (tooltip, FALSE);
645 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
646 gtk_widget_set_tooltip_markup (widget, markup);
647 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
648 gtk_widget_set_tooltip_markup (widget, markup);
651 /* Update tab and menu label */
652 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
653 gtk_label_set_text (GTK_LABEL (widget), name);
654 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
655 gtk_label_set_text (GTK_LABEL (widget), name);
657 /* Update the window if it's the current chat */
658 if (priv->current_chat == chat) {
659 chat_window_update (window);
664 chat_window_chat_notify_cb (EmpathyChat *chat)
666 EmpathyContact *old_remote_contact;
667 EmpathyContact *remote_contact = NULL;
669 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
670 remote_contact = empathy_chat_get_remote_contact (chat);
672 if (old_remote_contact != remote_contact) {
673 /* The remote-contact associated with the chat changed, we need
674 * to keep track of any change of that contact and update the
675 * window each time. */
676 if (remote_contact) {
677 g_signal_connect_swapped (remote_contact, "notify",
678 G_CALLBACK (chat_window_update_chat_tab),
681 if (old_remote_contact) {
682 g_signal_handlers_disconnect_by_func (old_remote_contact,
683 chat_window_update_chat_tab,
687 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
691 chat_window_update_chat_tab (chat);
695 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
696 EmpathySmiley *smiley,
699 EmpathyChatWindowPriv *priv = GET_PRIV (window);
701 GtkTextBuffer *buffer;
704 chat = priv->current_chat;
706 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
707 gtk_text_buffer_get_end_iter (buffer, &iter);
708 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
712 chat_window_conv_activate_cb (GtkAction *action,
713 EmpathyChatWindow *window)
715 EmpathyChatWindowPriv *priv = GET_PRIV (window);
718 EmpathyContact *remote_contact = NULL;
720 /* Favorite room menu */
721 is_room = empathy_chat_is_room (priv->current_chat);
725 gboolean found = FALSE;
726 EmpathyChatroom *chatroom;
728 room = empathy_chat_get_id (priv->current_chat);
729 account = empathy_chat_get_account (priv->current_chat);
730 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
732 if (chatroom != NULL)
733 found = empathy_chatroom_is_favorite (chatroom);
735 DEBUG ("This room %s favorite", found ? "is" : "is not");
736 gtk_toggle_action_set_active (
737 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
739 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
741 /* Show contacts menu */
742 g_object_get (priv->current_chat,
743 "remote-contact", &remote_contact,
744 "show-contacts", &active,
746 if (remote_contact == NULL) {
747 gtk_toggle_action_set_active (
748 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
751 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
752 (remote_contact == NULL));
753 if (remote_contact != NULL) {
754 g_object_unref (remote_contact);
759 chat_window_clear_activate_cb (GtkAction *action,
760 EmpathyChatWindow *window)
762 EmpathyChatWindowPriv *priv = GET_PRIV (window);
764 empathy_chat_clear (priv->current_chat);
768 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
769 EmpathyChatWindow *window)
771 EmpathyChatWindowPriv *priv = GET_PRIV (window);
775 EmpathyChatroom *chatroom;
777 active = gtk_toggle_action_get_active (toggle_action);
778 account = empathy_chat_get_account (priv->current_chat);
779 room = empathy_chat_get_id (priv->current_chat);
781 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
784 if (chatroom == NULL) {
787 name = empathy_chat_get_name (priv->current_chat);
788 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
789 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
790 g_object_unref (chatroom);
793 empathy_chatroom_set_favorite (chatroom, active);
797 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
798 EmpathyChatWindow *window)
800 EmpathyChatWindowPriv *priv = GET_PRIV (window);
803 active = gtk_toggle_action_get_active (toggle_action);
805 empathy_chat_set_show_contacts (priv->current_chat, active);
809 chat_window_close_activate_cb (GtkAction *action,
810 EmpathyChatWindow *window)
812 EmpathyChatWindowPriv *priv;
814 priv = GET_PRIV (window);
816 g_return_if_fail (priv->current_chat != NULL);
818 empathy_chat_window_remove_chat (window, priv->current_chat);
822 chat_window_edit_activate_cb (GtkAction *action,
823 EmpathyChatWindow *window)
825 EmpathyChatWindowPriv *priv;
826 GtkClipboard *clipboard;
827 GtkTextBuffer *buffer;
828 gboolean text_available;
830 priv = GET_PRIV (window);
832 g_return_if_fail (priv->current_chat != NULL);
834 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
835 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
836 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
837 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
841 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
842 if (gtk_text_buffer_get_has_selection (buffer)) {
843 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
844 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
848 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
850 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
851 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
854 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
855 text_available = gtk_clipboard_wait_is_text_available (clipboard);
856 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
860 chat_window_cut_activate_cb (GtkAction *action,
861 EmpathyChatWindow *window)
863 EmpathyChatWindowPriv *priv;
865 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
867 priv = GET_PRIV (window);
869 empathy_chat_cut (priv->current_chat);
873 chat_window_copy_activate_cb (GtkAction *action,
874 EmpathyChatWindow *window)
876 EmpathyChatWindowPriv *priv;
878 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
880 priv = GET_PRIV (window);
882 empathy_chat_copy (priv->current_chat);
886 chat_window_paste_activate_cb (GtkAction *action,
887 EmpathyChatWindow *window)
889 EmpathyChatWindowPriv *priv;
891 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
893 priv = GET_PRIV (window);
895 empathy_chat_paste (priv->current_chat);
899 chat_window_tabs_next_activate_cb (GtkAction *action,
900 EmpathyChatWindow *window)
902 EmpathyChatWindowPriv *priv;
904 gint index_, numPages;
906 priv = GET_PRIV (window);
908 chat = priv->current_chat;
909 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
910 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
912 if (index_ == (numPages - 1)) {
913 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
917 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
921 chat_window_tabs_previous_activate_cb (GtkAction *action,
922 EmpathyChatWindow *window)
924 EmpathyChatWindowPriv *priv;
926 gint index_, numPages;
928 priv = GET_PRIV (window);
930 chat = priv->current_chat;
931 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
932 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
935 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
939 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
943 chat_window_tabs_left_activate_cb (GtkAction *action,
944 EmpathyChatWindow *window)
946 EmpathyChatWindowPriv *priv;
950 priv = GET_PRIV (window);
952 chat = priv->current_chat;
953 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
958 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
964 chat_window_tabs_right_activate_cb (GtkAction *action,
965 EmpathyChatWindow *window)
967 EmpathyChatWindowPriv *priv;
971 priv = GET_PRIV (window);
973 chat = priv->current_chat;
974 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
976 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
982 chat_window_detach_activate_cb (GtkAction *action,
983 EmpathyChatWindow *window)
985 EmpathyChatWindowPriv *priv;
986 EmpathyChatWindow *new_window;
989 priv = GET_PRIV (window);
991 chat = priv->current_chat;
992 new_window = empathy_chat_window_new ();
994 empathy_chat_window_move_chat (window, new_window, chat);
996 priv = GET_PRIV (new_window);
997 gtk_widget_show (priv->dialog);
1001 chat_window_help_contents_activate_cb (GtkAction *action,
1002 EmpathyChatWindow *window)
1004 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1006 empathy_url_show (priv->dialog, "ghelp:empathy");
1010 chat_window_help_about_activate_cb (GtkAction *action,
1011 EmpathyChatWindow *window)
1013 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1015 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1019 chat_window_delete_event_cb (GtkWidget *dialog,
1021 EmpathyChatWindow *window)
1023 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1025 DEBUG ("Delete event received");
1027 g_object_ref (window);
1028 while (priv->chats) {
1029 empathy_chat_window_remove_chat (window, priv->chats->data);
1031 g_object_unref (window);
1037 chat_window_composing_cb (EmpathyChat *chat,
1038 gboolean is_composing,
1039 EmpathyChatWindow *window)
1041 EmpathyChatWindowPriv *priv;
1043 priv = GET_PRIV (window);
1045 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1046 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1048 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1051 chat_window_update_chat_tab (chat);
1055 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1058 EmpathyChatWindowPriv *priv;
1060 priv = GET_PRIV (window);
1062 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
1063 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1067 free_notification_data (NotificationData *data)
1069 g_object_unref (data->chat);
1070 g_slice_free (NotificationData, data);
1074 chat_window_notification_closed_cb (NotifyNotification *notify,
1075 NotificationData *cb_data)
1077 EmpathyNotificationClosedReason reason = 0;
1078 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1080 #ifdef notify_notification_get_closed_reason
1081 reason = notify_notification_get_closed_reason (notify);
1083 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1084 empathy_chat_window_present_chat (cb_data->chat);
1087 g_object_unref (notify);
1088 priv->notification = NULL;
1089 free_notification_data (cb_data);
1090 priv->notification_data = NULL;
1094 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1095 EmpathyMessage *message,
1098 EmpathyContact *sender;
1099 const gchar *header;
1103 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1106 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1109 empathy_conf_get_bool (empathy_conf_get (),
1110 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1116 sender = empathy_message_get_sender (message);
1117 header = empathy_contact_get_name (sender);
1118 body = empathy_message_get_body (message);
1119 escaped = g_markup_escape_text (body, -1);
1121 if (priv->notification != NULL) {
1122 notify_notification_update (priv->notification,
1123 header, escaped, NULL);
1125 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1127 cb_data->chat = g_object_ref (chat);
1128 cb_data->window = window;
1130 priv->notification_data = cb_data;
1131 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1132 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1134 g_signal_connect (priv->notification, "closed",
1135 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1138 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1139 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1141 if (pixbuf != NULL) {
1142 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1143 g_object_unref (pixbuf);
1146 notify_notification_show (priv->notification, NULL);
1152 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1157 if (!empathy_chat_is_room (chat))
1160 markup = g_markup_printf_escaped (
1161 "<span color=\"red\" weight=\"bold\">%s</span>",
1162 empathy_chat_get_name (chat));
1164 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1165 gtk_label_set_markup (GTK_LABEL (widget), markup);
1170 chat_window_new_message_cb (EmpathyChat *chat,
1171 EmpathyMessage *message,
1172 EmpathyChatWindow *window)
1174 EmpathyChatWindowPriv *priv;
1176 gboolean needs_urgency;
1177 EmpathyContact *sender;
1179 priv = GET_PRIV (window);
1181 has_focus = empathy_chat_window_has_focus (window);
1183 /* - if we're the sender, we play the sound if it's specified in the
1184 * preferences and we're not away.
1185 * - if we receive a message, we play the sound if it's specified in the
1186 * preferences and the window does not have focus on the chat receiving
1190 sender = empathy_message_get_sender (message);
1192 if (empathy_contact_is_user (sender)) {
1193 empathy_sound_play (GTK_WIDGET (priv->dialog),
1194 EMPATHY_SOUND_MESSAGE_OUTGOING);
1197 if (has_focus && priv->current_chat == chat) {
1198 /* window and tab are focused so consider than the message is
1200 empathy_chat_messages_read (chat);
1204 if (!g_list_find (priv->chats_new_msg, chat)) {
1205 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1206 chat_window_update_chat_tab (chat);
1209 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1210 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1211 * an unamed MUC (msn-like).
1212 * In case of a MUC, we set urgency only if the message contains our
1214 if (empathy_chat_is_room (chat) ||
1215 empathy_chat_get_remote_contact (chat) == NULL) {
1216 needs_urgency = empathy_message_should_highlight (message);
1218 needs_urgency = TRUE;
1221 if (needs_urgency) {
1223 chat_window_set_urgency_hint (window, TRUE);
1224 chat_window_set_highlight_room_tab_label (chat);
1227 empathy_sound_play (GTK_WIDGET (priv->dialog),
1228 EMPATHY_SOUND_MESSAGE_INCOMING);
1229 chat_window_show_or_update_notification (window, message, chat);
1232 /* update the number of unread messages */
1233 chat_window_title_update (priv);
1236 static GtkNotebook *
1237 chat_window_detach_hook (GtkNotebook *source,
1243 EmpathyChatWindowPriv *priv;
1244 EmpathyChatWindow *window, *new_window;
1247 chat = EMPATHY_CHAT (page);
1248 window = chat_window_find_chat (chat);
1250 new_window = empathy_chat_window_new ();
1251 priv = GET_PRIV (new_window);
1253 DEBUG ("Detach hook called");
1255 empathy_chat_window_move_chat (window, new_window, chat);
1257 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1258 gtk_widget_show (priv->dialog);
1264 chat_window_page_switched_cb (GtkNotebook *notebook,
1265 GtkNotebookPage *page,
1267 EmpathyChatWindow *window)
1269 EmpathyChatWindowPriv *priv;
1273 DEBUG ("Page switched");
1275 priv = GET_PRIV (window);
1277 child = gtk_notebook_get_nth_page (notebook, page_num);
1278 chat = EMPATHY_CHAT (child);
1280 if (priv->page_added) {
1281 priv->page_added = FALSE;
1282 empathy_chat_scroll_down (chat);
1284 else if (priv->current_chat == chat) {
1288 priv->current_chat = chat;
1289 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1290 empathy_chat_messages_read (chat);
1292 chat_window_update_chat_tab (chat);
1296 chat_window_page_added_cb (GtkNotebook *notebook,
1299 EmpathyChatWindow *window)
1301 EmpathyChatWindowPriv *priv;
1304 priv = GET_PRIV (window);
1306 /* If we just received DND to the same window, we don't want
1307 * to do anything here like removing the tab and then readding
1308 * it, so we return here and in "page-added".
1310 if (priv->dnd_same_window) {
1311 DEBUG ("Page added (back to the same window)");
1312 priv->dnd_same_window = FALSE;
1316 DEBUG ("Page added");
1318 /* Get chat object */
1319 chat = EMPATHY_CHAT (child);
1321 /* Connect chat signals for this window */
1322 g_signal_connect (chat, "composing",
1323 G_CALLBACK (chat_window_composing_cb),
1325 g_signal_connect (chat, "new-message",
1326 G_CALLBACK (chat_window_new_message_cb),
1328 g_signal_connect (chat, "notify::tp-chat",
1329 G_CALLBACK (chat_window_update_chat_tab),
1332 /* Set flag so we know to perform some special operations on
1333 * switch page due to the new page being added.
1335 priv->page_added = TRUE;
1337 /* Get list of chats up to date */
1338 priv->chats = g_list_append (priv->chats, chat);
1340 chat_window_update_chat_tab (chat);
1344 chat_window_page_removed_cb (GtkNotebook *notebook,
1347 EmpathyChatWindow *window)
1349 EmpathyChatWindowPriv *priv;
1352 priv = GET_PRIV (window);
1354 /* If we just received DND to the same window, we don't want
1355 * to do anything here like removing the tab and then readding
1356 * it, so we return here and in "page-added".
1358 if (priv->dnd_same_window) {
1359 DEBUG ("Page removed (and will be readded to same window)");
1363 DEBUG ("Page removed");
1365 /* Get chat object */
1366 chat = EMPATHY_CHAT (child);
1368 /* Disconnect all signal handlers for this chat and this window */
1369 g_signal_handlers_disconnect_by_func (chat,
1370 G_CALLBACK (chat_window_composing_cb),
1372 g_signal_handlers_disconnect_by_func (chat,
1373 G_CALLBACK (chat_window_new_message_cb),
1375 g_signal_handlers_disconnect_by_func (chat,
1376 G_CALLBACK (chat_window_update_chat_tab),
1379 /* Keep list of chats up to date */
1380 priv->chats = g_list_remove (priv->chats, chat);
1381 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1382 empathy_chat_messages_read (chat);
1383 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1385 if (priv->chats == NULL) {
1386 g_object_unref (window);
1388 chat_window_update (window);
1393 chat_window_focus_in_event_cb (GtkWidget *widget,
1395 EmpathyChatWindow *window)
1397 EmpathyChatWindowPriv *priv;
1399 DEBUG ("Focus in event, updating title");
1401 priv = GET_PRIV (window);
1403 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1404 empathy_chat_messages_read (priv->current_chat);
1406 chat_window_set_urgency_hint (window, FALSE);
1408 /* Update the title, since we now mark all unread messages as read. */
1409 chat_window_update_chat_tab (priv->current_chat);
1415 chat_window_drag_data_received (GtkWidget *widget,
1416 GdkDragContext *context,
1419 GtkSelectionData *selection,
1422 EmpathyChatWindow *window)
1424 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1425 EmpathyChat *chat = NULL;
1426 EmpathyChatWindow *old_window;
1427 TpAccount *account = NULL;
1428 TpAccountManager *account_manager;
1431 const gchar *account_id;
1432 const gchar *contact_id;
1434 id = (const gchar*) gtk_selection_data_get_data (selection);
1436 /* FIXME: Perhaps should be sure that the account manager is
1437 * prepared before calling _ensure_account on it. */
1438 account_manager = tp_account_manager_dup ();
1440 DEBUG ("DND contact from roster with id:'%s'", id);
1442 strv = g_strsplit (id, ":", 2);
1443 if (g_strv_length (strv) == 2) {
1444 account_id = strv[0];
1445 contact_id = strv[1];
1447 tp_account_manager_ensure_account (account_manager, account_id);
1448 if (account != NULL)
1449 chat = empathy_chat_window_find_chat (account, contact_id);
1452 if (account == NULL) {
1454 gtk_drag_finish (context, FALSE, FALSE, time_);
1459 TpConnection *connection;
1461 connection = tp_account_get_connection (account);
1464 empathy_dispatcher_chat_with_contact_id (
1465 connection, contact_id, NULL, NULL);
1471 g_object_unref (account_manager);
1474 old_window = chat_window_find_chat (chat);
1476 if (old_window == window) {
1477 gtk_drag_finish (context, TRUE, FALSE, time_);
1481 empathy_chat_window_move_chat (old_window, window, chat);
1483 empathy_chat_window_add_chat (window, chat);
1486 /* Added to take care of any outstanding chat events */
1487 empathy_chat_window_present_chat (chat);
1489 /* We should return TRUE to remove the data when doing
1490 * GDK_ACTION_MOVE, but we don't here otherwise it has
1491 * weird consequences, and we handle that internally
1492 * anyway with add_chat () and remove_chat ().
1494 gtk_drag_finish (context, TRUE, FALSE, time_);
1496 else if (info == DND_DRAG_TYPE_TAB) {
1498 EmpathyChatWindow *old_window = NULL;
1502 chat = (void *) gtk_selection_data_get_data (selection);
1503 old_window = chat_window_find_chat (*chat);
1506 EmpathyChatWindowPriv *priv;
1508 priv = GET_PRIV (window);
1510 if (old_window == window) {
1511 DEBUG ("DND tab (within same window)");
1512 priv->dnd_same_window = TRUE;
1513 gtk_drag_finish (context, TRUE, FALSE, time_);
1517 priv->dnd_same_window = FALSE;
1520 /* We should return TRUE to remove the data when doing
1521 * GDK_ACTION_MOVE, but we don't here otherwise it has
1522 * weird consequences, and we handle that internally
1523 * anyway with add_chat () and remove_chat ().
1525 gtk_drag_finish (context, TRUE, FALSE, time_);
1527 DEBUG ("DND from unknown source");
1528 gtk_drag_finish (context, FALSE, FALSE, time_);
1533 chat_window_finalize (GObject *object)
1535 EmpathyChatWindow *window;
1536 EmpathyChatWindowPriv *priv;
1538 window = EMPATHY_CHAT_WINDOW (object);
1539 priv = GET_PRIV (window);
1541 DEBUG ("Finalized: %p", object);
1543 g_object_unref (priv->ui_manager);
1544 g_object_unref (priv->chatroom_manager);
1545 g_object_unref (priv->notify_mgr);
1547 if (priv->notification != NULL) {
1548 notify_notification_close (priv->notification, NULL);
1549 g_object_unref (priv->notification);
1550 priv->notification = NULL;
1551 if (priv->notification_data != NULL)
1553 free_notification_data (priv->notification_data);
1554 priv->notification_data = NULL;
1558 chat_windows = g_list_remove (chat_windows, window);
1559 gtk_widget_destroy (priv->dialog);
1561 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1565 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1567 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1569 object_class->finalize = chat_window_finalize;
1571 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1573 /* Set up a style for the close button with no focus padding. */
1574 gtk_rc_parse_string (
1575 "style \"empathy-close-button-style\"\n"
1577 " GtkWidget::focus-padding = 0\n"
1581 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1583 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1587 empathy_chat_window_init (EmpathyChatWindow *window)
1590 GtkAccelGroup *accel_group;
1595 GtkWidget *chat_vbox;
1597 EmpathySmileyManager *smiley_manager;
1598 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1599 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1601 window->priv = priv;
1602 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1603 gui = empathy_builder_get_file (filename,
1604 "chat_window", &priv->dialog,
1605 "chat_vbox", &chat_vbox,
1606 "ui_manager", &priv->ui_manager,
1607 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1608 "menu_conv_favorite", &priv->menu_conv_favorite,
1609 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1610 "menu_edit_cut", &priv->menu_edit_cut,
1611 "menu_edit_copy", &priv->menu_edit_copy,
1612 "menu_edit_paste", &priv->menu_edit_paste,
1613 "menu_tabs_next", &priv->menu_tabs_next,
1614 "menu_tabs_prev", &priv->menu_tabs_prev,
1615 "menu_tabs_left", &priv->menu_tabs_left,
1616 "menu_tabs_right", &priv->menu_tabs_right,
1617 "menu_tabs_detach", &priv->menu_tabs_detach,
1621 empathy_builder_connect (gui, window,
1622 "menu_conv", "activate", chat_window_conv_activate_cb,
1623 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1624 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1625 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1626 "menu_conv_close", "activate", chat_window_close_activate_cb,
1627 "menu_edit", "activate", chat_window_edit_activate_cb,
1628 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1629 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1630 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1631 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1632 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1633 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1634 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1635 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1636 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1637 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1640 g_object_ref (priv->ui_manager);
1641 g_object_unref (gui);
1643 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1645 priv->notebook = gtk_notebook_new ();
1646 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1647 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1648 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1649 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1650 gtk_widget_show (priv->notebook);
1653 accel_group = gtk_accel_group_new ();
1654 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1656 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1657 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1660 gtk_accel_group_connect (accel_group,
1667 g_object_unref (accel_group);
1669 /* Set up smiley menu */
1670 smiley_manager = empathy_smiley_manager_dup_singleton ();
1671 submenu = empathy_smiley_menu_new (smiley_manager,
1672 chat_window_insert_smiley_activate_cb,
1674 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1675 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1676 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1677 g_object_unref (smiley_manager);
1679 /* Set up signals we can't do with ui file since we may need to
1680 * block/unblock them at some later stage.
1683 g_signal_connect (priv->dialog,
1685 G_CALLBACK (chat_window_delete_event_cb),
1687 g_signal_connect (priv->dialog,
1689 G_CALLBACK (chat_window_focus_in_event_cb),
1691 g_signal_connect_after (priv->notebook,
1693 G_CALLBACK (chat_window_page_switched_cb),
1695 g_signal_connect (priv->notebook,
1697 G_CALLBACK (chat_window_page_added_cb),
1699 g_signal_connect (priv->notebook,
1701 G_CALLBACK (chat_window_page_removed_cb),
1704 /* Set up drag and drop */
1705 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1706 GTK_DEST_DEFAULT_ALL,
1708 G_N_ELEMENTS (drag_types_dest),
1711 g_signal_connect (priv->notebook,
1712 "drag-data-received",
1713 G_CALLBACK (chat_window_drag_data_received),
1716 chat_windows = g_list_prepend (chat_windows, window);
1718 /* Set up private details */
1720 priv->chats_new_msg = NULL;
1721 priv->chats_composing = NULL;
1722 priv->current_chat = NULL;
1724 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1728 empathy_chat_window_new (void)
1730 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1733 /* Returns the window to open a new tab in if there is only one window
1734 * visble, otherwise, returns NULL indicating that a new window should
1738 empathy_chat_window_get_default (void)
1741 gboolean separate_windows = TRUE;
1743 empathy_conf_get_bool (empathy_conf_get (),
1744 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1747 if (separate_windows) {
1748 /* Always create a new window */
1752 for (l = chat_windows; l; l = l->next) {
1753 EmpathyChatWindow *chat_window;
1756 chat_window = l->data;
1758 dialog = empathy_chat_window_get_dialog (chat_window);
1759 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1760 /* Found a visible window on this desktop */
1769 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1771 EmpathyChatWindowPriv *priv;
1773 g_return_val_if_fail (window != NULL, NULL);
1775 priv = GET_PRIV (window);
1777 return priv->dialog;
1781 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1784 EmpathyChatWindowPriv *priv;
1786 GtkWidget *popup_label;
1789 g_return_if_fail (window != NULL);
1790 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1792 priv = GET_PRIV (window);
1794 /* Reference the chat object */
1795 g_object_ref (chat);
1797 /* If this window has just been created, position it */
1798 if (priv->chats == NULL) {
1799 const gchar *name = "chat-window";
1800 gboolean separate_windows;
1802 empathy_conf_get_bool (empathy_conf_get (),
1803 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1806 if (separate_windows) {
1807 name = empathy_chat_get_id (chat);
1810 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
1813 child = GTK_WIDGET (chat);
1814 label = chat_window_create_label (window, chat, TRUE);
1815 popup_label = chat_window_create_label (window, chat, FALSE);
1816 gtk_widget_show (child);
1818 g_signal_connect (chat, "notify::name",
1819 G_CALLBACK (chat_window_chat_notify_cb),
1821 g_signal_connect (chat, "notify::subject",
1822 G_CALLBACK (chat_window_chat_notify_cb),
1824 g_signal_connect (chat, "notify::remote-contact",
1825 G_CALLBACK (chat_window_chat_notify_cb),
1827 chat_window_chat_notify_cb (chat);
1829 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1830 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1831 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1832 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1833 TRUE, TRUE, GTK_PACK_START);
1835 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1839 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1842 EmpathyChatWindowPriv *priv;
1844 EmpathyContact *remote_contact;
1846 g_return_if_fail (window != NULL);
1847 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1849 priv = GET_PRIV (window);
1851 g_signal_handlers_disconnect_by_func (chat,
1852 chat_window_chat_notify_cb,
1854 remote_contact = g_object_get_data (G_OBJECT (chat),
1855 "chat-window-remote-contact");
1856 if (remote_contact) {
1857 g_signal_handlers_disconnect_by_func (remote_contact,
1858 chat_window_update_chat_tab,
1862 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1864 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1866 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1868 g_object_unref (chat);
1872 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1873 EmpathyChatWindow *new_window,
1878 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1879 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1880 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1882 widget = GTK_WIDGET (chat);
1884 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1885 G_OBJECT (widget)->ref_count);
1887 /* We reference here to make sure we don't loose the widget
1888 * and the EmpathyChat object during the move.
1890 g_object_ref (chat);
1891 g_object_ref (widget);
1893 empathy_chat_window_remove_chat (old_window, chat);
1894 empathy_chat_window_add_chat (new_window, chat);
1896 g_object_unref (widget);
1897 g_object_unref (chat);
1901 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1904 EmpathyChatWindowPriv *priv;
1907 g_return_if_fail (window != NULL);
1908 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1910 priv = GET_PRIV (window);
1912 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1914 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1919 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1921 EmpathyChatWindowPriv *priv;
1924 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1926 priv = GET_PRIV (window);
1928 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1934 empathy_chat_window_find_chat (TpAccount *account,
1939 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1941 for (l = chat_windows; l; l = l->next) {
1942 EmpathyChatWindowPriv *priv;
1943 EmpathyChatWindow *window;
1947 priv = GET_PRIV (window);
1949 for (ll = priv->chats; ll; ll = ll->next) {
1954 if (account == empathy_chat_get_account (chat) &&
1955 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1965 empathy_chat_window_present_chat (EmpathyChat *chat)
1967 EmpathyChatWindow *window;
1968 EmpathyChatWindowPriv *priv;
1970 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1972 window = chat_window_find_chat (chat);
1974 /* If the chat has no window, create one */
1975 if (window == NULL) {
1976 window = empathy_chat_window_get_default ();
1978 window = empathy_chat_window_new ();
1981 empathy_chat_window_add_chat (window, chat);
1984 priv = GET_PRIV (window);
1985 empathy_chat_window_switch_to_chat (window, chat);
1986 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1988 gtk_widget_grab_focus (chat->input_text_view);