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>
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;
77 GtkUIManager *ui_manager;
78 GtkAction *menu_conv_insert_smiley;
79 GtkAction *menu_conv_favorite;
80 GtkAction *menu_conv_toggle_contacts;
82 GtkAction *menu_edit_cut;
83 GtkAction *menu_edit_copy;
84 GtkAction *menu_edit_paste;
86 GtkAction *menu_tabs_next;
87 GtkAction *menu_tabs_prev;
88 GtkAction *menu_tabs_left;
89 GtkAction *menu_tabs_right;
90 GtkAction *menu_tabs_detach;
91 } EmpathyChatWindowPriv;
93 static GList *chat_windows = NULL;
95 static const guint tab_accel_keys[] = {
96 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
97 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
101 DND_DRAG_TYPE_CONTACT_ID,
105 static const GtkTargetEntry drag_types_dest[] = {
106 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
107 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
110 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
113 chat_window_accel_cb (GtkAccelGroup *accelgroup,
117 EmpathyChatWindow *window)
119 EmpathyChatWindowPriv *priv;
123 priv = GET_PRIV (window);
125 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
126 if (tab_accel_keys[i] == key) {
133 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
137 static EmpathyChatWindow *
138 chat_window_find_chat (EmpathyChat *chat)
140 EmpathyChatWindowPriv *priv;
143 for (l = chat_windows; l; l = l->next) {
144 priv = GET_PRIV (l->data);
145 ll = g_list_find (priv->chats, chat);
155 chat_window_close_clicked_cb (GtkAction *action,
158 EmpathyChatWindow *window;
160 window = chat_window_find_chat (chat);
161 empathy_chat_window_remove_chat (window, chat);
165 chat_tab_style_set_cb (GtkWidget *hbox,
166 GtkStyle *previous_style,
170 int char_width, h, w;
171 PangoContext *context;
172 PangoFontMetrics *metrics;
174 button = g_object_get_data (G_OBJECT (user_data),
175 "chat-window-tab-close-button");
176 context = gtk_widget_get_pango_context (hbox);
178 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
179 pango_context_get_language (context));
180 char_width = pango_font_metrics_get_approximate_char_width (metrics);
181 pango_font_metrics_unref (metrics);
183 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
184 GTK_ICON_SIZE_MENU, &w, &h);
186 /* Request at least about 12 chars width plus at least space for the status
187 * image and the close button */
188 gtk_widget_set_size_request (hbox,
189 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
191 gtk_widget_set_size_request (button, w, h);
195 chat_window_create_label (EmpathyChatWindow *window,
197 gboolean is_tab_label)
199 EmpathyChatWindowPriv *priv;
201 GtkWidget *name_label;
202 GtkWidget *status_image;
203 GtkWidget *close_button;
204 GtkWidget *close_image;
205 GtkWidget *event_box;
206 GtkWidget *event_box_hbox;
207 PangoAttrList *attr_list;
208 PangoAttribute *attr;
210 priv = GET_PRIV (window);
212 /* The spacing between the button and the label. */
213 hbox = gtk_hbox_new (FALSE, 0);
215 event_box = gtk_event_box_new ();
216 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
218 name_label = gtk_label_new (NULL);
220 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
222 attr_list = pango_attr_list_new ();
223 attr = pango_attr_scale_new (1/1.2);
224 attr->start_index = 0;
225 attr->end_index = -1;
226 pango_attr_list_insert (attr_list, attr);
227 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
228 pango_attr_list_unref (attr_list);
230 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
231 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
232 g_object_set_data (G_OBJECT (chat),
233 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
236 status_image = gtk_image_new ();
238 /* Spacing between the icon and label. */
239 event_box_hbox = gtk_hbox_new (FALSE, 0);
241 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
242 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
244 g_object_set_data (G_OBJECT (chat),
245 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
247 g_object_set_data (G_OBJECT (chat),
248 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
251 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
252 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
255 close_button = gtk_button_new ();
256 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
257 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
259 /* We don't want focus/keynav for the button to avoid clutter, and
260 * Ctrl-W works anyway.
262 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
263 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
265 /* Set the name to make the special rc style match. */
266 gtk_widget_set_name (close_button, "empathy-close-button");
268 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
270 gtk_container_add (GTK_CONTAINER (close_button), close_image);
272 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
274 g_signal_connect (close_button,
276 G_CALLBACK (chat_window_close_clicked_cb),
279 /* React to theme changes and also setup the size correctly. */
280 g_signal_connect (hbox,
282 G_CALLBACK (chat_tab_style_set_cb),
286 gtk_widget_show_all (hbox);
292 chat_window_update (EmpathyChatWindow *window)
294 EmpathyChatWindowPriv *priv = GET_PRIV (window);
297 gboolean is_connected;
304 EmpathyContact *remote_contact;
305 gboolean avatar_in_icon;
307 GtkWidget *chat_close_button;
311 /* Get information */
312 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
313 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
314 first_page = (page_num == 0);
315 last_page = (page_num == (num_pages - 1));
316 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
317 name = empathy_chat_get_name (priv->current_chat);
318 n_chats = g_list_length (priv->chats);
320 DEBUG ("Update window");
323 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
324 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
325 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
326 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
327 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
328 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
330 /* Update Contact menu */
331 menu = gtk_ui_manager_get_widget (priv->ui_manager,
332 "/chats_menubar/menu_contact");
333 submenu = empathy_chat_get_contact_menu (priv->current_chat);
334 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
335 gtk_widget_show (menu);
337 /* Update window title */
339 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
343 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
344 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
348 /* Update window icon */
349 if (priv->chats_new_msg) {
350 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
351 EMPATHY_IMAGE_MESSAGE);
353 empathy_conf_get_bool (empathy_conf_get (),
354 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
357 if (n_chats == 1 && avatar_in_icon) {
358 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
359 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
360 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
363 g_object_unref (icon);
366 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
370 if (num_pages == 1) {
371 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
372 chat_close_button = g_object_get_data (G_OBJECT (chat),
373 "chat-window-tab-close-button");
374 gtk_widget_hide (chat_close_button);
376 for (i=0; i<num_pages; i++) {
377 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
378 chat_close_button = g_object_get_data (G_OBJECT (chat),
379 "chat-window-tab-close-button");
380 gtk_widget_show (chat_close_button);
386 append_markup_printf (GString *string,
393 va_start (args, format);
395 tmp = g_markup_vprintf_escaped (format, args);
396 g_string_append (string, tmp);
403 chat_window_update_chat_tab (EmpathyChat *chat)
405 EmpathyChatWindow *window;
406 EmpathyChatWindowPriv *priv;
407 EmpathyContact *remote_contact;
410 EmpathyAccount *account;
411 const gchar *subject;
412 const gchar *status = NULL;
416 const gchar *icon_name;
418 window = chat_window_find_chat (chat);
422 priv = GET_PRIV (window);
424 /* Get information */
425 name = empathy_chat_get_name (chat);
426 account = empathy_chat_get_account (chat);
427 subject = empathy_chat_get_subject (chat);
428 remote_contact = empathy_chat_get_remote_contact (chat);
430 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
431 name, empathy_account_get_unique_name (account), subject, remote_contact);
433 /* Update tab image */
434 if (g_list_find (priv->chats_new_msg, chat)) {
435 icon_name = EMPATHY_IMAGE_MESSAGE;
437 else if (g_list_find (priv->chats_composing, chat)) {
438 icon_name = EMPATHY_IMAGE_TYPING;
440 else if (remote_contact) {
441 icon_name = empathy_icon_name_for_contact (remote_contact);
443 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
445 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
446 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
447 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
448 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
450 /* Update tab tooltip */
451 tooltip = g_string_new (NULL);
453 if (remote_contact) {
454 id = empathy_contact_get_id (remote_contact);
455 status = empathy_contact_get_presence_message (remote_contact);
460 append_markup_printf (tooltip,
461 "<b>%s</b><small> (%s)</small>",
463 empathy_account_get_display_name (account));
465 if (!EMP_STR_EMPTY (status)) {
466 append_markup_printf (tooltip, "\n<i>%s</i>", status);
470 append_markup_printf (tooltip, "\n<b>%s</b> %s",
471 _("Topic:"), subject);
474 if (g_list_find (priv->chats_composing, chat)) {
475 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
478 markup = g_string_free (tooltip, FALSE);
479 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
480 gtk_widget_set_tooltip_markup (widget, markup);
481 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
482 gtk_widget_set_tooltip_markup (widget, markup);
485 /* Update tab and menu label */
486 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
487 gtk_label_set_text (GTK_LABEL (widget), name);
488 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
489 gtk_label_set_text (GTK_LABEL (widget), name);
491 /* Update the window if it's the current chat */
492 if (priv->current_chat == chat) {
493 chat_window_update (window);
498 chat_window_chat_notify_cb (EmpathyChat *chat)
500 EmpathyContact *old_remote_contact;
501 EmpathyContact *remote_contact = NULL;
503 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
504 remote_contact = empathy_chat_get_remote_contact (chat);
506 if (old_remote_contact != remote_contact) {
507 /* The remote-contact associated with the chat changed, we need
508 * to keep track of any change of that contact and update the
509 * window each time. */
510 if (remote_contact) {
511 g_signal_connect_swapped (remote_contact, "notify",
512 G_CALLBACK (chat_window_update_chat_tab),
515 if (old_remote_contact) {
516 g_signal_handlers_disconnect_by_func (old_remote_contact,
517 chat_window_update_chat_tab,
521 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
525 chat_window_update_chat_tab (chat);
529 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
530 EmpathySmiley *smiley,
533 EmpathyChatWindowPriv *priv = GET_PRIV (window);
535 GtkTextBuffer *buffer;
538 chat = priv->current_chat;
540 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
541 gtk_text_buffer_get_end_iter (buffer, &iter);
542 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
546 chat_window_conv_activate_cb (GtkAction *action,
547 EmpathyChatWindow *window)
549 EmpathyChatWindowPriv *priv = GET_PRIV (window);
552 EmpathyContact *remote_contact = NULL;
554 /* Favorite room menu */
555 is_room = empathy_chat_is_room (priv->current_chat);
558 EmpathyAccount *account;
559 gboolean found = FALSE;
560 EmpathyChatroom *chatroom;
562 room = empathy_chat_get_id (priv->current_chat);
563 account = empathy_chat_get_account (priv->current_chat);
564 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
566 if (chatroom != NULL)
567 found = empathy_chatroom_is_favorite (chatroom);
569 DEBUG ("This room %s favorite", found ? "is" : "is not");
570 gtk_toggle_action_set_active (
571 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
573 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
575 /* Show contacts menu */
576 g_object_get (priv->current_chat,
577 "remote-contact", &remote_contact,
578 "show-contacts", &active,
580 if (remote_contact == NULL) {
581 gtk_toggle_action_set_active (
582 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
585 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
586 (remote_contact == NULL));
587 if (remote_contact != NULL) {
588 g_object_unref (remote_contact);
593 chat_window_clear_activate_cb (GtkAction *action,
594 EmpathyChatWindow *window)
596 EmpathyChatWindowPriv *priv = GET_PRIV (window);
598 empathy_chat_clear (priv->current_chat);
602 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
603 EmpathyChatWindow *window)
605 EmpathyChatWindowPriv *priv = GET_PRIV (window);
607 EmpathyAccount *account;
609 EmpathyChatroom *chatroom;
611 active = gtk_toggle_action_get_active (toggle_action);
612 account = empathy_chat_get_account (priv->current_chat);
613 room = empathy_chat_get_id (priv->current_chat);
615 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
618 if (chatroom == NULL) {
621 name = empathy_chat_get_name (priv->current_chat);
622 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
623 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
624 g_object_unref (chatroom);
627 empathy_chatroom_set_favorite (chatroom, active);
631 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
632 EmpathyChatWindow *window)
634 EmpathyChatWindowPriv *priv = GET_PRIV (window);
637 active = gtk_toggle_action_get_active (toggle_action);
639 empathy_chat_set_show_contacts (priv->current_chat, active);
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 /* if icon doesn't exist libnotify will crash */
978 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
980 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
981 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
982 /* if icon doesn't exist libnotify will crash */
984 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
986 g_signal_connect (priv->notification, "closed",
987 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
990 notify_notification_show (priv->notification, NULL);
992 g_object_unref (pixbuf);
997 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1002 if (empathy_chat_is_room (chat) == FALSE)
1005 markup = g_markup_printf_escaped ("<span color=\"%s\">%s</span>",
1007 empathy_chat_get_name (chat));
1009 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1010 gtk_label_set_markup (GTK_LABEL (widget), markup);
1015 chat_window_new_message_cb (EmpathyChat *chat,
1016 EmpathyMessage *message,
1017 EmpathyChatWindow *window)
1019 EmpathyChatWindowPriv *priv;
1021 gboolean needs_urgency;
1022 EmpathyContact *sender;
1024 priv = GET_PRIV (window);
1026 has_focus = empathy_chat_window_has_focus (window);
1028 /* - if we're the sender, we play the sound if it's specified in the
1029 * preferences and we're not away.
1030 * - if we receive a message, we play the sound if it's specified in the
1031 * preferences and the window does not have focus on the chat receiving
1035 sender = empathy_message_get_sender (message);
1037 if (empathy_contact_is_user (sender)) {
1038 empathy_sound_play (GTK_WIDGET (priv->dialog),
1039 EMPATHY_SOUND_MESSAGE_OUTGOING);
1042 if (has_focus && priv->current_chat == chat) {
1046 if (!g_list_find (priv->chats_new_msg, chat)) {
1047 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1048 chat_window_update_chat_tab (chat);
1051 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1052 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1053 * an unamed MUC (msn-like).
1054 * In case of a MUC, we set urgency only if the message contains our
1056 if (empathy_chat_is_room (chat) ||
1057 empathy_chat_get_remote_contact (chat) == NULL) {
1058 needs_urgency = empathy_message_should_highlight (message);
1060 needs_urgency = TRUE;
1063 if (needs_urgency) {
1065 chat_window_set_urgency_hint (window, TRUE);
1066 chat_window_set_highlight_room_tab_label (chat);
1069 empathy_sound_play (GTK_WIDGET (priv->dialog),
1070 EMPATHY_SOUND_MESSAGE_INCOMING);
1071 chat_window_show_or_update_notification (window, message, chat);
1075 static GtkNotebook *
1076 chat_window_detach_hook (GtkNotebook *source,
1082 EmpathyChatWindowPriv *priv;
1083 EmpathyChatWindow *window, *new_window;
1086 chat = EMPATHY_CHAT (page);
1087 window = chat_window_find_chat (chat);
1089 new_window = empathy_chat_window_new ();
1090 priv = GET_PRIV (new_window);
1092 DEBUG ("Detach hook called");
1094 empathy_chat_window_move_chat (window, new_window, chat);
1096 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1097 gtk_widget_show (priv->dialog);
1103 chat_window_page_switched_cb (GtkNotebook *notebook,
1104 GtkNotebookPage *page,
1106 EmpathyChatWindow *window)
1108 EmpathyChatWindowPriv *priv;
1112 DEBUG ("Page switched");
1114 priv = GET_PRIV (window);
1116 child = gtk_notebook_get_nth_page (notebook, page_num);
1117 chat = EMPATHY_CHAT (child);
1119 if (priv->page_added) {
1120 priv->page_added = FALSE;
1121 empathy_chat_scroll_down (chat);
1123 else if (priv->current_chat == chat) {
1127 priv->current_chat = chat;
1128 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1130 chat_window_update_chat_tab (chat);
1134 chat_window_page_added_cb (GtkNotebook *notebook,
1137 EmpathyChatWindow *window)
1139 EmpathyChatWindowPriv *priv;
1142 priv = GET_PRIV (window);
1144 /* If we just received DND to the same window, we don't want
1145 * to do anything here like removing the tab and then readding
1146 * it, so we return here and in "page-added".
1148 if (priv->dnd_same_window) {
1149 DEBUG ("Page added (back to the same window)");
1150 priv->dnd_same_window = FALSE;
1154 DEBUG ("Page added");
1156 /* Get chat object */
1157 chat = EMPATHY_CHAT (child);
1159 /* Connect chat signals for this window */
1160 g_signal_connect (chat, "composing",
1161 G_CALLBACK (chat_window_composing_cb),
1163 g_signal_connect (chat, "new-message",
1164 G_CALLBACK (chat_window_new_message_cb),
1167 /* Set flag so we know to perform some special operations on
1168 * switch page due to the new page being added.
1170 priv->page_added = TRUE;
1172 /* Get list of chats up to date */
1173 priv->chats = g_list_append (priv->chats, chat);
1175 chat_window_update_chat_tab (chat);
1179 chat_window_page_removed_cb (GtkNotebook *notebook,
1182 EmpathyChatWindow *window)
1184 EmpathyChatWindowPriv *priv;
1187 priv = GET_PRIV (window);
1189 /* If we just received DND to the same window, we don't want
1190 * to do anything here like removing the tab and then readding
1191 * it, so we return here and in "page-added".
1193 if (priv->dnd_same_window) {
1194 DEBUG ("Page removed (and will be readded to same window)");
1198 DEBUG ("Page removed");
1200 /* Get chat object */
1201 chat = EMPATHY_CHAT (child);
1203 /* Disconnect all signal handlers for this chat and this window */
1204 g_signal_handlers_disconnect_by_func (chat,
1205 G_CALLBACK (chat_window_composing_cb),
1207 g_signal_handlers_disconnect_by_func (chat,
1208 G_CALLBACK (chat_window_new_message_cb),
1211 /* Keep list of chats up to date */
1212 priv->chats = g_list_remove (priv->chats, chat);
1213 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1214 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1216 if (priv->chats == NULL) {
1217 g_object_unref (window);
1219 chat_window_update (window);
1224 chat_window_focus_in_event_cb (GtkWidget *widget,
1226 EmpathyChatWindow *window)
1228 EmpathyChatWindowPriv *priv;
1230 DEBUG ("Focus in event, updating title");
1232 priv = GET_PRIV (window);
1234 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1236 chat_window_set_urgency_hint (window, FALSE);
1238 /* Update the title, since we now mark all unread messages as read. */
1239 chat_window_update_chat_tab (priv->current_chat);
1245 chat_window_drag_data_received (GtkWidget *widget,
1246 GdkDragContext *context,
1249 GtkSelectionData *selection,
1252 EmpathyChatWindow *window)
1254 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1256 EmpathyChatWindow *old_window;
1257 EmpathyAccount *account;
1258 EmpathyAccountManager *account_manager;
1261 const gchar *account_id;
1262 const gchar *contact_id;
1264 id = (const gchar*) gtk_selection_data_get_data (selection);
1265 account_manager = empathy_account_manager_dup_singleton ();
1267 DEBUG ("DND contact from roster with id:'%s'", id);
1269 strv = g_strsplit (id, "/", 2);
1270 account_id = strv[0];
1271 contact_id = strv[1];
1272 account = empathy_account_manager_lookup (account_manager, account_id);
1273 chat = empathy_chat_window_find_chat (account, contact_id);
1276 TpConnection *connection;
1278 connection = empathy_account_get_connection (account);
1281 empathy_dispatcher_chat_with_contact_id (
1282 connection, contact_id, NULL, NULL);
1285 g_object_unref (account);
1289 g_object_unref (account);
1290 g_object_unref (account_manager);
1293 old_window = chat_window_find_chat (chat);
1295 if (old_window == window) {
1296 gtk_drag_finish (context, TRUE, FALSE, time);
1300 empathy_chat_window_move_chat (old_window, window, chat);
1302 empathy_chat_window_add_chat (window, chat);
1305 /* Added to take care of any outstanding chat events */
1306 empathy_chat_window_present_chat (chat);
1308 /* We should return TRUE to remove the data when doing
1309 * GDK_ACTION_MOVE, but we don't here otherwise it has
1310 * weird consequences, and we handle that internally
1311 * anyway with add_chat () and remove_chat ().
1313 gtk_drag_finish (context, TRUE, FALSE, time);
1315 else if (info == DND_DRAG_TYPE_TAB) {
1317 EmpathyChatWindow *old_window = NULL;
1321 chat = (void *) gtk_selection_data_get_data (selection);
1322 old_window = chat_window_find_chat (*chat);
1325 EmpathyChatWindowPriv *priv;
1327 priv = GET_PRIV (window);
1329 if (old_window == window) {
1330 DEBUG ("DND tab (within same window)");
1331 priv->dnd_same_window = TRUE;
1332 gtk_drag_finish (context, TRUE, FALSE, time);
1336 priv->dnd_same_window = FALSE;
1339 /* We should return TRUE to remove the data when doing
1340 * GDK_ACTION_MOVE, but we don't here otherwise it has
1341 * weird consequences, and we handle that internally
1342 * anyway with add_chat () and remove_chat ().
1344 gtk_drag_finish (context, TRUE, FALSE, time);
1346 DEBUG ("DND from unknown source");
1347 gtk_drag_finish (context, FALSE, FALSE, time);
1352 chat_window_finalize (GObject *object)
1354 EmpathyChatWindow *window;
1355 EmpathyChatWindowPriv *priv;
1357 window = EMPATHY_CHAT_WINDOW (object);
1358 priv = GET_PRIV (window);
1360 DEBUG ("Finalized: %p", object);
1362 g_object_unref (priv->ui_manager);
1363 g_object_unref (priv->chatroom_manager);
1364 if (priv->save_geometry_id != 0) {
1365 g_source_remove (priv->save_geometry_id);
1368 if (priv->notification != NULL) {
1369 notify_notification_close (priv->notification, NULL);
1370 g_object_unref (priv->notification);
1371 priv->notification = NULL;
1374 chat_windows = g_list_remove (chat_windows, window);
1375 gtk_widget_destroy (priv->dialog);
1377 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1381 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1383 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1385 object_class->finalize = chat_window_finalize;
1387 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1389 /* Set up a style for the close button with no focus padding. */
1390 gtk_rc_parse_string (
1391 "style \"empathy-close-button-style\"\n"
1393 " GtkWidget::focus-padding = 0\n"
1397 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1399 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1403 empathy_chat_window_init (EmpathyChatWindow *window)
1406 GtkAccelGroup *accel_group;
1411 GtkWidget *chat_vbox;
1413 EmpathySmileyManager *smiley_manager;
1414 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1415 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1417 window->priv = priv;
1418 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1419 gui = empathy_builder_get_file (filename,
1420 "chat_window", &priv->dialog,
1421 "chat_vbox", &chat_vbox,
1422 "ui_manager", &priv->ui_manager,
1423 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1424 "menu_conv_favorite", &priv->menu_conv_favorite,
1425 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1426 "menu_edit_cut", &priv->menu_edit_cut,
1427 "menu_edit_copy", &priv->menu_edit_copy,
1428 "menu_edit_paste", &priv->menu_edit_paste,
1429 "menu_tabs_next", &priv->menu_tabs_next,
1430 "menu_tabs_prev", &priv->menu_tabs_prev,
1431 "menu_tabs_left", &priv->menu_tabs_left,
1432 "menu_tabs_right", &priv->menu_tabs_right,
1433 "menu_tabs_detach", &priv->menu_tabs_detach,
1437 empathy_builder_connect (gui, window,
1438 "chat_window", "configure-event", chat_window_configure_event_cb,
1439 "menu_conv", "activate", chat_window_conv_activate_cb,
1440 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1441 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1442 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1443 "menu_conv_close", "activate", chat_window_close_activate_cb,
1444 "menu_edit", "activate", chat_window_edit_activate_cb,
1445 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1446 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1447 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1448 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1449 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1450 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1451 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1452 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1455 g_object_ref (priv->ui_manager);
1456 g_object_unref (gui);
1458 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1460 priv->notebook = gtk_notebook_new ();
1461 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1462 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1463 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1464 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1465 gtk_widget_show (priv->notebook);
1468 accel_group = gtk_accel_group_new ();
1469 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1471 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1472 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1475 gtk_accel_group_connect (accel_group,
1482 g_object_unref (accel_group);
1484 /* Set up smiley menu */
1485 smiley_manager = empathy_smiley_manager_dup_singleton ();
1486 submenu = empathy_smiley_menu_new (smiley_manager,
1487 chat_window_insert_smiley_activate_cb,
1489 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1490 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1491 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1492 g_object_unref (smiley_manager);
1494 /* Set up signals we can't do with ui file since we may need to
1495 * block/unblock them at some later stage.
1498 g_signal_connect (priv->dialog,
1500 G_CALLBACK (chat_window_delete_event_cb),
1503 g_signal_connect_swapped (priv->menu_tabs_prev,
1505 G_CALLBACK (gtk_notebook_prev_page),
1507 g_signal_connect_swapped (priv->menu_tabs_next,
1509 G_CALLBACK (gtk_notebook_next_page),
1512 g_signal_connect (priv->dialog,
1514 G_CALLBACK (chat_window_focus_in_event_cb),
1516 g_signal_connect_after (priv->notebook,
1518 G_CALLBACK (chat_window_page_switched_cb),
1520 g_signal_connect (priv->notebook,
1522 G_CALLBACK (chat_window_page_added_cb),
1524 g_signal_connect (priv->notebook,
1526 G_CALLBACK (chat_window_page_removed_cb),
1529 /* Set up drag and drop */
1530 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1531 GTK_DEST_DEFAULT_ALL,
1533 G_N_ELEMENTS (drag_types_dest),
1536 g_signal_connect (priv->notebook,
1537 "drag-data-received",
1538 G_CALLBACK (chat_window_drag_data_received),
1541 chat_windows = g_list_prepend (chat_windows, window);
1543 /* Set up private details */
1545 priv->chats_new_msg = NULL;
1546 priv->chats_composing = NULL;
1547 priv->current_chat = NULL;
1551 empathy_chat_window_new (void)
1553 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1556 /* Returns the window to open a new tab in if there is only one window
1557 * visble, otherwise, returns NULL indicating that a new window should
1561 empathy_chat_window_get_default (void)
1564 gboolean separate_windows = TRUE;
1566 empathy_conf_get_bool (empathy_conf_get (),
1567 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1570 if (separate_windows) {
1571 /* Always create a new window */
1575 for (l = chat_windows; l; l = l->next) {
1576 EmpathyChatWindow *chat_window;
1579 chat_window = l->data;
1581 dialog = empathy_chat_window_get_dialog (chat_window);
1582 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1583 /* Found a visible window on this desktop */
1592 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1594 EmpathyChatWindowPriv *priv;
1596 g_return_val_if_fail (window != NULL, NULL);
1598 priv = GET_PRIV (window);
1600 return priv->dialog;
1604 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1607 EmpathyChatWindowPriv *priv;
1609 GtkWidget *popup_label;
1613 g_return_if_fail (window != NULL);
1614 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1616 priv = GET_PRIV (window);
1618 /* Reference the chat object */
1619 g_object_ref (chat);
1621 /* If this window has just been created, position it */
1622 if (priv->chats == NULL) {
1623 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1625 if (x >= 0 && y >= 0) {
1626 /* Let the window manager position it if we don't have
1627 * good x, y coordinates.
1629 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1632 if (w > 0 && h > 0) {
1633 /* Use the defaults from the ui file if we don't have
1634 * good w, h geometry.
1636 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1640 child = GTK_WIDGET (chat);
1641 label = chat_window_create_label (window, chat, TRUE);
1642 popup_label = chat_window_create_label (window, chat, FALSE);
1643 gtk_widget_show (child);
1645 g_signal_connect (chat, "notify::name",
1646 G_CALLBACK (chat_window_chat_notify_cb),
1648 g_signal_connect (chat, "notify::subject",
1649 G_CALLBACK (chat_window_chat_notify_cb),
1651 g_signal_connect (chat, "notify::remote-contact",
1652 G_CALLBACK (chat_window_chat_notify_cb),
1654 chat_window_chat_notify_cb (chat);
1656 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1657 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1658 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1659 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1660 TRUE, TRUE, GTK_PACK_START);
1662 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1666 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1669 EmpathyChatWindowPriv *priv;
1671 EmpathyContact *remote_contact;
1673 g_return_if_fail (window != NULL);
1674 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1676 priv = GET_PRIV (window);
1678 g_signal_handlers_disconnect_by_func (chat,
1679 chat_window_chat_notify_cb,
1681 remote_contact = g_object_get_data (G_OBJECT (chat),
1682 "chat-window-remote-contact");
1683 if (remote_contact) {
1684 g_signal_handlers_disconnect_by_func (remote_contact,
1685 chat_window_update_chat_tab,
1689 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1691 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1693 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1695 g_object_unref (chat);
1699 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1700 EmpathyChatWindow *new_window,
1705 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1706 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1707 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1709 widget = GTK_WIDGET (chat);
1711 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1712 G_OBJECT (widget)->ref_count);
1714 /* We reference here to make sure we don't loose the widget
1715 * and the EmpathyChat object during the move.
1717 g_object_ref (chat);
1718 g_object_ref (widget);
1720 empathy_chat_window_remove_chat (old_window, chat);
1721 empathy_chat_window_add_chat (new_window, chat);
1723 g_object_unref (widget);
1724 g_object_unref (chat);
1728 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1731 EmpathyChatWindowPriv *priv;
1734 g_return_if_fail (window != NULL);
1735 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1737 priv = GET_PRIV (window);
1739 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1741 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1746 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1748 EmpathyChatWindowPriv *priv;
1751 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1753 priv = GET_PRIV (window);
1755 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1761 empathy_chat_window_find_chat (EmpathyAccount *account,
1766 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1768 for (l = chat_windows; l; l = l->next) {
1769 EmpathyChatWindowPriv *priv;
1770 EmpathyChatWindow *window;
1774 priv = GET_PRIV (window);
1776 for (ll = priv->chats; ll; ll = ll->next) {
1781 if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1782 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1792 empathy_chat_window_present_chat (EmpathyChat *chat)
1794 EmpathyChatWindow *window;
1795 EmpathyChatWindowPriv *priv;
1797 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1799 window = chat_window_find_chat (chat);
1801 /* If the chat has no window, create one */
1802 if (window == NULL) {
1803 window = empathy_chat_window_get_default ();
1805 window = empathy_chat_window_new ();
1808 empathy_chat_window_add_chat (window, chat);
1811 priv = GET_PRIV (window);
1812 empathy_chat_window_switch_to_chat (window, chat);
1813 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1815 gtk_widget_grab_focus (chat->input_text_view);