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 (empathy_chat_get_tp_chat (chat) == NULL) {
441 /* No TpChat, we are disconnected */
442 icon_name = EMPATHY_IMAGE_OFFLINE;
444 else if (g_list_find (priv->chats_new_msg, chat)) {
445 icon_name = EMPATHY_IMAGE_MESSAGE;
447 else if (g_list_find (priv->chats_composing, chat)) {
448 icon_name = EMPATHY_IMAGE_TYPING;
450 else if (remote_contact) {
451 icon_name = empathy_icon_name_for_contact (remote_contact);
453 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
455 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
456 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
457 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
458 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
460 /* Update tab tooltip */
461 tooltip = g_string_new (NULL);
463 if (remote_contact) {
464 id = empathy_contact_get_id (remote_contact);
465 status = empathy_contact_get_presence_message (remote_contact);
470 append_markup_printf (tooltip,
471 "<b>%s</b><small> (%s)</small>",
473 empathy_account_get_display_name (account));
475 if (!EMP_STR_EMPTY (status)) {
476 append_markup_printf (tooltip, "\n<i>%s</i>", status);
480 append_markup_printf (tooltip, "\n<b>%s</b> %s",
481 _("Topic:"), subject);
484 if (g_list_find (priv->chats_composing, chat)) {
485 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
488 markup = g_string_free (tooltip, FALSE);
489 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
490 gtk_widget_set_tooltip_markup (widget, markup);
491 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
492 gtk_widget_set_tooltip_markup (widget, markup);
495 /* Update tab and menu label */
496 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
497 gtk_label_set_text (GTK_LABEL (widget), name);
498 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
499 gtk_label_set_text (GTK_LABEL (widget), name);
501 /* Update the window if it's the current chat */
502 if (priv->current_chat == chat) {
503 chat_window_update (window);
508 chat_window_chat_notify_cb (EmpathyChat *chat)
510 EmpathyContact *old_remote_contact;
511 EmpathyContact *remote_contact = NULL;
513 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
514 remote_contact = empathy_chat_get_remote_contact (chat);
516 if (old_remote_contact != remote_contact) {
517 /* The remote-contact associated with the chat changed, we need
518 * to keep track of any change of that contact and update the
519 * window each time. */
520 if (remote_contact) {
521 g_signal_connect_swapped (remote_contact, "notify",
522 G_CALLBACK (chat_window_update_chat_tab),
525 if (old_remote_contact) {
526 g_signal_handlers_disconnect_by_func (old_remote_contact,
527 chat_window_update_chat_tab,
531 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
535 chat_window_update_chat_tab (chat);
539 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
540 EmpathySmiley *smiley,
543 EmpathyChatWindowPriv *priv = GET_PRIV (window);
545 GtkTextBuffer *buffer;
548 chat = priv->current_chat;
550 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
551 gtk_text_buffer_get_end_iter (buffer, &iter);
552 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
556 chat_window_conv_activate_cb (GtkAction *action,
557 EmpathyChatWindow *window)
559 EmpathyChatWindowPriv *priv = GET_PRIV (window);
562 EmpathyContact *remote_contact = NULL;
564 /* Favorite room menu */
565 is_room = empathy_chat_is_room (priv->current_chat);
568 EmpathyAccount *account;
569 gboolean found = FALSE;
570 EmpathyChatroom *chatroom;
572 room = empathy_chat_get_id (priv->current_chat);
573 account = empathy_chat_get_account (priv->current_chat);
574 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
576 if (chatroom != NULL)
577 found = empathy_chatroom_is_favorite (chatroom);
579 DEBUG ("This room %s favorite", found ? "is" : "is not");
580 gtk_toggle_action_set_active (
581 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
583 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
585 /* Show contacts menu */
586 g_object_get (priv->current_chat,
587 "remote-contact", &remote_contact,
588 "show-contacts", &active,
590 if (remote_contact == NULL) {
591 gtk_toggle_action_set_active (
592 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
595 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
596 (remote_contact == NULL));
597 if (remote_contact != NULL) {
598 g_object_unref (remote_contact);
603 chat_window_clear_activate_cb (GtkAction *action,
604 EmpathyChatWindow *window)
606 EmpathyChatWindowPriv *priv = GET_PRIV (window);
608 empathy_chat_clear (priv->current_chat);
612 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
613 EmpathyChatWindow *window)
615 EmpathyChatWindowPriv *priv = GET_PRIV (window);
617 EmpathyAccount *account;
619 EmpathyChatroom *chatroom;
621 active = gtk_toggle_action_get_active (toggle_action);
622 account = empathy_chat_get_account (priv->current_chat);
623 room = empathy_chat_get_id (priv->current_chat);
625 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
628 if (chatroom == NULL) {
631 name = empathy_chat_get_name (priv->current_chat);
632 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
633 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
634 g_object_unref (chatroom);
637 empathy_chatroom_set_favorite (chatroom, active);
641 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
642 EmpathyChatWindow *window)
644 EmpathyChatWindowPriv *priv = GET_PRIV (window);
647 active = gtk_toggle_action_get_active (toggle_action);
649 empathy_chat_set_show_contacts (priv->current_chat, active);
653 chat_get_window_id_for_geometry (EmpathyChat *chat)
655 const gchar *res = NULL;
656 gboolean separate_windows;
658 empathy_conf_get_bool (empathy_conf_get (),
659 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
662 if (separate_windows) {
663 res = empathy_chat_get_id (chat);
666 return res ? res : "chat-window";
670 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
672 EmpathyChatWindowPriv *priv;
675 priv = GET_PRIV (window);
677 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
678 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
680 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
683 priv->save_geometry_id = 0;
689 chat_window_configure_event_cb (GtkWidget *widget,
690 GdkEventConfigure *event,
691 EmpathyChatWindow *window)
693 EmpathyChatWindowPriv *priv;
695 priv = GET_PRIV (window);
697 if (priv->save_geometry_id != 0) {
698 g_source_remove (priv->save_geometry_id);
701 priv->save_geometry_id =
702 g_timeout_add_seconds (1,
703 (GSourceFunc) chat_window_save_geometry_timeout_cb,
710 chat_window_close_activate_cb (GtkAction *action,
711 EmpathyChatWindow *window)
713 EmpathyChatWindowPriv *priv;
715 priv = GET_PRIV (window);
717 g_return_if_fail (priv->current_chat != NULL);
719 empathy_chat_window_remove_chat (window, priv->current_chat);
723 chat_window_edit_activate_cb (GtkAction *action,
724 EmpathyChatWindow *window)
726 EmpathyChatWindowPriv *priv;
727 GtkClipboard *clipboard;
728 GtkTextBuffer *buffer;
729 gboolean text_available;
731 priv = GET_PRIV (window);
733 g_return_if_fail (priv->current_chat != NULL);
735 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
736 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
737 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
738 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
742 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
743 if (gtk_text_buffer_get_has_selection (buffer)) {
744 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
745 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
749 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
751 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
752 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
755 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
756 text_available = gtk_clipboard_wait_is_text_available (clipboard);
757 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
761 chat_window_cut_activate_cb (GtkAction *action,
762 EmpathyChatWindow *window)
764 EmpathyChatWindowPriv *priv;
766 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
768 priv = GET_PRIV (window);
770 empathy_chat_cut (priv->current_chat);
774 chat_window_copy_activate_cb (GtkAction *action,
775 EmpathyChatWindow *window)
777 EmpathyChatWindowPriv *priv;
779 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
781 priv = GET_PRIV (window);
783 empathy_chat_copy (priv->current_chat);
787 chat_window_paste_activate_cb (GtkAction *action,
788 EmpathyChatWindow *window)
790 EmpathyChatWindowPriv *priv;
792 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
794 priv = GET_PRIV (window);
796 empathy_chat_paste (priv->current_chat);
800 chat_window_tabs_left_activate_cb (GtkAction *action,
801 EmpathyChatWindow *window)
803 EmpathyChatWindowPriv *priv;
807 priv = GET_PRIV (window);
809 chat = priv->current_chat;
810 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
815 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
821 chat_window_tabs_right_activate_cb (GtkAction *action,
822 EmpathyChatWindow *window)
824 EmpathyChatWindowPriv *priv;
828 priv = GET_PRIV (window);
830 chat = priv->current_chat;
831 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
833 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
839 chat_window_detach_activate_cb (GtkAction *action,
840 EmpathyChatWindow *window)
842 EmpathyChatWindowPriv *priv;
843 EmpathyChatWindow *new_window;
846 priv = GET_PRIV (window);
848 chat = priv->current_chat;
849 new_window = empathy_chat_window_new ();
851 empathy_chat_window_move_chat (window, new_window, chat);
853 priv = GET_PRIV (new_window);
854 gtk_widget_show (priv->dialog);
858 chat_window_help_contents_activate_cb (GtkAction *action,
859 EmpathyChatWindow *window)
861 EmpathyChatWindowPriv *priv = GET_PRIV (window);
863 empathy_url_show (priv->dialog, "ghelp:empathy");
867 chat_window_help_about_activate_cb (GtkAction *action,
868 EmpathyChatWindow *window)
870 EmpathyChatWindowPriv *priv = GET_PRIV (window);
872 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
876 chat_window_delete_event_cb (GtkWidget *dialog,
878 EmpathyChatWindow *window)
880 EmpathyChatWindowPriv *priv = GET_PRIV (window);
882 DEBUG ("Delete event received");
884 g_object_ref (window);
885 while (priv->chats) {
886 empathy_chat_window_remove_chat (window, priv->chats->data);
888 g_object_unref (window);
894 chat_window_composing_cb (EmpathyChat *chat,
895 gboolean is_composing,
896 EmpathyChatWindow *window)
898 EmpathyChatWindowPriv *priv;
900 priv = GET_PRIV (window);
902 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
903 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
905 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
908 chat_window_update_chat_tab (chat);
912 chat_window_set_urgency_hint (EmpathyChatWindow *window,
915 EmpathyChatWindowPriv *priv;
917 priv = GET_PRIV (window);
919 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
920 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
924 free_notification_data (NotificationData *data)
926 g_object_unref (data->chat);
927 g_slice_free (NotificationData, data);
931 chat_window_notification_closed_cb (NotifyNotification *notify,
932 NotificationData *cb_data)
934 EmpathyNotificationClosedReason reason = 0;
935 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
937 #ifdef notify_notification_get_closed_reason
938 reason = notify_notification_get_closed_reason (notify);
940 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
941 empathy_chat_window_present_chat (cb_data->chat);
944 g_object_unref (notify);
945 priv->notification = NULL;
946 free_notification_data (cb_data);
947 priv->notification_data = NULL;
951 chat_window_show_or_update_notification (EmpathyChatWindow *window,
952 EmpathyMessage *message,
955 EmpathyContact *sender;
960 EmpathyChatWindowPriv *priv = GET_PRIV (window);
963 if (!empathy_notification_is_enabled ()) {
966 empathy_conf_get_bool (empathy_conf_get (),
967 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
973 sender = empathy_message_get_sender (message);
974 header = empathy_contact_get_name (sender);
975 body = empathy_message_get_body (message);
976 escaped = g_markup_escape_text (body, -1);
978 if (priv->notification != NULL) {
979 notify_notification_update (priv->notification,
980 header, escaped, NULL);
982 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
984 cb_data->chat = g_object_ref (chat);
985 cb_data->window = window;
987 priv->notification_data = cb_data;
988 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
989 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
991 g_signal_connect (priv->notification, "closed",
992 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
995 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
997 if (pixbuf != NULL) {
998 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
999 g_object_unref (pixbuf);
1002 notify_notification_show (priv->notification, NULL);
1008 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1013 if (!empathy_chat_is_room (chat))
1016 markup = g_markup_printf_escaped (
1017 "<span color=\"red\" weight=\"bold\">%s</span>",
1018 empathy_chat_get_name (chat));
1020 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1021 gtk_label_set_markup (GTK_LABEL (widget), markup);
1026 chat_window_new_message_cb (EmpathyChat *chat,
1027 EmpathyMessage *message,
1028 EmpathyChatWindow *window)
1030 EmpathyChatWindowPriv *priv;
1032 gboolean needs_urgency;
1033 EmpathyContact *sender;
1035 priv = GET_PRIV (window);
1037 has_focus = empathy_chat_window_has_focus (window);
1039 /* - if we're the sender, we play the sound if it's specified in the
1040 * preferences and we're not away.
1041 * - if we receive a message, we play the sound if it's specified in the
1042 * preferences and the window does not have focus on the chat receiving
1046 sender = empathy_message_get_sender (message);
1048 if (empathy_contact_is_user (sender)) {
1049 empathy_sound_play (GTK_WIDGET (priv->dialog),
1050 EMPATHY_SOUND_MESSAGE_OUTGOING);
1053 if (has_focus && priv->current_chat == chat) {
1057 if (!g_list_find (priv->chats_new_msg, chat)) {
1058 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1059 chat_window_update_chat_tab (chat);
1062 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1063 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1064 * an unamed MUC (msn-like).
1065 * In case of a MUC, we set urgency only if the message contains our
1067 if (empathy_chat_is_room (chat) ||
1068 empathy_chat_get_remote_contact (chat) == NULL) {
1069 needs_urgency = empathy_message_should_highlight (message);
1071 needs_urgency = TRUE;
1074 if (needs_urgency) {
1076 chat_window_set_urgency_hint (window, TRUE);
1077 chat_window_set_highlight_room_tab_label (chat);
1080 empathy_sound_play (GTK_WIDGET (priv->dialog),
1081 EMPATHY_SOUND_MESSAGE_INCOMING);
1082 chat_window_show_or_update_notification (window, message, chat);
1086 static GtkNotebook *
1087 chat_window_detach_hook (GtkNotebook *source,
1093 EmpathyChatWindowPriv *priv;
1094 EmpathyChatWindow *window, *new_window;
1097 chat = EMPATHY_CHAT (page);
1098 window = chat_window_find_chat (chat);
1100 new_window = empathy_chat_window_new ();
1101 priv = GET_PRIV (new_window);
1103 DEBUG ("Detach hook called");
1105 empathy_chat_window_move_chat (window, new_window, chat);
1107 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1108 gtk_widget_show (priv->dialog);
1114 chat_window_page_switched_cb (GtkNotebook *notebook,
1115 GtkNotebookPage *page,
1117 EmpathyChatWindow *window)
1119 EmpathyChatWindowPriv *priv;
1123 DEBUG ("Page switched");
1125 priv = GET_PRIV (window);
1127 child = gtk_notebook_get_nth_page (notebook, page_num);
1128 chat = EMPATHY_CHAT (child);
1130 if (priv->page_added) {
1131 priv->page_added = FALSE;
1132 empathy_chat_scroll_down (chat);
1134 else if (priv->current_chat == chat) {
1138 priv->current_chat = chat;
1139 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1141 chat_window_update_chat_tab (chat);
1145 chat_window_page_added_cb (GtkNotebook *notebook,
1148 EmpathyChatWindow *window)
1150 EmpathyChatWindowPriv *priv;
1153 priv = GET_PRIV (window);
1155 /* If we just received DND to the same window, we don't want
1156 * to do anything here like removing the tab and then readding
1157 * it, so we return here and in "page-added".
1159 if (priv->dnd_same_window) {
1160 DEBUG ("Page added (back to the same window)");
1161 priv->dnd_same_window = FALSE;
1165 DEBUG ("Page added");
1167 /* Get chat object */
1168 chat = EMPATHY_CHAT (child);
1170 /* Connect chat signals for this window */
1171 g_signal_connect (chat, "composing",
1172 G_CALLBACK (chat_window_composing_cb),
1174 g_signal_connect (chat, "new-message",
1175 G_CALLBACK (chat_window_new_message_cb),
1177 g_signal_connect (chat, "notify::tp-chat",
1178 G_CALLBACK (chat_window_update_chat_tab),
1181 /* Set flag so we know to perform some special operations on
1182 * switch page due to the new page being added.
1184 priv->page_added = TRUE;
1186 /* Get list of chats up to date */
1187 priv->chats = g_list_append (priv->chats, chat);
1189 chat_window_update_chat_tab (chat);
1193 chat_window_page_removed_cb (GtkNotebook *notebook,
1196 EmpathyChatWindow *window)
1198 EmpathyChatWindowPriv *priv;
1201 priv = GET_PRIV (window);
1203 /* If we just received DND to the same window, we don't want
1204 * to do anything here like removing the tab and then readding
1205 * it, so we return here and in "page-added".
1207 if (priv->dnd_same_window) {
1208 DEBUG ("Page removed (and will be readded to same window)");
1212 DEBUG ("Page removed");
1214 /* Get chat object */
1215 chat = EMPATHY_CHAT (child);
1217 /* Disconnect all signal handlers for this chat and this window */
1218 g_signal_handlers_disconnect_by_func (chat,
1219 G_CALLBACK (chat_window_composing_cb),
1221 g_signal_handlers_disconnect_by_func (chat,
1222 G_CALLBACK (chat_window_new_message_cb),
1224 g_signal_handlers_disconnect_by_func (chat,
1225 G_CALLBACK (chat_window_update_chat_tab),
1228 /* Keep list of chats up to date */
1229 priv->chats = g_list_remove (priv->chats, chat);
1230 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1231 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1233 if (priv->chats == NULL) {
1234 g_object_unref (window);
1236 chat_window_update (window);
1241 chat_window_focus_in_event_cb (GtkWidget *widget,
1243 EmpathyChatWindow *window)
1245 EmpathyChatWindowPriv *priv;
1247 DEBUG ("Focus in event, updating title");
1249 priv = GET_PRIV (window);
1251 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1253 chat_window_set_urgency_hint (window, FALSE);
1255 /* Update the title, since we now mark all unread messages as read. */
1256 chat_window_update_chat_tab (priv->current_chat);
1262 chat_window_drag_data_received (GtkWidget *widget,
1263 GdkDragContext *context,
1266 GtkSelectionData *selection,
1269 EmpathyChatWindow *window)
1271 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1272 EmpathyChat *chat = NULL;
1273 EmpathyChatWindow *old_window;
1274 EmpathyAccount *account = NULL;
1275 EmpathyAccountManager *account_manager;
1278 const gchar *account_id;
1279 const gchar *contact_id;
1281 id = (const gchar*) gtk_selection_data_get_data (selection);
1282 account_manager = empathy_account_manager_dup_singleton ();
1284 DEBUG ("DND contact from roster with id:'%s'", id);
1286 strv = g_strsplit (id, ":", 2);
1287 if (g_strv_length (strv) == 2) {
1288 account_id = strv[0];
1289 contact_id = strv[1];
1291 empathy_account_manager_get_account (account_manager, account_id);
1292 if (account != NULL)
1293 chat = empathy_chat_window_find_chat (account, contact_id);
1296 if (account == NULL) {
1298 gtk_drag_finish (context, FALSE, FALSE, time);
1303 TpConnection *connection;
1305 connection = empathy_account_get_connection (account);
1308 empathy_dispatcher_chat_with_contact_id (
1309 connection, contact_id, NULL, NULL);
1315 g_object_unref (account_manager);
1318 old_window = chat_window_find_chat (chat);
1320 if (old_window == window) {
1321 gtk_drag_finish (context, TRUE, FALSE, time);
1325 empathy_chat_window_move_chat (old_window, window, chat);
1327 empathy_chat_window_add_chat (window, chat);
1330 /* Added to take care of any outstanding chat events */
1331 empathy_chat_window_present_chat (chat);
1333 /* We should return TRUE to remove the data when doing
1334 * GDK_ACTION_MOVE, but we don't here otherwise it has
1335 * weird consequences, and we handle that internally
1336 * anyway with add_chat () and remove_chat ().
1338 gtk_drag_finish (context, TRUE, FALSE, time);
1340 else if (info == DND_DRAG_TYPE_TAB) {
1342 EmpathyChatWindow *old_window = NULL;
1346 chat = (void *) gtk_selection_data_get_data (selection);
1347 old_window = chat_window_find_chat (*chat);
1350 EmpathyChatWindowPriv *priv;
1352 priv = GET_PRIV (window);
1354 if (old_window == window) {
1355 DEBUG ("DND tab (within same window)");
1356 priv->dnd_same_window = TRUE;
1357 gtk_drag_finish (context, TRUE, FALSE, time);
1361 priv->dnd_same_window = FALSE;
1364 /* We should return TRUE to remove the data when doing
1365 * GDK_ACTION_MOVE, but we don't here otherwise it has
1366 * weird consequences, and we handle that internally
1367 * anyway with add_chat () and remove_chat ().
1369 gtk_drag_finish (context, TRUE, FALSE, time);
1371 DEBUG ("DND from unknown source");
1372 gtk_drag_finish (context, FALSE, FALSE, time);
1377 chat_window_finalize (GObject *object)
1379 EmpathyChatWindow *window;
1380 EmpathyChatWindowPriv *priv;
1382 window = EMPATHY_CHAT_WINDOW (object);
1383 priv = GET_PRIV (window);
1385 DEBUG ("Finalized: %p", object);
1387 g_object_unref (priv->ui_manager);
1388 g_object_unref (priv->chatroom_manager);
1389 if (priv->save_geometry_id != 0) {
1390 g_source_remove (priv->save_geometry_id);
1393 if (priv->notification != NULL) {
1394 notify_notification_close (priv->notification, NULL);
1395 g_object_unref (priv->notification);
1396 priv->notification = NULL;
1397 if (priv->notification_data != NULL)
1399 free_notification_data (priv->notification_data);
1400 priv->notification_data = NULL;
1404 chat_windows = g_list_remove (chat_windows, window);
1405 gtk_widget_destroy (priv->dialog);
1407 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1411 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1413 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1415 object_class->finalize = chat_window_finalize;
1417 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1419 /* Set up a style for the close button with no focus padding. */
1420 gtk_rc_parse_string (
1421 "style \"empathy-close-button-style\"\n"
1423 " GtkWidget::focus-padding = 0\n"
1427 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1429 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1433 empathy_chat_window_init (EmpathyChatWindow *window)
1436 GtkAccelGroup *accel_group;
1441 GtkWidget *chat_vbox;
1443 EmpathySmileyManager *smiley_manager;
1444 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1445 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1447 window->priv = priv;
1448 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1449 gui = empathy_builder_get_file (filename,
1450 "chat_window", &priv->dialog,
1451 "chat_vbox", &chat_vbox,
1452 "ui_manager", &priv->ui_manager,
1453 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1454 "menu_conv_favorite", &priv->menu_conv_favorite,
1455 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1456 "menu_edit_cut", &priv->menu_edit_cut,
1457 "menu_edit_copy", &priv->menu_edit_copy,
1458 "menu_edit_paste", &priv->menu_edit_paste,
1459 "menu_tabs_next", &priv->menu_tabs_next,
1460 "menu_tabs_prev", &priv->menu_tabs_prev,
1461 "menu_tabs_left", &priv->menu_tabs_left,
1462 "menu_tabs_right", &priv->menu_tabs_right,
1463 "menu_tabs_detach", &priv->menu_tabs_detach,
1467 empathy_builder_connect (gui, window,
1468 "chat_window", "configure-event", chat_window_configure_event_cb,
1469 "menu_conv", "activate", chat_window_conv_activate_cb,
1470 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1471 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1472 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1473 "menu_conv_close", "activate", chat_window_close_activate_cb,
1474 "menu_edit", "activate", chat_window_edit_activate_cb,
1475 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1476 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1477 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1478 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1479 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1480 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1481 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1482 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1485 g_object_ref (priv->ui_manager);
1486 g_object_unref (gui);
1488 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1490 priv->notebook = gtk_notebook_new ();
1491 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1492 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1493 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1494 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1495 gtk_widget_show (priv->notebook);
1498 accel_group = gtk_accel_group_new ();
1499 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1501 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1502 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1505 gtk_accel_group_connect (accel_group,
1512 g_object_unref (accel_group);
1514 /* Set up smiley menu */
1515 smiley_manager = empathy_smiley_manager_dup_singleton ();
1516 submenu = empathy_smiley_menu_new (smiley_manager,
1517 chat_window_insert_smiley_activate_cb,
1519 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1520 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1521 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1522 g_object_unref (smiley_manager);
1524 /* Set up signals we can't do with ui file since we may need to
1525 * block/unblock them at some later stage.
1528 g_signal_connect (priv->dialog,
1530 G_CALLBACK (chat_window_delete_event_cb),
1533 g_signal_connect_swapped (priv->menu_tabs_prev,
1535 G_CALLBACK (gtk_notebook_prev_page),
1537 g_signal_connect_swapped (priv->menu_tabs_next,
1539 G_CALLBACK (gtk_notebook_next_page),
1542 g_signal_connect (priv->dialog,
1544 G_CALLBACK (chat_window_focus_in_event_cb),
1546 g_signal_connect_after (priv->notebook,
1548 G_CALLBACK (chat_window_page_switched_cb),
1550 g_signal_connect (priv->notebook,
1552 G_CALLBACK (chat_window_page_added_cb),
1554 g_signal_connect (priv->notebook,
1556 G_CALLBACK (chat_window_page_removed_cb),
1559 /* Set up drag and drop */
1560 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1561 GTK_DEST_DEFAULT_ALL,
1563 G_N_ELEMENTS (drag_types_dest),
1566 g_signal_connect (priv->notebook,
1567 "drag-data-received",
1568 G_CALLBACK (chat_window_drag_data_received),
1571 chat_windows = g_list_prepend (chat_windows, window);
1573 /* Set up private details */
1575 priv->chats_new_msg = NULL;
1576 priv->chats_composing = NULL;
1577 priv->current_chat = NULL;
1581 empathy_chat_window_new (void)
1583 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1586 /* Returns the window to open a new tab in if there is only one window
1587 * visble, otherwise, returns NULL indicating that a new window should
1591 empathy_chat_window_get_default (void)
1594 gboolean separate_windows = TRUE;
1596 empathy_conf_get_bool (empathy_conf_get (),
1597 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1600 if (separate_windows) {
1601 /* Always create a new window */
1605 for (l = chat_windows; l; l = l->next) {
1606 EmpathyChatWindow *chat_window;
1609 chat_window = l->data;
1611 dialog = empathy_chat_window_get_dialog (chat_window);
1612 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1613 /* Found a visible window on this desktop */
1622 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1624 EmpathyChatWindowPriv *priv;
1626 g_return_val_if_fail (window != NULL, NULL);
1628 priv = GET_PRIV (window);
1630 return priv->dialog;
1634 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1637 EmpathyChatWindowPriv *priv;
1639 GtkWidget *popup_label;
1643 g_return_if_fail (window != NULL);
1644 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1646 priv = GET_PRIV (window);
1648 /* Reference the chat object */
1649 g_object_ref (chat);
1651 /* If this window has just been created, position it */
1652 if (priv->chats == NULL) {
1653 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1655 if (x >= 0 && y >= 0) {
1656 /* Let the window manager position it if we don't have
1657 * good x, y coordinates.
1659 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1662 if (w > 0 && h > 0) {
1663 /* Use the defaults from the ui file if we don't have
1664 * good w, h geometry.
1666 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1670 child = GTK_WIDGET (chat);
1671 label = chat_window_create_label (window, chat, TRUE);
1672 popup_label = chat_window_create_label (window, chat, FALSE);
1673 gtk_widget_show (child);
1675 g_signal_connect (chat, "notify::name",
1676 G_CALLBACK (chat_window_chat_notify_cb),
1678 g_signal_connect (chat, "notify::subject",
1679 G_CALLBACK (chat_window_chat_notify_cb),
1681 g_signal_connect (chat, "notify::remote-contact",
1682 G_CALLBACK (chat_window_chat_notify_cb),
1684 chat_window_chat_notify_cb (chat);
1686 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1687 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1688 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1689 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1690 TRUE, TRUE, GTK_PACK_START);
1692 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1696 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1699 EmpathyChatWindowPriv *priv;
1701 EmpathyContact *remote_contact;
1703 g_return_if_fail (window != NULL);
1704 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1706 priv = GET_PRIV (window);
1708 g_signal_handlers_disconnect_by_func (chat,
1709 chat_window_chat_notify_cb,
1711 remote_contact = g_object_get_data (G_OBJECT (chat),
1712 "chat-window-remote-contact");
1713 if (remote_contact) {
1714 g_signal_handlers_disconnect_by_func (remote_contact,
1715 chat_window_update_chat_tab,
1719 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1721 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1723 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1725 g_object_unref (chat);
1729 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1730 EmpathyChatWindow *new_window,
1735 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1736 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1737 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1739 widget = GTK_WIDGET (chat);
1741 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1742 G_OBJECT (widget)->ref_count);
1744 /* We reference here to make sure we don't loose the widget
1745 * and the EmpathyChat object during the move.
1747 g_object_ref (chat);
1748 g_object_ref (widget);
1750 empathy_chat_window_remove_chat (old_window, chat);
1751 empathy_chat_window_add_chat (new_window, chat);
1753 g_object_unref (widget);
1754 g_object_unref (chat);
1758 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1761 EmpathyChatWindowPriv *priv;
1764 g_return_if_fail (window != NULL);
1765 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1767 priv = GET_PRIV (window);
1769 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1771 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1776 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1778 EmpathyChatWindowPriv *priv;
1781 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1783 priv = GET_PRIV (window);
1785 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1791 empathy_chat_window_find_chat (EmpathyAccount *account,
1796 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1798 for (l = chat_windows; l; l = l->next) {
1799 EmpathyChatWindowPriv *priv;
1800 EmpathyChatWindow *window;
1804 priv = GET_PRIV (window);
1806 for (ll = priv->chats; ll; ll = ll->next) {
1811 if (account == empathy_chat_get_account (chat) &&
1812 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1822 empathy_chat_window_present_chat (EmpathyChat *chat)
1824 EmpathyChatWindow *window;
1825 EmpathyChatWindowPriv *priv;
1827 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1829 window = chat_window_find_chat (chat);
1831 /* If the chat has no window, create one */
1832 if (window == NULL) {
1833 window = empathy_chat_window_get_default ();
1835 window = empathy_chat_window_new ();
1838 empathy_chat_window_add_chat (window, chat);
1841 priv = GET_PRIV (window);
1842 empathy_chat_window_switch_to_chat (window, chat);
1843 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1845 gtk_widget_grab_focus (chat->input_text_view);