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>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <libnotify/notification.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.h>
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy/empathy-chatroom-manager.h>
44 #include <libempathy/empathy-account-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>
56 #include "empathy-chat-window.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-misc.h"
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
65 EmpathyChat *current_chat;
68 GList *chats_composing;
70 gboolean dnd_same_window;
71 guint save_geometry_id;
72 EmpathyChatroomManager *chatroom_manager;
75 NotifyNotification *notification;
78 GtkUIManager *ui_manager;
79 GtkAction *menu_conv_insert_smiley;
80 GtkAction *menu_conv_favorite;
81 GtkAction *menu_conv_toggle_contacts;
83 GtkAction *menu_edit_cut;
84 GtkAction *menu_edit_copy;
85 GtkAction *menu_edit_paste;
87 GtkAction *menu_tabs_next;
88 GtkAction *menu_tabs_prev;
89 GtkAction *menu_tabs_left;
90 GtkAction *menu_tabs_right;
91 GtkAction *menu_tabs_detach;
92 } EmpathyChatWindowPriv;
94 static GList *chat_windows = NULL;
96 static const guint tab_accel_keys[] = {
97 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
98 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
102 DND_DRAG_TYPE_CONTACT_ID,
106 static const GtkTargetEntry drag_types_dest[] = {
107 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
108 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
111 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
114 chat_window_accel_cb (GtkAccelGroup *accelgroup,
118 EmpathyChatWindow *window)
120 EmpathyChatWindowPriv *priv;
124 priv = GET_PRIV (window);
126 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
127 if (tab_accel_keys[i] == key) {
134 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
138 static EmpathyChatWindow *
139 chat_window_find_chat (EmpathyChat *chat)
141 EmpathyChatWindowPriv *priv;
144 for (l = chat_windows; l; l = l->next) {
145 priv = GET_PRIV (l->data);
146 ll = g_list_find (priv->chats, chat);
156 chat_window_close_clicked_cb (GtkAction *action,
159 EmpathyChatWindow *window;
161 window = chat_window_find_chat (chat);
162 empathy_chat_window_remove_chat (window, chat);
166 chat_tab_style_set_cb (GtkWidget *hbox,
167 GtkStyle *previous_style,
171 int char_width, h, w;
172 PangoContext *context;
173 PangoFontMetrics *metrics;
175 button = g_object_get_data (G_OBJECT (user_data),
176 "chat-window-tab-close-button");
177 context = gtk_widget_get_pango_context (hbox);
179 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
180 pango_context_get_language (context));
181 char_width = pango_font_metrics_get_approximate_char_width (metrics);
182 pango_font_metrics_unref (metrics);
184 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
185 GTK_ICON_SIZE_MENU, &w, &h);
187 /* Request at least about 12 chars width plus at least space for the status
188 * image and the close button */
189 gtk_widget_set_size_request (hbox,
190 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
192 gtk_widget_set_size_request (button, w, h);
196 chat_window_create_label (EmpathyChatWindow *window,
198 gboolean is_tab_label)
200 EmpathyChatWindowPriv *priv;
202 GtkWidget *name_label;
203 GtkWidget *status_image;
204 GtkWidget *close_button;
205 GtkWidget *close_image;
206 GtkWidget *event_box;
207 GtkWidget *event_box_hbox;
208 PangoAttrList *attr_list;
209 PangoAttribute *attr;
211 priv = GET_PRIV (window);
213 /* The spacing between the button and the label. */
214 hbox = gtk_hbox_new (FALSE, 0);
216 event_box = gtk_event_box_new ();
217 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
219 name_label = gtk_label_new (NULL);
221 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
223 attr_list = pango_attr_list_new ();
224 attr = pango_attr_scale_new (1/1.2);
225 attr->start_index = 0;
226 attr->end_index = -1;
227 pango_attr_list_insert (attr_list, attr);
228 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
229 pango_attr_list_unref (attr_list);
231 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
232 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
233 g_object_set_data (G_OBJECT (chat),
234 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
237 status_image = gtk_image_new ();
239 /* Spacing between the icon and label. */
240 event_box_hbox = gtk_hbox_new (FALSE, 0);
242 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
243 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
245 g_object_set_data (G_OBJECT (chat),
246 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
248 g_object_set_data (G_OBJECT (chat),
249 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
252 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
253 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
256 close_button = gtk_button_new ();
257 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
258 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
260 /* We don't want focus/keynav for the button to avoid clutter, and
261 * Ctrl-W works anyway.
263 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
264 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
266 /* Set the name to make the special rc style match. */
267 gtk_widget_set_name (close_button, "empathy-close-button");
269 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
271 gtk_container_add (GTK_CONTAINER (close_button), close_image);
273 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
275 g_signal_connect (close_button,
277 G_CALLBACK (chat_window_close_clicked_cb),
280 /* React to theme changes and also setup the size correctly. */
281 g_signal_connect (hbox,
283 G_CALLBACK (chat_tab_style_set_cb),
287 gtk_widget_show_all (hbox);
293 chat_window_update (EmpathyChatWindow *window)
295 EmpathyChatWindowPriv *priv = GET_PRIV (window);
298 gboolean is_connected;
305 EmpathyContact *remote_contact;
306 gboolean avatar_in_icon;
308 GtkWidget *chat_close_button;
312 /* Get information */
313 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
314 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
315 first_page = (page_num == 0);
316 last_page = (page_num == (num_pages - 1));
317 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
318 name = empathy_chat_get_name (priv->current_chat);
319 n_chats = g_list_length (priv->chats);
321 DEBUG ("Update window");
324 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
325 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
326 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
327 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
328 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
329 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
331 /* Update Contact menu */
332 menu = gtk_ui_manager_get_widget (priv->ui_manager,
333 "/chats_menubar/menu_contact");
334 submenu = empathy_chat_get_contact_menu (priv->current_chat);
335 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
336 gtk_widget_show (menu);
338 /* Update window title */
340 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
344 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
345 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
349 /* Update window icon */
350 if (priv->chats_new_msg) {
351 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
352 EMPATHY_IMAGE_MESSAGE);
354 empathy_conf_get_bool (empathy_conf_get (),
355 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
358 if (n_chats == 1 && avatar_in_icon) {
359 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
360 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
361 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
364 g_object_unref (icon);
367 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
371 if (num_pages == 1) {
372 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
373 chat_close_button = g_object_get_data (G_OBJECT (chat),
374 "chat-window-tab-close-button");
375 gtk_widget_hide (chat_close_button);
377 for (i=0; i<num_pages; i++) {
378 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
379 chat_close_button = g_object_get_data (G_OBJECT (chat),
380 "chat-window-tab-close-button");
381 gtk_widget_show (chat_close_button);
387 append_markup_printf (GString *string,
394 va_start (args, format);
396 tmp = g_markup_vprintf_escaped (format, args);
397 g_string_append (string, tmp);
404 chat_window_update_chat_tab (EmpathyChat *chat)
406 EmpathyChatWindow *window;
407 EmpathyChatWindowPriv *priv;
408 EmpathyContact *remote_contact;
411 EmpathyAccount *account;
412 const gchar *subject;
413 const gchar *status = NULL;
417 const gchar *icon_name;
419 window = chat_window_find_chat (chat);
423 priv = GET_PRIV (window);
425 /* Get information */
426 name = empathy_chat_get_name (chat);
427 account = empathy_chat_get_account (chat);
428 subject = empathy_chat_get_subject (chat);
429 remote_contact = empathy_chat_get_remote_contact (chat);
431 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
432 name, empathy_account_get_unique_name (account), subject, remote_contact);
434 /* Update tab image */
435 if (g_list_find (priv->chats_new_msg, chat)) {
436 icon_name = EMPATHY_IMAGE_MESSAGE;
438 else if (g_list_find (priv->chats_composing, chat)) {
439 icon_name = EMPATHY_IMAGE_TYPING;
441 else if (remote_contact) {
442 icon_name = empathy_icon_name_for_contact (remote_contact);
444 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
446 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
447 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
448 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
449 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
451 /* Update tab tooltip */
452 tooltip = g_string_new (NULL);
454 if (remote_contact) {
455 id = empathy_contact_get_id (remote_contact);
456 status = empathy_contact_get_presence_message (remote_contact);
461 append_markup_printf (tooltip,
462 "<b>%s</b><small> (%s)</small>",
464 empathy_account_get_display_name (account));
466 if (!EMP_STR_EMPTY (status)) {
467 append_markup_printf (tooltip, "\n<i>%s</i>", status);
471 append_markup_printf (tooltip, "\n<b>%s</b> %s",
472 _("Topic:"), subject);
475 if (g_list_find (priv->chats_composing, chat)) {
476 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
479 markup = g_string_free (tooltip, FALSE);
480 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
481 gtk_widget_set_tooltip_markup (widget, markup);
482 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
483 gtk_widget_set_tooltip_markup (widget, markup);
486 /* Update tab and menu label */
487 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
488 gtk_label_set_text (GTK_LABEL (widget), name);
489 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
490 gtk_label_set_text (GTK_LABEL (widget), name);
492 /* Update the window if it's the current chat */
493 if (priv->current_chat == chat) {
494 chat_window_update (window);
499 chat_window_chat_notify_cb (EmpathyChat *chat)
501 EmpathyContact *old_remote_contact;
502 EmpathyContact *remote_contact = NULL;
504 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
505 remote_contact = empathy_chat_get_remote_contact (chat);
507 if (old_remote_contact != remote_contact) {
508 /* The remote-contact associated with the chat changed, we need
509 * to keep track of any change of that contact and update the
510 * window each time. */
511 if (remote_contact) {
512 g_signal_connect_swapped (remote_contact, "notify",
513 G_CALLBACK (chat_window_update_chat_tab),
516 if (old_remote_contact) {
517 g_signal_handlers_disconnect_by_func (old_remote_contact,
518 chat_window_update_chat_tab,
522 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
526 chat_window_update_chat_tab (chat);
530 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
531 EmpathySmiley *smiley,
534 EmpathyChatWindowPriv *priv = GET_PRIV (window);
536 GtkTextBuffer *buffer;
539 chat = priv->current_chat;
541 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
542 gtk_text_buffer_get_end_iter (buffer, &iter);
543 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
547 chat_window_conv_activate_cb (GtkAction *action,
548 EmpathyChatWindow *window)
550 EmpathyChatWindowPriv *priv = GET_PRIV (window);
553 EmpathyContact *remote_contact = NULL;
555 /* Favorite room menu */
556 is_room = empathy_chat_is_room (priv->current_chat);
559 EmpathyAccount *account;
560 gboolean found = FALSE;
561 EmpathyChatroom *chatroom;
563 room = empathy_chat_get_id (priv->current_chat);
564 account = empathy_chat_get_account (priv->current_chat);
565 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
567 if (chatroom != NULL)
568 found = empathy_chatroom_is_favorite (chatroom);
570 DEBUG ("This room %s favorite", found ? "is" : "is not");
571 gtk_toggle_action_set_active (
572 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
574 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
576 /* Show contacts menu */
577 g_object_get (priv->current_chat,
578 "remote-contact", &remote_contact,
579 "show-contacts", &active,
581 if (remote_contact == NULL) {
582 gtk_toggle_action_set_active (
583 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
586 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
587 (remote_contact == NULL));
588 if (remote_contact != NULL) {
589 g_object_unref (remote_contact);
594 chat_window_clear_activate_cb (GtkAction *action,
595 EmpathyChatWindow *window)
597 EmpathyChatWindowPriv *priv = GET_PRIV (window);
599 empathy_chat_clear (priv->current_chat);
603 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
604 EmpathyChatWindow *window)
606 EmpathyChatWindowPriv *priv = GET_PRIV (window);
608 EmpathyAccount *account;
610 EmpathyChatroom *chatroom;
612 active = gtk_toggle_action_get_active (toggle_action);
613 account = empathy_chat_get_account (priv->current_chat);
614 room = empathy_chat_get_id (priv->current_chat);
616 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
619 if (chatroom == NULL) {
622 name = empathy_chat_get_name (priv->current_chat);
623 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
624 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
625 g_object_unref (chatroom);
628 empathy_chatroom_set_favorite (chatroom, active);
632 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
633 EmpathyChatWindow *window)
635 EmpathyChatWindowPriv *priv = GET_PRIV (window);
638 active = gtk_toggle_action_get_active (toggle_action);
640 empathy_chat_set_show_contacts (priv->current_chat, active);
644 chat_get_window_id_for_geometry (EmpathyChat *chat)
646 const gchar *res = NULL;
647 gboolean separate_windows;
649 empathy_conf_get_bool (empathy_conf_get (),
650 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
653 if (separate_windows) {
654 res = empathy_chat_get_id (chat);
657 return res ? res : "chat-window";
661 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
663 EmpathyChatWindowPriv *priv;
666 priv = GET_PRIV (window);
668 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
669 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
671 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
674 priv->save_geometry_id = 0;
680 chat_window_configure_event_cb (GtkWidget *widget,
681 GdkEventConfigure *event,
682 EmpathyChatWindow *window)
684 EmpathyChatWindowPriv *priv;
686 priv = GET_PRIV (window);
688 if (priv->save_geometry_id != 0) {
689 g_source_remove (priv->save_geometry_id);
692 priv->save_geometry_id =
693 g_timeout_add_seconds (1,
694 (GSourceFunc) chat_window_save_geometry_timeout_cb,
701 chat_window_close_activate_cb (GtkAction *action,
702 EmpathyChatWindow *window)
704 EmpathyChatWindowPriv *priv;
706 priv = GET_PRIV (window);
708 g_return_if_fail (priv->current_chat != NULL);
710 empathy_chat_window_remove_chat (window, priv->current_chat);
714 chat_window_edit_activate_cb (GtkAction *action,
715 EmpathyChatWindow *window)
717 EmpathyChatWindowPriv *priv;
718 GtkClipboard *clipboard;
719 GtkTextBuffer *buffer;
720 gboolean text_available;
722 priv = GET_PRIV (window);
724 g_return_if_fail (priv->current_chat != NULL);
726 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
727 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
728 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
729 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
733 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
734 if (gtk_text_buffer_get_has_selection (buffer)) {
735 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
736 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
740 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
742 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
743 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
746 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
747 text_available = gtk_clipboard_wait_is_text_available (clipboard);
748 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
752 chat_window_cut_activate_cb (GtkAction *action,
753 EmpathyChatWindow *window)
755 EmpathyChatWindowPriv *priv;
757 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
759 priv = GET_PRIV (window);
761 empathy_chat_cut (priv->current_chat);
765 chat_window_copy_activate_cb (GtkAction *action,
766 EmpathyChatWindow *window)
768 EmpathyChatWindowPriv *priv;
770 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
772 priv = GET_PRIV (window);
774 empathy_chat_copy (priv->current_chat);
778 chat_window_paste_activate_cb (GtkAction *action,
779 EmpathyChatWindow *window)
781 EmpathyChatWindowPriv *priv;
783 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
785 priv = GET_PRIV (window);
787 empathy_chat_paste (priv->current_chat);
791 chat_window_tabs_left_activate_cb (GtkAction *action,
792 EmpathyChatWindow *window)
794 EmpathyChatWindowPriv *priv;
798 priv = GET_PRIV (window);
800 chat = priv->current_chat;
801 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
806 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
812 chat_window_tabs_right_activate_cb (GtkAction *action,
813 EmpathyChatWindow *window)
815 EmpathyChatWindowPriv *priv;
819 priv = GET_PRIV (window);
821 chat = priv->current_chat;
822 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
824 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
830 chat_window_detach_activate_cb (GtkAction *action,
831 EmpathyChatWindow *window)
833 EmpathyChatWindowPriv *priv;
834 EmpathyChatWindow *new_window;
837 priv = GET_PRIV (window);
839 chat = priv->current_chat;
840 new_window = empathy_chat_window_new ();
842 empathy_chat_window_move_chat (window, new_window, chat);
844 priv = GET_PRIV (new_window);
845 gtk_widget_show (priv->dialog);
849 chat_window_help_contents_activate_cb (GtkAction *action,
850 EmpathyChatWindow *window)
852 EmpathyChatWindowPriv *priv = GET_PRIV (window);
854 empathy_url_show (priv->dialog, "ghelp:empathy?chat");
858 chat_window_help_about_activate_cb (GtkAction *action,
859 EmpathyChatWindow *window)
861 EmpathyChatWindowPriv *priv = GET_PRIV (window);
863 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
867 chat_window_delete_event_cb (GtkWidget *dialog,
869 EmpathyChatWindow *window)
871 EmpathyChatWindowPriv *priv = GET_PRIV (window);
873 DEBUG ("Delete event received");
875 g_object_ref (window);
876 while (priv->chats) {
877 empathy_chat_window_remove_chat (window, priv->chats->data);
879 g_object_unref (window);
885 chat_window_composing_cb (EmpathyChat *chat,
886 gboolean is_composing,
887 EmpathyChatWindow *window)
889 EmpathyChatWindowPriv *priv;
891 priv = GET_PRIV (window);
893 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
894 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
896 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
899 chat_window_update_chat_tab (chat);
903 chat_window_set_urgency_hint (EmpathyChatWindow *window,
906 EmpathyChatWindowPriv *priv;
908 priv = GET_PRIV (window);
910 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
911 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
915 EmpathyChatWindow *window;
920 chat_window_notification_closed_cb (NotifyNotification *notify,
921 NotificationData *cb_data)
923 EmpathyNotificationClosedReason reason = 0;
924 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
926 #ifdef notify_notification_get_closed_reason
927 reason = notify_notification_get_closed_reason (notify);
929 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
930 empathy_chat_window_present_chat (cb_data->chat);
933 g_object_unref (notify);
934 priv->notification = NULL;
935 g_object_unref (cb_data->chat);
936 g_slice_free (NotificationData, cb_data);
940 chat_window_show_or_update_notification (EmpathyChatWindow *window,
941 EmpathyMessage *message,
944 EmpathyContact *sender;
949 NotificationData *cb_data;
950 EmpathyChatWindowPriv *priv = GET_PRIV (window);
953 if (!empathy_notification_is_enabled ()) {
956 empathy_conf_get_bool (empathy_conf_get (),
957 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
963 cb_data = g_slice_new0 (NotificationData);
964 cb_data->chat = g_object_ref (chat);
965 cb_data->window = window;
967 sender = empathy_message_get_sender (message);
968 header = empathy_contact_get_name (sender);
969 body = empathy_message_get_body (message);
970 escaped = g_markup_escape_text (body, -1);
972 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
974 if (priv->notification != NULL) {
975 notify_notification_update (priv->notification,
976 header, escaped, NULL);
977 /* if icon doesn't exist libnotify will crash */
979 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
981 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
982 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
983 /* if icon doesn't exist libnotify will crash */
985 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
987 g_signal_connect (priv->notification, "closed",
988 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
991 notify_notification_show (priv->notification, NULL);
993 g_object_unref (pixbuf);
998 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1003 if (empathy_chat_is_room (chat) == FALSE)
1006 markup = g_markup_printf_escaped ("<span color=\"%s\">%s</span>",
1008 empathy_chat_get_name (chat));
1010 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1011 gtk_label_set_markup (GTK_LABEL (widget), markup);
1016 chat_window_new_message_cb (EmpathyChat *chat,
1017 EmpathyMessage *message,
1018 EmpathyChatWindow *window)
1020 EmpathyChatWindowPriv *priv;
1022 gboolean needs_urgency;
1023 EmpathyContact *sender;
1025 priv = GET_PRIV (window);
1027 has_focus = empathy_chat_window_has_focus (window);
1029 /* - if we're the sender, we play the sound if it's specified in the
1030 * preferences and we're not away.
1031 * - if we receive a message, we play the sound if it's specified in the
1032 * preferences and the window does not have focus on the chat receiving
1036 sender = empathy_message_get_sender (message);
1038 if (empathy_contact_is_user (sender)) {
1039 empathy_sound_play (GTK_WIDGET (priv->dialog),
1040 EMPATHY_SOUND_MESSAGE_OUTGOING);
1043 if (has_focus && priv->current_chat == chat) {
1047 if (!g_list_find (priv->chats_new_msg, chat)) {
1048 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1049 chat_window_update_chat_tab (chat);
1052 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1053 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1054 * an unamed MUC (msn-like).
1055 * In case of a MUC, we set urgency only if the message contains our
1057 if (empathy_chat_is_room (chat) ||
1058 empathy_chat_get_remote_contact (chat) == NULL) {
1059 needs_urgency = empathy_message_should_highlight (message);
1061 needs_urgency = TRUE;
1064 if (needs_urgency) {
1066 chat_window_set_urgency_hint (window, TRUE);
1067 chat_window_set_highlight_room_tab_label (chat);
1070 empathy_sound_play (GTK_WIDGET (priv->dialog),
1071 EMPATHY_SOUND_MESSAGE_INCOMING);
1072 chat_window_show_or_update_notification (window, message, chat);
1076 static GtkNotebook *
1077 chat_window_detach_hook (GtkNotebook *source,
1083 EmpathyChatWindowPriv *priv;
1084 EmpathyChatWindow *window, *new_window;
1087 chat = EMPATHY_CHAT (page);
1088 window = chat_window_find_chat (chat);
1090 new_window = empathy_chat_window_new ();
1091 priv = GET_PRIV (new_window);
1093 DEBUG ("Detach hook called");
1095 empathy_chat_window_move_chat (window, new_window, chat);
1097 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1098 gtk_widget_show (priv->dialog);
1104 chat_window_page_switched_cb (GtkNotebook *notebook,
1105 GtkNotebookPage *page,
1107 EmpathyChatWindow *window)
1109 EmpathyChatWindowPriv *priv;
1113 DEBUG ("Page switched");
1115 priv = GET_PRIV (window);
1117 child = gtk_notebook_get_nth_page (notebook, page_num);
1118 chat = EMPATHY_CHAT (child);
1120 if (priv->page_added) {
1121 priv->page_added = FALSE;
1122 empathy_chat_scroll_down (chat);
1124 else if (priv->current_chat == chat) {
1128 priv->current_chat = chat;
1129 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1131 chat_window_update_chat_tab (chat);
1135 chat_window_page_added_cb (GtkNotebook *notebook,
1138 EmpathyChatWindow *window)
1140 EmpathyChatWindowPriv *priv;
1143 priv = GET_PRIV (window);
1145 /* If we just received DND to the same window, we don't want
1146 * to do anything here like removing the tab and then readding
1147 * it, so we return here and in "page-added".
1149 if (priv->dnd_same_window) {
1150 DEBUG ("Page added (back to the same window)");
1151 priv->dnd_same_window = FALSE;
1155 DEBUG ("Page added");
1157 /* Get chat object */
1158 chat = EMPATHY_CHAT (child);
1160 /* Connect chat signals for this window */
1161 g_signal_connect (chat, "composing",
1162 G_CALLBACK (chat_window_composing_cb),
1164 g_signal_connect (chat, "new-message",
1165 G_CALLBACK (chat_window_new_message_cb),
1168 /* Set flag so we know to perform some special operations on
1169 * switch page due to the new page being added.
1171 priv->page_added = TRUE;
1173 /* Get list of chats up to date */
1174 priv->chats = g_list_append (priv->chats, chat);
1176 chat_window_update_chat_tab (chat);
1180 chat_window_page_removed_cb (GtkNotebook *notebook,
1183 EmpathyChatWindow *window)
1185 EmpathyChatWindowPriv *priv;
1188 priv = GET_PRIV (window);
1190 /* If we just received DND to the same window, we don't want
1191 * to do anything here like removing the tab and then readding
1192 * it, so we return here and in "page-added".
1194 if (priv->dnd_same_window) {
1195 DEBUG ("Page removed (and will be readded to same window)");
1199 DEBUG ("Page removed");
1201 /* Get chat object */
1202 chat = EMPATHY_CHAT (child);
1204 /* Disconnect all signal handlers for this chat and this window */
1205 g_signal_handlers_disconnect_by_func (chat,
1206 G_CALLBACK (chat_window_composing_cb),
1208 g_signal_handlers_disconnect_by_func (chat,
1209 G_CALLBACK (chat_window_new_message_cb),
1212 /* Keep list of chats up to date */
1213 priv->chats = g_list_remove (priv->chats, chat);
1214 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1215 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1217 if (priv->chats == NULL) {
1218 g_object_unref (window);
1220 chat_window_update (window);
1225 chat_window_focus_in_event_cb (GtkWidget *widget,
1227 EmpathyChatWindow *window)
1229 EmpathyChatWindowPriv *priv;
1231 DEBUG ("Focus in event, updating title");
1233 priv = GET_PRIV (window);
1235 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1237 chat_window_set_urgency_hint (window, FALSE);
1239 /* Update the title, since we now mark all unread messages as read. */
1240 chat_window_update_chat_tab (priv->current_chat);
1246 chat_window_drag_data_received (GtkWidget *widget,
1247 GdkDragContext *context,
1250 GtkSelectionData *selection,
1253 EmpathyChatWindow *window)
1255 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1257 EmpathyChatWindow *old_window;
1258 EmpathyAccount *account;
1259 EmpathyAccountManager *account_manager;
1262 const gchar *account_id;
1263 const gchar *contact_id;
1265 id = (const gchar*) gtk_selection_data_get_data (selection);
1266 account_manager = empathy_account_manager_dup_singleton ();
1268 DEBUG ("DND contact from roster with id:'%s'", id);
1270 strv = g_strsplit (id, "/", 2);
1271 account_id = strv[0];
1272 contact_id = strv[1];
1273 account = empathy_account_manager_lookup (account_manager, account_id);
1274 chat = empathy_chat_window_find_chat (account, contact_id);
1277 TpConnection *connection;
1279 connection = empathy_account_get_connection (account);
1282 empathy_dispatcher_chat_with_contact_id (
1283 connection, contact_id, NULL, NULL);
1286 g_object_unref (account);
1290 g_object_unref (account);
1291 g_object_unref (account_manager);
1294 old_window = chat_window_find_chat (chat);
1296 if (old_window == window) {
1297 gtk_drag_finish (context, TRUE, FALSE, time);
1301 empathy_chat_window_move_chat (old_window, window, chat);
1303 empathy_chat_window_add_chat (window, chat);
1306 /* Added to take care of any outstanding chat events */
1307 empathy_chat_window_present_chat (chat);
1309 /* We should return TRUE to remove the data when doing
1310 * GDK_ACTION_MOVE, but we don't here otherwise it has
1311 * weird consequences, and we handle that internally
1312 * anyway with add_chat () and remove_chat ().
1314 gtk_drag_finish (context, TRUE, FALSE, time);
1316 else if (info == DND_DRAG_TYPE_TAB) {
1318 EmpathyChatWindow *old_window = NULL;
1322 chat = (void *) gtk_selection_data_get_data (selection);
1323 old_window = chat_window_find_chat (*chat);
1326 EmpathyChatWindowPriv *priv;
1328 priv = GET_PRIV (window);
1330 if (old_window == window) {
1331 DEBUG ("DND tab (within same window)");
1332 priv->dnd_same_window = TRUE;
1333 gtk_drag_finish (context, TRUE, FALSE, time);
1337 priv->dnd_same_window = FALSE;
1340 /* We should return TRUE to remove the data when doing
1341 * GDK_ACTION_MOVE, but we don't here otherwise it has
1342 * weird consequences, and we handle that internally
1343 * anyway with add_chat () and remove_chat ().
1345 gtk_drag_finish (context, TRUE, FALSE, time);
1347 DEBUG ("DND from unknown source");
1348 gtk_drag_finish (context, FALSE, FALSE, time);
1353 chat_window_finalize (GObject *object)
1355 EmpathyChatWindow *window;
1356 EmpathyChatWindowPriv *priv;
1358 window = EMPATHY_CHAT_WINDOW (object);
1359 priv = GET_PRIV (window);
1361 DEBUG ("Finalized: %p", object);
1363 g_object_unref (priv->ui_manager);
1364 g_object_unref (priv->chatroom_manager);
1365 if (priv->save_geometry_id != 0) {
1366 g_source_remove (priv->save_geometry_id);
1369 if (priv->notification != NULL) {
1370 notify_notification_close (priv->notification, NULL);
1371 g_object_unref (priv->notification);
1372 priv->notification = NULL;
1375 chat_windows = g_list_remove (chat_windows, window);
1376 gtk_widget_destroy (priv->dialog);
1378 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1382 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1384 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1386 object_class->finalize = chat_window_finalize;
1388 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1390 /* Set up a style for the close button with no focus padding. */
1391 gtk_rc_parse_string (
1392 "style \"empathy-close-button-style\"\n"
1394 " GtkWidget::focus-padding = 0\n"
1398 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1400 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1404 empathy_chat_window_init (EmpathyChatWindow *window)
1407 GtkAccelGroup *accel_group;
1412 GtkWidget *chat_vbox;
1414 EmpathySmileyManager *smiley_manager;
1415 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1416 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1418 window->priv = priv;
1419 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1420 gui = empathy_builder_get_file (filename,
1421 "chat_window", &priv->dialog,
1422 "chat_vbox", &chat_vbox,
1423 "ui_manager", &priv->ui_manager,
1424 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1425 "menu_conv_favorite", &priv->menu_conv_favorite,
1426 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1427 "menu_edit_cut", &priv->menu_edit_cut,
1428 "menu_edit_copy", &priv->menu_edit_copy,
1429 "menu_edit_paste", &priv->menu_edit_paste,
1430 "menu_tabs_next", &priv->menu_tabs_next,
1431 "menu_tabs_prev", &priv->menu_tabs_prev,
1432 "menu_tabs_left", &priv->menu_tabs_left,
1433 "menu_tabs_right", &priv->menu_tabs_right,
1434 "menu_tabs_detach", &priv->menu_tabs_detach,
1438 empathy_builder_connect (gui, window,
1439 "chat_window", "configure-event", chat_window_configure_event_cb,
1440 "menu_conv", "activate", chat_window_conv_activate_cb,
1441 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1442 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1443 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1444 "menu_conv_close", "activate", chat_window_close_activate_cb,
1445 "menu_edit", "activate", chat_window_edit_activate_cb,
1446 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1447 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1448 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1449 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1450 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1451 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1452 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1453 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1456 g_object_ref (priv->ui_manager);
1457 g_object_unref (gui);
1459 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1461 priv->notebook = gtk_notebook_new ();
1462 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1463 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1464 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1465 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1466 gtk_widget_show (priv->notebook);
1469 accel_group = gtk_accel_group_new ();
1470 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1472 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1473 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1476 gtk_accel_group_connect (accel_group,
1483 g_object_unref (accel_group);
1485 /* Set up smiley menu */
1486 smiley_manager = empathy_smiley_manager_dup_singleton ();
1487 submenu = empathy_smiley_menu_new (smiley_manager,
1488 chat_window_insert_smiley_activate_cb,
1490 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1491 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1492 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1493 g_object_unref (smiley_manager);
1495 /* Set up signals we can't do with ui file since we may need to
1496 * block/unblock them at some later stage.
1499 g_signal_connect (priv->dialog,
1501 G_CALLBACK (chat_window_delete_event_cb),
1504 g_signal_connect_swapped (priv->menu_tabs_prev,
1506 G_CALLBACK (gtk_notebook_prev_page),
1508 g_signal_connect_swapped (priv->menu_tabs_next,
1510 G_CALLBACK (gtk_notebook_next_page),
1513 g_signal_connect (priv->dialog,
1515 G_CALLBACK (chat_window_focus_in_event_cb),
1517 g_signal_connect_after (priv->notebook,
1519 G_CALLBACK (chat_window_page_switched_cb),
1521 g_signal_connect (priv->notebook,
1523 G_CALLBACK (chat_window_page_added_cb),
1525 g_signal_connect (priv->notebook,
1527 G_CALLBACK (chat_window_page_removed_cb),
1530 /* Set up drag and drop */
1531 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1532 GTK_DEST_DEFAULT_ALL,
1534 G_N_ELEMENTS (drag_types_dest),
1537 g_signal_connect (priv->notebook,
1538 "drag-data-received",
1539 G_CALLBACK (chat_window_drag_data_received),
1542 chat_windows = g_list_prepend (chat_windows, window);
1544 /* Set up private details */
1546 priv->chats_new_msg = NULL;
1547 priv->chats_composing = NULL;
1548 priv->current_chat = NULL;
1552 empathy_chat_window_new (void)
1554 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1557 /* Returns the window to open a new tab in if there is only one window
1558 * visble, otherwise, returns NULL indicating that a new window should
1562 empathy_chat_window_get_default (void)
1565 gboolean separate_windows = TRUE;
1567 empathy_conf_get_bool (empathy_conf_get (),
1568 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1571 if (separate_windows) {
1572 /* Always create a new window */
1576 for (l = chat_windows; l; l = l->next) {
1577 EmpathyChatWindow *chat_window;
1580 chat_window = l->data;
1582 dialog = empathy_chat_window_get_dialog (chat_window);
1583 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1584 /* Found a visible window on this desktop */
1593 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1595 EmpathyChatWindowPriv *priv;
1597 g_return_val_if_fail (window != NULL, NULL);
1599 priv = GET_PRIV (window);
1601 return priv->dialog;
1605 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1608 EmpathyChatWindowPriv *priv;
1610 GtkWidget *popup_label;
1614 g_return_if_fail (window != NULL);
1615 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1617 priv = GET_PRIV (window);
1619 /* Reference the chat object */
1620 g_object_ref (chat);
1622 /* If this window has just been created, position it */
1623 if (priv->chats == NULL) {
1624 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1626 if (x >= 0 && y >= 0) {
1627 /* Let the window manager position it if we don't have
1628 * good x, y coordinates.
1630 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1633 if (w > 0 && h > 0) {
1634 /* Use the defaults from the ui file if we don't have
1635 * good w, h geometry.
1637 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1641 child = GTK_WIDGET (chat);
1642 label = chat_window_create_label (window, chat, TRUE);
1643 popup_label = chat_window_create_label (window, chat, FALSE);
1644 gtk_widget_show (child);
1646 g_signal_connect (chat, "notify::name",
1647 G_CALLBACK (chat_window_chat_notify_cb),
1649 g_signal_connect (chat, "notify::subject",
1650 G_CALLBACK (chat_window_chat_notify_cb),
1652 g_signal_connect (chat, "notify::remote-contact",
1653 G_CALLBACK (chat_window_chat_notify_cb),
1655 chat_window_chat_notify_cb (chat);
1657 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1658 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1659 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1660 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1661 TRUE, TRUE, GTK_PACK_START);
1663 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1667 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1670 EmpathyChatWindowPriv *priv;
1672 EmpathyContact *remote_contact;
1674 g_return_if_fail (window != NULL);
1675 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1677 priv = GET_PRIV (window);
1679 g_signal_handlers_disconnect_by_func (chat,
1680 chat_window_chat_notify_cb,
1682 remote_contact = g_object_get_data (G_OBJECT (chat),
1683 "chat-window-remote-contact");
1684 if (remote_contact) {
1685 g_signal_handlers_disconnect_by_func (remote_contact,
1686 chat_window_update_chat_tab,
1690 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1692 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1694 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1696 g_object_unref (chat);
1700 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1701 EmpathyChatWindow *new_window,
1706 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1707 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1708 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1710 widget = GTK_WIDGET (chat);
1712 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1713 G_OBJECT (widget)->ref_count);
1715 /* We reference here to make sure we don't loose the widget
1716 * and the EmpathyChat object during the move.
1718 g_object_ref (chat);
1719 g_object_ref (widget);
1721 empathy_chat_window_remove_chat (old_window, chat);
1722 empathy_chat_window_add_chat (new_window, chat);
1724 g_object_unref (widget);
1725 g_object_unref (chat);
1729 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1732 EmpathyChatWindowPriv *priv;
1735 g_return_if_fail (window != NULL);
1736 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1738 priv = GET_PRIV (window);
1740 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1742 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1747 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1749 EmpathyChatWindowPriv *priv;
1752 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1754 priv = GET_PRIV (window);
1756 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1762 empathy_chat_window_find_chat (EmpathyAccount *account,
1767 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1769 for (l = chat_windows; l; l = l->next) {
1770 EmpathyChatWindowPriv *priv;
1771 EmpathyChatWindow *window;
1775 priv = GET_PRIV (window);
1777 for (ll = priv->chats; ll; ll = ll->next) {
1782 if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1783 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1793 empathy_chat_window_present_chat (EmpathyChat *chat)
1795 EmpathyChatWindow *window;
1796 EmpathyChatWindowPriv *priv;
1798 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1800 window = chat_window_find_chat (chat);
1802 /* If the chat has no window, create one */
1803 if (window == NULL) {
1804 window = empathy_chat_window_get_default ();
1806 window = empathy_chat_window_new ();
1809 empathy_chat_window_add_chat (window, chat);
1812 priv = GET_PRIV (window);
1813 empathy_chat_window_switch_to_chat (window, chat);
1814 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1816 gtk_widget_grab_focus (chat->input_text_view);