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/account-manager.h>
38 #include <telepathy-glib/util.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-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;
417 const gchar *subject;
418 const gchar *status = NULL;
422 const gchar *icon_name;
423 GtkWidget *tab_image;
424 GtkWidget *menu_image;
426 window = chat_window_find_chat (chat);
430 priv = GET_PRIV (window);
432 /* Get information */
433 name = empathy_chat_get_name (chat);
434 account = empathy_chat_get_account (chat);
435 subject = empathy_chat_get_subject (chat);
436 remote_contact = empathy_chat_get_remote_contact (chat);
438 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
439 name, tp_proxy_get_object_path (account), subject, remote_contact);
441 /* Update tab image */
442 if (empathy_chat_get_tp_chat (chat) == NULL) {
443 /* No TpChat, we are disconnected */
446 else if (g_list_find (priv->chats_new_msg, chat)) {
447 icon_name = EMPATHY_IMAGE_MESSAGE;
449 else if (g_list_find (priv->chats_composing, chat)) {
450 icon_name = EMPATHY_IMAGE_TYPING;
452 else if (remote_contact) {
453 icon_name = empathy_icon_name_for_contact (remote_contact);
455 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
458 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
459 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
460 if (icon_name != NULL) {
461 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
462 gtk_widget_show (tab_image);
463 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
464 gtk_widget_show (menu_image);
466 gtk_widget_hide (tab_image);
467 gtk_widget_hide (menu_image);
470 /* Update tab tooltip */
471 tooltip = g_string_new (NULL);
473 if (remote_contact) {
474 id = empathy_contact_get_id (remote_contact);
475 status = empathy_contact_get_presence_message (remote_contact);
480 append_markup_printf (tooltip,
481 "<b>%s</b><small> (%s)</small>",
483 tp_account_get_display_name (account));
485 if (!EMP_STR_EMPTY (status)) {
486 append_markup_printf (tooltip, "\n<i>%s</i>", status);
490 append_markup_printf (tooltip, "\n<b>%s</b> %s",
491 _("Topic:"), subject);
494 if (g_list_find (priv->chats_composing, chat)) {
495 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
498 markup = g_string_free (tooltip, FALSE);
499 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
500 gtk_widget_set_tooltip_markup (widget, markup);
501 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
502 gtk_widget_set_tooltip_markup (widget, markup);
505 /* Update tab and menu label */
506 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
507 gtk_label_set_text (GTK_LABEL (widget), name);
508 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
509 gtk_label_set_text (GTK_LABEL (widget), name);
511 /* Update the window if it's the current chat */
512 if (priv->current_chat == chat) {
513 chat_window_update (window);
518 chat_window_chat_notify_cb (EmpathyChat *chat)
520 EmpathyContact *old_remote_contact;
521 EmpathyContact *remote_contact = NULL;
523 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
524 remote_contact = empathy_chat_get_remote_contact (chat);
526 if (old_remote_contact != remote_contact) {
527 /* The remote-contact associated with the chat changed, we need
528 * to keep track of any change of that contact and update the
529 * window each time. */
530 if (remote_contact) {
531 g_signal_connect_swapped (remote_contact, "notify",
532 G_CALLBACK (chat_window_update_chat_tab),
535 if (old_remote_contact) {
536 g_signal_handlers_disconnect_by_func (old_remote_contact,
537 chat_window_update_chat_tab,
541 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
545 chat_window_update_chat_tab (chat);
549 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
550 EmpathySmiley *smiley,
553 EmpathyChatWindowPriv *priv = GET_PRIV (window);
555 GtkTextBuffer *buffer;
558 chat = priv->current_chat;
560 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
561 gtk_text_buffer_get_end_iter (buffer, &iter);
562 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
566 chat_window_conv_activate_cb (GtkAction *action,
567 EmpathyChatWindow *window)
569 EmpathyChatWindowPriv *priv = GET_PRIV (window);
572 EmpathyContact *remote_contact = NULL;
574 /* Favorite room menu */
575 is_room = empathy_chat_is_room (priv->current_chat);
579 gboolean found = FALSE;
580 EmpathyChatroom *chatroom;
582 room = empathy_chat_get_id (priv->current_chat);
583 account = empathy_chat_get_account (priv->current_chat);
584 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
586 if (chatroom != NULL)
587 found = empathy_chatroom_is_favorite (chatroom);
589 DEBUG ("This room %s favorite", found ? "is" : "is not");
590 gtk_toggle_action_set_active (
591 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
593 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
595 /* Show contacts menu */
596 g_object_get (priv->current_chat,
597 "remote-contact", &remote_contact,
598 "show-contacts", &active,
600 if (remote_contact == NULL) {
601 gtk_toggle_action_set_active (
602 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
605 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
606 (remote_contact == NULL));
607 if (remote_contact != NULL) {
608 g_object_unref (remote_contact);
613 chat_window_clear_activate_cb (GtkAction *action,
614 EmpathyChatWindow *window)
616 EmpathyChatWindowPriv *priv = GET_PRIV (window);
618 empathy_chat_clear (priv->current_chat);
622 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
623 EmpathyChatWindow *window)
625 EmpathyChatWindowPriv *priv = GET_PRIV (window);
629 EmpathyChatroom *chatroom;
631 active = gtk_toggle_action_get_active (toggle_action);
632 account = empathy_chat_get_account (priv->current_chat);
633 room = empathy_chat_get_id (priv->current_chat);
635 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
638 if (chatroom == NULL) {
641 name = empathy_chat_get_name (priv->current_chat);
642 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
643 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
644 g_object_unref (chatroom);
647 empathy_chatroom_set_favorite (chatroom, active);
651 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
652 EmpathyChatWindow *window)
654 EmpathyChatWindowPriv *priv = GET_PRIV (window);
657 active = gtk_toggle_action_get_active (toggle_action);
659 empathy_chat_set_show_contacts (priv->current_chat, active);
663 chat_get_window_id_for_geometry (EmpathyChat *chat)
665 const gchar *res = NULL;
666 gboolean separate_windows;
668 empathy_conf_get_bool (empathy_conf_get (),
669 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
672 if (separate_windows) {
673 res = empathy_chat_get_id (chat);
676 return res ? res : "chat-window";
680 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
682 EmpathyChatWindowPriv *priv;
685 priv = GET_PRIV (window);
687 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
688 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
690 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
693 priv->save_geometry_id = 0;
699 chat_window_configure_event_cb (GtkWidget *widget,
700 GdkEventConfigure *event,
701 EmpathyChatWindow *window)
703 EmpathyChatWindowPriv *priv;
705 priv = GET_PRIV (window);
707 if (priv->save_geometry_id != 0) {
708 g_source_remove (priv->save_geometry_id);
711 priv->save_geometry_id =
712 g_timeout_add_seconds (1,
713 (GSourceFunc) chat_window_save_geometry_timeout_cb,
720 chat_window_close_activate_cb (GtkAction *action,
721 EmpathyChatWindow *window)
723 EmpathyChatWindowPriv *priv;
725 priv = GET_PRIV (window);
727 g_return_if_fail (priv->current_chat != NULL);
729 empathy_chat_window_remove_chat (window, priv->current_chat);
733 chat_window_edit_activate_cb (GtkAction *action,
734 EmpathyChatWindow *window)
736 EmpathyChatWindowPriv *priv;
737 GtkClipboard *clipboard;
738 GtkTextBuffer *buffer;
739 gboolean text_available;
741 priv = GET_PRIV (window);
743 g_return_if_fail (priv->current_chat != NULL);
745 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
746 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
747 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
748 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
752 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
753 if (gtk_text_buffer_get_has_selection (buffer)) {
754 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
755 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
759 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
761 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
762 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
765 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
766 text_available = gtk_clipboard_wait_is_text_available (clipboard);
767 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
771 chat_window_cut_activate_cb (GtkAction *action,
772 EmpathyChatWindow *window)
774 EmpathyChatWindowPriv *priv;
776 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
778 priv = GET_PRIV (window);
780 empathy_chat_cut (priv->current_chat);
784 chat_window_copy_activate_cb (GtkAction *action,
785 EmpathyChatWindow *window)
787 EmpathyChatWindowPriv *priv;
789 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
791 priv = GET_PRIV (window);
793 empathy_chat_copy (priv->current_chat);
797 chat_window_paste_activate_cb (GtkAction *action,
798 EmpathyChatWindow *window)
800 EmpathyChatWindowPriv *priv;
802 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
804 priv = GET_PRIV (window);
806 empathy_chat_paste (priv->current_chat);
810 chat_window_tabs_left_activate_cb (GtkAction *action,
811 EmpathyChatWindow *window)
813 EmpathyChatWindowPriv *priv;
817 priv = GET_PRIV (window);
819 chat = priv->current_chat;
820 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
825 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
831 chat_window_tabs_right_activate_cb (GtkAction *action,
832 EmpathyChatWindow *window)
834 EmpathyChatWindowPriv *priv;
838 priv = GET_PRIV (window);
840 chat = priv->current_chat;
841 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
843 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
849 chat_window_detach_activate_cb (GtkAction *action,
850 EmpathyChatWindow *window)
852 EmpathyChatWindowPriv *priv;
853 EmpathyChatWindow *new_window;
856 priv = GET_PRIV (window);
858 chat = priv->current_chat;
859 new_window = empathy_chat_window_new ();
861 empathy_chat_window_move_chat (window, new_window, chat);
863 priv = GET_PRIV (new_window);
864 gtk_widget_show (priv->dialog);
868 chat_window_help_contents_activate_cb (GtkAction *action,
869 EmpathyChatWindow *window)
871 EmpathyChatWindowPriv *priv = GET_PRIV (window);
873 empathy_url_show (priv->dialog, "ghelp:empathy");
877 chat_window_help_about_activate_cb (GtkAction *action,
878 EmpathyChatWindow *window)
880 EmpathyChatWindowPriv *priv = GET_PRIV (window);
882 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
886 chat_window_delete_event_cb (GtkWidget *dialog,
888 EmpathyChatWindow *window)
890 EmpathyChatWindowPriv *priv = GET_PRIV (window);
892 DEBUG ("Delete event received");
894 g_object_ref (window);
895 while (priv->chats) {
896 empathy_chat_window_remove_chat (window, priv->chats->data);
898 g_object_unref (window);
904 chat_window_composing_cb (EmpathyChat *chat,
905 gboolean is_composing,
906 EmpathyChatWindow *window)
908 EmpathyChatWindowPriv *priv;
910 priv = GET_PRIV (window);
912 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
913 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
915 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
918 chat_window_update_chat_tab (chat);
922 chat_window_set_urgency_hint (EmpathyChatWindow *window,
925 EmpathyChatWindowPriv *priv;
927 priv = GET_PRIV (window);
929 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
930 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
934 free_notification_data (NotificationData *data)
936 g_object_unref (data->chat);
937 g_slice_free (NotificationData, data);
941 chat_window_notification_closed_cb (NotifyNotification *notify,
942 NotificationData *cb_data)
944 EmpathyNotificationClosedReason reason = 0;
945 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
947 #ifdef notify_notification_get_closed_reason
948 reason = notify_notification_get_closed_reason (notify);
950 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
951 empathy_chat_window_present_chat (cb_data->chat);
954 g_object_unref (notify);
955 priv->notification = NULL;
956 free_notification_data (cb_data);
957 priv->notification_data = NULL;
961 chat_window_show_or_update_notification (EmpathyChatWindow *window,
962 EmpathyMessage *message,
965 EmpathyContact *sender;
970 EmpathyChatWindowPriv *priv = GET_PRIV (window);
973 if (!empathy_notification_is_enabled ()) {
976 empathy_conf_get_bool (empathy_conf_get (),
977 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
983 sender = empathy_message_get_sender (message);
984 header = empathy_contact_get_name (sender);
985 body = empathy_message_get_body (message);
986 escaped = g_markup_escape_text (body, -1);
988 if (priv->notification != NULL) {
989 notify_notification_update (priv->notification,
990 header, escaped, NULL);
992 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
994 cb_data->chat = g_object_ref (chat);
995 cb_data->window = window;
997 priv->notification_data = cb_data;
998 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
999 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1001 g_signal_connect (priv->notification, "closed",
1002 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1005 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
1007 if (pixbuf != NULL) {
1008 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1009 g_object_unref (pixbuf);
1012 notify_notification_show (priv->notification, NULL);
1018 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1023 if (!empathy_chat_is_room (chat))
1026 markup = g_markup_printf_escaped (
1027 "<span color=\"red\" weight=\"bold\">%s</span>",
1028 empathy_chat_get_name (chat));
1030 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1031 gtk_label_set_markup (GTK_LABEL (widget), markup);
1036 chat_window_new_message_cb (EmpathyChat *chat,
1037 EmpathyMessage *message,
1038 EmpathyChatWindow *window)
1040 EmpathyChatWindowPriv *priv;
1042 gboolean needs_urgency;
1043 EmpathyContact *sender;
1045 priv = GET_PRIV (window);
1047 has_focus = empathy_chat_window_has_focus (window);
1049 /* - if we're the sender, we play the sound if it's specified in the
1050 * preferences and we're not away.
1051 * - if we receive a message, we play the sound if it's specified in the
1052 * preferences and the window does not have focus on the chat receiving
1056 sender = empathy_message_get_sender (message);
1058 if (empathy_contact_is_user (sender)) {
1059 empathy_sound_play (GTK_WIDGET (priv->dialog),
1060 EMPATHY_SOUND_MESSAGE_OUTGOING);
1063 if (has_focus && priv->current_chat == chat) {
1067 if (!g_list_find (priv->chats_new_msg, chat)) {
1068 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1069 chat_window_update_chat_tab (chat);
1072 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1073 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1074 * an unamed MUC (msn-like).
1075 * In case of a MUC, we set urgency only if the message contains our
1077 if (empathy_chat_is_room (chat) ||
1078 empathy_chat_get_remote_contact (chat) == NULL) {
1079 needs_urgency = empathy_message_should_highlight (message);
1081 needs_urgency = TRUE;
1084 if (needs_urgency) {
1086 chat_window_set_urgency_hint (window, TRUE);
1087 chat_window_set_highlight_room_tab_label (chat);
1090 empathy_sound_play (GTK_WIDGET (priv->dialog),
1091 EMPATHY_SOUND_MESSAGE_INCOMING);
1092 chat_window_show_or_update_notification (window, message, chat);
1096 static GtkNotebook *
1097 chat_window_detach_hook (GtkNotebook *source,
1103 EmpathyChatWindowPriv *priv;
1104 EmpathyChatWindow *window, *new_window;
1107 chat = EMPATHY_CHAT (page);
1108 window = chat_window_find_chat (chat);
1110 new_window = empathy_chat_window_new ();
1111 priv = GET_PRIV (new_window);
1113 DEBUG ("Detach hook called");
1115 empathy_chat_window_move_chat (window, new_window, chat);
1117 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1118 gtk_widget_show (priv->dialog);
1124 chat_window_page_switched_cb (GtkNotebook *notebook,
1125 GtkNotebookPage *page,
1127 EmpathyChatWindow *window)
1129 EmpathyChatWindowPriv *priv;
1133 DEBUG ("Page switched");
1135 priv = GET_PRIV (window);
1137 child = gtk_notebook_get_nth_page (notebook, page_num);
1138 chat = EMPATHY_CHAT (child);
1140 if (priv->page_added) {
1141 priv->page_added = FALSE;
1142 empathy_chat_scroll_down (chat);
1144 else if (priv->current_chat == chat) {
1148 priv->current_chat = chat;
1149 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1151 chat_window_update_chat_tab (chat);
1155 chat_window_page_added_cb (GtkNotebook *notebook,
1158 EmpathyChatWindow *window)
1160 EmpathyChatWindowPriv *priv;
1163 priv = GET_PRIV (window);
1165 /* If we just received DND to the same window, we don't want
1166 * to do anything here like removing the tab and then readding
1167 * it, so we return here and in "page-added".
1169 if (priv->dnd_same_window) {
1170 DEBUG ("Page added (back to the same window)");
1171 priv->dnd_same_window = FALSE;
1175 DEBUG ("Page added");
1177 /* Get chat object */
1178 chat = EMPATHY_CHAT (child);
1180 /* Connect chat signals for this window */
1181 g_signal_connect (chat, "composing",
1182 G_CALLBACK (chat_window_composing_cb),
1184 g_signal_connect (chat, "new-message",
1185 G_CALLBACK (chat_window_new_message_cb),
1187 g_signal_connect (chat, "notify::tp-chat",
1188 G_CALLBACK (chat_window_update_chat_tab),
1191 /* Set flag so we know to perform some special operations on
1192 * switch page due to the new page being added.
1194 priv->page_added = TRUE;
1196 /* Get list of chats up to date */
1197 priv->chats = g_list_append (priv->chats, chat);
1199 chat_window_update_chat_tab (chat);
1203 chat_window_page_removed_cb (GtkNotebook *notebook,
1206 EmpathyChatWindow *window)
1208 EmpathyChatWindowPriv *priv;
1211 priv = GET_PRIV (window);
1213 /* If we just received DND to the same window, we don't want
1214 * to do anything here like removing the tab and then readding
1215 * it, so we return here and in "page-added".
1217 if (priv->dnd_same_window) {
1218 DEBUG ("Page removed (and will be readded to same window)");
1222 DEBUG ("Page removed");
1224 /* Get chat object */
1225 chat = EMPATHY_CHAT (child);
1227 /* Disconnect all signal handlers for this chat and this window */
1228 g_signal_handlers_disconnect_by_func (chat,
1229 G_CALLBACK (chat_window_composing_cb),
1231 g_signal_handlers_disconnect_by_func (chat,
1232 G_CALLBACK (chat_window_new_message_cb),
1234 g_signal_handlers_disconnect_by_func (chat,
1235 G_CALLBACK (chat_window_update_chat_tab),
1238 /* Keep list of chats up to date */
1239 priv->chats = g_list_remove (priv->chats, chat);
1240 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1241 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1243 if (priv->chats == NULL) {
1244 g_object_unref (window);
1246 chat_window_update (window);
1251 chat_window_focus_in_event_cb (GtkWidget *widget,
1253 EmpathyChatWindow *window)
1255 EmpathyChatWindowPriv *priv;
1257 DEBUG ("Focus in event, updating title");
1259 priv = GET_PRIV (window);
1261 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1263 chat_window_set_urgency_hint (window, FALSE);
1265 /* Update the title, since we now mark all unread messages as read. */
1266 chat_window_update_chat_tab (priv->current_chat);
1272 chat_window_drag_data_received (GtkWidget *widget,
1273 GdkDragContext *context,
1276 GtkSelectionData *selection,
1279 EmpathyChatWindow *window)
1281 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1282 EmpathyChat *chat = NULL;
1283 EmpathyChatWindow *old_window;
1284 TpAccount *account = NULL;
1285 TpAccountManager *account_manager;
1288 const gchar *account_id;
1289 const gchar *contact_id;
1291 id = (const gchar*) gtk_selection_data_get_data (selection);
1293 /* FIXME: Perhaps should be sure that the account manager is
1294 * prepared before calling _ensure_account on it. */
1295 account_manager = tp_account_manager_dup ();
1297 DEBUG ("DND contact from roster with id:'%s'", id);
1299 strv = g_strsplit (id, ":", 2);
1300 if (g_strv_length (strv) == 2) {
1301 account_id = strv[0];
1302 contact_id = strv[1];
1304 tp_account_manager_ensure_account (account_manager, account_id);
1305 if (account != NULL)
1306 chat = empathy_chat_window_find_chat (account, contact_id);
1309 if (account == NULL) {
1311 gtk_drag_finish (context, FALSE, FALSE, time_);
1316 TpConnection *connection;
1318 connection = tp_account_get_connection (account);
1321 empathy_dispatcher_chat_with_contact_id (
1322 connection, contact_id, NULL, NULL);
1328 g_object_unref (account_manager);
1331 old_window = chat_window_find_chat (chat);
1333 if (old_window == window) {
1334 gtk_drag_finish (context, TRUE, FALSE, time_);
1338 empathy_chat_window_move_chat (old_window, window, chat);
1340 empathy_chat_window_add_chat (window, chat);
1343 /* Added to take care of any outstanding chat events */
1344 empathy_chat_window_present_chat (chat);
1346 /* We should return TRUE to remove the data when doing
1347 * GDK_ACTION_MOVE, but we don't here otherwise it has
1348 * weird consequences, and we handle that internally
1349 * anyway with add_chat () and remove_chat ().
1351 gtk_drag_finish (context, TRUE, FALSE, time_);
1353 else if (info == DND_DRAG_TYPE_TAB) {
1355 EmpathyChatWindow *old_window = NULL;
1359 chat = (void *) gtk_selection_data_get_data (selection);
1360 old_window = chat_window_find_chat (*chat);
1363 EmpathyChatWindowPriv *priv;
1365 priv = GET_PRIV (window);
1367 if (old_window == window) {
1368 DEBUG ("DND tab (within same window)");
1369 priv->dnd_same_window = TRUE;
1370 gtk_drag_finish (context, TRUE, FALSE, time_);
1374 priv->dnd_same_window = FALSE;
1377 /* We should return TRUE to remove the data when doing
1378 * GDK_ACTION_MOVE, but we don't here otherwise it has
1379 * weird consequences, and we handle that internally
1380 * anyway with add_chat () and remove_chat ().
1382 gtk_drag_finish (context, TRUE, FALSE, time_);
1384 DEBUG ("DND from unknown source");
1385 gtk_drag_finish (context, FALSE, FALSE, time_);
1390 chat_window_finalize (GObject *object)
1392 EmpathyChatWindow *window;
1393 EmpathyChatWindowPriv *priv;
1395 window = EMPATHY_CHAT_WINDOW (object);
1396 priv = GET_PRIV (window);
1398 DEBUG ("Finalized: %p", object);
1400 g_object_unref (priv->ui_manager);
1401 g_object_unref (priv->chatroom_manager);
1402 if (priv->save_geometry_id != 0) {
1403 g_source_remove (priv->save_geometry_id);
1406 if (priv->notification != NULL) {
1407 notify_notification_close (priv->notification, NULL);
1408 g_object_unref (priv->notification);
1409 priv->notification = NULL;
1410 if (priv->notification_data != NULL)
1412 free_notification_data (priv->notification_data);
1413 priv->notification_data = NULL;
1417 chat_windows = g_list_remove (chat_windows, window);
1418 gtk_widget_destroy (priv->dialog);
1420 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1424 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1426 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1428 object_class->finalize = chat_window_finalize;
1430 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1432 /* Set up a style for the close button with no focus padding. */
1433 gtk_rc_parse_string (
1434 "style \"empathy-close-button-style\"\n"
1436 " GtkWidget::focus-padding = 0\n"
1440 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1442 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1446 empathy_chat_window_init (EmpathyChatWindow *window)
1449 GtkAccelGroup *accel_group;
1454 GtkWidget *chat_vbox;
1456 EmpathySmileyManager *smiley_manager;
1457 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1458 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1460 window->priv = priv;
1461 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1462 gui = empathy_builder_get_file (filename,
1463 "chat_window", &priv->dialog,
1464 "chat_vbox", &chat_vbox,
1465 "ui_manager", &priv->ui_manager,
1466 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1467 "menu_conv_favorite", &priv->menu_conv_favorite,
1468 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1469 "menu_edit_cut", &priv->menu_edit_cut,
1470 "menu_edit_copy", &priv->menu_edit_copy,
1471 "menu_edit_paste", &priv->menu_edit_paste,
1472 "menu_tabs_next", &priv->menu_tabs_next,
1473 "menu_tabs_prev", &priv->menu_tabs_prev,
1474 "menu_tabs_left", &priv->menu_tabs_left,
1475 "menu_tabs_right", &priv->menu_tabs_right,
1476 "menu_tabs_detach", &priv->menu_tabs_detach,
1480 empathy_builder_connect (gui, window,
1481 "chat_window", "configure-event", chat_window_configure_event_cb,
1482 "menu_conv", "activate", chat_window_conv_activate_cb,
1483 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1484 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1485 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1486 "menu_conv_close", "activate", chat_window_close_activate_cb,
1487 "menu_edit", "activate", chat_window_edit_activate_cb,
1488 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1489 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1490 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1491 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1492 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1493 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1494 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1495 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1498 g_object_ref (priv->ui_manager);
1499 g_object_unref (gui);
1501 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1503 priv->notebook = gtk_notebook_new ();
1504 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1505 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1506 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1507 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1508 gtk_widget_show (priv->notebook);
1511 accel_group = gtk_accel_group_new ();
1512 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1514 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1515 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1518 gtk_accel_group_connect (accel_group,
1525 g_object_unref (accel_group);
1527 /* Set up smiley menu */
1528 smiley_manager = empathy_smiley_manager_dup_singleton ();
1529 submenu = empathy_smiley_menu_new (smiley_manager,
1530 chat_window_insert_smiley_activate_cb,
1532 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1533 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1534 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1535 g_object_unref (smiley_manager);
1537 /* Set up signals we can't do with ui file since we may need to
1538 * block/unblock them at some later stage.
1541 g_signal_connect (priv->dialog,
1543 G_CALLBACK (chat_window_delete_event_cb),
1546 g_signal_connect_swapped (priv->menu_tabs_prev,
1548 G_CALLBACK (gtk_notebook_prev_page),
1550 g_signal_connect_swapped (priv->menu_tabs_next,
1552 G_CALLBACK (gtk_notebook_next_page),
1555 g_signal_connect (priv->dialog,
1557 G_CALLBACK (chat_window_focus_in_event_cb),
1559 g_signal_connect_after (priv->notebook,
1561 G_CALLBACK (chat_window_page_switched_cb),
1563 g_signal_connect (priv->notebook,
1565 G_CALLBACK (chat_window_page_added_cb),
1567 g_signal_connect (priv->notebook,
1569 G_CALLBACK (chat_window_page_removed_cb),
1572 /* Set up drag and drop */
1573 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1574 GTK_DEST_DEFAULT_ALL,
1576 G_N_ELEMENTS (drag_types_dest),
1579 g_signal_connect (priv->notebook,
1580 "drag-data-received",
1581 G_CALLBACK (chat_window_drag_data_received),
1584 chat_windows = g_list_prepend (chat_windows, window);
1586 /* Set up private details */
1588 priv->chats_new_msg = NULL;
1589 priv->chats_composing = NULL;
1590 priv->current_chat = NULL;
1594 empathy_chat_window_new (void)
1596 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1599 /* Returns the window to open a new tab in if there is only one window
1600 * visble, otherwise, returns NULL indicating that a new window should
1604 empathy_chat_window_get_default (void)
1607 gboolean separate_windows = TRUE;
1609 empathy_conf_get_bool (empathy_conf_get (),
1610 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1613 if (separate_windows) {
1614 /* Always create a new window */
1618 for (l = chat_windows; l; l = l->next) {
1619 EmpathyChatWindow *chat_window;
1622 chat_window = l->data;
1624 dialog = empathy_chat_window_get_dialog (chat_window);
1625 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1626 /* Found a visible window on this desktop */
1635 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1637 EmpathyChatWindowPriv *priv;
1639 g_return_val_if_fail (window != NULL, NULL);
1641 priv = GET_PRIV (window);
1643 return priv->dialog;
1647 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1650 EmpathyChatWindowPriv *priv;
1652 GtkWidget *popup_label;
1656 g_return_if_fail (window != NULL);
1657 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1659 priv = GET_PRIV (window);
1661 /* Reference the chat object */
1662 g_object_ref (chat);
1664 /* If this window has just been created, position it */
1665 if (priv->chats == NULL) {
1666 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1668 if (x >= 0 && y >= 0) {
1669 /* Let the window manager position it if we don't have
1670 * good x, y coordinates.
1672 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1675 if (w > 0 && h > 0) {
1676 /* Use the defaults from the ui file if we don't have
1677 * good w, h geometry.
1679 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1683 child = GTK_WIDGET (chat);
1684 label = chat_window_create_label (window, chat, TRUE);
1685 popup_label = chat_window_create_label (window, chat, FALSE);
1686 gtk_widget_show (child);
1688 g_signal_connect (chat, "notify::name",
1689 G_CALLBACK (chat_window_chat_notify_cb),
1691 g_signal_connect (chat, "notify::subject",
1692 G_CALLBACK (chat_window_chat_notify_cb),
1694 g_signal_connect (chat, "notify::remote-contact",
1695 G_CALLBACK (chat_window_chat_notify_cb),
1697 chat_window_chat_notify_cb (chat);
1699 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1700 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1701 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1702 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1703 TRUE, TRUE, GTK_PACK_START);
1705 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1709 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1712 EmpathyChatWindowPriv *priv;
1714 EmpathyContact *remote_contact;
1716 g_return_if_fail (window != NULL);
1717 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1719 priv = GET_PRIV (window);
1721 g_signal_handlers_disconnect_by_func (chat,
1722 chat_window_chat_notify_cb,
1724 remote_contact = g_object_get_data (G_OBJECT (chat),
1725 "chat-window-remote-contact");
1726 if (remote_contact) {
1727 g_signal_handlers_disconnect_by_func (remote_contact,
1728 chat_window_update_chat_tab,
1732 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1734 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1736 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1738 g_object_unref (chat);
1742 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1743 EmpathyChatWindow *new_window,
1748 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1749 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1750 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1752 widget = GTK_WIDGET (chat);
1754 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1755 G_OBJECT (widget)->ref_count);
1757 /* We reference here to make sure we don't loose the widget
1758 * and the EmpathyChat object during the move.
1760 g_object_ref (chat);
1761 g_object_ref (widget);
1763 empathy_chat_window_remove_chat (old_window, chat);
1764 empathy_chat_window_add_chat (new_window, chat);
1766 g_object_unref (widget);
1767 g_object_unref (chat);
1771 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1774 EmpathyChatWindowPriv *priv;
1777 g_return_if_fail (window != NULL);
1778 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1780 priv = GET_PRIV (window);
1782 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1784 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1789 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1791 EmpathyChatWindowPriv *priv;
1794 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1796 priv = GET_PRIV (window);
1798 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1804 empathy_chat_window_find_chat (TpAccount *account,
1809 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1811 for (l = chat_windows; l; l = l->next) {
1812 EmpathyChatWindowPriv *priv;
1813 EmpathyChatWindow *window;
1817 priv = GET_PRIV (window);
1819 for (ll = priv->chats; ll; ll = ll->next) {
1824 if (account == empathy_chat_get_account (chat) &&
1825 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1835 empathy_chat_window_present_chat (EmpathyChat *chat)
1837 EmpathyChatWindow *window;
1838 EmpathyChatWindowPriv *priv;
1840 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1842 window = chat_window_find_chat (chat);
1844 /* If the chat has no window, create one */
1845 if (window == NULL) {
1846 window = empathy_chat_window_get_default ();
1848 window = empathy_chat_window_new ();
1851 empathy_chat_window_add_chat (window, chat);
1854 priv = GET_PRIV (window);
1855 empathy_chat_window_switch_to_chat (window, chat);
1856 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1858 gtk_widget_grab_focus (chat->input_text_view);