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;
83 GtkTargetList *move_targets;
86 GtkUIManager *ui_manager;
87 GtkAction *menu_conv_insert_smiley;
88 GtkAction *menu_conv_favorite;
89 GtkAction *menu_conv_toggle_contacts;
91 GtkAction *menu_edit_cut;
92 GtkAction *menu_edit_copy;
93 GtkAction *menu_edit_paste;
95 GtkAction *menu_tabs_next;
96 GtkAction *menu_tabs_prev;
97 GtkAction *menu_tabs_left;
98 GtkAction *menu_tabs_right;
99 GtkAction *menu_tabs_detach;
100 } EmpathyChatWindowPriv;
102 static GList *chat_windows = NULL;
104 static const guint tab_accel_keys[] = {
105 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
106 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
110 DND_DRAG_TYPE_CONTACT_ID,
111 DND_DRAG_TYPE_URI_LIST,
115 static const GtkTargetEntry drag_types_dest[] = {
116 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
117 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
118 { "text/uri-list", 0, DND_DRAG_TYPE_URI_LIST },
121 static const GtkTargetEntry drag_types_dest_move[] = {
122 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
125 static void chat_window_update (EmpathyChatWindow *window);
127 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
130 chat_window_accel_cb (GtkAccelGroup *accelgroup,
134 EmpathyChatWindow *window)
136 EmpathyChatWindowPriv *priv;
140 priv = GET_PRIV (window);
142 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
143 if (tab_accel_keys[i] == key) {
150 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
154 static EmpathyChatWindow *
155 chat_window_find_chat (EmpathyChat *chat)
157 EmpathyChatWindowPriv *priv;
160 for (l = chat_windows; l; l = l->next) {
161 priv = GET_PRIV (l->data);
162 ll = g_list_find (priv->chats, chat);
172 chat_window_close_clicked_cb (GtkAction *action,
175 EmpathyChatWindow *window;
177 window = chat_window_find_chat (chat);
178 empathy_chat_window_remove_chat (window, chat);
182 chat_tab_style_set_cb (GtkWidget *hbox,
183 GtkStyle *previous_style,
187 int char_width, h, w;
188 PangoContext *context;
189 PangoFontMetrics *metrics;
191 button = g_object_get_data (G_OBJECT (user_data),
192 "chat-window-tab-close-button");
193 context = gtk_widget_get_pango_context (hbox);
195 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
196 pango_context_get_language (context));
197 char_width = pango_font_metrics_get_approximate_char_width (metrics);
198 pango_font_metrics_unref (metrics);
200 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
201 GTK_ICON_SIZE_MENU, &w, &h);
203 /* Request at least about 12 chars width plus at least space for the status
204 * image and the close button */
205 gtk_widget_set_size_request (hbox,
206 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
208 gtk_widget_set_size_request (button, w, h);
212 chat_window_create_label (EmpathyChatWindow *window,
214 gboolean is_tab_label)
216 EmpathyChatWindowPriv *priv;
218 GtkWidget *name_label;
219 GtkWidget *status_image;
220 GtkWidget *close_button;
221 GtkWidget *close_image;
222 GtkWidget *event_box;
223 GtkWidget *event_box_hbox;
224 PangoAttrList *attr_list;
225 PangoAttribute *attr;
227 priv = GET_PRIV (window);
229 /* The spacing between the button and the label. */
230 hbox = gtk_hbox_new (FALSE, 0);
232 event_box = gtk_event_box_new ();
233 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
235 name_label = gtk_label_new (NULL);
237 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
239 attr_list = pango_attr_list_new ();
240 attr = pango_attr_scale_new (1/1.2);
241 attr->start_index = 0;
242 attr->end_index = -1;
243 pango_attr_list_insert (attr_list, attr);
244 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
245 pango_attr_list_unref (attr_list);
247 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
248 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
249 g_object_set_data (G_OBJECT (chat),
250 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
253 status_image = gtk_image_new ();
255 /* Spacing between the icon and label. */
256 event_box_hbox = gtk_hbox_new (FALSE, 0);
258 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
259 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
261 g_object_set_data (G_OBJECT (chat),
262 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
264 g_object_set_data (G_OBJECT (chat),
265 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
268 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
269 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
272 close_button = gtk_button_new ();
273 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
274 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
276 /* We don't want focus/keynav for the button to avoid clutter, and
277 * Ctrl-W works anyway.
279 gtk_widget_set_can_focus (close_button, FALSE);
280 gtk_widget_set_can_default (close_button, FALSE);
282 /* Set the name to make the special rc style match. */
283 gtk_widget_set_name (close_button, "empathy-close-button");
285 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
287 gtk_container_add (GTK_CONTAINER (close_button), close_image);
289 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
291 g_signal_connect (close_button,
293 G_CALLBACK (chat_window_close_clicked_cb),
296 /* React to theme changes and also setup the size correctly. */
297 g_signal_connect (hbox,
299 G_CALLBACK (chat_tab_style_set_cb),
303 gtk_widget_show_all (hbox);
309 _submenu_notify_visible_changed_cb (GObject *object,
313 g_signal_handlers_disconnect_by_func (object,
314 _submenu_notify_visible_changed_cb,
316 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
320 chat_window_menu_context_update (EmpathyChatWindowPriv *priv,
325 gboolean is_connected;
328 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
329 first_page = (page_num == 0);
330 last_page = (page_num == (num_pages - 1));
331 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
333 DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
335 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
336 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
337 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
338 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
339 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
340 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
344 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
345 EmpathyChatWindow *window)
347 GtkWidget *menu, *submenu, *orig_submenu;
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));
353 DEBUG ("Update window : Contact Menu");
355 if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
356 submenu = empathy_chat_get_contact_menu (priv->current_chat);
357 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
358 gtk_widget_show (menu);
360 empathy_signal_connect_weak (orig_submenu,
362 (GCallback)_submenu_notify_visible_changed_cb,
368 chat_window_title_update (EmpathyChatWindowPriv *priv)
372 name = empathy_chat_get_name (priv->current_chat);
374 DEBUG ("Update window : Title");
376 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
380 chat_window_icon_update (EmpathyChatWindowPriv *priv)
383 EmpathyContact *remote_contact;
384 gboolean avatar_in_icon;
387 n_chats = g_list_length (priv->chats);
389 DEBUG ("Update window : Icon");
391 /* Update window icon */
392 if (priv->chats_new_msg) {
393 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
394 EMPATHY_IMAGE_MESSAGE);
396 empathy_conf_get_bool (empathy_conf_get (),
397 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
400 if (n_chats == 1 && avatar_in_icon) {
401 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
402 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
403 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
406 g_object_unref (icon);
409 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
415 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
419 GtkWidget *chat_close_button;
422 DEBUG ("Update window : Close Button");
424 if (num_pages == 1) {
425 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
426 chat_close_button = g_object_get_data (G_OBJECT (chat),
427 "chat-window-tab-close-button");
428 gtk_widget_hide (chat_close_button);
430 for (i=0; i<num_pages; i++) {
431 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
432 chat_close_button = g_object_get_data (G_OBJECT (chat),
433 "chat-window-tab-close-button");
434 gtk_widget_show (chat_close_button);
440 chat_window_update (EmpathyChatWindow *window)
442 EmpathyChatWindowPriv *priv = GET_PRIV (window);
445 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
447 DEBUG ("Update window");
449 /* Update Tab menu */
450 chat_window_menu_context_update (priv,
453 chat_window_contact_menu_update (priv,
456 chat_window_title_update (priv);
458 chat_window_icon_update (priv);
460 chat_window_close_button_update (priv,
465 append_markup_printf (GString *string,
472 va_start (args, format);
474 tmp = g_markup_vprintf_escaped (format, args);
475 g_string_append (string, tmp);
482 chat_window_update_chat_tab (EmpathyChat *chat)
484 EmpathyChatWindow *window;
485 EmpathyChatWindowPriv *priv;
486 EmpathyContact *remote_contact;
490 const gchar *subject;
491 const gchar *status = NULL;
495 const gchar *icon_name;
496 GtkWidget *tab_image;
497 GtkWidget *menu_image;
499 window = chat_window_find_chat (chat);
503 priv = GET_PRIV (window);
505 /* Get information */
506 name = empathy_chat_get_name (chat);
507 account = empathy_chat_get_account (chat);
508 subject = empathy_chat_get_subject (chat);
509 remote_contact = empathy_chat_get_remote_contact (chat);
511 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
512 name, tp_proxy_get_object_path (account), subject, remote_contact);
514 /* Update tab image */
515 if (empathy_chat_get_tp_chat (chat) == NULL) {
516 /* No TpChat, we are disconnected */
519 else if (g_list_find (priv->chats_new_msg, chat)) {
520 icon_name = EMPATHY_IMAGE_MESSAGE;
522 else if (g_list_find (priv->chats_composing, chat)) {
523 icon_name = EMPATHY_IMAGE_TYPING;
525 else if (remote_contact) {
526 icon_name = empathy_icon_name_for_contact (remote_contact);
528 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
531 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
532 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
533 if (icon_name != NULL) {
534 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
535 gtk_widget_show (tab_image);
536 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
537 gtk_widget_show (menu_image);
539 gtk_widget_hide (tab_image);
540 gtk_widget_hide (menu_image);
543 /* Update tab tooltip */
544 tooltip = g_string_new (NULL);
546 if (remote_contact) {
547 id = empathy_contact_get_id (remote_contact);
548 status = empathy_contact_get_presence_message (remote_contact);
553 append_markup_printf (tooltip,
554 "<b>%s</b><small> (%s)</small>",
556 tp_account_get_display_name (account));
558 if (!EMP_STR_EMPTY (status)) {
559 append_markup_printf (tooltip, "\n<i>%s</i>", status);
563 append_markup_printf (tooltip, "\n<b>%s</b> %s",
564 _("Topic:"), subject);
567 if (g_list_find (priv->chats_composing, chat)) {
568 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
571 markup = g_string_free (tooltip, FALSE);
572 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
573 gtk_widget_set_tooltip_markup (widget, markup);
574 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
575 gtk_widget_set_tooltip_markup (widget, markup);
578 /* Update tab and menu label */
579 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
580 gtk_label_set_text (GTK_LABEL (widget), name);
581 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
582 gtk_label_set_text (GTK_LABEL (widget), name);
584 /* Update the window if it's the current chat */
585 if (priv->current_chat == chat) {
586 chat_window_update (window);
591 chat_window_chat_notify_cb (EmpathyChat *chat)
593 EmpathyContact *old_remote_contact;
594 EmpathyContact *remote_contact = NULL;
596 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
597 remote_contact = empathy_chat_get_remote_contact (chat);
599 if (old_remote_contact != remote_contact) {
600 /* The remote-contact associated with the chat changed, we need
601 * to keep track of any change of that contact and update the
602 * window each time. */
603 if (remote_contact) {
604 g_signal_connect_swapped (remote_contact, "notify",
605 G_CALLBACK (chat_window_update_chat_tab),
608 if (old_remote_contact) {
609 g_signal_handlers_disconnect_by_func (old_remote_contact,
610 chat_window_update_chat_tab,
614 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
618 chat_window_update_chat_tab (chat);
622 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
623 EmpathySmiley *smiley,
626 EmpathyChatWindowPriv *priv = GET_PRIV (window);
628 GtkTextBuffer *buffer;
631 chat = priv->current_chat;
633 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
634 gtk_text_buffer_get_end_iter (buffer, &iter);
635 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
639 chat_window_conv_activate_cb (GtkAction *action,
640 EmpathyChatWindow *window)
642 EmpathyChatWindowPriv *priv = GET_PRIV (window);
645 EmpathyContact *remote_contact = NULL;
647 /* Favorite room menu */
648 is_room = empathy_chat_is_room (priv->current_chat);
652 gboolean found = FALSE;
653 EmpathyChatroom *chatroom;
655 room = empathy_chat_get_id (priv->current_chat);
656 account = empathy_chat_get_account (priv->current_chat);
657 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
659 if (chatroom != NULL)
660 found = empathy_chatroom_is_favorite (chatroom);
662 DEBUG ("This room %s favorite", found ? "is" : "is not");
663 gtk_toggle_action_set_active (
664 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
666 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
668 /* Show contacts menu */
669 g_object_get (priv->current_chat,
670 "remote-contact", &remote_contact,
671 "show-contacts", &active,
673 if (remote_contact == NULL) {
674 gtk_toggle_action_set_active (
675 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
678 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
679 (remote_contact == NULL));
680 if (remote_contact != NULL) {
681 g_object_unref (remote_contact);
686 chat_window_clear_activate_cb (GtkAction *action,
687 EmpathyChatWindow *window)
689 EmpathyChatWindowPriv *priv = GET_PRIV (window);
691 empathy_chat_clear (priv->current_chat);
695 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
696 EmpathyChatWindow *window)
698 EmpathyChatWindowPriv *priv = GET_PRIV (window);
702 EmpathyChatroom *chatroom;
704 active = gtk_toggle_action_get_active (toggle_action);
705 account = empathy_chat_get_account (priv->current_chat);
706 room = empathy_chat_get_id (priv->current_chat);
708 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
711 if (chatroom == NULL) {
714 name = empathy_chat_get_name (priv->current_chat);
715 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
716 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
717 g_object_unref (chatroom);
720 empathy_chatroom_set_favorite (chatroom, active);
724 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
725 EmpathyChatWindow *window)
727 EmpathyChatWindowPriv *priv = GET_PRIV (window);
730 active = gtk_toggle_action_get_active (toggle_action);
732 empathy_chat_set_show_contacts (priv->current_chat, active);
736 chat_window_close_activate_cb (GtkAction *action,
737 EmpathyChatWindow *window)
739 EmpathyChatWindowPriv *priv;
741 priv = GET_PRIV (window);
743 g_return_if_fail (priv->current_chat != NULL);
745 empathy_chat_window_remove_chat (window, priv->current_chat);
749 chat_window_edit_activate_cb (GtkAction *action,
750 EmpathyChatWindow *window)
752 EmpathyChatWindowPriv *priv;
753 GtkClipboard *clipboard;
754 GtkTextBuffer *buffer;
755 gboolean text_available;
757 priv = GET_PRIV (window);
759 g_return_if_fail (priv->current_chat != NULL);
761 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
762 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
763 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
764 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
768 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
769 if (gtk_text_buffer_get_has_selection (buffer)) {
770 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
771 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
775 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
777 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
778 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
781 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
782 text_available = gtk_clipboard_wait_is_text_available (clipboard);
783 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
787 chat_window_cut_activate_cb (GtkAction *action,
788 EmpathyChatWindow *window)
790 EmpathyChatWindowPriv *priv;
792 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
794 priv = GET_PRIV (window);
796 empathy_chat_cut (priv->current_chat);
800 chat_window_copy_activate_cb (GtkAction *action,
801 EmpathyChatWindow *window)
803 EmpathyChatWindowPriv *priv;
805 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
807 priv = GET_PRIV (window);
809 empathy_chat_copy (priv->current_chat);
813 chat_window_paste_activate_cb (GtkAction *action,
814 EmpathyChatWindow *window)
816 EmpathyChatWindowPriv *priv;
818 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
820 priv = GET_PRIV (window);
822 empathy_chat_paste (priv->current_chat);
826 chat_window_tabs_next_activate_cb (GtkAction *action,
827 EmpathyChatWindow *window)
829 EmpathyChatWindowPriv *priv;
831 gint index_, numPages;
833 priv = GET_PRIV (window);
835 chat = priv->current_chat;
836 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
837 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
839 if (index_ == (numPages - 1)) {
840 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
844 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
848 chat_window_tabs_previous_activate_cb (GtkAction *action,
849 EmpathyChatWindow *window)
851 EmpathyChatWindowPriv *priv;
853 gint index_, numPages;
855 priv = GET_PRIV (window);
857 chat = priv->current_chat;
858 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
859 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
862 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
866 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
870 chat_window_tabs_left_activate_cb (GtkAction *action,
871 EmpathyChatWindow *window)
873 EmpathyChatWindowPriv *priv;
877 priv = GET_PRIV (window);
879 chat = priv->current_chat;
880 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
885 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
891 chat_window_tabs_right_activate_cb (GtkAction *action,
892 EmpathyChatWindow *window)
894 EmpathyChatWindowPriv *priv;
898 priv = GET_PRIV (window);
900 chat = priv->current_chat;
901 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
903 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
909 chat_window_detach_activate_cb (GtkAction *action,
910 EmpathyChatWindow *window)
912 EmpathyChatWindowPriv *priv;
913 EmpathyChatWindow *new_window;
916 priv = GET_PRIV (window);
918 chat = priv->current_chat;
919 new_window = empathy_chat_window_new ();
921 empathy_chat_window_move_chat (window, new_window, chat);
923 priv = GET_PRIV (new_window);
924 gtk_widget_show (priv->dialog);
928 chat_window_help_contents_activate_cb (GtkAction *action,
929 EmpathyChatWindow *window)
931 EmpathyChatWindowPriv *priv = GET_PRIV (window);
933 empathy_url_show (priv->dialog, "ghelp:empathy");
937 chat_window_help_about_activate_cb (GtkAction *action,
938 EmpathyChatWindow *window)
940 EmpathyChatWindowPriv *priv = GET_PRIV (window);
942 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
946 chat_window_delete_event_cb (GtkWidget *dialog,
948 EmpathyChatWindow *window)
950 EmpathyChatWindowPriv *priv = GET_PRIV (window);
952 DEBUG ("Delete event received");
954 g_object_ref (window);
955 while (priv->chats) {
956 empathy_chat_window_remove_chat (window, priv->chats->data);
958 g_object_unref (window);
964 chat_window_composing_cb (EmpathyChat *chat,
965 gboolean is_composing,
966 EmpathyChatWindow *window)
968 EmpathyChatWindowPriv *priv;
970 priv = GET_PRIV (window);
972 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
973 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
975 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
978 chat_window_update_chat_tab (chat);
982 chat_window_set_urgency_hint (EmpathyChatWindow *window,
985 EmpathyChatWindowPriv *priv;
987 priv = GET_PRIV (window);
989 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
990 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
994 free_notification_data (NotificationData *data)
996 g_object_unref (data->chat);
997 g_slice_free (NotificationData, data);
1001 chat_window_notification_closed_cb (NotifyNotification *notify,
1002 NotificationData *cb_data)
1004 EmpathyNotificationClosedReason reason = 0;
1005 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1007 #ifdef notify_notification_get_closed_reason
1008 reason = notify_notification_get_closed_reason (notify);
1010 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1011 empathy_chat_window_present_chat (cb_data->chat);
1014 g_object_unref (notify);
1015 priv->notification = NULL;
1016 free_notification_data (cb_data);
1017 priv->notification_data = NULL;
1021 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1022 EmpathyMessage *message,
1025 EmpathyContact *sender;
1026 const gchar *header;
1030 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1033 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1036 empathy_conf_get_bool (empathy_conf_get (),
1037 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1043 sender = empathy_message_get_sender (message);
1044 header = empathy_contact_get_name (sender);
1045 body = empathy_message_get_body (message);
1046 escaped = g_markup_escape_text (body, -1);
1048 if (priv->notification != NULL) {
1049 notify_notification_update (priv->notification,
1050 header, escaped, NULL);
1052 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1054 cb_data->chat = g_object_ref (chat);
1055 cb_data->window = window;
1057 priv->notification_data = cb_data;
1058 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1059 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1061 g_signal_connect (priv->notification, "closed",
1062 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1065 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1066 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1068 if (pixbuf != NULL) {
1069 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1070 g_object_unref (pixbuf);
1073 notify_notification_show (priv->notification, NULL);
1079 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1084 if (!empathy_chat_is_room (chat))
1087 markup = g_markup_printf_escaped (
1088 "<span color=\"red\" weight=\"bold\">%s</span>",
1089 empathy_chat_get_name (chat));
1091 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1092 gtk_label_set_markup (GTK_LABEL (widget), markup);
1097 chat_window_new_message_cb (EmpathyChat *chat,
1098 EmpathyMessage *message,
1099 EmpathyChatWindow *window)
1101 EmpathyChatWindowPriv *priv;
1103 gboolean needs_urgency;
1104 EmpathyContact *sender;
1106 priv = GET_PRIV (window);
1108 has_focus = empathy_chat_window_has_focus (window);
1110 /* - if we're the sender, we play the sound if it's specified in the
1111 * preferences and we're not away.
1112 * - if we receive a message, we play the sound if it's specified in the
1113 * preferences and the window does not have focus on the chat receiving
1117 sender = empathy_message_get_sender (message);
1119 if (empathy_contact_is_user (sender)) {
1120 empathy_sound_play (GTK_WIDGET (priv->dialog),
1121 EMPATHY_SOUND_MESSAGE_OUTGOING);
1124 if (has_focus && priv->current_chat == chat) {
1128 if (!g_list_find (priv->chats_new_msg, chat)) {
1129 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1130 chat_window_update_chat_tab (chat);
1133 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1134 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1135 * an unamed MUC (msn-like).
1136 * In case of a MUC, we set urgency only if the message contains our
1138 if (empathy_chat_is_room (chat) ||
1139 empathy_chat_get_remote_contact (chat) == NULL) {
1140 needs_urgency = empathy_message_should_highlight (message);
1142 needs_urgency = TRUE;
1145 if (needs_urgency) {
1147 chat_window_set_urgency_hint (window, TRUE);
1148 chat_window_set_highlight_room_tab_label (chat);
1151 empathy_sound_play (GTK_WIDGET (priv->dialog),
1152 EMPATHY_SOUND_MESSAGE_INCOMING);
1153 chat_window_show_or_update_notification (window, message, chat);
1157 static GtkNotebook *
1158 chat_window_detach_hook (GtkNotebook *source,
1164 EmpathyChatWindowPriv *priv;
1165 EmpathyChatWindow *window, *new_window;
1168 chat = EMPATHY_CHAT (page);
1169 window = chat_window_find_chat (chat);
1171 new_window = empathy_chat_window_new ();
1172 priv = GET_PRIV (new_window);
1174 DEBUG ("Detach hook called");
1176 empathy_chat_window_move_chat (window, new_window, chat);
1178 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1179 gtk_widget_show (priv->dialog);
1185 chat_window_page_switched_cb (GtkNotebook *notebook,
1186 GtkNotebookPage *page,
1188 EmpathyChatWindow *window)
1190 EmpathyChatWindowPriv *priv;
1194 DEBUG ("Page switched");
1196 priv = GET_PRIV (window);
1198 child = gtk_notebook_get_nth_page (notebook, page_num);
1199 chat = EMPATHY_CHAT (child);
1201 if (priv->page_added) {
1202 priv->page_added = FALSE;
1203 empathy_chat_scroll_down (chat);
1205 else if (priv->current_chat == chat) {
1209 priv->current_chat = chat;
1210 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1212 chat_window_update_chat_tab (chat);
1216 chat_window_page_added_cb (GtkNotebook *notebook,
1219 EmpathyChatWindow *window)
1221 EmpathyChatWindowPriv *priv;
1224 priv = GET_PRIV (window);
1226 /* If we just received DND to the same window, we don't want
1227 * to do anything here like removing the tab and then readding
1228 * it, so we return here and in "page-added".
1230 if (priv->dnd_same_window) {
1231 DEBUG ("Page added (back to the same window)");
1232 priv->dnd_same_window = FALSE;
1236 DEBUG ("Page added");
1238 /* Get chat object */
1239 chat = EMPATHY_CHAT (child);
1241 /* Connect chat signals for this window */
1242 g_signal_connect (chat, "composing",
1243 G_CALLBACK (chat_window_composing_cb),
1245 g_signal_connect (chat, "new-message",
1246 G_CALLBACK (chat_window_new_message_cb),
1248 g_signal_connect (chat, "notify::tp-chat",
1249 G_CALLBACK (chat_window_update_chat_tab),
1252 /* Set flag so we know to perform some special operations on
1253 * switch page due to the new page being added.
1255 priv->page_added = TRUE;
1257 /* Get list of chats up to date */
1258 priv->chats = g_list_append (priv->chats, chat);
1260 chat_window_update_chat_tab (chat);
1264 chat_window_page_removed_cb (GtkNotebook *notebook,
1267 EmpathyChatWindow *window)
1269 EmpathyChatWindowPriv *priv;
1272 priv = GET_PRIV (window);
1274 /* If we just received DND to the same window, we don't want
1275 * to do anything here like removing the tab and then readding
1276 * it, so we return here and in "page-added".
1278 if (priv->dnd_same_window) {
1279 DEBUG ("Page removed (and will be readded to same window)");
1283 DEBUG ("Page removed");
1285 /* Get chat object */
1286 chat = EMPATHY_CHAT (child);
1288 /* Disconnect all signal handlers for this chat and this window */
1289 g_signal_handlers_disconnect_by_func (chat,
1290 G_CALLBACK (chat_window_composing_cb),
1292 g_signal_handlers_disconnect_by_func (chat,
1293 G_CALLBACK (chat_window_new_message_cb),
1295 g_signal_handlers_disconnect_by_func (chat,
1296 G_CALLBACK (chat_window_update_chat_tab),
1299 /* Keep list of chats up to date */
1300 priv->chats = g_list_remove (priv->chats, chat);
1301 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1302 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1304 if (priv->chats == NULL) {
1305 g_object_unref (window);
1307 chat_window_update (window);
1312 chat_window_focus_in_event_cb (GtkWidget *widget,
1314 EmpathyChatWindow *window)
1316 EmpathyChatWindowPriv *priv;
1318 DEBUG ("Focus in event, updating title");
1320 priv = GET_PRIV (window);
1322 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1324 chat_window_set_urgency_hint (window, FALSE);
1326 /* Update the title, since we now mark all unread messages as read. */
1327 chat_window_update_chat_tab (priv->current_chat);
1333 chat_window_drag_motion (GtkWidget *widget,
1334 GdkDragContext *context,
1338 EmpathyChatWindow *window)
1341 EmpathyChatWindowPriv *priv;
1343 priv = GET_PRIV (window);
1345 target = gtk_drag_dest_find_target (widget, context, priv->move_targets);
1346 /* If target != GDK_NONE, this target type is a type we should move
1347 instead of copy. That's a notebook tab. Other drag types, such
1348 as files or contacts, use copy.
1350 if (target == GDK_NONE) {
1351 gdk_drag_status (context, GDK_ACTION_COPY, time);
1354 gdk_drag_status (context, GDK_ACTION_MOVE, time);
1359 chat_window_drag_data_received (GtkWidget *widget,
1360 GdkDragContext *context,
1363 GtkSelectionData *selection,
1366 EmpathyChatWindow *window)
1368 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1369 EmpathyChat *chat = NULL;
1370 EmpathyChatWindow *old_window;
1371 TpAccount *account = NULL;
1372 TpAccountManager *account_manager;
1375 const gchar *account_id;
1376 const gchar *contact_id;
1378 id = (const gchar*) gtk_selection_data_get_data (selection);
1380 /* FIXME: Perhaps should be sure that the account manager is
1381 * prepared before calling _ensure_account on it. */
1382 account_manager = tp_account_manager_dup ();
1384 DEBUG ("DND contact from roster with id:'%s'", id);
1386 strv = g_strsplit (id, ":", 2);
1387 if (g_strv_length (strv) == 2) {
1388 account_id = strv[0];
1389 contact_id = strv[1];
1391 tp_account_manager_ensure_account (account_manager, account_id);
1392 if (account != NULL)
1393 chat = empathy_chat_window_find_chat (account, contact_id);
1396 if (account == NULL) {
1398 gtk_drag_finish (context, FALSE, FALSE, time_);
1403 TpConnection *connection;
1405 connection = tp_account_get_connection (account);
1408 empathy_dispatcher_chat_with_contact_id (
1409 connection, contact_id, NULL, NULL);
1415 g_object_unref (account_manager);
1418 old_window = chat_window_find_chat (chat);
1420 if (old_window == window) {
1421 gtk_drag_finish (context, TRUE, FALSE, time_);
1425 empathy_chat_window_move_chat (old_window, window, chat);
1427 empathy_chat_window_add_chat (window, chat);
1430 /* Added to take care of any outstanding chat events */
1431 empathy_chat_window_present_chat (chat);
1433 /* We should return TRUE to remove the data when doing
1434 * GDK_ACTION_MOVE, but we don't here otherwise it has
1435 * weird consequences, and we handle that internally
1436 * anyway with add_chat () and remove_chat ().
1438 gtk_drag_finish (context, TRUE, FALSE, time_);
1440 else if (info == DND_DRAG_TYPE_URI_LIST) {
1441 EmpathyChatWindowPriv *priv;
1442 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 data = (const gchar *) gtk_selection_data_get_data (selection);
1454 empathy_send_file_from_uri_list (contact, data);
1456 gtk_drag_finish (context, TRUE, FALSE, time);
1458 else if (info == DND_DRAG_TYPE_TAB) {
1460 EmpathyChatWindow *old_window = NULL;
1464 chat = (void *) gtk_selection_data_get_data (selection);
1465 old_window = chat_window_find_chat (*chat);
1468 EmpathyChatWindowPriv *priv;
1470 priv = GET_PRIV (window);
1472 if (old_window == window) {
1473 DEBUG ("DND tab (within same window)");
1474 priv->dnd_same_window = TRUE;
1475 gtk_drag_finish (context, TRUE, FALSE, time_);
1479 priv->dnd_same_window = FALSE;
1482 /* We should return TRUE to remove the data when doing
1483 * GDK_ACTION_MOVE, but we don't here otherwise it has
1484 * weird consequences, and we handle that internally
1485 * anyway with add_chat () and remove_chat ().
1487 gtk_drag_finish (context, TRUE, FALSE, time_);
1489 DEBUG ("DND from unknown source");
1490 gtk_drag_finish (context, FALSE, FALSE, time_);
1495 chat_window_finalize (GObject *object)
1497 EmpathyChatWindow *window;
1498 EmpathyChatWindowPriv *priv;
1500 window = EMPATHY_CHAT_WINDOW (object);
1501 priv = GET_PRIV (window);
1503 DEBUG ("Finalized: %p", object);
1505 g_object_unref (priv->ui_manager);
1506 g_object_unref (priv->chatroom_manager);
1507 g_object_unref (priv->notify_mgr);
1509 if (priv->notification != NULL) {
1510 notify_notification_close (priv->notification, NULL);
1511 g_object_unref (priv->notification);
1512 priv->notification = NULL;
1513 if (priv->notification_data != NULL)
1515 free_notification_data (priv->notification_data);
1516 priv->notification_data = NULL;
1520 if (priv->move_targets) {
1521 gtk_target_list_unref (priv->move_targets);
1524 chat_windows = g_list_remove (chat_windows, window);
1525 gtk_widget_destroy (priv->dialog);
1527 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1531 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1533 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1535 object_class->finalize = chat_window_finalize;
1537 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1539 /* Set up a style for the close button with no focus padding. */
1540 gtk_rc_parse_string (
1541 "style \"empathy-close-button-style\"\n"
1543 " GtkWidget::focus-padding = 0\n"
1547 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1549 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1553 empathy_chat_window_init (EmpathyChatWindow *window)
1556 GtkAccelGroup *accel_group;
1561 GtkWidget *chat_vbox;
1563 EmpathySmileyManager *smiley_manager;
1564 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1565 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1567 window->priv = priv;
1568 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1569 gui = empathy_builder_get_file (filename,
1570 "chat_window", &priv->dialog,
1571 "chat_vbox", &chat_vbox,
1572 "ui_manager", &priv->ui_manager,
1573 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1574 "menu_conv_favorite", &priv->menu_conv_favorite,
1575 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1576 "menu_edit_cut", &priv->menu_edit_cut,
1577 "menu_edit_copy", &priv->menu_edit_copy,
1578 "menu_edit_paste", &priv->menu_edit_paste,
1579 "menu_tabs_next", &priv->menu_tabs_next,
1580 "menu_tabs_prev", &priv->menu_tabs_prev,
1581 "menu_tabs_left", &priv->menu_tabs_left,
1582 "menu_tabs_right", &priv->menu_tabs_right,
1583 "menu_tabs_detach", &priv->menu_tabs_detach,
1587 empathy_builder_connect (gui, window,
1588 "menu_conv", "activate", chat_window_conv_activate_cb,
1589 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1590 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1591 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1592 "menu_conv_close", "activate", chat_window_close_activate_cb,
1593 "menu_edit", "activate", chat_window_edit_activate_cb,
1594 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1595 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1596 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1597 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1598 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1599 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1600 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1601 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1602 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1603 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1606 g_object_ref (priv->ui_manager);
1607 g_object_unref (gui);
1609 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1611 priv->notebook = gtk_notebook_new ();
1612 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1613 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1614 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1615 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1616 gtk_widget_show (priv->notebook);
1619 accel_group = gtk_accel_group_new ();
1620 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1622 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1623 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1626 gtk_accel_group_connect (accel_group,
1633 g_object_unref (accel_group);
1635 /* Set up drag target lists */
1636 priv->move_targets = gtk_target_list_new (drag_types_dest_move,
1637 G_N_ELEMENTS (drag_types_dest_move));
1639 /* Set up smiley menu */
1640 smiley_manager = empathy_smiley_manager_dup_singleton ();
1641 submenu = empathy_smiley_menu_new (smiley_manager,
1642 chat_window_insert_smiley_activate_cb,
1644 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1645 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1646 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1647 g_object_unref (smiley_manager);
1649 /* Set up signals we can't do with ui file since we may need to
1650 * block/unblock them at some later stage.
1653 g_signal_connect (priv->dialog,
1655 G_CALLBACK (chat_window_delete_event_cb),
1657 g_signal_connect (priv->dialog,
1659 G_CALLBACK (chat_window_focus_in_event_cb),
1661 g_signal_connect_after (priv->notebook,
1663 G_CALLBACK (chat_window_page_switched_cb),
1665 g_signal_connect (priv->notebook,
1667 G_CALLBACK (chat_window_page_added_cb),
1669 g_signal_connect (priv->notebook,
1671 G_CALLBACK (chat_window_page_removed_cb),
1674 /* Set up drag and drop */
1675 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1676 GTK_DEST_DEFAULT_ALL,
1678 G_N_ELEMENTS (drag_types_dest),
1679 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1681 g_signal_connect (priv->notebook,
1683 G_CALLBACK (chat_window_drag_motion),
1685 g_signal_connect (priv->notebook,
1686 "drag-data-received",
1687 G_CALLBACK (chat_window_drag_data_received),
1690 chat_windows = g_list_prepend (chat_windows, window);
1692 /* Set up private details */
1694 priv->chats_new_msg = NULL;
1695 priv->chats_composing = NULL;
1696 priv->current_chat = NULL;
1698 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1702 empathy_chat_window_new (void)
1704 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1707 /* Returns the window to open a new tab in if there is only one window
1708 * visble, otherwise, returns NULL indicating that a new window should
1712 empathy_chat_window_get_default (void)
1715 gboolean separate_windows = TRUE;
1717 empathy_conf_get_bool (empathy_conf_get (),
1718 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1721 if (separate_windows) {
1722 /* Always create a new window */
1726 for (l = chat_windows; l; l = l->next) {
1727 EmpathyChatWindow *chat_window;
1730 chat_window = l->data;
1732 dialog = empathy_chat_window_get_dialog (chat_window);
1733 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1734 /* Found a visible window on this desktop */
1743 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1745 EmpathyChatWindowPriv *priv;
1747 g_return_val_if_fail (window != NULL, NULL);
1749 priv = GET_PRIV (window);
1751 return priv->dialog;
1755 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1758 EmpathyChatWindowPriv *priv;
1760 GtkWidget *popup_label;
1763 g_return_if_fail (window != NULL);
1764 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1766 priv = GET_PRIV (window);
1768 /* Reference the chat object */
1769 g_object_ref (chat);
1771 /* If this window has just been created, position it */
1772 if (priv->chats == NULL) {
1773 const gchar *name = "chat-window";
1774 gboolean separate_windows;
1776 empathy_conf_get_bool (empathy_conf_get (),
1777 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1780 if (separate_windows) {
1781 name = empathy_chat_get_id (chat);
1784 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
1787 child = GTK_WIDGET (chat);
1788 label = chat_window_create_label (window, chat, TRUE);
1789 popup_label = chat_window_create_label (window, chat, FALSE);
1790 gtk_widget_show (child);
1792 g_signal_connect (chat, "notify::name",
1793 G_CALLBACK (chat_window_chat_notify_cb),
1795 g_signal_connect (chat, "notify::subject",
1796 G_CALLBACK (chat_window_chat_notify_cb),
1798 g_signal_connect (chat, "notify::remote-contact",
1799 G_CALLBACK (chat_window_chat_notify_cb),
1801 chat_window_chat_notify_cb (chat);
1803 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1804 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1805 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1806 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1807 TRUE, TRUE, GTK_PACK_START);
1809 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1813 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1816 EmpathyChatWindowPriv *priv;
1818 EmpathyContact *remote_contact;
1820 g_return_if_fail (window != NULL);
1821 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1823 priv = GET_PRIV (window);
1825 g_signal_handlers_disconnect_by_func (chat,
1826 chat_window_chat_notify_cb,
1828 remote_contact = g_object_get_data (G_OBJECT (chat),
1829 "chat-window-remote-contact");
1830 if (remote_contact) {
1831 g_signal_handlers_disconnect_by_func (remote_contact,
1832 chat_window_update_chat_tab,
1836 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1838 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1840 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1842 g_object_unref (chat);
1846 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1847 EmpathyChatWindow *new_window,
1852 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1853 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1854 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1856 widget = GTK_WIDGET (chat);
1858 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1859 G_OBJECT (widget)->ref_count);
1861 /* We reference here to make sure we don't loose the widget
1862 * and the EmpathyChat object during the move.
1864 g_object_ref (chat);
1865 g_object_ref (widget);
1867 empathy_chat_window_remove_chat (old_window, chat);
1868 empathy_chat_window_add_chat (new_window, chat);
1870 g_object_unref (widget);
1871 g_object_unref (chat);
1875 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1878 EmpathyChatWindowPriv *priv;
1881 g_return_if_fail (window != NULL);
1882 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1884 priv = GET_PRIV (window);
1886 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1888 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1893 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1895 EmpathyChatWindowPriv *priv;
1898 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1900 priv = GET_PRIV (window);
1902 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1908 empathy_chat_window_find_chat (TpAccount *account,
1913 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1915 for (l = chat_windows; l; l = l->next) {
1916 EmpathyChatWindowPriv *priv;
1917 EmpathyChatWindow *window;
1921 priv = GET_PRIV (window);
1923 for (ll = priv->chats; ll; ll = ll->next) {
1928 if (account == empathy_chat_get_account (chat) &&
1929 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1939 empathy_chat_window_present_chat (EmpathyChat *chat)
1941 EmpathyChatWindow *window;
1942 EmpathyChatWindowPriv *priv;
1944 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1946 window = chat_window_find_chat (chat);
1948 /* If the chat has no window, create one */
1949 if (window == NULL) {
1950 window = empathy_chat_window_get_default ();
1952 window = empathy_chat_window_new ();
1955 empathy_chat_window_add_chat (window, chat);
1958 priv = GET_PRIV (window);
1959 empathy_chat_window_switch_to_chat (window, chat);
1960 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1962 gtk_widget_grab_focus (chat->input_text_view);