1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Geert-Jan Van den Bogaerde <geertjan@gnome.org>
25 * Xavier Claessens <xclaesse@gmail.com>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <libnotify/notification.h>
37 #include <telepathy-glib/util.h>
39 #include <libempathy/empathy-contact.h>
40 #include <libempathy/empathy-message.h>
41 #include <libempathy/empathy-dispatcher.h>
42 #include <libempathy/empathy-chatroom-manager.h>
43 #include <libempathy/empathy-account-manager.h>
44 #include <libempathy/empathy-utils.h>
46 #include <libempathy-gtk/empathy-images.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-contact-dialogs.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-geometry.h>
51 #include <libempathy-gtk/empathy-smiley-manager.h>
52 #include <libempathy-gtk/empathy-sound.h>
53 #include <libempathy-gtk/empathy-ui-utils.h>
55 #include "empathy-chat-window.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-misc.h"
59 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
60 #include <libempathy/empathy-debug.h>
63 EmpathyChatWindow *window;
67 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
69 EmpathyChat *current_chat;
72 GList *chats_composing;
74 gboolean dnd_same_window;
75 guint save_geometry_id;
76 EmpathyChatroomManager *chatroom_manager;
79 NotifyNotification *notification;
80 NotificationData *notification_data;
83 GtkUIManager *ui_manager;
84 GtkAction *menu_conv_insert_smiley;
85 GtkAction *menu_conv_favorite;
86 GtkAction *menu_conv_toggle_contacts;
88 GtkAction *menu_edit_cut;
89 GtkAction *menu_edit_copy;
90 GtkAction *menu_edit_paste;
92 GtkAction *menu_tabs_next;
93 GtkAction *menu_tabs_prev;
94 GtkAction *menu_tabs_left;
95 GtkAction *menu_tabs_right;
96 GtkAction *menu_tabs_detach;
97 } EmpathyChatWindowPriv;
99 static GList *chat_windows = NULL;
101 static const guint tab_accel_keys[] = {
102 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
103 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
107 DND_DRAG_TYPE_CONTACT_ID,
111 static const GtkTargetEntry drag_types_dest[] = {
112 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
113 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
116 static void chat_window_update (EmpathyChatWindow *window);
118 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
121 chat_window_accel_cb (GtkAccelGroup *accelgroup,
125 EmpathyChatWindow *window)
127 EmpathyChatWindowPriv *priv;
131 priv = GET_PRIV (window);
133 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
134 if (tab_accel_keys[i] == key) {
141 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
145 static EmpathyChatWindow *
146 chat_window_find_chat (EmpathyChat *chat)
148 EmpathyChatWindowPriv *priv;
151 for (l = chat_windows; l; l = l->next) {
152 priv = GET_PRIV (l->data);
153 ll = g_list_find (priv->chats, chat);
163 chat_window_close_clicked_cb (GtkAction *action,
166 EmpathyChatWindow *window;
168 window = chat_window_find_chat (chat);
169 empathy_chat_window_remove_chat (window, chat);
173 chat_tab_style_set_cb (GtkWidget *hbox,
174 GtkStyle *previous_style,
178 int char_width, h, w;
179 PangoContext *context;
180 PangoFontMetrics *metrics;
182 button = g_object_get_data (G_OBJECT (user_data),
183 "chat-window-tab-close-button");
184 context = gtk_widget_get_pango_context (hbox);
186 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
187 pango_context_get_language (context));
188 char_width = pango_font_metrics_get_approximate_char_width (metrics);
189 pango_font_metrics_unref (metrics);
191 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
192 GTK_ICON_SIZE_MENU, &w, &h);
194 /* Request at least about 12 chars width plus at least space for the status
195 * image and the close button */
196 gtk_widget_set_size_request (hbox,
197 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
199 gtk_widget_set_size_request (button, w, h);
203 chat_window_create_label (EmpathyChatWindow *window,
205 gboolean is_tab_label)
207 EmpathyChatWindowPriv *priv;
209 GtkWidget *name_label;
210 GtkWidget *status_image;
211 GtkWidget *close_button;
212 GtkWidget *close_image;
213 GtkWidget *event_box;
214 GtkWidget *event_box_hbox;
215 PangoAttrList *attr_list;
216 PangoAttribute *attr;
218 priv = GET_PRIV (window);
220 /* The spacing between the button and the label. */
221 hbox = gtk_hbox_new (FALSE, 0);
223 event_box = gtk_event_box_new ();
224 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
226 name_label = gtk_label_new (NULL);
228 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
230 attr_list = pango_attr_list_new ();
231 attr = pango_attr_scale_new (1/1.2);
232 attr->start_index = 0;
233 attr->end_index = -1;
234 pango_attr_list_insert (attr_list, attr);
235 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
236 pango_attr_list_unref (attr_list);
238 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
239 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
240 g_object_set_data (G_OBJECT (chat),
241 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
244 status_image = gtk_image_new ();
246 /* Spacing between the icon and label. */
247 event_box_hbox = gtk_hbox_new (FALSE, 0);
249 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
250 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
252 g_object_set_data (G_OBJECT (chat),
253 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
255 g_object_set_data (G_OBJECT (chat),
256 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
259 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
260 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
263 close_button = gtk_button_new ();
264 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
265 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
267 /* We don't want focus/keynav for the button to avoid clutter, and
268 * Ctrl-W works anyway.
270 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
271 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
273 /* Set the name to make the special rc style match. */
274 gtk_widget_set_name (close_button, "empathy-close-button");
276 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
278 gtk_container_add (GTK_CONTAINER (close_button), close_image);
280 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
282 g_signal_connect (close_button,
284 G_CALLBACK (chat_window_close_clicked_cb),
287 /* React to theme changes and also setup the size correctly. */
288 g_signal_connect (hbox,
290 G_CALLBACK (chat_tab_style_set_cb),
294 gtk_widget_show_all (hbox);
300 _submenu_notify_visible_changed_cb (GObject *object,
304 g_signal_handlers_disconnect_by_func (object,
305 _submenu_notify_visible_changed_cb,
307 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
311 chat_window_update (EmpathyChatWindow *window)
313 EmpathyChatWindowPriv *priv = GET_PRIV (window);
316 gboolean is_connected;
323 EmpathyContact *remote_contact;
324 gboolean avatar_in_icon;
326 GtkWidget *chat_close_button;
327 GtkWidget *menu, *submenu, *orig_submenu;
329 /* Get information */
330 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
331 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
332 first_page = (page_num == 0);
333 last_page = (page_num == (num_pages - 1));
334 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
335 name = empathy_chat_get_name (priv->current_chat);
336 n_chats = g_list_length (priv->chats);
338 DEBUG ("Update window");
341 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
342 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
343 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
344 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
345 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
346 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
348 /* Update Contact menu */
349 menu = gtk_ui_manager_get_widget (priv->ui_manager,
350 "/chats_menubar/menu_contact");
351 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
352 if (!GTK_WIDGET_VISIBLE (orig_submenu))
354 submenu = empathy_chat_get_contact_menu (priv->current_chat);
355 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
356 gtk_widget_show (menu);
358 empathy_signal_connect_weak (orig_submenu,
360 (GCallback)_submenu_notify_visible_changed_cb,
364 /* Update window title */
366 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
370 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
371 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
375 /* Update window icon */
376 if (priv->chats_new_msg) {
377 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
378 EMPATHY_IMAGE_MESSAGE);
380 empathy_conf_get_bool (empathy_conf_get (),
381 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
384 if (n_chats == 1 && avatar_in_icon) {
385 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
386 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
387 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
390 g_object_unref (icon);
393 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
397 if (num_pages == 1) {
398 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
399 chat_close_button = g_object_get_data (G_OBJECT (chat),
400 "chat-window-tab-close-button");
401 gtk_widget_hide (chat_close_button);
403 for (i=0; i<num_pages; i++) {
404 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
405 chat_close_button = g_object_get_data (G_OBJECT (chat),
406 "chat-window-tab-close-button");
407 gtk_widget_show (chat_close_button);
413 append_markup_printf (GString *string,
420 va_start (args, format);
422 tmp = g_markup_vprintf_escaped (format, args);
423 g_string_append (string, tmp);
430 chat_window_update_chat_tab (EmpathyChat *chat)
432 EmpathyChatWindow *window;
433 EmpathyChatWindowPriv *priv;
434 EmpathyContact *remote_contact;
437 EmpathyAccount *account;
438 const gchar *subject;
439 const gchar *status = NULL;
443 const gchar *icon_name;
444 GtkWidget *tab_image;
445 GtkWidget *menu_image;
447 window = chat_window_find_chat (chat);
451 priv = GET_PRIV (window);
453 /* Get information */
454 name = empathy_chat_get_name (chat);
455 account = empathy_chat_get_account (chat);
456 subject = empathy_chat_get_subject (chat);
457 remote_contact = empathy_chat_get_remote_contact (chat);
459 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
460 name, empathy_account_get_unique_name (account), subject, remote_contact);
462 /* Update tab image */
463 if (empathy_chat_get_tp_chat (chat) == NULL) {
464 /* No TpChat, we are disconnected */
467 else if (g_list_find (priv->chats_new_msg, chat)) {
468 icon_name = EMPATHY_IMAGE_MESSAGE;
470 else if (g_list_find (priv->chats_composing, chat)) {
471 icon_name = EMPATHY_IMAGE_TYPING;
473 else if (remote_contact) {
474 icon_name = empathy_icon_name_for_contact (remote_contact);
476 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
479 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
480 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
481 if (icon_name != NULL) {
482 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
483 gtk_widget_show (tab_image);
484 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
485 gtk_widget_show (menu_image);
487 gtk_widget_hide (tab_image);
488 gtk_widget_hide (menu_image);
491 /* Update tab tooltip */
492 tooltip = g_string_new (NULL);
494 if (remote_contact) {
495 id = empathy_contact_get_id (remote_contact);
496 status = empathy_contact_get_presence_message (remote_contact);
501 append_markup_printf (tooltip,
502 "<b>%s</b><small> (%s)</small>",
504 empathy_account_get_display_name (account));
506 if (!EMP_STR_EMPTY (status)) {
507 append_markup_printf (tooltip, "\n<i>%s</i>", status);
511 append_markup_printf (tooltip, "\n<b>%s</b> %s",
512 _("Topic:"), subject);
515 if (g_list_find (priv->chats_composing, chat)) {
516 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
519 markup = g_string_free (tooltip, FALSE);
520 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
521 gtk_widget_set_tooltip_markup (widget, markup);
522 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
523 gtk_widget_set_tooltip_markup (widget, markup);
526 /* Update tab and menu label */
527 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
528 gtk_label_set_text (GTK_LABEL (widget), name);
529 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
530 gtk_label_set_text (GTK_LABEL (widget), name);
532 /* Update the window if it's the current chat */
533 if (priv->current_chat == chat) {
534 chat_window_update (window);
539 chat_window_chat_notify_cb (EmpathyChat *chat)
541 EmpathyContact *old_remote_contact;
542 EmpathyContact *remote_contact = NULL;
544 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
545 remote_contact = empathy_chat_get_remote_contact (chat);
547 if (old_remote_contact != remote_contact) {
548 /* The remote-contact associated with the chat changed, we need
549 * to keep track of any change of that contact and update the
550 * window each time. */
551 if (remote_contact) {
552 g_signal_connect_swapped (remote_contact, "notify",
553 G_CALLBACK (chat_window_update_chat_tab),
556 if (old_remote_contact) {
557 g_signal_handlers_disconnect_by_func (old_remote_contact,
558 chat_window_update_chat_tab,
562 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
566 chat_window_update_chat_tab (chat);
570 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
571 EmpathySmiley *smiley,
574 EmpathyChatWindowPriv *priv = GET_PRIV (window);
576 GtkTextBuffer *buffer;
579 chat = priv->current_chat;
581 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
582 gtk_text_buffer_get_end_iter (buffer, &iter);
583 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
587 chat_window_conv_activate_cb (GtkAction *action,
588 EmpathyChatWindow *window)
590 EmpathyChatWindowPriv *priv = GET_PRIV (window);
593 EmpathyContact *remote_contact = NULL;
595 /* Favorite room menu */
596 is_room = empathy_chat_is_room (priv->current_chat);
599 EmpathyAccount *account;
600 gboolean found = FALSE;
601 EmpathyChatroom *chatroom;
603 room = empathy_chat_get_id (priv->current_chat);
604 account = empathy_chat_get_account (priv->current_chat);
605 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
607 if (chatroom != NULL)
608 found = empathy_chatroom_is_favorite (chatroom);
610 DEBUG ("This room %s favorite", found ? "is" : "is not");
611 gtk_toggle_action_set_active (
612 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
614 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
616 /* Show contacts menu */
617 g_object_get (priv->current_chat,
618 "remote-contact", &remote_contact,
619 "show-contacts", &active,
621 if (remote_contact == NULL) {
622 gtk_toggle_action_set_active (
623 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
626 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
627 (remote_contact == NULL));
628 if (remote_contact != NULL) {
629 g_object_unref (remote_contact);
634 chat_window_clear_activate_cb (GtkAction *action,
635 EmpathyChatWindow *window)
637 EmpathyChatWindowPriv *priv = GET_PRIV (window);
639 empathy_chat_clear (priv->current_chat);
643 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
644 EmpathyChatWindow *window)
646 EmpathyChatWindowPriv *priv = GET_PRIV (window);
648 EmpathyAccount *account;
650 EmpathyChatroom *chatroom;
652 active = gtk_toggle_action_get_active (toggle_action);
653 account = empathy_chat_get_account (priv->current_chat);
654 room = empathy_chat_get_id (priv->current_chat);
656 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
659 if (chatroom == NULL) {
662 name = empathy_chat_get_name (priv->current_chat);
663 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
664 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
665 g_object_unref (chatroom);
668 empathy_chatroom_set_favorite (chatroom, active);
672 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
673 EmpathyChatWindow *window)
675 EmpathyChatWindowPriv *priv = GET_PRIV (window);
678 active = gtk_toggle_action_get_active (toggle_action);
680 empathy_chat_set_show_contacts (priv->current_chat, active);
684 chat_get_window_id_for_geometry (EmpathyChat *chat)
686 const gchar *res = NULL;
687 gboolean separate_windows;
689 empathy_conf_get_bool (empathy_conf_get (),
690 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
693 if (separate_windows) {
694 res = empathy_chat_get_id (chat);
697 return res ? res : "chat-window";
701 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
703 EmpathyChatWindowPriv *priv;
706 priv = GET_PRIV (window);
708 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
709 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
711 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
714 priv->save_geometry_id = 0;
720 chat_window_configure_event_cb (GtkWidget *widget,
721 GdkEventConfigure *event,
722 EmpathyChatWindow *window)
724 EmpathyChatWindowPriv *priv;
726 priv = GET_PRIV (window);
728 if (priv->save_geometry_id != 0) {
729 g_source_remove (priv->save_geometry_id);
732 priv->save_geometry_id =
733 g_timeout_add_seconds (1,
734 (GSourceFunc) chat_window_save_geometry_timeout_cb,
741 chat_window_close_activate_cb (GtkAction *action,
742 EmpathyChatWindow *window)
744 EmpathyChatWindowPriv *priv;
746 priv = GET_PRIV (window);
748 g_return_if_fail (priv->current_chat != NULL);
750 empathy_chat_window_remove_chat (window, priv->current_chat);
754 chat_window_edit_activate_cb (GtkAction *action,
755 EmpathyChatWindow *window)
757 EmpathyChatWindowPriv *priv;
758 GtkClipboard *clipboard;
759 GtkTextBuffer *buffer;
760 gboolean text_available;
762 priv = GET_PRIV (window);
764 g_return_if_fail (priv->current_chat != NULL);
766 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
767 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
768 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
769 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
773 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
774 if (gtk_text_buffer_get_has_selection (buffer)) {
775 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
776 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
780 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
782 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
783 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
786 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
787 text_available = gtk_clipboard_wait_is_text_available (clipboard);
788 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
792 chat_window_cut_activate_cb (GtkAction *action,
793 EmpathyChatWindow *window)
795 EmpathyChatWindowPriv *priv;
797 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
799 priv = GET_PRIV (window);
801 empathy_chat_cut (priv->current_chat);
805 chat_window_copy_activate_cb (GtkAction *action,
806 EmpathyChatWindow *window)
808 EmpathyChatWindowPriv *priv;
810 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
812 priv = GET_PRIV (window);
814 empathy_chat_copy (priv->current_chat);
818 chat_window_paste_activate_cb (GtkAction *action,
819 EmpathyChatWindow *window)
821 EmpathyChatWindowPriv *priv;
823 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
825 priv = GET_PRIV (window);
827 empathy_chat_paste (priv->current_chat);
831 chat_window_tabs_left_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));
846 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
852 chat_window_tabs_right_activate_cb (GtkAction *action,
853 EmpathyChatWindow *window)
855 EmpathyChatWindowPriv *priv;
859 priv = GET_PRIV (window);
861 chat = priv->current_chat;
862 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
864 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
870 chat_window_detach_activate_cb (GtkAction *action,
871 EmpathyChatWindow *window)
873 EmpathyChatWindowPriv *priv;
874 EmpathyChatWindow *new_window;
877 priv = GET_PRIV (window);
879 chat = priv->current_chat;
880 new_window = empathy_chat_window_new ();
882 empathy_chat_window_move_chat (window, new_window, chat);
884 priv = GET_PRIV (new_window);
885 gtk_widget_show (priv->dialog);
889 chat_window_help_contents_activate_cb (GtkAction *action,
890 EmpathyChatWindow *window)
892 EmpathyChatWindowPriv *priv = GET_PRIV (window);
894 empathy_url_show (priv->dialog, "ghelp:empathy");
898 chat_window_help_about_activate_cb (GtkAction *action,
899 EmpathyChatWindow *window)
901 EmpathyChatWindowPriv *priv = GET_PRIV (window);
903 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
907 chat_window_delete_event_cb (GtkWidget *dialog,
909 EmpathyChatWindow *window)
911 EmpathyChatWindowPriv *priv = GET_PRIV (window);
913 DEBUG ("Delete event received");
915 g_object_ref (window);
916 while (priv->chats) {
917 empathy_chat_window_remove_chat (window, priv->chats->data);
919 g_object_unref (window);
925 chat_window_composing_cb (EmpathyChat *chat,
926 gboolean is_composing,
927 EmpathyChatWindow *window)
929 EmpathyChatWindowPriv *priv;
931 priv = GET_PRIV (window);
933 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
934 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
936 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
939 chat_window_update_chat_tab (chat);
943 chat_window_set_urgency_hint (EmpathyChatWindow *window,
946 EmpathyChatWindowPriv *priv;
948 priv = GET_PRIV (window);
950 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
951 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
955 free_notification_data (NotificationData *data)
957 g_object_unref (data->chat);
958 g_slice_free (NotificationData, data);
962 chat_window_notification_closed_cb (NotifyNotification *notify,
963 NotificationData *cb_data)
965 EmpathyNotificationClosedReason reason = 0;
966 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
968 #ifdef notify_notification_get_closed_reason
969 reason = notify_notification_get_closed_reason (notify);
971 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
972 empathy_chat_window_present_chat (cb_data->chat);
975 g_object_unref (notify);
976 priv->notification = NULL;
977 free_notification_data (cb_data);
978 priv->notification_data = NULL;
982 chat_window_show_or_update_notification (EmpathyChatWindow *window,
983 EmpathyMessage *message,
986 EmpathyContact *sender;
991 EmpathyChatWindowPriv *priv = GET_PRIV (window);
994 if (!empathy_notification_is_enabled ()) {
997 empathy_conf_get_bool (empathy_conf_get (),
998 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1004 sender = empathy_message_get_sender (message);
1005 header = empathy_contact_get_name (sender);
1006 body = empathy_message_get_body (message);
1007 escaped = g_markup_escape_text (body, -1);
1009 if (priv->notification != NULL) {
1010 notify_notification_update (priv->notification,
1011 header, escaped, NULL);
1013 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1015 cb_data->chat = g_object_ref (chat);
1016 cb_data->window = window;
1018 priv->notification_data = cb_data;
1019 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1020 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1022 g_signal_connect (priv->notification, "closed",
1023 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1026 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
1028 if (pixbuf != NULL) {
1029 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1030 g_object_unref (pixbuf);
1033 notify_notification_show (priv->notification, NULL);
1039 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1044 if (!empathy_chat_is_room (chat))
1047 markup = g_markup_printf_escaped (
1048 "<span color=\"red\" weight=\"bold\">%s</span>",
1049 empathy_chat_get_name (chat));
1051 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1052 gtk_label_set_markup (GTK_LABEL (widget), markup);
1057 chat_window_new_message_cb (EmpathyChat *chat,
1058 EmpathyMessage *message,
1059 EmpathyChatWindow *window)
1061 EmpathyChatWindowPriv *priv;
1063 gboolean needs_urgency;
1064 EmpathyContact *sender;
1066 priv = GET_PRIV (window);
1068 has_focus = empathy_chat_window_has_focus (window);
1070 /* - if we're the sender, we play the sound if it's specified in the
1071 * preferences and we're not away.
1072 * - if we receive a message, we play the sound if it's specified in the
1073 * preferences and the window does not have focus on the chat receiving
1077 sender = empathy_message_get_sender (message);
1079 if (empathy_contact_is_user (sender)) {
1080 empathy_sound_play (GTK_WIDGET (priv->dialog),
1081 EMPATHY_SOUND_MESSAGE_OUTGOING);
1084 if (has_focus && priv->current_chat == chat) {
1088 if (!g_list_find (priv->chats_new_msg, chat)) {
1089 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1090 chat_window_update_chat_tab (chat);
1093 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1094 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1095 * an unamed MUC (msn-like).
1096 * In case of a MUC, we set urgency only if the message contains our
1098 if (empathy_chat_is_room (chat) ||
1099 empathy_chat_get_remote_contact (chat) == NULL) {
1100 needs_urgency = empathy_message_should_highlight (message);
1102 needs_urgency = TRUE;
1105 if (needs_urgency) {
1107 chat_window_set_urgency_hint (window, TRUE);
1108 chat_window_set_highlight_room_tab_label (chat);
1111 empathy_sound_play (GTK_WIDGET (priv->dialog),
1112 EMPATHY_SOUND_MESSAGE_INCOMING);
1113 chat_window_show_or_update_notification (window, message, chat);
1117 static GtkNotebook *
1118 chat_window_detach_hook (GtkNotebook *source,
1124 EmpathyChatWindowPriv *priv;
1125 EmpathyChatWindow *window, *new_window;
1128 chat = EMPATHY_CHAT (page);
1129 window = chat_window_find_chat (chat);
1131 new_window = empathy_chat_window_new ();
1132 priv = GET_PRIV (new_window);
1134 DEBUG ("Detach hook called");
1136 empathy_chat_window_move_chat (window, new_window, chat);
1138 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1139 gtk_widget_show (priv->dialog);
1145 chat_window_page_switched_cb (GtkNotebook *notebook,
1146 GtkNotebookPage *page,
1148 EmpathyChatWindow *window)
1150 EmpathyChatWindowPriv *priv;
1154 DEBUG ("Page switched");
1156 priv = GET_PRIV (window);
1158 child = gtk_notebook_get_nth_page (notebook, page_num);
1159 chat = EMPATHY_CHAT (child);
1161 if (priv->page_added) {
1162 priv->page_added = FALSE;
1163 empathy_chat_scroll_down (chat);
1165 else if (priv->current_chat == chat) {
1169 priv->current_chat = chat;
1170 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1172 chat_window_update_chat_tab (chat);
1176 chat_window_page_added_cb (GtkNotebook *notebook,
1179 EmpathyChatWindow *window)
1181 EmpathyChatWindowPriv *priv;
1184 priv = GET_PRIV (window);
1186 /* If we just received DND to the same window, we don't want
1187 * to do anything here like removing the tab and then readding
1188 * it, so we return here and in "page-added".
1190 if (priv->dnd_same_window) {
1191 DEBUG ("Page added (back to the same window)");
1192 priv->dnd_same_window = FALSE;
1196 DEBUG ("Page added");
1198 /* Get chat object */
1199 chat = EMPATHY_CHAT (child);
1201 /* Connect chat signals for this window */
1202 g_signal_connect (chat, "composing",
1203 G_CALLBACK (chat_window_composing_cb),
1205 g_signal_connect (chat, "new-message",
1206 G_CALLBACK (chat_window_new_message_cb),
1208 g_signal_connect (chat, "notify::tp-chat",
1209 G_CALLBACK (chat_window_update_chat_tab),
1212 /* Set flag so we know to perform some special operations on
1213 * switch page due to the new page being added.
1215 priv->page_added = TRUE;
1217 /* Get list of chats up to date */
1218 priv->chats = g_list_append (priv->chats, chat);
1220 chat_window_update_chat_tab (chat);
1224 chat_window_page_removed_cb (GtkNotebook *notebook,
1227 EmpathyChatWindow *window)
1229 EmpathyChatWindowPriv *priv;
1232 priv = GET_PRIV (window);
1234 /* If we just received DND to the same window, we don't want
1235 * to do anything here like removing the tab and then readding
1236 * it, so we return here and in "page-added".
1238 if (priv->dnd_same_window) {
1239 DEBUG ("Page removed (and will be readded to same window)");
1243 DEBUG ("Page removed");
1245 /* Get chat object */
1246 chat = EMPATHY_CHAT (child);
1248 /* Disconnect all signal handlers for this chat and this window */
1249 g_signal_handlers_disconnect_by_func (chat,
1250 G_CALLBACK (chat_window_composing_cb),
1252 g_signal_handlers_disconnect_by_func (chat,
1253 G_CALLBACK (chat_window_new_message_cb),
1255 g_signal_handlers_disconnect_by_func (chat,
1256 G_CALLBACK (chat_window_update_chat_tab),
1259 /* Keep list of chats up to date */
1260 priv->chats = g_list_remove (priv->chats, chat);
1261 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1262 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1264 if (priv->chats == NULL) {
1265 g_object_unref (window);
1267 chat_window_update (window);
1272 chat_window_focus_in_event_cb (GtkWidget *widget,
1274 EmpathyChatWindow *window)
1276 EmpathyChatWindowPriv *priv;
1278 DEBUG ("Focus in event, updating title");
1280 priv = GET_PRIV (window);
1282 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1284 chat_window_set_urgency_hint (window, FALSE);
1286 /* Update the title, since we now mark all unread messages as read. */
1287 chat_window_update_chat_tab (priv->current_chat);
1293 chat_window_drag_data_received (GtkWidget *widget,
1294 GdkDragContext *context,
1297 GtkSelectionData *selection,
1300 EmpathyChatWindow *window)
1302 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1303 EmpathyChat *chat = NULL;
1304 EmpathyChatWindow *old_window;
1305 EmpathyAccount *account = NULL;
1306 EmpathyAccountManager *account_manager;
1309 const gchar *account_id;
1310 const gchar *contact_id;
1312 id = (const gchar*) gtk_selection_data_get_data (selection);
1313 account_manager = empathy_account_manager_dup_singleton ();
1315 DEBUG ("DND contact from roster with id:'%s'", id);
1317 strv = g_strsplit (id, ":", 2);
1318 if (g_strv_length (strv) == 2) {
1319 account_id = strv[0];
1320 contact_id = strv[1];
1322 empathy_account_manager_get_account (account_manager, account_id);
1323 if (account != NULL)
1324 chat = empathy_chat_window_find_chat (account, contact_id);
1327 if (account == NULL) {
1329 gtk_drag_finish (context, FALSE, FALSE, time_);
1334 TpConnection *connection;
1336 connection = empathy_account_get_connection (account);
1339 empathy_dispatcher_chat_with_contact_id (
1340 connection, contact_id, NULL, NULL);
1346 g_object_unref (account_manager);
1349 old_window = chat_window_find_chat (chat);
1351 if (old_window == window) {
1352 gtk_drag_finish (context, TRUE, FALSE, time_);
1356 empathy_chat_window_move_chat (old_window, window, chat);
1358 empathy_chat_window_add_chat (window, chat);
1361 /* Added to take care of any outstanding chat events */
1362 empathy_chat_window_present_chat (chat);
1364 /* We should return TRUE to remove the data when doing
1365 * GDK_ACTION_MOVE, but we don't here otherwise it has
1366 * weird consequences, and we handle that internally
1367 * anyway with add_chat () and remove_chat ().
1369 gtk_drag_finish (context, TRUE, FALSE, time_);
1371 else if (info == DND_DRAG_TYPE_TAB) {
1373 EmpathyChatWindow *old_window = NULL;
1377 chat = (void *) gtk_selection_data_get_data (selection);
1378 old_window = chat_window_find_chat (*chat);
1381 EmpathyChatWindowPriv *priv;
1383 priv = GET_PRIV (window);
1385 if (old_window == window) {
1386 DEBUG ("DND tab (within same window)");
1387 priv->dnd_same_window = TRUE;
1388 gtk_drag_finish (context, TRUE, FALSE, time_);
1392 priv->dnd_same_window = FALSE;
1395 /* We should return TRUE to remove the data when doing
1396 * GDK_ACTION_MOVE, but we don't here otherwise it has
1397 * weird consequences, and we handle that internally
1398 * anyway with add_chat () and remove_chat ().
1400 gtk_drag_finish (context, TRUE, FALSE, time_);
1402 DEBUG ("DND from unknown source");
1403 gtk_drag_finish (context, FALSE, FALSE, time_);
1408 chat_window_finalize (GObject *object)
1410 EmpathyChatWindow *window;
1411 EmpathyChatWindowPriv *priv;
1413 window = EMPATHY_CHAT_WINDOW (object);
1414 priv = GET_PRIV (window);
1416 DEBUG ("Finalized: %p", object);
1418 g_object_unref (priv->ui_manager);
1419 g_object_unref (priv->chatroom_manager);
1420 if (priv->save_geometry_id != 0) {
1421 g_source_remove (priv->save_geometry_id);
1424 if (priv->notification != NULL) {
1425 notify_notification_close (priv->notification, NULL);
1426 g_object_unref (priv->notification);
1427 priv->notification = NULL;
1428 if (priv->notification_data != NULL)
1430 free_notification_data (priv->notification_data);
1431 priv->notification_data = NULL;
1435 chat_windows = g_list_remove (chat_windows, window);
1436 gtk_widget_destroy (priv->dialog);
1438 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1442 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1444 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1446 object_class->finalize = chat_window_finalize;
1448 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1450 /* Set up a style for the close button with no focus padding. */
1451 gtk_rc_parse_string (
1452 "style \"empathy-close-button-style\"\n"
1454 " GtkWidget::focus-padding = 0\n"
1458 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1460 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1464 empathy_chat_window_init (EmpathyChatWindow *window)
1467 GtkAccelGroup *accel_group;
1472 GtkWidget *chat_vbox;
1474 EmpathySmileyManager *smiley_manager;
1475 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1476 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1478 window->priv = priv;
1479 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1480 gui = empathy_builder_get_file (filename,
1481 "chat_window", &priv->dialog,
1482 "chat_vbox", &chat_vbox,
1483 "ui_manager", &priv->ui_manager,
1484 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1485 "menu_conv_favorite", &priv->menu_conv_favorite,
1486 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1487 "menu_edit_cut", &priv->menu_edit_cut,
1488 "menu_edit_copy", &priv->menu_edit_copy,
1489 "menu_edit_paste", &priv->menu_edit_paste,
1490 "menu_tabs_next", &priv->menu_tabs_next,
1491 "menu_tabs_prev", &priv->menu_tabs_prev,
1492 "menu_tabs_left", &priv->menu_tabs_left,
1493 "menu_tabs_right", &priv->menu_tabs_right,
1494 "menu_tabs_detach", &priv->menu_tabs_detach,
1498 empathy_builder_connect (gui, window,
1499 "chat_window", "configure-event", chat_window_configure_event_cb,
1500 "menu_conv", "activate", chat_window_conv_activate_cb,
1501 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1502 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1503 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1504 "menu_conv_close", "activate", chat_window_close_activate_cb,
1505 "menu_edit", "activate", chat_window_edit_activate_cb,
1506 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1507 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1508 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1509 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1510 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1511 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1512 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1513 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1516 g_object_ref (priv->ui_manager);
1517 g_object_unref (gui);
1519 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1521 priv->notebook = gtk_notebook_new ();
1522 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1523 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1524 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1525 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1526 gtk_widget_show (priv->notebook);
1529 accel_group = gtk_accel_group_new ();
1530 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1532 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1533 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1536 gtk_accel_group_connect (accel_group,
1543 g_object_unref (accel_group);
1545 /* Set up smiley menu */
1546 smiley_manager = empathy_smiley_manager_dup_singleton ();
1547 submenu = empathy_smiley_menu_new (smiley_manager,
1548 chat_window_insert_smiley_activate_cb,
1550 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1551 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1552 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1553 g_object_unref (smiley_manager);
1555 /* Set up signals we can't do with ui file since we may need to
1556 * block/unblock them at some later stage.
1559 g_signal_connect (priv->dialog,
1561 G_CALLBACK (chat_window_delete_event_cb),
1564 g_signal_connect_swapped (priv->menu_tabs_prev,
1566 G_CALLBACK (gtk_notebook_prev_page),
1568 g_signal_connect_swapped (priv->menu_tabs_next,
1570 G_CALLBACK (gtk_notebook_next_page),
1573 g_signal_connect (priv->dialog,
1575 G_CALLBACK (chat_window_focus_in_event_cb),
1577 g_signal_connect_after (priv->notebook,
1579 G_CALLBACK (chat_window_page_switched_cb),
1581 g_signal_connect (priv->notebook,
1583 G_CALLBACK (chat_window_page_added_cb),
1585 g_signal_connect (priv->notebook,
1587 G_CALLBACK (chat_window_page_removed_cb),
1590 /* Set up drag and drop */
1591 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1592 GTK_DEST_DEFAULT_ALL,
1594 G_N_ELEMENTS (drag_types_dest),
1597 g_signal_connect (priv->notebook,
1598 "drag-data-received",
1599 G_CALLBACK (chat_window_drag_data_received),
1602 chat_windows = g_list_prepend (chat_windows, window);
1604 /* Set up private details */
1606 priv->chats_new_msg = NULL;
1607 priv->chats_composing = NULL;
1608 priv->current_chat = NULL;
1612 empathy_chat_window_new (void)
1614 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1617 /* Returns the window to open a new tab in if there is only one window
1618 * visble, otherwise, returns NULL indicating that a new window should
1622 empathy_chat_window_get_default (void)
1625 gboolean separate_windows = TRUE;
1627 empathy_conf_get_bool (empathy_conf_get (),
1628 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1631 if (separate_windows) {
1632 /* Always create a new window */
1636 for (l = chat_windows; l; l = l->next) {
1637 EmpathyChatWindow *chat_window;
1640 chat_window = l->data;
1642 dialog = empathy_chat_window_get_dialog (chat_window);
1643 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1644 /* Found a visible window on this desktop */
1653 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1655 EmpathyChatWindowPriv *priv;
1657 g_return_val_if_fail (window != NULL, NULL);
1659 priv = GET_PRIV (window);
1661 return priv->dialog;
1665 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1668 EmpathyChatWindowPriv *priv;
1670 GtkWidget *popup_label;
1674 g_return_if_fail (window != NULL);
1675 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1677 priv = GET_PRIV (window);
1679 /* Reference the chat object */
1680 g_object_ref (chat);
1682 /* If this window has just been created, position it */
1683 if (priv->chats == NULL) {
1684 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1686 if (x >= 0 && y >= 0) {
1687 /* Let the window manager position it if we don't have
1688 * good x, y coordinates.
1690 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1693 if (w > 0 && h > 0) {
1694 /* Use the defaults from the ui file if we don't have
1695 * good w, h geometry.
1697 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1701 child = GTK_WIDGET (chat);
1702 label = chat_window_create_label (window, chat, TRUE);
1703 popup_label = chat_window_create_label (window, chat, FALSE);
1704 gtk_widget_show (child);
1706 g_signal_connect (chat, "notify::name",
1707 G_CALLBACK (chat_window_chat_notify_cb),
1709 g_signal_connect (chat, "notify::subject",
1710 G_CALLBACK (chat_window_chat_notify_cb),
1712 g_signal_connect (chat, "notify::remote-contact",
1713 G_CALLBACK (chat_window_chat_notify_cb),
1715 chat_window_chat_notify_cb (chat);
1717 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1718 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1719 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1720 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1721 TRUE, TRUE, GTK_PACK_START);
1723 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1727 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1730 EmpathyChatWindowPriv *priv;
1732 EmpathyContact *remote_contact;
1734 g_return_if_fail (window != NULL);
1735 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1737 priv = GET_PRIV (window);
1739 g_signal_handlers_disconnect_by_func (chat,
1740 chat_window_chat_notify_cb,
1742 remote_contact = g_object_get_data (G_OBJECT (chat),
1743 "chat-window-remote-contact");
1744 if (remote_contact) {
1745 g_signal_handlers_disconnect_by_func (remote_contact,
1746 chat_window_update_chat_tab,
1750 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1752 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1754 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1756 g_object_unref (chat);
1760 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1761 EmpathyChatWindow *new_window,
1766 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1767 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1768 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1770 widget = GTK_WIDGET (chat);
1772 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1773 G_OBJECT (widget)->ref_count);
1775 /* We reference here to make sure we don't loose the widget
1776 * and the EmpathyChat object during the move.
1778 g_object_ref (chat);
1779 g_object_ref (widget);
1781 empathy_chat_window_remove_chat (old_window, chat);
1782 empathy_chat_window_add_chat (new_window, chat);
1784 g_object_unref (widget);
1785 g_object_unref (chat);
1789 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1792 EmpathyChatWindowPriv *priv;
1795 g_return_if_fail (window != NULL);
1796 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1798 priv = GET_PRIV (window);
1800 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1802 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1807 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1809 EmpathyChatWindowPriv *priv;
1812 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1814 priv = GET_PRIV (window);
1816 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1822 empathy_chat_window_find_chat (EmpathyAccount *account,
1827 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1829 for (l = chat_windows; l; l = l->next) {
1830 EmpathyChatWindowPriv *priv;
1831 EmpathyChatWindow *window;
1835 priv = GET_PRIV (window);
1837 for (ll = priv->chats; ll; ll = ll->next) {
1842 if (account == empathy_chat_get_account (chat) &&
1843 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1853 empathy_chat_window_present_chat (EmpathyChat *chat)
1855 EmpathyChatWindow *window;
1856 EmpathyChatWindowPriv *priv;
1858 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1860 window = chat_window_find_chat (chat);
1862 /* If the chat has no window, create one */
1863 if (window == NULL) {
1864 window = empathy_chat_window_get_default ();
1866 window = empathy_chat_window_new ();
1869 empathy_chat_window_add_chat (window, chat);
1872 priv = GET_PRIV (window);
1873 empathy_chat_window_switch_to_chat (window, chat);
1874 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1876 gtk_widget_grab_focus (chat->input_text_view);