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>
26 * RĂ´mulo Fernandes Machado <romulo@castorgroup.net>
34 #include <gdk/gdkkeysyms.h>
35 #include <glib/gi18n.h>
36 #include <libnotify/notification.h>
38 #include <telepathy-glib/account-manager.h>
39 #include <telepathy-glib/util.h>
41 #include <libempathy/empathy-contact.h>
42 #include <libempathy/empathy-message.h>
43 #include <libempathy/empathy-dispatcher.h>
44 #include <libempathy/empathy-chatroom-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-sound.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
55 #include <libempathy-gtk/empathy-notify-manager.h>
57 #include "empathy-chat-window.h"
58 #include "empathy-about-dialog.h"
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
64 EmpathyChatWindow *window;
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
70 EmpathyChat *current_chat;
73 GList *chats_composing;
75 gboolean dnd_same_window;
76 EmpathyChatroomManager *chatroom_manager;
77 EmpathyNotifyManager *notify_mgr;
80 NotifyNotification *notification;
81 NotificationData *notification_data;
84 GtkUIManager *ui_manager;
85 GtkAction *menu_conv_insert_smiley;
86 GtkAction *menu_conv_favorite;
87 GtkAction *menu_conv_toggle_contacts;
89 GtkAction *menu_edit_cut;
90 GtkAction *menu_edit_copy;
91 GtkAction *menu_edit_paste;
93 GtkAction *menu_tabs_next;
94 GtkAction *menu_tabs_prev;
95 GtkAction *menu_tabs_left;
96 GtkAction *menu_tabs_right;
97 GtkAction *menu_tabs_detach;
98 } EmpathyChatWindowPriv;
100 static GList *chat_windows = NULL;
102 static const guint tab_accel_keys[] = {
103 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
104 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
108 DND_DRAG_TYPE_CONTACT_ID,
109 DND_DRAG_TYPE_URI_LIST,
113 static const GtkTargetEntry drag_types_dest[] = {
114 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
115 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
116 { "text/uri-list", 0, DND_DRAG_TYPE_URI_LIST },
119 static const GtkTargetEntry drag_types_dest_move[] = {
120 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
123 static void chat_window_update (EmpathyChatWindow *window);
125 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
128 chat_window_accel_cb (GtkAccelGroup *accelgroup,
132 EmpathyChatWindow *window)
134 EmpathyChatWindowPriv *priv;
138 priv = GET_PRIV (window);
140 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
141 if (tab_accel_keys[i] == key) {
148 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
152 static EmpathyChatWindow *
153 chat_window_find_chat (EmpathyChat *chat)
155 EmpathyChatWindowPriv *priv;
158 for (l = chat_windows; l; l = l->next) {
159 priv = GET_PRIV (l->data);
160 ll = g_list_find (priv->chats, chat);
170 chat_window_close_clicked_cb (GtkAction *action,
173 EmpathyChatWindow *window;
175 window = chat_window_find_chat (chat);
176 empathy_chat_window_remove_chat (window, chat);
180 chat_tab_style_set_cb (GtkWidget *hbox,
181 GtkStyle *previous_style,
185 int char_width, h, w;
186 PangoContext *context;
187 PangoFontMetrics *metrics;
189 button = g_object_get_data (G_OBJECT (user_data),
190 "chat-window-tab-close-button");
191 context = gtk_widget_get_pango_context (hbox);
193 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
194 pango_context_get_language (context));
195 char_width = pango_font_metrics_get_approximate_char_width (metrics);
196 pango_font_metrics_unref (metrics);
198 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
199 GTK_ICON_SIZE_MENU, &w, &h);
201 /* Request at least about 12 chars width plus at least space for the status
202 * image and the close button */
203 gtk_widget_set_size_request (hbox,
204 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
206 gtk_widget_set_size_request (button, w, h);
210 chat_window_create_label (EmpathyChatWindow *window,
212 gboolean is_tab_label)
214 EmpathyChatWindowPriv *priv;
216 GtkWidget *name_label;
217 GtkWidget *status_image;
218 GtkWidget *close_button;
219 GtkWidget *close_image;
220 GtkWidget *event_box;
221 GtkWidget *event_box_hbox;
222 PangoAttrList *attr_list;
223 PangoAttribute *attr;
225 priv = GET_PRIV (window);
227 /* The spacing between the button and the label. */
228 hbox = gtk_hbox_new (FALSE, 0);
230 event_box = gtk_event_box_new ();
231 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
233 name_label = gtk_label_new (NULL);
235 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
237 attr_list = pango_attr_list_new ();
238 attr = pango_attr_scale_new (1/1.2);
239 attr->start_index = 0;
240 attr->end_index = -1;
241 pango_attr_list_insert (attr_list, attr);
242 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
243 pango_attr_list_unref (attr_list);
245 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
246 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
247 g_object_set_data (G_OBJECT (chat),
248 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
251 status_image = gtk_image_new ();
253 /* Spacing between the icon and label. */
254 event_box_hbox = gtk_hbox_new (FALSE, 0);
256 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
257 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
259 g_object_set_data (G_OBJECT (chat),
260 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
262 g_object_set_data (G_OBJECT (chat),
263 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
266 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
267 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
270 close_button = gtk_button_new ();
271 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
272 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
274 /* We don't want focus/keynav for the button to avoid clutter, and
275 * Ctrl-W works anyway.
277 gtk_widget_set_can_focus (close_button, FALSE);
278 gtk_widget_set_can_default (close_button, FALSE);
280 /* Set the name to make the special rc style match. */
281 gtk_widget_set_name (close_button, "empathy-close-button");
283 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
285 gtk_container_add (GTK_CONTAINER (close_button), close_image);
287 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
289 g_signal_connect (close_button,
291 G_CALLBACK (chat_window_close_clicked_cb),
294 /* React to theme changes and also setup the size correctly. */
295 g_signal_connect (hbox,
297 G_CALLBACK (chat_tab_style_set_cb),
301 gtk_widget_show_all (hbox);
307 _submenu_notify_visible_changed_cb (GObject *object,
311 g_signal_handlers_disconnect_by_func (object,
312 _submenu_notify_visible_changed_cb,
314 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
318 chat_window_menu_context_update (EmpathyChatWindowPriv *priv,
323 gboolean is_connected;
326 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
327 first_page = (page_num == 0);
328 last_page = (page_num == (num_pages - 1));
329 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
331 DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
333 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
334 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
335 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
336 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
337 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
338 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
342 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
343 EmpathyChatWindow *window)
345 GtkWidget *menu, *submenu, *orig_submenu;
347 menu = gtk_ui_manager_get_widget (priv->ui_manager,
348 "/chats_menubar/menu_contact");
349 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
351 DEBUG ("Update window : Contact Menu");
353 if (orig_submenu == NULL || !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,
366 chat_window_title_update (EmpathyChatWindowPriv *priv)
370 name = empathy_chat_get_name (priv->current_chat);
372 DEBUG ("Update window : Title");
374 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
378 chat_window_icon_update (EmpathyChatWindowPriv *priv)
381 EmpathyContact *remote_contact;
382 gboolean avatar_in_icon;
385 n_chats = g_list_length (priv->chats);
387 DEBUG ("Update window : Icon");
389 /* Update window icon */
390 if (priv->chats_new_msg) {
391 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
392 EMPATHY_IMAGE_MESSAGE);
394 empathy_conf_get_bool (empathy_conf_get (),
395 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
398 if (n_chats == 1 && avatar_in_icon) {
399 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
400 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
401 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
404 g_object_unref (icon);
407 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
413 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
417 GtkWidget *chat_close_button;
420 DEBUG ("Update window : Close Button");
422 if (num_pages == 1) {
423 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
424 chat_close_button = g_object_get_data (G_OBJECT (chat),
425 "chat-window-tab-close-button");
426 gtk_widget_hide (chat_close_button);
428 for (i=0; i<num_pages; i++) {
429 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
430 chat_close_button = g_object_get_data (G_OBJECT (chat),
431 "chat-window-tab-close-button");
432 gtk_widget_show (chat_close_button);
438 chat_window_update (EmpathyChatWindow *window)
440 EmpathyChatWindowPriv *priv = GET_PRIV (window);
443 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
445 DEBUG ("Update window");
447 /* Update Tab menu */
448 chat_window_menu_context_update (priv,
451 chat_window_contact_menu_update (priv,
454 chat_window_title_update (priv);
456 chat_window_icon_update (priv);
458 chat_window_close_button_update (priv,
463 append_markup_printf (GString *string,
470 va_start (args, format);
472 tmp = g_markup_vprintf_escaped (format, args);
473 g_string_append (string, tmp);
480 chat_window_update_chat_tab (EmpathyChat *chat)
482 EmpathyChatWindow *window;
483 EmpathyChatWindowPriv *priv;
484 EmpathyContact *remote_contact;
488 const gchar *subject;
489 const gchar *status = NULL;
493 const gchar *icon_name;
494 GtkWidget *tab_image;
495 GtkWidget *menu_image;
497 window = chat_window_find_chat (chat);
501 priv = GET_PRIV (window);
503 /* Get information */
504 name = empathy_chat_get_name (chat);
505 account = empathy_chat_get_account (chat);
506 subject = empathy_chat_get_subject (chat);
507 remote_contact = empathy_chat_get_remote_contact (chat);
509 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
510 name, tp_proxy_get_object_path (account), subject, remote_contact);
512 /* Update tab image */
513 if (empathy_chat_get_tp_chat (chat) == NULL) {
514 /* No TpChat, we are disconnected */
517 else if (g_list_find (priv->chats_new_msg, chat)) {
518 icon_name = EMPATHY_IMAGE_MESSAGE;
520 else if (g_list_find (priv->chats_composing, chat)) {
521 icon_name = EMPATHY_IMAGE_TYPING;
523 else if (remote_contact) {
524 icon_name = empathy_icon_name_for_contact (remote_contact);
526 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
529 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
530 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
531 if (icon_name != NULL) {
532 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
533 gtk_widget_show (tab_image);
534 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
535 gtk_widget_show (menu_image);
537 gtk_widget_hide (tab_image);
538 gtk_widget_hide (menu_image);
541 /* Update tab tooltip */
542 tooltip = g_string_new (NULL);
544 if (remote_contact) {
545 id = empathy_contact_get_id (remote_contact);
546 status = empathy_contact_get_presence_message (remote_contact);
551 append_markup_printf (tooltip,
552 "<b>%s</b><small> (%s)</small>",
554 tp_account_get_display_name (account));
556 if (!EMP_STR_EMPTY (status)) {
557 append_markup_printf (tooltip, "\n<i>%s</i>", status);
561 append_markup_printf (tooltip, "\n<b>%s</b> %s",
562 _("Topic:"), subject);
565 if (g_list_find (priv->chats_composing, chat)) {
566 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
569 markup = g_string_free (tooltip, FALSE);
570 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
571 gtk_widget_set_tooltip_markup (widget, markup);
572 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
573 gtk_widget_set_tooltip_markup (widget, markup);
576 /* Update tab and menu label */
577 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
578 gtk_label_set_text (GTK_LABEL (widget), name);
579 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
580 gtk_label_set_text (GTK_LABEL (widget), name);
582 /* Update the window if it's the current chat */
583 if (priv->current_chat == chat) {
584 chat_window_update (window);
589 chat_window_chat_notify_cb (EmpathyChat *chat)
591 EmpathyContact *old_remote_contact;
592 EmpathyContact *remote_contact = NULL;
594 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
595 remote_contact = empathy_chat_get_remote_contact (chat);
597 if (old_remote_contact != remote_contact) {
598 /* The remote-contact associated with the chat changed, we need
599 * to keep track of any change of that contact and update the
600 * window each time. */
601 if (remote_contact) {
602 g_signal_connect_swapped (remote_contact, "notify",
603 G_CALLBACK (chat_window_update_chat_tab),
606 if (old_remote_contact) {
607 g_signal_handlers_disconnect_by_func (old_remote_contact,
608 chat_window_update_chat_tab,
612 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
616 chat_window_update_chat_tab (chat);
620 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
621 EmpathySmiley *smiley,
624 EmpathyChatWindowPriv *priv = GET_PRIV (window);
626 GtkTextBuffer *buffer;
629 chat = priv->current_chat;
631 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
632 gtk_text_buffer_get_end_iter (buffer, &iter);
633 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
637 chat_window_conv_activate_cb (GtkAction *action,
638 EmpathyChatWindow *window)
640 EmpathyChatWindowPriv *priv = GET_PRIV (window);
643 EmpathyContact *remote_contact = NULL;
645 /* Favorite room menu */
646 is_room = empathy_chat_is_room (priv->current_chat);
650 gboolean found = FALSE;
651 EmpathyChatroom *chatroom;
653 room = empathy_chat_get_id (priv->current_chat);
654 account = empathy_chat_get_account (priv->current_chat);
655 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
657 if (chatroom != NULL)
658 found = empathy_chatroom_is_favorite (chatroom);
660 DEBUG ("This room %s favorite", found ? "is" : "is not");
661 gtk_toggle_action_set_active (
662 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
664 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
666 /* Show contacts menu */
667 g_object_get (priv->current_chat,
668 "remote-contact", &remote_contact,
669 "show-contacts", &active,
671 if (remote_contact == NULL) {
672 gtk_toggle_action_set_active (
673 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
676 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
677 (remote_contact == NULL));
678 if (remote_contact != NULL) {
679 g_object_unref (remote_contact);
684 chat_window_clear_activate_cb (GtkAction *action,
685 EmpathyChatWindow *window)
687 EmpathyChatWindowPriv *priv = GET_PRIV (window);
689 empathy_chat_clear (priv->current_chat);
693 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
694 EmpathyChatWindow *window)
696 EmpathyChatWindowPriv *priv = GET_PRIV (window);
700 EmpathyChatroom *chatroom;
702 active = gtk_toggle_action_get_active (toggle_action);
703 account = empathy_chat_get_account (priv->current_chat);
704 room = empathy_chat_get_id (priv->current_chat);
706 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
709 if (chatroom == NULL) {
712 name = empathy_chat_get_name (priv->current_chat);
713 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
714 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
715 g_object_unref (chatroom);
718 empathy_chatroom_set_favorite (chatroom, active);
722 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
723 EmpathyChatWindow *window)
725 EmpathyChatWindowPriv *priv = GET_PRIV (window);
728 active = gtk_toggle_action_get_active (toggle_action);
730 empathy_chat_set_show_contacts (priv->current_chat, active);
734 chat_window_close_activate_cb (GtkAction *action,
735 EmpathyChatWindow *window)
737 EmpathyChatWindowPriv *priv;
739 priv = GET_PRIV (window);
741 g_return_if_fail (priv->current_chat != NULL);
743 empathy_chat_window_remove_chat (window, priv->current_chat);
747 chat_window_edit_activate_cb (GtkAction *action,
748 EmpathyChatWindow *window)
750 EmpathyChatWindowPriv *priv;
751 GtkClipboard *clipboard;
752 GtkTextBuffer *buffer;
753 gboolean text_available;
755 priv = GET_PRIV (window);
757 g_return_if_fail (priv->current_chat != NULL);
759 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
760 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
761 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
762 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
766 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
767 if (gtk_text_buffer_get_has_selection (buffer)) {
768 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
769 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
773 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
775 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
776 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
779 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
780 text_available = gtk_clipboard_wait_is_text_available (clipboard);
781 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
785 chat_window_cut_activate_cb (GtkAction *action,
786 EmpathyChatWindow *window)
788 EmpathyChatWindowPriv *priv;
790 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
792 priv = GET_PRIV (window);
794 empathy_chat_cut (priv->current_chat);
798 chat_window_copy_activate_cb (GtkAction *action,
799 EmpathyChatWindow *window)
801 EmpathyChatWindowPriv *priv;
803 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
805 priv = GET_PRIV (window);
807 empathy_chat_copy (priv->current_chat);
811 chat_window_paste_activate_cb (GtkAction *action,
812 EmpathyChatWindow *window)
814 EmpathyChatWindowPriv *priv;
816 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
818 priv = GET_PRIV (window);
820 empathy_chat_paste (priv->current_chat);
824 chat_window_tabs_next_activate_cb (GtkAction *action,
825 EmpathyChatWindow *window)
827 EmpathyChatWindowPriv *priv;
829 gint index_, numPages;
831 priv = GET_PRIV (window);
833 chat = priv->current_chat;
834 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
835 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
837 if (index_ == (numPages - 1)) {
838 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
842 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
846 chat_window_tabs_previous_activate_cb (GtkAction *action,
847 EmpathyChatWindow *window)
849 EmpathyChatWindowPriv *priv;
851 gint index_, numPages;
853 priv = GET_PRIV (window);
855 chat = priv->current_chat;
856 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
857 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
860 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
864 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
868 chat_window_tabs_left_activate_cb (GtkAction *action,
869 EmpathyChatWindow *window)
871 EmpathyChatWindowPriv *priv;
875 priv = GET_PRIV (window);
877 chat = priv->current_chat;
878 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
883 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
889 chat_window_tabs_right_activate_cb (GtkAction *action,
890 EmpathyChatWindow *window)
892 EmpathyChatWindowPriv *priv;
896 priv = GET_PRIV (window);
898 chat = priv->current_chat;
899 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
901 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
907 chat_window_detach_activate_cb (GtkAction *action,
908 EmpathyChatWindow *window)
910 EmpathyChatWindowPriv *priv;
911 EmpathyChatWindow *new_window;
914 priv = GET_PRIV (window);
916 chat = priv->current_chat;
917 new_window = empathy_chat_window_new ();
919 empathy_chat_window_move_chat (window, new_window, chat);
921 priv = GET_PRIV (new_window);
922 gtk_widget_show (priv->dialog);
926 chat_window_help_contents_activate_cb (GtkAction *action,
927 EmpathyChatWindow *window)
929 EmpathyChatWindowPriv *priv = GET_PRIV (window);
931 empathy_url_show (priv->dialog, "ghelp:empathy");
935 chat_window_help_about_activate_cb (GtkAction *action,
936 EmpathyChatWindow *window)
938 EmpathyChatWindowPriv *priv = GET_PRIV (window);
940 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
944 chat_window_delete_event_cb (GtkWidget *dialog,
946 EmpathyChatWindow *window)
948 EmpathyChatWindowPriv *priv = GET_PRIV (window);
950 DEBUG ("Delete event received");
952 g_object_ref (window);
953 while (priv->chats) {
954 empathy_chat_window_remove_chat (window, priv->chats->data);
956 g_object_unref (window);
962 chat_window_composing_cb (EmpathyChat *chat,
963 gboolean is_composing,
964 EmpathyChatWindow *window)
966 EmpathyChatWindowPriv *priv;
968 priv = GET_PRIV (window);
970 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
971 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
973 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
976 chat_window_update_chat_tab (chat);
980 chat_window_set_urgency_hint (EmpathyChatWindow *window,
983 EmpathyChatWindowPriv *priv;
985 priv = GET_PRIV (window);
987 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
988 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
992 free_notification_data (NotificationData *data)
994 g_object_unref (data->chat);
995 g_slice_free (NotificationData, data);
999 chat_window_notification_closed_cb (NotifyNotification *notify,
1000 NotificationData *cb_data)
1002 EmpathyNotificationClosedReason reason = 0;
1003 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1005 #ifdef notify_notification_get_closed_reason
1006 reason = notify_notification_get_closed_reason (notify);
1008 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1009 empathy_chat_window_present_chat (cb_data->chat);
1012 g_object_unref (notify);
1013 priv->notification = NULL;
1014 free_notification_data (cb_data);
1015 priv->notification_data = NULL;
1019 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1020 EmpathyMessage *message,
1023 EmpathyContact *sender;
1024 const gchar *header;
1028 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1031 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1034 empathy_conf_get_bool (empathy_conf_get (),
1035 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1041 sender = empathy_message_get_sender (message);
1042 header = empathy_contact_get_name (sender);
1043 body = empathy_message_get_body (message);
1044 escaped = g_markup_escape_text (body, -1);
1046 if (priv->notification != NULL) {
1047 notify_notification_update (priv->notification,
1048 header, escaped, NULL);
1050 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1052 cb_data->chat = g_object_ref (chat);
1053 cb_data->window = window;
1055 priv->notification_data = cb_data;
1056 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1057 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1059 g_signal_connect (priv->notification, "closed",
1060 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1063 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1064 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1066 if (pixbuf != NULL) {
1067 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1068 g_object_unref (pixbuf);
1071 notify_notification_show (priv->notification, NULL);
1077 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1082 if (!empathy_chat_is_room (chat))
1085 markup = g_markup_printf_escaped (
1086 "<span color=\"red\" weight=\"bold\">%s</span>",
1087 empathy_chat_get_name (chat));
1089 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1090 gtk_label_set_markup (GTK_LABEL (widget), markup);
1095 chat_window_new_message_cb (EmpathyChat *chat,
1096 EmpathyMessage *message,
1097 EmpathyChatWindow *window)
1099 EmpathyChatWindowPriv *priv;
1101 gboolean needs_urgency;
1102 EmpathyContact *sender;
1104 priv = GET_PRIV (window);
1106 has_focus = empathy_chat_window_has_focus (window);
1108 /* - if we're the sender, we play the sound if it's specified in the
1109 * preferences and we're not away.
1110 * - if we receive a message, we play the sound if it's specified in the
1111 * preferences and the window does not have focus on the chat receiving
1115 sender = empathy_message_get_sender (message);
1117 if (empathy_contact_is_user (sender)) {
1118 empathy_sound_play (GTK_WIDGET (priv->dialog),
1119 EMPATHY_SOUND_MESSAGE_OUTGOING);
1122 if (has_focus && priv->current_chat == chat) {
1126 if (!g_list_find (priv->chats_new_msg, chat)) {
1127 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1128 chat_window_update_chat_tab (chat);
1131 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1132 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1133 * an unamed MUC (msn-like).
1134 * In case of a MUC, we set urgency only if the message contains our
1136 if (empathy_chat_is_room (chat) ||
1137 empathy_chat_get_remote_contact (chat) == NULL) {
1138 needs_urgency = empathy_message_should_highlight (message);
1140 needs_urgency = TRUE;
1143 if (needs_urgency) {
1145 chat_window_set_urgency_hint (window, TRUE);
1146 chat_window_set_highlight_room_tab_label (chat);
1149 empathy_sound_play (GTK_WIDGET (priv->dialog),
1150 EMPATHY_SOUND_MESSAGE_INCOMING);
1151 chat_window_show_or_update_notification (window, message, chat);
1155 static GtkNotebook *
1156 chat_window_detach_hook (GtkNotebook *source,
1162 EmpathyChatWindowPriv *priv;
1163 EmpathyChatWindow *window, *new_window;
1166 chat = EMPATHY_CHAT (page);
1167 window = chat_window_find_chat (chat);
1169 new_window = empathy_chat_window_new ();
1170 priv = GET_PRIV (new_window);
1172 DEBUG ("Detach hook called");
1174 empathy_chat_window_move_chat (window, new_window, chat);
1176 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1177 gtk_widget_show (priv->dialog);
1183 chat_window_page_switched_cb (GtkNotebook *notebook,
1184 GtkNotebookPage *page,
1186 EmpathyChatWindow *window)
1188 EmpathyChatWindowPriv *priv;
1192 DEBUG ("Page switched");
1194 priv = GET_PRIV (window);
1196 child = gtk_notebook_get_nth_page (notebook, page_num);
1197 chat = EMPATHY_CHAT (child);
1199 if (priv->page_added) {
1200 priv->page_added = FALSE;
1201 empathy_chat_scroll_down (chat);
1203 else if (priv->current_chat == chat) {
1207 priv->current_chat = chat;
1208 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1210 chat_window_update_chat_tab (chat);
1214 chat_window_page_added_cb (GtkNotebook *notebook,
1217 EmpathyChatWindow *window)
1219 EmpathyChatWindowPriv *priv;
1222 priv = GET_PRIV (window);
1224 /* If we just received DND to the same window, we don't want
1225 * to do anything here like removing the tab and then readding
1226 * it, so we return here and in "page-added".
1228 if (priv->dnd_same_window) {
1229 DEBUG ("Page added (back to the same window)");
1230 priv->dnd_same_window = FALSE;
1234 DEBUG ("Page added");
1236 /* Get chat object */
1237 chat = EMPATHY_CHAT (child);
1239 /* Connect chat signals for this window */
1240 g_signal_connect (chat, "composing",
1241 G_CALLBACK (chat_window_composing_cb),
1243 g_signal_connect (chat, "new-message",
1244 G_CALLBACK (chat_window_new_message_cb),
1246 g_signal_connect (chat, "notify::tp-chat",
1247 G_CALLBACK (chat_window_update_chat_tab),
1250 /* Set flag so we know to perform some special operations on
1251 * switch page due to the new page being added.
1253 priv->page_added = TRUE;
1255 /* Get list of chats up to date */
1256 priv->chats = g_list_append (priv->chats, chat);
1258 chat_window_update_chat_tab (chat);
1262 chat_window_page_removed_cb (GtkNotebook *notebook,
1265 EmpathyChatWindow *window)
1267 EmpathyChatWindowPriv *priv;
1270 priv = GET_PRIV (window);
1272 /* If we just received DND to the same window, we don't want
1273 * to do anything here like removing the tab and then readding
1274 * it, so we return here and in "page-added".
1276 if (priv->dnd_same_window) {
1277 DEBUG ("Page removed (and will be readded to same window)");
1281 DEBUG ("Page removed");
1283 /* Get chat object */
1284 chat = EMPATHY_CHAT (child);
1286 /* Disconnect all signal handlers for this chat and this window */
1287 g_signal_handlers_disconnect_by_func (chat,
1288 G_CALLBACK (chat_window_composing_cb),
1290 g_signal_handlers_disconnect_by_func (chat,
1291 G_CALLBACK (chat_window_new_message_cb),
1293 g_signal_handlers_disconnect_by_func (chat,
1294 G_CALLBACK (chat_window_update_chat_tab),
1297 /* Keep list of chats up to date */
1298 priv->chats = g_list_remove (priv->chats, chat);
1299 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1300 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1302 if (priv->chats == NULL) {
1303 g_object_unref (window);
1305 chat_window_update (window);
1310 chat_window_focus_in_event_cb (GtkWidget *widget,
1312 EmpathyChatWindow *window)
1314 EmpathyChatWindowPriv *priv;
1316 DEBUG ("Focus in event, updating title");
1318 priv = GET_PRIV (window);
1320 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1322 chat_window_set_urgency_hint (window, FALSE);
1324 /* Update the title, since we now mark all unread messages as read. */
1325 chat_window_update_chat_tab (priv->current_chat);
1331 chat_window_drag_motion (GtkWidget *widget,
1332 GdkDragContext *context,
1336 EmpathyChatWindow *window)
1338 static GtkTargetList *list = NULL;
1342 list = gtk_target_list_new (drag_types_dest_move,
1343 G_N_ELEMENTS (drag_types_dest_move));
1346 target = gtk_drag_dest_find_target (widget, context, list);
1347 if (target == GDK_NONE) {
1348 gdk_drag_status (context, GDK_ACTION_COPY, time);
1351 gdk_drag_status (context, GDK_ACTION_MOVE, time);
1356 chat_window_drag_data_received (GtkWidget *widget,
1357 GdkDragContext *context,
1360 GtkSelectionData *selection,
1363 EmpathyChatWindow *window)
1365 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1366 EmpathyChat *chat = NULL;
1367 EmpathyChatWindow *old_window;
1368 TpAccount *account = NULL;
1369 TpAccountManager *account_manager;
1372 const gchar *account_id;
1373 const gchar *contact_id;
1375 id = (const gchar*) gtk_selection_data_get_data (selection);
1377 /* FIXME: Perhaps should be sure that the account manager is
1378 * prepared before calling _ensure_account on it. */
1379 account_manager = tp_account_manager_dup ();
1381 DEBUG ("DND contact from roster with id:'%s'", id);
1383 strv = g_strsplit (id, ":", 2);
1384 if (g_strv_length (strv) == 2) {
1385 account_id = strv[0];
1386 contact_id = strv[1];
1388 tp_account_manager_ensure_account (account_manager, account_id);
1389 if (account != NULL)
1390 chat = empathy_chat_window_find_chat (account, contact_id);
1393 if (account == NULL) {
1395 gtk_drag_finish (context, FALSE, FALSE, time_);
1400 TpConnection *connection;
1402 connection = tp_account_get_connection (account);
1405 empathy_dispatcher_chat_with_contact_id (
1406 connection, contact_id, NULL, NULL);
1412 g_object_unref (account_manager);
1415 old_window = chat_window_find_chat (chat);
1417 if (old_window == window) {
1418 gtk_drag_finish (context, TRUE, FALSE, time_);
1422 empathy_chat_window_move_chat (old_window, window, chat);
1424 empathy_chat_window_add_chat (window, chat);
1427 /* Added to take care of any outstanding chat events */
1428 empathy_chat_window_present_chat (chat);
1430 /* We should return TRUE to remove the data when doing
1431 * GDK_ACTION_MOVE, but we don't here otherwise it has
1432 * weird consequences, and we handle that internally
1433 * anyway with add_chat () and remove_chat ().
1435 gtk_drag_finish (context, TRUE, FALSE, time_);
1437 else if (info == DND_DRAG_TYPE_URI_LIST) {
1438 EmpathyChatWindowPriv *priv;
1439 EmpathyContact *contact;
1445 priv = GET_PRIV (window);
1446 contact = empathy_chat_get_remote_contact (priv->current_chat);
1448 if (!EMPATHY_IS_CONTACT (contact)) {
1449 gtk_drag_finish (context, TRUE, FALSE, time);
1453 /* Only handle a single file for new. It would be wicked cool to be
1454 able to do multiple files, offering to zip them or whatever like
1455 nautilus-sendto does. Note that text/uri-list is defined to have
1456 each line terminated by \r\n, but we can be tolerant of applications
1457 that only use \n or don't terminate single-line entries.
1459 data = (const gchar*) gtk_selection_data_get_data (selection);
1460 nl = strstr (data, "\r\n");
1462 nl = strchr (data, '\n');
1465 uri = g_strndup (data, nl - data);
1466 file = g_file_new_for_uri (uri);
1470 file = g_file_new_for_uri (data);
1473 empathy_send_file (contact, file);
1475 g_object_unref (file);
1476 gtk_drag_finish (context, TRUE, FALSE, time);
1478 else if (info == DND_DRAG_TYPE_TAB) {
1480 EmpathyChatWindow *old_window = NULL;
1484 chat = (void *) gtk_selection_data_get_data (selection);
1485 old_window = chat_window_find_chat (*chat);
1488 EmpathyChatWindowPriv *priv;
1490 priv = GET_PRIV (window);
1492 if (old_window == window) {
1493 DEBUG ("DND tab (within same window)");
1494 priv->dnd_same_window = TRUE;
1495 gtk_drag_finish (context, TRUE, FALSE, time_);
1499 priv->dnd_same_window = FALSE;
1502 /* We should return TRUE to remove the data when doing
1503 * GDK_ACTION_MOVE, but we don't here otherwise it has
1504 * weird consequences, and we handle that internally
1505 * anyway with add_chat () and remove_chat ().
1507 gtk_drag_finish (context, TRUE, FALSE, time_);
1509 DEBUG ("DND from unknown source");
1510 gtk_drag_finish (context, FALSE, FALSE, time_);
1515 chat_window_finalize (GObject *object)
1517 EmpathyChatWindow *window;
1518 EmpathyChatWindowPriv *priv;
1520 window = EMPATHY_CHAT_WINDOW (object);
1521 priv = GET_PRIV (window);
1523 DEBUG ("Finalized: %p", object);
1525 g_object_unref (priv->ui_manager);
1526 g_object_unref (priv->chatroom_manager);
1527 g_object_unref (priv->notify_mgr);
1529 if (priv->notification != NULL) {
1530 notify_notification_close (priv->notification, NULL);
1531 g_object_unref (priv->notification);
1532 priv->notification = NULL;
1533 if (priv->notification_data != NULL)
1535 free_notification_data (priv->notification_data);
1536 priv->notification_data = NULL;
1540 chat_windows = g_list_remove (chat_windows, window);
1541 gtk_widget_destroy (priv->dialog);
1543 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1547 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1549 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1551 object_class->finalize = chat_window_finalize;
1553 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1555 /* Set up a style for the close button with no focus padding. */
1556 gtk_rc_parse_string (
1557 "style \"empathy-close-button-style\"\n"
1559 " GtkWidget::focus-padding = 0\n"
1563 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1565 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1569 empathy_chat_window_init (EmpathyChatWindow *window)
1572 GtkAccelGroup *accel_group;
1577 GtkWidget *chat_vbox;
1579 EmpathySmileyManager *smiley_manager;
1580 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1581 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1583 window->priv = priv;
1584 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1585 gui = empathy_builder_get_file (filename,
1586 "chat_window", &priv->dialog,
1587 "chat_vbox", &chat_vbox,
1588 "ui_manager", &priv->ui_manager,
1589 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1590 "menu_conv_favorite", &priv->menu_conv_favorite,
1591 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1592 "menu_edit_cut", &priv->menu_edit_cut,
1593 "menu_edit_copy", &priv->menu_edit_copy,
1594 "menu_edit_paste", &priv->menu_edit_paste,
1595 "menu_tabs_next", &priv->menu_tabs_next,
1596 "menu_tabs_prev", &priv->menu_tabs_prev,
1597 "menu_tabs_left", &priv->menu_tabs_left,
1598 "menu_tabs_right", &priv->menu_tabs_right,
1599 "menu_tabs_detach", &priv->menu_tabs_detach,
1603 empathy_builder_connect (gui, window,
1604 "menu_conv", "activate", chat_window_conv_activate_cb,
1605 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1606 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1607 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1608 "menu_conv_close", "activate", chat_window_close_activate_cb,
1609 "menu_edit", "activate", chat_window_edit_activate_cb,
1610 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1611 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1612 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1613 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1614 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1615 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1616 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1617 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1618 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1619 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1622 g_object_ref (priv->ui_manager);
1623 g_object_unref (gui);
1625 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1627 priv->notebook = gtk_notebook_new ();
1628 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1629 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1630 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1631 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1632 gtk_widget_show (priv->notebook);
1635 accel_group = gtk_accel_group_new ();
1636 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1638 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1639 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1642 gtk_accel_group_connect (accel_group,
1649 g_object_unref (accel_group);
1651 /* Set up smiley menu */
1652 smiley_manager = empathy_smiley_manager_dup_singleton ();
1653 submenu = empathy_smiley_menu_new (smiley_manager,
1654 chat_window_insert_smiley_activate_cb,
1656 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1657 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1658 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1659 g_object_unref (smiley_manager);
1661 /* Set up signals we can't do with ui file since we may need to
1662 * block/unblock them at some later stage.
1665 g_signal_connect (priv->dialog,
1667 G_CALLBACK (chat_window_delete_event_cb),
1669 g_signal_connect (priv->dialog,
1671 G_CALLBACK (chat_window_focus_in_event_cb),
1673 g_signal_connect_after (priv->notebook,
1675 G_CALLBACK (chat_window_page_switched_cb),
1677 g_signal_connect (priv->notebook,
1679 G_CALLBACK (chat_window_page_added_cb),
1681 g_signal_connect (priv->notebook,
1683 G_CALLBACK (chat_window_page_removed_cb),
1686 /* Set up drag and drop */
1687 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1688 GTK_DEST_DEFAULT_ALL,
1690 G_N_ELEMENTS (drag_types_dest),
1691 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1693 g_signal_connect (priv->notebook,
1695 G_CALLBACK (chat_window_drag_motion),
1697 g_signal_connect (priv->notebook,
1698 "drag-data-received",
1699 G_CALLBACK (chat_window_drag_data_received),
1702 chat_windows = g_list_prepend (chat_windows, window);
1704 /* Set up private details */
1706 priv->chats_new_msg = NULL;
1707 priv->chats_composing = NULL;
1708 priv->current_chat = NULL;
1710 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1714 empathy_chat_window_new (void)
1716 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1719 /* Returns the window to open a new tab in if there is only one window
1720 * visble, otherwise, returns NULL indicating that a new window should
1724 empathy_chat_window_get_default (void)
1727 gboolean separate_windows = TRUE;
1729 empathy_conf_get_bool (empathy_conf_get (),
1730 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1733 if (separate_windows) {
1734 /* Always create a new window */
1738 for (l = chat_windows; l; l = l->next) {
1739 EmpathyChatWindow *chat_window;
1742 chat_window = l->data;
1744 dialog = empathy_chat_window_get_dialog (chat_window);
1745 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1746 /* Found a visible window on this desktop */
1755 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1757 EmpathyChatWindowPriv *priv;
1759 g_return_val_if_fail (window != NULL, NULL);
1761 priv = GET_PRIV (window);
1763 return priv->dialog;
1767 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1770 EmpathyChatWindowPriv *priv;
1772 GtkWidget *popup_label;
1775 g_return_if_fail (window != NULL);
1776 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1778 priv = GET_PRIV (window);
1780 /* Reference the chat object */
1781 g_object_ref (chat);
1783 /* If this window has just been created, position it */
1784 if (priv->chats == NULL) {
1785 const gchar *name = "chat-window";
1786 gboolean separate_windows;
1788 empathy_conf_get_bool (empathy_conf_get (),
1789 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1792 if (separate_windows) {
1793 name = empathy_chat_get_id (chat);
1796 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
1799 child = GTK_WIDGET (chat);
1800 label = chat_window_create_label (window, chat, TRUE);
1801 popup_label = chat_window_create_label (window, chat, FALSE);
1802 gtk_widget_show (child);
1804 g_signal_connect (chat, "notify::name",
1805 G_CALLBACK (chat_window_chat_notify_cb),
1807 g_signal_connect (chat, "notify::subject",
1808 G_CALLBACK (chat_window_chat_notify_cb),
1810 g_signal_connect (chat, "notify::remote-contact",
1811 G_CALLBACK (chat_window_chat_notify_cb),
1813 chat_window_chat_notify_cb (chat);
1815 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1816 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1817 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1818 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1819 TRUE, TRUE, GTK_PACK_START);
1821 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1825 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1828 EmpathyChatWindowPriv *priv;
1830 EmpathyContact *remote_contact;
1832 g_return_if_fail (window != NULL);
1833 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1835 priv = GET_PRIV (window);
1837 g_signal_handlers_disconnect_by_func (chat,
1838 chat_window_chat_notify_cb,
1840 remote_contact = g_object_get_data (G_OBJECT (chat),
1841 "chat-window-remote-contact");
1842 if (remote_contact) {
1843 g_signal_handlers_disconnect_by_func (remote_contact,
1844 chat_window_update_chat_tab,
1848 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1850 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1852 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1854 g_object_unref (chat);
1858 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1859 EmpathyChatWindow *new_window,
1864 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1865 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1866 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1868 widget = GTK_WIDGET (chat);
1870 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1871 G_OBJECT (widget)->ref_count);
1873 /* We reference here to make sure we don't loose the widget
1874 * and the EmpathyChat object during the move.
1876 g_object_ref (chat);
1877 g_object_ref (widget);
1879 empathy_chat_window_remove_chat (old_window, chat);
1880 empathy_chat_window_add_chat (new_window, chat);
1882 g_object_unref (widget);
1883 g_object_unref (chat);
1887 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1890 EmpathyChatWindowPriv *priv;
1893 g_return_if_fail (window != NULL);
1894 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1896 priv = GET_PRIV (window);
1898 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1900 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1905 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1907 EmpathyChatWindowPriv *priv;
1910 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1912 priv = GET_PRIV (window);
1914 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1920 empathy_chat_window_find_chat (TpAccount *account,
1925 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1927 for (l = chat_windows; l; l = l->next) {
1928 EmpathyChatWindowPriv *priv;
1929 EmpathyChatWindow *window;
1933 priv = GET_PRIV (window);
1935 for (ll = priv->chats; ll; ll = ll->next) {
1940 if (account == empathy_chat_get_account (chat) &&
1941 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1951 empathy_chat_window_present_chat (EmpathyChat *chat)
1953 EmpathyChatWindow *window;
1954 EmpathyChatWindowPriv *priv;
1956 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1958 window = chat_window_find_chat (chat);
1960 /* If the chat has no window, create one */
1961 if (window == NULL) {
1962 window = empathy_chat_window_get_default ();
1964 window = empathy_chat_window_new ();
1967 empathy_chat_window_add_chat (window, chat);
1970 priv = GET_PRIV (window);
1971 empathy_chat_window_switch_to_chat (window, chat);
1972 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1974 gtk_widget_grab_focus (chat->input_text_view);