1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <libnotify/notification.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.h>
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy/empathy-chatroom-manager.h>
44 #include <libempathy/empathy-account-manager.h>
45 #include <libempathy/empathy-utils.h>
47 #include <libempathy-gtk/empathy-images.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-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>
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
64 EmpathyChat *current_chat;
67 GList *chats_composing;
69 gboolean dnd_same_window;
70 guint save_geometry_id;
71 EmpathyChatroomManager *chatroom_manager;
74 NotifyNotification *notification;
75 guint notify_id; /* show contacts' notification */
78 GtkUIManager *ui_manager;
79 GtkAction *menu_conv_insert_smiley;
80 GtkAction *menu_conv_favorite;
81 GtkAction *menu_conv_toggle_contacts;
83 GtkAction *menu_edit_cut;
84 GtkAction *menu_edit_copy;
85 GtkAction *menu_edit_paste;
87 GtkAction *menu_tabs_next;
88 GtkAction *menu_tabs_prev;
89 GtkAction *menu_tabs_left;
90 GtkAction *menu_tabs_right;
91 GtkAction *menu_tabs_detach;
92 } EmpathyChatWindowPriv;
94 static GList *chat_windows = NULL;
96 static const guint tab_accel_keys[] = {
97 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
98 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
102 DND_DRAG_TYPE_CONTACT_ID,
106 static const GtkTargetEntry drag_types_dest[] = {
107 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
108 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
111 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
114 chat_window_accel_cb (GtkAccelGroup *accelgroup,
118 EmpathyChatWindow *window)
120 EmpathyChatWindowPriv *priv;
124 priv = GET_PRIV (window);
126 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
127 if (tab_accel_keys[i] == key) {
134 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
139 chat_window_notify_show_contacts_cb (EmpathyConf *conf,
144 EmpathyChatWindowPriv *priv;
146 priv = GET_PRIV (user_data);
148 if (empathy_conf_get_bool (empathy_conf_get (), key, &value)) {
149 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts), value);
153 static EmpathyChatWindow *
154 chat_window_find_chat (EmpathyChat *chat)
156 EmpathyChatWindowPriv *priv;
159 for (l = chat_windows; l; l = l->next) {
160 priv = GET_PRIV (l->data);
161 ll = g_list_find (priv->chats, chat);
171 chat_window_close_clicked_cb (GtkAction *action,
174 EmpathyChatWindow *window;
176 window = chat_window_find_chat (chat);
177 empathy_chat_window_remove_chat (window, chat);
181 chat_tab_style_set_cb (GtkWidget *hbox,
182 GtkStyle *previous_style,
186 int char_width, h, w;
187 PangoContext *context;
188 PangoFontMetrics *metrics;
190 button = g_object_get_data (G_OBJECT (user_data),
191 "chat-window-tab-close-button");
192 context = gtk_widget_get_pango_context (hbox);
194 metrics = pango_context_get_metrics (context, hbox->style->font_desc,
195 pango_context_get_language (context));
196 char_width = pango_font_metrics_get_approximate_char_width (metrics);
197 pango_font_metrics_unref (metrics);
199 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
200 GTK_ICON_SIZE_MENU, &w, &h);
202 /* Request at least about 12 chars width plus at least space for the status
203 * image and the close button */
204 gtk_widget_set_size_request (hbox,
205 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
207 gtk_widget_set_size_request (button, w, h);
211 chat_window_create_label (EmpathyChatWindow *window,
213 gboolean is_tab_label)
215 EmpathyChatWindowPriv *priv;
217 GtkWidget *name_label;
218 GtkWidget *status_image;
219 GtkWidget *close_button;
220 GtkWidget *close_image;
221 GtkWidget *event_box;
222 GtkWidget *event_box_hbox;
223 PangoAttrList *attr_list;
224 PangoAttribute *attr;
226 priv = GET_PRIV (window);
228 /* The spacing between the button and the label. */
229 hbox = gtk_hbox_new (FALSE, 0);
231 event_box = gtk_event_box_new ();
232 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
234 name_label = gtk_label_new (NULL);
236 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
238 attr_list = pango_attr_list_new ();
239 attr = pango_attr_scale_new (1/1.2);
240 attr->start_index = 0;
241 attr->end_index = -1;
242 pango_attr_list_insert (attr_list, attr);
243 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
244 pango_attr_list_unref (attr_list);
246 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
247 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
248 g_object_set_data (G_OBJECT (chat),
249 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
252 status_image = gtk_image_new ();
254 /* Spacing between the icon and label. */
255 event_box_hbox = gtk_hbox_new (FALSE, 0);
257 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
258 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
260 g_object_set_data (G_OBJECT (chat),
261 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
263 g_object_set_data (G_OBJECT (chat),
264 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
267 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
268 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
271 close_button = gtk_button_new ();
272 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
273 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
275 /* We don't want focus/keynav for the button to avoid clutter, and
276 * Ctrl-W works anyway.
278 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
279 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
281 /* Set the name to make the special rc style match. */
282 gtk_widget_set_name (close_button, "empathy-close-button");
284 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
286 gtk_container_add (GTK_CONTAINER (close_button), close_image);
288 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
290 g_signal_connect (close_button,
292 G_CALLBACK (chat_window_close_clicked_cb),
295 /* React to theme changes and also setup the size correctly. */
296 g_signal_connect (hbox,
298 G_CALLBACK (chat_tab_style_set_cb),
302 gtk_widget_show_all (hbox);
308 chat_window_update (EmpathyChatWindow *window)
310 EmpathyChatWindowPriv *priv = GET_PRIV (window);
313 gboolean is_connected;
320 EmpathyContact *remote_contact;
321 gboolean avatar_in_icon;
323 GtkWidget *chat_close_button;
327 /* Get information */
328 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
329 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
330 first_page = (page_num == 0);
331 last_page = (page_num == (num_pages - 1));
332 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
333 name = empathy_chat_get_name (priv->current_chat);
334 n_chats = g_list_length (priv->chats);
336 DEBUG ("Update window");
339 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
340 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
341 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
342 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
343 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
344 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
346 /* Update Contact menu */
347 menu = gtk_ui_manager_get_widget (priv->ui_manager,
348 "/chats_menubar/menu_contact");
349 submenu = empathy_chat_get_contact_menu (priv->current_chat);
350 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
351 gtk_widget_show (menu);
353 /* Update window title */
355 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
359 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
360 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
364 /* Update window icon */
365 if (priv->chats_new_msg) {
366 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
367 EMPATHY_IMAGE_MESSAGE);
369 empathy_conf_get_bool (empathy_conf_get (),
370 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
373 if (n_chats == 1 && avatar_in_icon) {
374 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
375 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
376 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
379 g_object_unref (icon);
382 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
386 if (num_pages == 1) {
387 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
388 chat_close_button = g_object_get_data (G_OBJECT (chat),
389 "chat-window-tab-close-button");
390 gtk_widget_hide (chat_close_button);
392 for (i=0; i<num_pages; i++) {
393 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
394 chat_close_button = g_object_get_data (G_OBJECT (chat),
395 "chat-window-tab-close-button");
396 gtk_widget_show (chat_close_button);
402 append_markup_printf (GString *string,
409 va_start (args, format);
411 tmp = g_markup_vprintf_escaped (format, args);
412 g_string_append (string, tmp);
419 chat_window_update_chat_tab (EmpathyChat *chat)
421 EmpathyChatWindow *window;
422 EmpathyChatWindowPriv *priv;
423 EmpathyContact *remote_contact;
427 const gchar *subject;
428 const gchar *status = NULL;
432 const gchar *icon_name;
434 window = chat_window_find_chat (chat);
438 priv = GET_PRIV (window);
440 /* Get information */
441 name = empathy_chat_get_name (chat);
442 account = empathy_chat_get_account (chat);
443 subject = empathy_chat_get_subject (chat);
444 remote_contact = empathy_chat_get_remote_contact (chat);
446 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
447 name, mc_account_get_unique_name (account), subject, remote_contact);
449 /* Update tab image */
450 if (g_list_find (priv->chats_new_msg, chat)) {
451 icon_name = EMPATHY_IMAGE_MESSAGE;
453 else if (g_list_find (priv->chats_composing, chat)) {
454 icon_name = EMPATHY_IMAGE_TYPING;
456 else if (remote_contact) {
457 icon_name = empathy_icon_name_for_contact (remote_contact);
459 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
461 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
462 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
463 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
464 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
466 /* Update tab tooltip */
467 tooltip = g_string_new (NULL);
469 if (remote_contact) {
470 id = empathy_contact_get_id (remote_contact);
471 status = empathy_contact_get_presence_message (remote_contact);
476 append_markup_printf (tooltip,
477 "<b>%s</b><small> (%s)</small>",
479 mc_account_get_display_name (account));
481 if (!EMP_STR_EMPTY (status)) {
482 append_markup_printf (tooltip, "\n<i>%s</i>", status);
486 append_markup_printf (tooltip, "\n<b>%s</b> %s",
487 _("Topic:"), subject);
490 if (g_list_find (priv->chats_composing, chat)) {
491 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
494 markup = g_string_free (tooltip, FALSE);
495 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
496 gtk_widget_set_tooltip_markup (widget, markup);
497 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
498 gtk_widget_set_tooltip_markup (widget, markup);
501 /* Update tab and menu label */
502 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
503 gtk_label_set_text (GTK_LABEL (widget), name);
504 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
505 gtk_label_set_text (GTK_LABEL (widget), name);
507 /* Update the window if it's the current chat */
508 if (priv->current_chat == chat) {
509 chat_window_update (window);
514 chat_window_chat_notify_cb (EmpathyChat *chat)
516 EmpathyContact *old_remote_contact;
517 EmpathyContact *remote_contact = NULL;
519 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
520 remote_contact = empathy_chat_get_remote_contact (chat);
522 if (old_remote_contact != remote_contact) {
523 /* The remote-contact associated with the chat changed, we need
524 * to keep track of any change of that contact and update the
525 * window each time. */
526 if (remote_contact) {
527 g_signal_connect_swapped (remote_contact, "notify",
528 G_CALLBACK (chat_window_update_chat_tab),
531 if (old_remote_contact) {
532 g_signal_handlers_disconnect_by_func (old_remote_contact,
533 chat_window_update_chat_tab,
537 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
541 chat_window_update_chat_tab (chat);
545 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
546 EmpathySmiley *smiley,
549 EmpathyChatWindowPriv *priv = GET_PRIV (window);
551 GtkTextBuffer *buffer;
554 chat = priv->current_chat;
556 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
557 gtk_text_buffer_get_end_iter (buffer, &iter);
558 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
562 chat_window_conv_activate_cb (GtkAction *action,
563 EmpathyChatWindow *window)
565 EmpathyChatWindowPriv *priv = GET_PRIV (window);
568 /* Favorite room menu */
569 is_room = empathy_chat_is_room (priv->current_chat);
575 room = empathy_chat_get_id (priv->current_chat);
576 account = empathy_chat_get_account (priv->current_chat);
577 found = empathy_chatroom_manager_find (priv->chatroom_manager,
578 account, room) != NULL;
580 DEBUG ("This room %s favorite", found ? "is" : "is not");
581 gtk_toggle_action_set_active (
582 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
584 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
585 gtk_action_set_visible (priv->menu_conv_toggle_contacts, is_room);
589 chat_window_clear_activate_cb (GtkAction *action,
590 EmpathyChatWindow *window)
592 EmpathyChatWindowPriv *priv = GET_PRIV (window);
594 empathy_chat_clear (priv->current_chat);
598 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
599 EmpathyChatWindow *window)
601 EmpathyChatWindowPriv *priv = GET_PRIV (window);
605 EmpathyChatroom *chatroom;
607 active = gtk_toggle_action_get_active (toggle_action);
608 account = empathy_chat_get_account (priv->current_chat);
609 room = empathy_chat_get_id (priv->current_chat);
611 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
614 if (active && !chatroom) {
617 name = empathy_chat_get_name (priv->current_chat);
618 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
619 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
620 g_object_unref (chatroom);
624 if (!active && chatroom) {
625 empathy_chatroom_manager_remove (priv->chatroom_manager, chatroom);
630 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
631 EmpathyChatWindow *window)
635 active = gtk_toggle_action_get_active (toggle_action);
637 empathy_conf_set_bool (empathy_conf_get (),
638 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
643 chat_get_window_id_for_geometry (EmpathyChat *chat)
645 const gchar *res = NULL;
646 gboolean separate_windows;
648 empathy_conf_get_bool (empathy_conf_get (),
649 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
652 if (separate_windows) {
653 res = empathy_chat_get_id (chat);
656 return res ? res : "chat-window";
660 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
662 EmpathyChatWindowPriv *priv;
665 priv = GET_PRIV (window);
667 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
668 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
670 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
673 priv->save_geometry_id = 0;
679 chat_window_configure_event_cb (GtkWidget *widget,
680 GdkEventConfigure *event,
681 EmpathyChatWindow *window)
683 EmpathyChatWindowPriv *priv;
685 priv = GET_PRIV (window);
687 if (priv->save_geometry_id != 0) {
688 g_source_remove (priv->save_geometry_id);
691 priv->save_geometry_id =
692 g_timeout_add_seconds (1,
693 (GSourceFunc) chat_window_save_geometry_timeout_cb,
700 chat_window_close_activate_cb (GtkAction *action,
701 EmpathyChatWindow *window)
703 EmpathyChatWindowPriv *priv;
705 priv = GET_PRIV (window);
707 g_return_if_fail (priv->current_chat != NULL);
709 empathy_chat_window_remove_chat (window, priv->current_chat);
713 chat_window_edit_activate_cb (GtkAction *action,
714 EmpathyChatWindow *window)
716 EmpathyChatWindowPriv *priv;
717 GtkClipboard *clipboard;
718 GtkTextBuffer *buffer;
719 gboolean text_available;
721 priv = GET_PRIV (window);
723 g_return_if_fail (priv->current_chat != NULL);
725 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
726 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
727 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
728 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
732 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
733 if (gtk_text_buffer_get_has_selection (buffer)) {
734 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
735 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
739 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
741 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
742 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
745 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
746 text_available = gtk_clipboard_wait_is_text_available (clipboard);
747 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
751 chat_window_cut_activate_cb (GtkAction *action,
752 EmpathyChatWindow *window)
754 EmpathyChatWindowPriv *priv;
756 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
758 priv = GET_PRIV (window);
760 empathy_chat_cut (priv->current_chat);
764 chat_window_copy_activate_cb (GtkAction *action,
765 EmpathyChatWindow *window)
767 EmpathyChatWindowPriv *priv;
769 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
771 priv = GET_PRIV (window);
773 empathy_chat_copy (priv->current_chat);
777 chat_window_paste_activate_cb (GtkAction *action,
778 EmpathyChatWindow *window)
780 EmpathyChatWindowPriv *priv;
782 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
784 priv = GET_PRIV (window);
786 empathy_chat_paste (priv->current_chat);
790 chat_window_tabs_left_activate_cb (GtkAction *action,
791 EmpathyChatWindow *window)
793 EmpathyChatWindowPriv *priv;
797 priv = GET_PRIV (window);
799 chat = priv->current_chat;
800 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
805 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
811 chat_window_tabs_right_activate_cb (GtkAction *action,
812 EmpathyChatWindow *window)
814 EmpathyChatWindowPriv *priv;
818 priv = GET_PRIV (window);
820 chat = priv->current_chat;
821 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
823 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
829 chat_window_detach_activate_cb (GtkAction *action,
830 EmpathyChatWindow *window)
832 EmpathyChatWindowPriv *priv;
833 EmpathyChatWindow *new_window;
836 priv = GET_PRIV (window);
838 chat = priv->current_chat;
839 new_window = empathy_chat_window_new ();
841 empathy_chat_window_move_chat (window, new_window, chat);
843 priv = GET_PRIV (new_window);
844 gtk_widget_show (priv->dialog);
848 chat_window_help_contents_activate_cb (GtkAction *action,
849 EmpathyChatWindow *window)
851 EmpathyChatWindowPriv *priv = GET_PRIV (window);
853 empathy_url_show (priv->dialog, "ghelp:empathy?chat");
857 chat_window_help_about_activate_cb (GtkAction *action,
858 EmpathyChatWindow *window)
860 EmpathyChatWindowPriv *priv = GET_PRIV (window);
862 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
866 chat_window_delete_event_cb (GtkWidget *dialog,
868 EmpathyChatWindow *window)
870 EmpathyChatWindowPriv *priv = GET_PRIV (window);
872 DEBUG ("Delete event received");
874 g_object_ref (window);
875 while (priv->chats) {
876 empathy_chat_window_remove_chat (window, priv->chats->data);
878 g_object_unref (window);
884 chat_window_composing_cb (EmpathyChat *chat,
885 gboolean is_composing,
886 EmpathyChatWindow *window)
888 EmpathyChatWindowPriv *priv;
890 priv = GET_PRIV (window);
892 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
893 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
895 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
898 chat_window_update_chat_tab (chat);
902 chat_window_set_urgency_hint (EmpathyChatWindow *window,
905 EmpathyChatWindowPriv *priv;
907 priv = GET_PRIV (window);
909 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
910 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
914 EmpathyChatWindow *window;
919 chat_window_notification_closed_cb (NotifyNotification *notify,
920 NotificationData *cb_data)
922 EmpathyNotificationClosedReason reason = 0;
923 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
925 #ifdef notify_notification_get_closed_reason
926 reason = notify_notification_get_closed_reason (notify);
928 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
929 empathy_chat_window_present_chat (cb_data->chat);
932 g_object_unref (notify);
933 priv->notification = NULL;
934 g_object_unref (cb_data->chat);
935 g_slice_free (NotificationData, cb_data);
939 chat_window_show_or_update_notification (EmpathyChatWindow *window,
940 EmpathyMessage *message,
943 EmpathyContact *sender;
948 NotificationData *cb_data;
949 EmpathyChatWindowPriv *priv = GET_PRIV (window);
952 if (!empathy_notification_is_enabled ()) {
955 empathy_conf_get_bool (empathy_conf_get (),
956 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
962 cb_data = g_slice_new0 (NotificationData);
963 cb_data->chat = g_object_ref (chat);
964 cb_data->window = window;
966 sender = empathy_message_get_sender (message);
967 header = empathy_contact_get_name (sender);
968 body = empathy_message_get_body (message);
969 escaped = g_markup_escape_text (body, -1);
971 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
973 if (priv->notification != NULL) {
974 notify_notification_update (priv->notification,
975 header, escaped, NULL);
976 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
978 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
979 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
980 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
982 g_signal_connect (priv->notification, "closed",
983 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
986 notify_notification_show (priv->notification, NULL);
988 g_object_unref (pixbuf);
993 chat_window_new_message_cb (EmpathyChat *chat,
994 EmpathyMessage *message,
995 EmpathyChatWindow *window)
997 EmpathyChatWindowPriv *priv;
999 gboolean needs_urgency;
1000 EmpathyContact *sender;
1002 priv = GET_PRIV (window);
1004 has_focus = empathy_chat_window_has_focus (window);
1006 /* - if we're the sender, we play the sound if it's specified in the
1007 * preferences and we're not away.
1008 * - if we receive a message, we play the sound if it's specified in the
1009 * preferences and the window does not have focus on the chat receiving
1013 sender = empathy_message_get_sender (message);
1015 if (empathy_contact_is_user (sender)) {
1016 empathy_sound_play (GTK_WIDGET (priv->dialog),
1017 EMPATHY_SOUND_MESSAGE_OUTGOING);
1020 if (has_focus && priv->current_chat == chat) {
1024 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1025 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1026 * an unamed MUC (msn-like).
1027 * In case of a MUC, we set urgency only if the message contains our
1029 if (empathy_chat_is_room (chat) ||
1030 empathy_chat_get_remote_contact (chat) == NULL) {
1031 needs_urgency = empathy_message_should_highlight (message);
1033 needs_urgency = TRUE;
1036 if (needs_urgency) {
1038 chat_window_set_urgency_hint (window, TRUE);
1040 empathy_sound_play (GTK_WIDGET (priv->dialog),
1041 EMPATHY_SOUND_MESSAGE_INCOMING);
1042 chat_window_show_or_update_notification (window, message, chat);
1045 if (!g_list_find (priv->chats_new_msg, chat)) {
1046 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1047 chat_window_update_chat_tab (chat);
1051 static GtkNotebook *
1052 chat_window_detach_hook (GtkNotebook *source,
1058 EmpathyChatWindowPriv *priv;
1059 EmpathyChatWindow *window, *new_window;
1062 chat = EMPATHY_CHAT (page);
1063 window = chat_window_find_chat (chat);
1065 new_window = empathy_chat_window_new ();
1066 priv = GET_PRIV (new_window);
1068 DEBUG ("Detach hook called");
1070 empathy_chat_window_move_chat (window, new_window, chat);
1072 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1073 gtk_widget_show (priv->dialog);
1079 chat_window_page_switched_cb (GtkNotebook *notebook,
1080 GtkNotebookPage *page,
1082 EmpathyChatWindow *window)
1084 EmpathyChatWindowPriv *priv;
1088 DEBUG ("Page switched");
1090 priv = GET_PRIV (window);
1092 child = gtk_notebook_get_nth_page (notebook, page_num);
1093 chat = EMPATHY_CHAT (child);
1095 if (priv->page_added) {
1096 priv->page_added = FALSE;
1097 empathy_chat_scroll_down (chat);
1099 else if (priv->current_chat == chat) {
1103 priv->current_chat = chat;
1104 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1106 chat_window_update_chat_tab (chat);
1110 chat_window_page_added_cb (GtkNotebook *notebook,
1113 EmpathyChatWindow *window)
1115 EmpathyChatWindowPriv *priv;
1118 priv = GET_PRIV (window);
1120 /* If we just received DND to the same window, we don't want
1121 * to do anything here like removing the tab and then readding
1122 * it, so we return here and in "page-added".
1124 if (priv->dnd_same_window) {
1125 DEBUG ("Page added (back to the same window)");
1126 priv->dnd_same_window = FALSE;
1130 DEBUG ("Page added");
1132 /* Get chat object */
1133 chat = EMPATHY_CHAT (child);
1135 /* Connect chat signals for this window */
1136 g_signal_connect (chat, "composing",
1137 G_CALLBACK (chat_window_composing_cb),
1139 g_signal_connect (chat, "new-message",
1140 G_CALLBACK (chat_window_new_message_cb),
1143 /* Set flag so we know to perform some special operations on
1144 * switch page due to the new page being added.
1146 priv->page_added = TRUE;
1148 /* Get list of chats up to date */
1149 priv->chats = g_list_append (priv->chats, chat);
1151 chat_window_update_chat_tab (chat);
1155 chat_window_page_removed_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 removed (and will be readded to same window)");
1174 DEBUG ("Page removed");
1176 /* Get chat object */
1177 chat = EMPATHY_CHAT (child);
1179 /* Disconnect all signal handlers for this chat and this window */
1180 g_signal_handlers_disconnect_by_func (chat,
1181 G_CALLBACK (chat_window_composing_cb),
1183 g_signal_handlers_disconnect_by_func (chat,
1184 G_CALLBACK (chat_window_new_message_cb),
1187 /* Keep list of chats up to date */
1188 priv->chats = g_list_remove (priv->chats, chat);
1189 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1190 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1192 if (priv->chats == NULL) {
1193 g_object_unref (window);
1195 chat_window_update (window);
1200 chat_window_focus_in_event_cb (GtkWidget *widget,
1202 EmpathyChatWindow *window)
1204 EmpathyChatWindowPriv *priv;
1206 DEBUG ("Focus in event, updating title");
1208 priv = GET_PRIV (window);
1210 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1212 chat_window_set_urgency_hint (window, FALSE);
1214 /* Update the title, since we now mark all unread messages as read. */
1215 chat_window_update_chat_tab (priv->current_chat);
1221 chat_window_drag_data_received (GtkWidget *widget,
1222 GdkDragContext *context,
1225 GtkSelectionData *selection,
1228 EmpathyChatWindow *window)
1230 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1232 EmpathyChatWindow *old_window;
1236 const gchar *account_id;
1237 const gchar *contact_id;
1239 id = (const gchar*) selection->data;
1241 DEBUG ("DND contact from roster with id:'%s'", id);
1243 strv = g_strsplit (id, "/", 2);
1244 account_id = strv[0];
1245 contact_id = strv[1];
1246 account = mc_account_lookup (account_id);
1247 chat = empathy_chat_window_find_chat (account, contact_id);
1250 EmpathyAccountManager *account_manager;
1251 TpConnection *connection;
1253 account_manager = empathy_account_manager_dup_singleton ();
1254 connection = empathy_account_manager_get_connection (
1255 account_manager, account);
1258 empathy_dispatcher_chat_with_contact_id (
1259 connection, contact_id, NULL, NULL);
1262 g_object_unref (account_manager);
1263 g_object_unref (account);
1267 g_object_unref (account);
1270 old_window = chat_window_find_chat (chat);
1272 if (old_window == window) {
1273 gtk_drag_finish (context, TRUE, FALSE, time);
1277 empathy_chat_window_move_chat (old_window, window, chat);
1279 empathy_chat_window_add_chat (window, chat);
1282 /* Added to take care of any outstanding chat events */
1283 empathy_chat_window_present_chat (chat);
1285 /* We should return TRUE to remove the data when doing
1286 * GDK_ACTION_MOVE, but we don't here otherwise it has
1287 * weird consequences, and we handle that internally
1288 * anyway with add_chat () and remove_chat ().
1290 gtk_drag_finish (context, TRUE, FALSE, time);
1292 else if (info == DND_DRAG_TYPE_TAB) {
1294 EmpathyChatWindow *old_window = NULL;
1298 chat = (void *) selection->data;
1299 old_window = chat_window_find_chat (*chat);
1302 EmpathyChatWindowPriv *priv;
1304 priv = GET_PRIV (window);
1306 if (old_window == window) {
1307 DEBUG ("DND tab (within same window)");
1308 priv->dnd_same_window = TRUE;
1309 gtk_drag_finish (context, TRUE, FALSE, time);
1313 priv->dnd_same_window = FALSE;
1316 /* We should return TRUE to remove the data when doing
1317 * GDK_ACTION_MOVE, but we don't here otherwise it has
1318 * weird consequences, and we handle that internally
1319 * anyway with add_chat () and remove_chat ().
1321 gtk_drag_finish (context, TRUE, FALSE, time);
1323 DEBUG ("DND from unknown source");
1324 gtk_drag_finish (context, FALSE, FALSE, time);
1329 chat_window_finalize (GObject *object)
1331 EmpathyChatWindow *window;
1332 EmpathyChatWindowPriv *priv;
1334 window = EMPATHY_CHAT_WINDOW (object);
1335 priv = GET_PRIV (window);
1337 DEBUG ("Finalized: %p", object);
1339 g_object_unref (priv->ui_manager);
1340 g_object_unref (priv->chatroom_manager);
1341 if (priv->save_geometry_id != 0) {
1342 g_source_remove (priv->save_geometry_id);
1345 if (priv->notification != NULL) {
1346 notify_notification_close (priv->notification, NULL);
1347 g_object_unref (priv->notification);
1348 priv->notification = NULL;
1351 chat_windows = g_list_remove (chat_windows, window);
1352 gtk_widget_destroy (priv->dialog);
1354 empathy_conf_notify_remove (empathy_conf_get (), priv->notify_id);
1356 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1360 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1362 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1364 object_class->finalize = chat_window_finalize;
1366 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1368 /* Set up a style for the close button with no focus padding. */
1369 gtk_rc_parse_string (
1370 "style \"empathy-close-button-style\"\n"
1372 " GtkWidget::focus-padding = 0\n"
1376 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1378 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1382 empathy_chat_window_init (EmpathyChatWindow *window)
1385 GtkAccelGroup *accel_group;
1390 GtkWidget *chat_vbox;
1392 EmpathySmileyManager *smiley_manager;
1393 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1394 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1396 window->priv = priv;
1397 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1398 gui = empathy_builder_get_file (filename,
1399 "chat_window", &priv->dialog,
1400 "chat_vbox", &chat_vbox,
1401 "ui_manager", &priv->ui_manager,
1402 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1403 "menu_conv_favorite", &priv->menu_conv_favorite,
1404 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1405 "menu_edit_cut", &priv->menu_edit_cut,
1406 "menu_edit_copy", &priv->menu_edit_copy,
1407 "menu_edit_paste", &priv->menu_edit_paste,
1408 "menu_tabs_next", &priv->menu_tabs_next,
1409 "menu_tabs_prev", &priv->menu_tabs_prev,
1410 "menu_tabs_left", &priv->menu_tabs_left,
1411 "menu_tabs_right", &priv->menu_tabs_right,
1412 "menu_tabs_detach", &priv->menu_tabs_detach,
1416 empathy_builder_connect (gui, window,
1417 "chat_window", "configure-event", chat_window_configure_event_cb,
1418 "menu_conv", "activate", chat_window_conv_activate_cb,
1419 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1420 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1421 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1422 "menu_conv_close", "activate", chat_window_close_activate_cb,
1423 "menu_edit", "activate", chat_window_edit_activate_cb,
1424 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1425 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1426 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1427 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1428 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1429 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1430 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1431 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1434 g_object_ref (priv->ui_manager);
1435 g_object_unref (gui);
1437 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1439 priv->notebook = gtk_notebook_new ();
1440 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1441 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1442 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1443 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1444 gtk_widget_show (priv->notebook);
1447 accel_group = gtk_accel_group_new ();
1448 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1450 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1451 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1454 gtk_accel_group_connect (accel_group,
1461 g_object_unref (accel_group);
1463 /* Set up smiley menu */
1464 smiley_manager = empathy_smiley_manager_dup_singleton ();
1465 submenu = empathy_smiley_menu_new (smiley_manager,
1466 chat_window_insert_smiley_activate_cb,
1468 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1469 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1470 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1471 g_object_unref (smiley_manager);
1473 /* Set up signals we can't do with ui file since we may need to
1474 * block/unblock them at some later stage.
1477 g_signal_connect (priv->dialog,
1479 G_CALLBACK (chat_window_delete_event_cb),
1482 g_signal_connect_swapped (priv->menu_tabs_prev,
1484 G_CALLBACK (gtk_notebook_prev_page),
1486 g_signal_connect_swapped (priv->menu_tabs_next,
1488 G_CALLBACK (gtk_notebook_next_page),
1491 g_signal_connect (priv->dialog,
1493 G_CALLBACK (chat_window_focus_in_event_cb),
1495 g_signal_connect_after (priv->notebook,
1497 G_CALLBACK (chat_window_page_switched_cb),
1499 g_signal_connect (priv->notebook,
1501 G_CALLBACK (chat_window_page_added_cb),
1503 g_signal_connect (priv->notebook,
1505 G_CALLBACK (chat_window_page_removed_cb),
1507 priv->notify_id = empathy_conf_notify_add (empathy_conf_get (),
1508 EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS,
1509 chat_window_notify_show_contacts_cb,
1512 /* Set up drag and drop */
1513 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1514 GTK_DEST_DEFAULT_ALL,
1516 G_N_ELEMENTS (drag_types_dest),
1519 g_signal_connect (priv->notebook,
1520 "drag-data-received",
1521 G_CALLBACK (chat_window_drag_data_received),
1524 chat_windows = g_list_prepend (chat_windows, window);
1526 /* Set up private details */
1528 priv->chats_new_msg = NULL;
1529 priv->chats_composing = NULL;
1530 priv->current_chat = NULL;
1534 empathy_chat_window_new (void)
1536 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1539 /* Returns the window to open a new tab in if there is only one window
1540 * visble, otherwise, returns NULL indicating that a new window should
1544 empathy_chat_window_get_default (void)
1547 gboolean separate_windows = TRUE;
1549 empathy_conf_get_bool (empathy_conf_get (),
1550 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1553 if (separate_windows) {
1554 /* Always create a new window */
1558 for (l = chat_windows; l; l = l->next) {
1559 EmpathyChatWindow *chat_window;
1562 chat_window = l->data;
1564 dialog = empathy_chat_window_get_dialog (chat_window);
1565 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1566 /* Found a visible window on this desktop */
1575 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1577 EmpathyChatWindowPriv *priv;
1579 g_return_val_if_fail (window != NULL, NULL);
1581 priv = GET_PRIV (window);
1583 return priv->dialog;
1587 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1590 EmpathyChatWindowPriv *priv;
1592 GtkWidget *popup_label;
1596 g_return_if_fail (window != NULL);
1597 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1599 priv = GET_PRIV (window);
1601 /* Reference the chat object */
1602 g_object_ref (chat);
1604 /* If this window has just been created, position it */
1605 if (priv->chats == NULL) {
1606 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1608 if (x >= 0 && y >= 0) {
1609 /* Let the window manager position it if we don't have
1610 * good x, y coordinates.
1612 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1615 if (w > 0 && h > 0) {
1616 /* Use the defaults from the ui file if we don't have
1617 * good w, h geometry.
1619 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1623 child = GTK_WIDGET (chat);
1624 label = chat_window_create_label (window, chat, TRUE);
1625 popup_label = chat_window_create_label (window, chat, FALSE);
1626 gtk_widget_show (child);
1628 g_signal_connect (chat, "notify::name",
1629 G_CALLBACK (chat_window_chat_notify_cb),
1631 g_signal_connect (chat, "notify::subject",
1632 G_CALLBACK (chat_window_chat_notify_cb),
1634 g_signal_connect (chat, "notify::remote-contact",
1635 G_CALLBACK (chat_window_chat_notify_cb),
1637 chat_window_chat_notify_cb (chat);
1639 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1640 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1641 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1642 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1643 TRUE, TRUE, GTK_PACK_START);
1645 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1649 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1652 EmpathyChatWindowPriv *priv;
1654 EmpathyContact *remote_contact;
1656 g_return_if_fail (window != NULL);
1657 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1659 priv = GET_PRIV (window);
1661 g_signal_handlers_disconnect_by_func (chat,
1662 chat_window_chat_notify_cb,
1664 remote_contact = g_object_get_data (G_OBJECT (chat),
1665 "chat-window-remote-contact");
1666 if (remote_contact) {
1667 g_signal_handlers_disconnect_by_func (remote_contact,
1668 chat_window_update_chat_tab,
1672 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1674 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1676 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1678 g_object_unref (chat);
1682 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1683 EmpathyChatWindow *new_window,
1688 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1689 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1690 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1692 widget = GTK_WIDGET (chat);
1694 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1695 G_OBJECT (widget)->ref_count);
1697 /* We reference here to make sure we don't loose the widget
1698 * and the EmpathyChat object during the move.
1700 g_object_ref (chat);
1701 g_object_ref (widget);
1703 empathy_chat_window_remove_chat (old_window, chat);
1704 empathy_chat_window_add_chat (new_window, chat);
1706 g_object_unref (widget);
1707 g_object_unref (chat);
1711 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1714 EmpathyChatWindowPriv *priv;
1717 g_return_if_fail (window != NULL);
1718 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1720 priv = GET_PRIV (window);
1722 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1724 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1729 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1731 EmpathyChatWindowPriv *priv;
1734 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1736 priv = GET_PRIV (window);
1738 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1744 empathy_chat_window_find_chat (McAccount *account,
1749 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1750 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1752 for (l = chat_windows; l; l = l->next) {
1753 EmpathyChatWindowPriv *priv;
1754 EmpathyChatWindow *window;
1758 priv = GET_PRIV (window);
1760 for (ll = priv->chats; ll; ll = ll->next) {
1765 if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1766 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1776 empathy_chat_window_present_chat (EmpathyChat *chat)
1778 EmpathyChatWindow *window;
1779 EmpathyChatWindowPriv *priv;
1781 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1783 window = chat_window_find_chat (chat);
1785 /* If the chat has no window, create one */
1786 if (window == NULL) {
1787 window = empathy_chat_window_get_default ();
1789 window = empathy_chat_window_new ();
1792 empathy_chat_window_add_chat (window, chat);
1795 priv = GET_PRIV (window);
1796 empathy_chat_window_switch_to_chat (window, chat);
1797 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1799 gtk_widget_grab_focus (chat->input_text_view);