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>
39 #include <libempathy/empathy-contact.h>
40 #include <libempathy/empathy-message.h>
41 #include <libempathy/empathy-dispatcher.h>
42 #include <libempathy/empathy-chatroom-manager.h>
43 #include <libempathy/empathy-account-manager.h>
44 #include <libempathy/empathy-utils.h>
46 #include <libempathy-gtk/empathy-images.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-contact-dialogs.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-geometry.h>
51 #include <libempathy-gtk/empathy-smiley-manager.h>
52 #include <libempathy-gtk/empathy-sound.h>
53 #include <libempathy-gtk/empathy-ui-utils.h>
55 #include "empathy-chat-window.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-misc.h"
59 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
60 #include <libempathy/empathy-debug.h>
63 EmpathyChatWindow *window;
67 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
69 EmpathyChat *current_chat;
72 GList *chats_composing;
74 gboolean dnd_same_window;
75 guint save_geometry_id;
76 EmpathyChatroomManager *chatroom_manager;
79 NotifyNotification *notification;
80 NotificationData *notification_data;
83 GtkUIManager *ui_manager;
84 GtkAction *menu_conv_insert_smiley;
85 GtkAction *menu_conv_favorite;
86 GtkAction *menu_conv_toggle_contacts;
88 GtkAction *menu_edit_cut;
89 GtkAction *menu_edit_copy;
90 GtkAction *menu_edit_paste;
92 GtkAction *menu_tabs_next;
93 GtkAction *menu_tabs_prev;
94 GtkAction *menu_tabs_left;
95 GtkAction *menu_tabs_right;
96 GtkAction *menu_tabs_detach;
97 } EmpathyChatWindowPriv;
99 static GList *chat_windows = NULL;
101 static const guint tab_accel_keys[] = {
102 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
103 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
107 DND_DRAG_TYPE_CONTACT_ID,
111 static const GtkTargetEntry drag_types_dest[] = {
112 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
113 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
116 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
119 chat_window_accel_cb (GtkAccelGroup *accelgroup,
123 EmpathyChatWindow *window)
125 EmpathyChatWindowPriv *priv;
129 priv = GET_PRIV (window);
131 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
132 if (tab_accel_keys[i] == key) {
139 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
143 static EmpathyChatWindow *
144 chat_window_find_chat (EmpathyChat *chat)
146 EmpathyChatWindowPriv *priv;
149 for (l = chat_windows; l; l = l->next) {
150 priv = GET_PRIV (l->data);
151 ll = g_list_find (priv->chats, chat);
161 chat_window_close_clicked_cb (GtkAction *action,
164 EmpathyChatWindow *window;
166 window = chat_window_find_chat (chat);
167 empathy_chat_window_remove_chat (window, chat);
171 chat_tab_style_set_cb (GtkWidget *hbox,
172 GtkStyle *previous_style,
176 int char_width, h, w;
177 PangoContext *context;
178 PangoFontMetrics *metrics;
180 button = g_object_get_data (G_OBJECT (user_data),
181 "chat-window-tab-close-button");
182 context = gtk_widget_get_pango_context (hbox);
184 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
185 pango_context_get_language (context));
186 char_width = pango_font_metrics_get_approximate_char_width (metrics);
187 pango_font_metrics_unref (metrics);
189 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
190 GTK_ICON_SIZE_MENU, &w, &h);
192 /* Request at least about 12 chars width plus at least space for the status
193 * image and the close button */
194 gtk_widget_set_size_request (hbox,
195 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
197 gtk_widget_set_size_request (button, w, h);
201 chat_window_create_label (EmpathyChatWindow *window,
203 gboolean is_tab_label)
205 EmpathyChatWindowPriv *priv;
207 GtkWidget *name_label;
208 GtkWidget *status_image;
209 GtkWidget *close_button;
210 GtkWidget *close_image;
211 GtkWidget *event_box;
212 GtkWidget *event_box_hbox;
213 PangoAttrList *attr_list;
214 PangoAttribute *attr;
216 priv = GET_PRIV (window);
218 /* The spacing between the button and the label. */
219 hbox = gtk_hbox_new (FALSE, 0);
221 event_box = gtk_event_box_new ();
222 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
224 name_label = gtk_label_new (NULL);
226 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
228 attr_list = pango_attr_list_new ();
229 attr = pango_attr_scale_new (1/1.2);
230 attr->start_index = 0;
231 attr->end_index = -1;
232 pango_attr_list_insert (attr_list, attr);
233 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
234 pango_attr_list_unref (attr_list);
236 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
237 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
238 g_object_set_data (G_OBJECT (chat),
239 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
242 status_image = gtk_image_new ();
244 /* Spacing between the icon and label. */
245 event_box_hbox = gtk_hbox_new (FALSE, 0);
247 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
248 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
250 g_object_set_data (G_OBJECT (chat),
251 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
253 g_object_set_data (G_OBJECT (chat),
254 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
257 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
258 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
261 close_button = gtk_button_new ();
262 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
263 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
265 /* We don't want focus/keynav for the button to avoid clutter, and
266 * Ctrl-W works anyway.
268 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
269 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
271 /* Set the name to make the special rc style match. */
272 gtk_widget_set_name (close_button, "empathy-close-button");
274 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
276 gtk_container_add (GTK_CONTAINER (close_button), close_image);
278 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
280 g_signal_connect (close_button,
282 G_CALLBACK (chat_window_close_clicked_cb),
285 /* React to theme changes and also setup the size correctly. */
286 g_signal_connect (hbox,
288 G_CALLBACK (chat_tab_style_set_cb),
292 gtk_widget_show_all (hbox);
298 chat_window_update (EmpathyChatWindow *window)
300 EmpathyChatWindowPriv *priv = GET_PRIV (window);
303 gboolean is_connected;
310 EmpathyContact *remote_contact;
311 gboolean avatar_in_icon;
313 GtkWidget *chat_close_button;
317 /* Get information */
318 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
319 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
320 first_page = (page_num == 0);
321 last_page = (page_num == (num_pages - 1));
322 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
323 name = empathy_chat_get_name (priv->current_chat);
324 n_chats = g_list_length (priv->chats);
326 DEBUG ("Update window");
329 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
330 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
331 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
332 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
333 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
334 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
336 /* Update Contact menu */
337 menu = gtk_ui_manager_get_widget (priv->ui_manager,
338 "/chats_menubar/menu_contact");
339 submenu = empathy_chat_get_contact_menu (priv->current_chat);
340 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
341 gtk_widget_show (menu);
343 /* Update window title */
345 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
349 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
350 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
354 /* Update window icon */
355 if (priv->chats_new_msg) {
356 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
357 EMPATHY_IMAGE_MESSAGE);
359 empathy_conf_get_bool (empathy_conf_get (),
360 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
363 if (n_chats == 1 && avatar_in_icon) {
364 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
365 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
366 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
369 g_object_unref (icon);
372 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
376 if (num_pages == 1) {
377 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
378 chat_close_button = g_object_get_data (G_OBJECT (chat),
379 "chat-window-tab-close-button");
380 gtk_widget_hide (chat_close_button);
382 for (i=0; i<num_pages; i++) {
383 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
384 chat_close_button = g_object_get_data (G_OBJECT (chat),
385 "chat-window-tab-close-button");
386 gtk_widget_show (chat_close_button);
392 append_markup_printf (GString *string,
399 va_start (args, format);
401 tmp = g_markup_vprintf_escaped (format, args);
402 g_string_append (string, tmp);
409 chat_window_update_chat_tab (EmpathyChat *chat)
411 EmpathyChatWindow *window;
412 EmpathyChatWindowPriv *priv;
413 EmpathyContact *remote_contact;
416 EmpathyAccount *account;
417 const gchar *subject;
418 const gchar *status = NULL;
422 const gchar *icon_name;
424 window = chat_window_find_chat (chat);
428 priv = GET_PRIV (window);
430 /* Get information */
431 name = empathy_chat_get_name (chat);
432 account = empathy_chat_get_account (chat);
433 subject = empathy_chat_get_subject (chat);
434 remote_contact = empathy_chat_get_remote_contact (chat);
436 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
437 name, empathy_account_get_unique_name (account), subject, remote_contact);
439 /* Update tab image */
440 if (g_list_find (priv->chats_new_msg, chat)) {
441 icon_name = EMPATHY_IMAGE_MESSAGE;
443 else if (g_list_find (priv->chats_composing, chat)) {
444 icon_name = EMPATHY_IMAGE_TYPING;
446 else if (remote_contact) {
447 icon_name = empathy_icon_name_for_contact (remote_contact);
449 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
451 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
452 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
453 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
454 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
456 /* Update tab tooltip */
457 tooltip = g_string_new (NULL);
459 if (remote_contact) {
460 id = empathy_contact_get_id (remote_contact);
461 status = empathy_contact_get_presence_message (remote_contact);
466 append_markup_printf (tooltip,
467 "<b>%s</b><small> (%s)</small>",
469 empathy_account_get_display_name (account));
471 if (!EMP_STR_EMPTY (status)) {
472 append_markup_printf (tooltip, "\n<i>%s</i>", status);
476 append_markup_printf (tooltip, "\n<b>%s</b> %s",
477 _("Topic:"), subject);
480 if (g_list_find (priv->chats_composing, chat)) {
481 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
484 markup = g_string_free (tooltip, FALSE);
485 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
486 gtk_widget_set_tooltip_markup (widget, markup);
487 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
488 gtk_widget_set_tooltip_markup (widget, markup);
491 /* Update tab and menu label */
492 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
493 gtk_label_set_text (GTK_LABEL (widget), name);
494 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
495 gtk_label_set_text (GTK_LABEL (widget), name);
497 /* Update the window if it's the current chat */
498 if (priv->current_chat == chat) {
499 chat_window_update (window);
504 chat_window_chat_notify_cb (EmpathyChat *chat)
506 EmpathyContact *old_remote_contact;
507 EmpathyContact *remote_contact = NULL;
509 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
510 remote_contact = empathy_chat_get_remote_contact (chat);
512 if (old_remote_contact != remote_contact) {
513 /* The remote-contact associated with the chat changed, we need
514 * to keep track of any change of that contact and update the
515 * window each time. */
516 if (remote_contact) {
517 g_signal_connect_swapped (remote_contact, "notify",
518 G_CALLBACK (chat_window_update_chat_tab),
521 if (old_remote_contact) {
522 g_signal_handlers_disconnect_by_func (old_remote_contact,
523 chat_window_update_chat_tab,
527 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
531 chat_window_update_chat_tab (chat);
535 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
536 EmpathySmiley *smiley,
539 EmpathyChatWindowPriv *priv = GET_PRIV (window);
541 GtkTextBuffer *buffer;
544 chat = priv->current_chat;
546 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
547 gtk_text_buffer_get_end_iter (buffer, &iter);
548 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
552 chat_window_conv_activate_cb (GtkAction *action,
553 EmpathyChatWindow *window)
555 EmpathyChatWindowPriv *priv = GET_PRIV (window);
558 EmpathyContact *remote_contact = NULL;
560 /* Favorite room menu */
561 is_room = empathy_chat_is_room (priv->current_chat);
564 EmpathyAccount *account;
565 gboolean found = FALSE;
566 EmpathyChatroom *chatroom;
568 room = empathy_chat_get_id (priv->current_chat);
569 account = empathy_chat_get_account (priv->current_chat);
570 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
572 if (chatroom != NULL)
573 found = empathy_chatroom_is_favorite (chatroom);
575 DEBUG ("This room %s favorite", found ? "is" : "is not");
576 gtk_toggle_action_set_active (
577 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
579 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
581 /* Show contacts menu */
582 g_object_get (priv->current_chat,
583 "remote-contact", &remote_contact,
584 "show-contacts", &active,
586 if (remote_contact == NULL) {
587 gtk_toggle_action_set_active (
588 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
591 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
592 (remote_contact == NULL));
593 if (remote_contact != NULL) {
594 g_object_unref (remote_contact);
599 chat_window_clear_activate_cb (GtkAction *action,
600 EmpathyChatWindow *window)
602 EmpathyChatWindowPriv *priv = GET_PRIV (window);
604 empathy_chat_clear (priv->current_chat);
608 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
609 EmpathyChatWindow *window)
611 EmpathyChatWindowPriv *priv = GET_PRIV (window);
613 EmpathyAccount *account;
615 EmpathyChatroom *chatroom;
617 active = gtk_toggle_action_get_active (toggle_action);
618 account = empathy_chat_get_account (priv->current_chat);
619 room = empathy_chat_get_id (priv->current_chat);
621 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
624 if (chatroom == NULL) {
627 name = empathy_chat_get_name (priv->current_chat);
628 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
629 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
630 g_object_unref (chatroom);
633 empathy_chatroom_set_favorite (chatroom, active);
637 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
638 EmpathyChatWindow *window)
640 EmpathyChatWindowPriv *priv = GET_PRIV (window);
643 active = gtk_toggle_action_get_active (toggle_action);
645 empathy_chat_set_show_contacts (priv->current_chat, active);
649 chat_get_window_id_for_geometry (EmpathyChat *chat)
651 const gchar *res = NULL;
652 gboolean separate_windows;
654 empathy_conf_get_bool (empathy_conf_get (),
655 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
658 if (separate_windows) {
659 res = empathy_chat_get_id (chat);
662 return res ? res : "chat-window";
666 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
668 EmpathyChatWindowPriv *priv;
671 priv = GET_PRIV (window);
673 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
674 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
676 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
679 priv->save_geometry_id = 0;
685 chat_window_configure_event_cb (GtkWidget *widget,
686 GdkEventConfigure *event,
687 EmpathyChatWindow *window)
689 EmpathyChatWindowPriv *priv;
691 priv = GET_PRIV (window);
693 if (priv->save_geometry_id != 0) {
694 g_source_remove (priv->save_geometry_id);
697 priv->save_geometry_id =
698 g_timeout_add_seconds (1,
699 (GSourceFunc) chat_window_save_geometry_timeout_cb,
706 chat_window_close_activate_cb (GtkAction *action,
707 EmpathyChatWindow *window)
709 EmpathyChatWindowPriv *priv;
711 priv = GET_PRIV (window);
713 g_return_if_fail (priv->current_chat != NULL);
715 empathy_chat_window_remove_chat (window, priv->current_chat);
719 chat_window_edit_activate_cb (GtkAction *action,
720 EmpathyChatWindow *window)
722 EmpathyChatWindowPriv *priv;
723 GtkClipboard *clipboard;
724 GtkTextBuffer *buffer;
725 gboolean text_available;
727 priv = GET_PRIV (window);
729 g_return_if_fail (priv->current_chat != NULL);
731 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
732 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
733 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
734 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
738 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
739 if (gtk_text_buffer_get_has_selection (buffer)) {
740 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
741 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
745 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
747 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
748 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
751 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
752 text_available = gtk_clipboard_wait_is_text_available (clipboard);
753 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
757 chat_window_cut_activate_cb (GtkAction *action,
758 EmpathyChatWindow *window)
760 EmpathyChatWindowPriv *priv;
762 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
764 priv = GET_PRIV (window);
766 empathy_chat_cut (priv->current_chat);
770 chat_window_copy_activate_cb (GtkAction *action,
771 EmpathyChatWindow *window)
773 EmpathyChatWindowPriv *priv;
775 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
777 priv = GET_PRIV (window);
779 empathy_chat_copy (priv->current_chat);
783 chat_window_paste_activate_cb (GtkAction *action,
784 EmpathyChatWindow *window)
786 EmpathyChatWindowPriv *priv;
788 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
790 priv = GET_PRIV (window);
792 empathy_chat_paste (priv->current_chat);
796 chat_window_tabs_left_activate_cb (GtkAction *action,
797 EmpathyChatWindow *window)
799 EmpathyChatWindowPriv *priv;
803 priv = GET_PRIV (window);
805 chat = priv->current_chat;
806 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
811 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
817 chat_window_tabs_right_activate_cb (GtkAction *action,
818 EmpathyChatWindow *window)
820 EmpathyChatWindowPriv *priv;
824 priv = GET_PRIV (window);
826 chat = priv->current_chat;
827 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
829 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
835 chat_window_detach_activate_cb (GtkAction *action,
836 EmpathyChatWindow *window)
838 EmpathyChatWindowPriv *priv;
839 EmpathyChatWindow *new_window;
842 priv = GET_PRIV (window);
844 chat = priv->current_chat;
845 new_window = empathy_chat_window_new ();
847 empathy_chat_window_move_chat (window, new_window, chat);
849 priv = GET_PRIV (new_window);
850 gtk_widget_show (priv->dialog);
854 chat_window_help_contents_activate_cb (GtkAction *action,
855 EmpathyChatWindow *window)
857 EmpathyChatWindowPriv *priv = GET_PRIV (window);
859 empathy_url_show (priv->dialog, "ghelp:empathy");
863 chat_window_help_about_activate_cb (GtkAction *action,
864 EmpathyChatWindow *window)
866 EmpathyChatWindowPriv *priv = GET_PRIV (window);
868 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
872 chat_window_delete_event_cb (GtkWidget *dialog,
874 EmpathyChatWindow *window)
876 EmpathyChatWindowPriv *priv = GET_PRIV (window);
878 DEBUG ("Delete event received");
880 g_object_ref (window);
881 while (priv->chats) {
882 empathy_chat_window_remove_chat (window, priv->chats->data);
884 g_object_unref (window);
890 chat_window_composing_cb (EmpathyChat *chat,
891 gboolean is_composing,
892 EmpathyChatWindow *window)
894 EmpathyChatWindowPriv *priv;
896 priv = GET_PRIV (window);
898 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
899 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
901 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
904 chat_window_update_chat_tab (chat);
908 chat_window_set_urgency_hint (EmpathyChatWindow *window,
911 EmpathyChatWindowPriv *priv;
913 priv = GET_PRIV (window);
915 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
916 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
920 free_notification_data (NotificationData *data)
922 g_object_unref (data->chat);
923 g_slice_free (NotificationData, data);
927 chat_window_notification_closed_cb (NotifyNotification *notify,
928 NotificationData *cb_data)
930 EmpathyNotificationClosedReason reason = 0;
931 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
933 #ifdef notify_notification_get_closed_reason
934 reason = notify_notification_get_closed_reason (notify);
936 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
937 empathy_chat_window_present_chat (cb_data->chat);
940 g_object_unref (notify);
941 priv->notification = NULL;
942 free_notification_data (cb_data);
943 priv->notification_data = NULL;
947 chat_window_show_or_update_notification (EmpathyChatWindow *window,
948 EmpathyMessage *message,
951 EmpathyContact *sender;
956 EmpathyChatWindowPriv *priv = GET_PRIV (window);
959 if (!empathy_notification_is_enabled ()) {
962 empathy_conf_get_bool (empathy_conf_get (),
963 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
969 sender = empathy_message_get_sender (message);
970 header = empathy_contact_get_name (sender);
971 body = empathy_message_get_body (message);
972 escaped = g_markup_escape_text (body, -1);
974 if (priv->notification != NULL) {
975 notify_notification_update (priv->notification,
976 header, escaped, NULL);
978 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
980 cb_data->chat = g_object_ref (chat);
981 cb_data->window = window;
983 priv->notification_data = cb_data;
984 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
985 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
987 g_signal_connect (priv->notification, "closed",
988 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
991 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
993 if (pixbuf != NULL) {
994 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
995 g_object_unref (pixbuf);
998 notify_notification_show (priv->notification, NULL);
1004 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1009 if (!empathy_chat_is_room (chat))
1012 markup = g_markup_printf_escaped (
1013 "<span color=\"red\" weight=\"bold\">%s</span>",
1014 empathy_chat_get_name (chat));
1016 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1017 gtk_label_set_markup (GTK_LABEL (widget), markup);
1022 chat_window_new_message_cb (EmpathyChat *chat,
1023 EmpathyMessage *message,
1024 EmpathyChatWindow *window)
1026 EmpathyChatWindowPriv *priv;
1028 gboolean needs_urgency;
1029 EmpathyContact *sender;
1031 priv = GET_PRIV (window);
1033 has_focus = empathy_chat_window_has_focus (window);
1035 /* - if we're the sender, we play the sound if it's specified in the
1036 * preferences and we're not away.
1037 * - if we receive a message, we play the sound if it's specified in the
1038 * preferences and the window does not have focus on the chat receiving
1042 sender = empathy_message_get_sender (message);
1044 if (empathy_contact_is_user (sender)) {
1045 empathy_sound_play (GTK_WIDGET (priv->dialog),
1046 EMPATHY_SOUND_MESSAGE_OUTGOING);
1049 if (has_focus && priv->current_chat == chat) {
1053 if (!g_list_find (priv->chats_new_msg, chat)) {
1054 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1055 chat_window_update_chat_tab (chat);
1058 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1059 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1060 * an unamed MUC (msn-like).
1061 * In case of a MUC, we set urgency only if the message contains our
1063 if (empathy_chat_is_room (chat) ||
1064 empathy_chat_get_remote_contact (chat) == NULL) {
1065 needs_urgency = empathy_message_should_highlight (message);
1067 needs_urgency = TRUE;
1070 if (needs_urgency) {
1072 chat_window_set_urgency_hint (window, TRUE);
1073 chat_window_set_highlight_room_tab_label (chat);
1076 empathy_sound_play (GTK_WIDGET (priv->dialog),
1077 EMPATHY_SOUND_MESSAGE_INCOMING);
1078 chat_window_show_or_update_notification (window, message, chat);
1082 static GtkNotebook *
1083 chat_window_detach_hook (GtkNotebook *source,
1089 EmpathyChatWindowPriv *priv;
1090 EmpathyChatWindow *window, *new_window;
1093 chat = EMPATHY_CHAT (page);
1094 window = chat_window_find_chat (chat);
1096 new_window = empathy_chat_window_new ();
1097 priv = GET_PRIV (new_window);
1099 DEBUG ("Detach hook called");
1101 empathy_chat_window_move_chat (window, new_window, chat);
1103 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1104 gtk_widget_show (priv->dialog);
1110 chat_window_page_switched_cb (GtkNotebook *notebook,
1111 GtkNotebookPage *page,
1113 EmpathyChatWindow *window)
1115 EmpathyChatWindowPriv *priv;
1119 DEBUG ("Page switched");
1121 priv = GET_PRIV (window);
1123 child = gtk_notebook_get_nth_page (notebook, page_num);
1124 chat = EMPATHY_CHAT (child);
1126 if (priv->page_added) {
1127 priv->page_added = FALSE;
1128 empathy_chat_scroll_down (chat);
1130 else if (priv->current_chat == chat) {
1134 priv->current_chat = chat;
1135 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1137 chat_window_update_chat_tab (chat);
1141 chat_window_page_added_cb (GtkNotebook *notebook,
1144 EmpathyChatWindow *window)
1146 EmpathyChatWindowPriv *priv;
1149 priv = GET_PRIV (window);
1151 /* If we just received DND to the same window, we don't want
1152 * to do anything here like removing the tab and then readding
1153 * it, so we return here and in "page-added".
1155 if (priv->dnd_same_window) {
1156 DEBUG ("Page added (back to the same window)");
1157 priv->dnd_same_window = FALSE;
1161 DEBUG ("Page added");
1163 /* Get chat object */
1164 chat = EMPATHY_CHAT (child);
1166 /* Connect chat signals for this window */
1167 g_signal_connect (chat, "composing",
1168 G_CALLBACK (chat_window_composing_cb),
1170 g_signal_connect (chat, "new-message",
1171 G_CALLBACK (chat_window_new_message_cb),
1174 /* Set flag so we know to perform some special operations on
1175 * switch page due to the new page being added.
1177 priv->page_added = TRUE;
1179 /* Get list of chats up to date */
1180 priv->chats = g_list_append (priv->chats, chat);
1182 chat_window_update_chat_tab (chat);
1186 chat_window_page_removed_cb (GtkNotebook *notebook,
1189 EmpathyChatWindow *window)
1191 EmpathyChatWindowPriv *priv;
1194 priv = GET_PRIV (window);
1196 /* If we just received DND to the same window, we don't want
1197 * to do anything here like removing the tab and then readding
1198 * it, so we return here and in "page-added".
1200 if (priv->dnd_same_window) {
1201 DEBUG ("Page removed (and will be readded to same window)");
1205 DEBUG ("Page removed");
1207 /* Get chat object */
1208 chat = EMPATHY_CHAT (child);
1210 /* Disconnect all signal handlers for this chat and this window */
1211 g_signal_handlers_disconnect_by_func (chat,
1212 G_CALLBACK (chat_window_composing_cb),
1214 g_signal_handlers_disconnect_by_func (chat,
1215 G_CALLBACK (chat_window_new_message_cb),
1218 /* Keep list of chats up to date */
1219 priv->chats = g_list_remove (priv->chats, chat);
1220 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1221 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1223 if (priv->chats == NULL) {
1224 g_object_unref (window);
1226 chat_window_update (window);
1231 chat_window_focus_in_event_cb (GtkWidget *widget,
1233 EmpathyChatWindow *window)
1235 EmpathyChatWindowPriv *priv;
1237 DEBUG ("Focus in event, updating title");
1239 priv = GET_PRIV (window);
1241 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1243 chat_window_set_urgency_hint (window, FALSE);
1245 /* Update the title, since we now mark all unread messages as read. */
1246 chat_window_update_chat_tab (priv->current_chat);
1252 chat_window_drag_data_received (GtkWidget *widget,
1253 GdkDragContext *context,
1256 GtkSelectionData *selection,
1259 EmpathyChatWindow *window)
1261 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1262 EmpathyChat *chat = NULL;
1263 EmpathyChatWindow *old_window;
1264 EmpathyAccount *account = NULL;
1265 EmpathyAccountManager *account_manager;
1268 const gchar *account_id;
1269 const gchar *contact_id;
1271 id = (const gchar*) gtk_selection_data_get_data (selection);
1272 account_manager = empathy_account_manager_dup_singleton ();
1274 DEBUG ("DND contact from roster with id:'%s'", id);
1276 strv = g_strsplit (id, ":", 2);
1277 if (g_strv_length (strv) == 2) {
1278 account_id = strv[0];
1279 contact_id = strv[1];
1281 empathy_account_manager_get_account (account_manager, account_id);
1282 if (account != NULL)
1283 chat = empathy_chat_window_find_chat (account, contact_id);
1286 if (account == NULL) {
1288 gtk_drag_finish (context, FALSE, FALSE, time);
1293 TpConnection *connection;
1295 connection = empathy_account_get_connection (account);
1298 empathy_dispatcher_chat_with_contact_id (
1299 connection, contact_id, NULL, NULL);
1305 g_object_unref (account_manager);
1308 old_window = chat_window_find_chat (chat);
1310 if (old_window == window) {
1311 gtk_drag_finish (context, TRUE, FALSE, time);
1315 empathy_chat_window_move_chat (old_window, window, chat);
1317 empathy_chat_window_add_chat (window, chat);
1320 /* Added to take care of any outstanding chat events */
1321 empathy_chat_window_present_chat (chat);
1323 /* We should return TRUE to remove the data when doing
1324 * GDK_ACTION_MOVE, but we don't here otherwise it has
1325 * weird consequences, and we handle that internally
1326 * anyway with add_chat () and remove_chat ().
1328 gtk_drag_finish (context, TRUE, FALSE, time);
1330 else if (info == DND_DRAG_TYPE_TAB) {
1332 EmpathyChatWindow *old_window = NULL;
1336 chat = (void *) gtk_selection_data_get_data (selection);
1337 old_window = chat_window_find_chat (*chat);
1340 EmpathyChatWindowPriv *priv;
1342 priv = GET_PRIV (window);
1344 if (old_window == window) {
1345 DEBUG ("DND tab (within same window)");
1346 priv->dnd_same_window = TRUE;
1347 gtk_drag_finish (context, TRUE, FALSE, time);
1351 priv->dnd_same_window = FALSE;
1354 /* We should return TRUE to remove the data when doing
1355 * GDK_ACTION_MOVE, but we don't here otherwise it has
1356 * weird consequences, and we handle that internally
1357 * anyway with add_chat () and remove_chat ().
1359 gtk_drag_finish (context, TRUE, FALSE, time);
1361 DEBUG ("DND from unknown source");
1362 gtk_drag_finish (context, FALSE, FALSE, time);
1367 chat_window_finalize (GObject *object)
1369 EmpathyChatWindow *window;
1370 EmpathyChatWindowPriv *priv;
1372 window = EMPATHY_CHAT_WINDOW (object);
1373 priv = GET_PRIV (window);
1375 DEBUG ("Finalized: %p", object);
1377 g_object_unref (priv->ui_manager);
1378 g_object_unref (priv->chatroom_manager);
1379 if (priv->save_geometry_id != 0) {
1380 g_source_remove (priv->save_geometry_id);
1383 if (priv->notification != NULL) {
1384 notify_notification_close (priv->notification, NULL);
1385 g_object_unref (priv->notification);
1386 priv->notification = NULL;
1387 if (priv->notification_data != NULL)
1389 free_notification_data (priv->notification_data);
1390 priv->notification_data = NULL;
1394 chat_windows = g_list_remove (chat_windows, window);
1395 gtk_widget_destroy (priv->dialog);
1397 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1401 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1403 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1405 object_class->finalize = chat_window_finalize;
1407 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1409 /* Set up a style for the close button with no focus padding. */
1410 gtk_rc_parse_string (
1411 "style \"empathy-close-button-style\"\n"
1413 " GtkWidget::focus-padding = 0\n"
1417 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1419 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1423 empathy_chat_window_init (EmpathyChatWindow *window)
1426 GtkAccelGroup *accel_group;
1431 GtkWidget *chat_vbox;
1433 EmpathySmileyManager *smiley_manager;
1434 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1435 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1437 window->priv = priv;
1438 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1439 gui = empathy_builder_get_file (filename,
1440 "chat_window", &priv->dialog,
1441 "chat_vbox", &chat_vbox,
1442 "ui_manager", &priv->ui_manager,
1443 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1444 "menu_conv_favorite", &priv->menu_conv_favorite,
1445 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1446 "menu_edit_cut", &priv->menu_edit_cut,
1447 "menu_edit_copy", &priv->menu_edit_copy,
1448 "menu_edit_paste", &priv->menu_edit_paste,
1449 "menu_tabs_next", &priv->menu_tabs_next,
1450 "menu_tabs_prev", &priv->menu_tabs_prev,
1451 "menu_tabs_left", &priv->menu_tabs_left,
1452 "menu_tabs_right", &priv->menu_tabs_right,
1453 "menu_tabs_detach", &priv->menu_tabs_detach,
1457 empathy_builder_connect (gui, window,
1458 "chat_window", "configure-event", chat_window_configure_event_cb,
1459 "menu_conv", "activate", chat_window_conv_activate_cb,
1460 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1461 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1462 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1463 "menu_conv_close", "activate", chat_window_close_activate_cb,
1464 "menu_edit", "activate", chat_window_edit_activate_cb,
1465 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1466 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1467 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1468 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1469 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1470 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1471 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1472 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1475 g_object_ref (priv->ui_manager);
1476 g_object_unref (gui);
1478 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1480 priv->notebook = gtk_notebook_new ();
1481 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1482 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1483 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1484 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1485 gtk_widget_show (priv->notebook);
1488 accel_group = gtk_accel_group_new ();
1489 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1491 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1492 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1495 gtk_accel_group_connect (accel_group,
1502 g_object_unref (accel_group);
1504 /* Set up smiley menu */
1505 smiley_manager = empathy_smiley_manager_dup_singleton ();
1506 submenu = empathy_smiley_menu_new (smiley_manager,
1507 chat_window_insert_smiley_activate_cb,
1509 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1510 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1511 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1512 g_object_unref (smiley_manager);
1514 /* Set up signals we can't do with ui file since we may need to
1515 * block/unblock them at some later stage.
1518 g_signal_connect (priv->dialog,
1520 G_CALLBACK (chat_window_delete_event_cb),
1523 g_signal_connect_swapped (priv->menu_tabs_prev,
1525 G_CALLBACK (gtk_notebook_prev_page),
1527 g_signal_connect_swapped (priv->menu_tabs_next,
1529 G_CALLBACK (gtk_notebook_next_page),
1532 g_signal_connect (priv->dialog,
1534 G_CALLBACK (chat_window_focus_in_event_cb),
1536 g_signal_connect_after (priv->notebook,
1538 G_CALLBACK (chat_window_page_switched_cb),
1540 g_signal_connect (priv->notebook,
1542 G_CALLBACK (chat_window_page_added_cb),
1544 g_signal_connect (priv->notebook,
1546 G_CALLBACK (chat_window_page_removed_cb),
1549 /* Set up drag and drop */
1550 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1551 GTK_DEST_DEFAULT_ALL,
1553 G_N_ELEMENTS (drag_types_dest),
1556 g_signal_connect (priv->notebook,
1557 "drag-data-received",
1558 G_CALLBACK (chat_window_drag_data_received),
1561 chat_windows = g_list_prepend (chat_windows, window);
1563 /* Set up private details */
1565 priv->chats_new_msg = NULL;
1566 priv->chats_composing = NULL;
1567 priv->current_chat = NULL;
1571 empathy_chat_window_new (void)
1573 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1576 /* Returns the window to open a new tab in if there is only one window
1577 * visble, otherwise, returns NULL indicating that a new window should
1581 empathy_chat_window_get_default (void)
1584 gboolean separate_windows = TRUE;
1586 empathy_conf_get_bool (empathy_conf_get (),
1587 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1590 if (separate_windows) {
1591 /* Always create a new window */
1595 for (l = chat_windows; l; l = l->next) {
1596 EmpathyChatWindow *chat_window;
1599 chat_window = l->data;
1601 dialog = empathy_chat_window_get_dialog (chat_window);
1602 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1603 /* Found a visible window on this desktop */
1612 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1614 EmpathyChatWindowPriv *priv;
1616 g_return_val_if_fail (window != NULL, NULL);
1618 priv = GET_PRIV (window);
1620 return priv->dialog;
1624 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1627 EmpathyChatWindowPriv *priv;
1629 GtkWidget *popup_label;
1633 g_return_if_fail (window != NULL);
1634 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1636 priv = GET_PRIV (window);
1638 /* Reference the chat object */
1639 g_object_ref (chat);
1641 /* If this window has just been created, position it */
1642 if (priv->chats == NULL) {
1643 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1645 if (x >= 0 && y >= 0) {
1646 /* Let the window manager position it if we don't have
1647 * good x, y coordinates.
1649 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1652 if (w > 0 && h > 0) {
1653 /* Use the defaults from the ui file if we don't have
1654 * good w, h geometry.
1656 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1660 child = GTK_WIDGET (chat);
1661 label = chat_window_create_label (window, chat, TRUE);
1662 popup_label = chat_window_create_label (window, chat, FALSE);
1663 gtk_widget_show (child);
1665 g_signal_connect (chat, "notify::name",
1666 G_CALLBACK (chat_window_chat_notify_cb),
1668 g_signal_connect (chat, "notify::subject",
1669 G_CALLBACK (chat_window_chat_notify_cb),
1671 g_signal_connect (chat, "notify::remote-contact",
1672 G_CALLBACK (chat_window_chat_notify_cb),
1674 chat_window_chat_notify_cb (chat);
1676 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1677 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1678 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1679 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1680 TRUE, TRUE, GTK_PACK_START);
1682 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1686 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1689 EmpathyChatWindowPriv *priv;
1691 EmpathyContact *remote_contact;
1693 g_return_if_fail (window != NULL);
1694 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1696 priv = GET_PRIV (window);
1698 g_signal_handlers_disconnect_by_func (chat,
1699 chat_window_chat_notify_cb,
1701 remote_contact = g_object_get_data (G_OBJECT (chat),
1702 "chat-window-remote-contact");
1703 if (remote_contact) {
1704 g_signal_handlers_disconnect_by_func (remote_contact,
1705 chat_window_update_chat_tab,
1709 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1711 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1713 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1715 g_object_unref (chat);
1719 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1720 EmpathyChatWindow *new_window,
1725 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1726 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1727 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1729 widget = GTK_WIDGET (chat);
1731 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1732 G_OBJECT (widget)->ref_count);
1734 /* We reference here to make sure we don't loose the widget
1735 * and the EmpathyChat object during the move.
1737 g_object_ref (chat);
1738 g_object_ref (widget);
1740 empathy_chat_window_remove_chat (old_window, chat);
1741 empathy_chat_window_add_chat (new_window, chat);
1743 g_object_unref (widget);
1744 g_object_unref (chat);
1748 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1751 EmpathyChatWindowPriv *priv;
1754 g_return_if_fail (window != NULL);
1755 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1757 priv = GET_PRIV (window);
1759 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1761 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1766 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1768 EmpathyChatWindowPriv *priv;
1771 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1773 priv = GET_PRIV (window);
1775 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1781 empathy_chat_window_find_chat (EmpathyAccount *account,
1786 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1788 for (l = chat_windows; l; l = l->next) {
1789 EmpathyChatWindowPriv *priv;
1790 EmpathyChatWindow *window;
1794 priv = GET_PRIV (window);
1796 for (ll = priv->chats; ll; ll = ll->next) {
1801 if (account == empathy_chat_get_account (chat) &&
1802 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1812 empathy_chat_window_present_chat (EmpathyChat *chat)
1814 EmpathyChatWindow *window;
1815 EmpathyChatWindowPriv *priv;
1817 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1819 window = chat_window_find_chat (chat);
1821 /* If the chat has no window, create one */
1822 if (window == NULL) {
1823 window = empathy_chat_window_get_default ();
1825 window = empathy_chat_window_new ();
1828 empathy_chat_window_add_chat (window, chat);
1831 priv = GET_PRIV (window);
1832 empathy_chat_window_switch_to_chat (window, chat);
1833 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1835 gtk_widget_grab_focus (chat->input_text_view);