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/telepathy-glib.h>
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-chatroom-manager.h>
43 #include <libempathy/empathy-utils.h>
44 #include <libempathy/empathy-tp-contact-factory.h>
45 #include <libempathy/empathy-contact-list.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"
59 #include "empathy-invite-participant-dialog.h"
61 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
62 #include <libempathy/empathy-debug.h>
65 EmpathyChatWindow *window;
69 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
71 EmpathyChat *current_chat;
74 GList *chats_composing;
76 gboolean dnd_same_window;
77 EmpathyChatroomManager *chatroom_manager;
78 EmpathyNotifyManager *notify_mgr;
81 NotifyNotification *notification;
82 NotificationData *notification_data;
84 GtkTargetList *contact_targets;
85 GtkTargetList *file_targets;
88 GtkUIManager *ui_manager;
89 GtkAction *menu_conv_insert_smiley;
90 GtkAction *menu_conv_favorite;
91 GtkAction *menu_conv_toggle_contacts;
93 GtkAction *menu_edit_cut;
94 GtkAction *menu_edit_copy;
95 GtkAction *menu_edit_paste;
97 GtkAction *menu_tabs_next;
98 GtkAction *menu_tabs_prev;
99 GtkAction *menu_tabs_left;
100 GtkAction *menu_tabs_right;
101 GtkAction *menu_tabs_detach;
102 } EmpathyChatWindowPriv;
104 static GList *chat_windows = NULL;
106 static const guint tab_accel_keys[] = {
107 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
108 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
112 DND_DRAG_TYPE_CONTACT_ID,
113 DND_DRAG_TYPE_URI_LIST,
117 static const GtkTargetEntry drag_types_dest[] = {
118 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
119 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
120 { "text/uri-list", 0, DND_DRAG_TYPE_URI_LIST },
123 static const GtkTargetEntry drag_types_dest_contact[] = {
124 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
127 static const GtkTargetEntry drag_types_dest_file[] = {
128 { "text/uri-list", 0, DND_DRAG_TYPE_URI_LIST },
131 static void chat_window_update (EmpathyChatWindow *window);
133 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
136 chat_window_accel_cb (GtkAccelGroup *accelgroup,
140 EmpathyChatWindow *window)
142 EmpathyChatWindowPriv *priv;
146 priv = GET_PRIV (window);
148 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
149 if (tab_accel_keys[i] == key) {
156 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
160 static EmpathyChatWindow *
161 chat_window_find_chat (EmpathyChat *chat)
163 EmpathyChatWindowPriv *priv;
166 for (l = chat_windows; l; l = l->next) {
167 priv = GET_PRIV (l->data);
168 ll = g_list_find (priv->chats, chat);
178 chat_window_close_clicked_cb (GtkAction *action,
181 EmpathyChatWindow *window;
183 window = chat_window_find_chat (chat);
184 empathy_chat_window_remove_chat (window, chat);
188 chat_tab_style_set_cb (GtkWidget *hbox,
189 GtkStyle *previous_style,
193 int char_width, h, w;
194 PangoContext *context;
195 PangoFontMetrics *metrics;
197 button = g_object_get_data (G_OBJECT (user_data),
198 "chat-window-tab-close-button");
199 context = gtk_widget_get_pango_context (hbox);
201 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
202 pango_context_get_language (context));
203 char_width = pango_font_metrics_get_approximate_char_width (metrics);
204 pango_font_metrics_unref (metrics);
206 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
207 GTK_ICON_SIZE_MENU, &w, &h);
209 /* Request at least about 12 chars width plus at least space for the status
210 * image and the close button */
211 gtk_widget_set_size_request (hbox,
212 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
214 gtk_widget_set_size_request (button, w, h);
218 chat_window_create_label (EmpathyChatWindow *window,
220 gboolean is_tab_label)
222 EmpathyChatWindowPriv *priv;
224 GtkWidget *name_label;
225 GtkWidget *status_image;
226 GtkWidget *close_button;
227 GtkWidget *close_image;
228 GtkWidget *event_box;
229 GtkWidget *event_box_hbox;
230 PangoAttrList *attr_list;
231 PangoAttribute *attr;
233 priv = GET_PRIV (window);
235 /* The spacing between the button and the label. */
236 hbox = gtk_hbox_new (FALSE, 0);
238 event_box = gtk_event_box_new ();
239 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
241 name_label = gtk_label_new (NULL);
243 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
245 attr_list = pango_attr_list_new ();
246 attr = pango_attr_scale_new (1/1.2);
247 attr->start_index = 0;
248 attr->end_index = -1;
249 pango_attr_list_insert (attr_list, attr);
250 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
251 pango_attr_list_unref (attr_list);
253 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
254 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
255 g_object_set_data_full (G_OBJECT (chat),
256 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
257 g_object_ref (name_label), g_object_unref);
259 status_image = gtk_image_new ();
261 /* Spacing between the icon and label. */
262 event_box_hbox = gtk_hbox_new (FALSE, 0);
264 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
265 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
267 g_object_set_data_full (G_OBJECT (chat),
268 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
269 g_object_ref (status_image), g_object_unref);
270 g_object_set_data_full (G_OBJECT (chat),
271 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
272 g_object_ref (event_box), g_object_unref);
274 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
275 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
278 close_button = gtk_button_new ();
279 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
280 g_object_set_data_full (G_OBJECT (chat), "chat-window-tab-close-button",
281 g_object_ref (close_button), g_object_unref);
283 /* We don't want focus/keynav for the button to avoid clutter, and
284 * Ctrl-W works anyway.
286 gtk_widget_set_can_focus (close_button, FALSE);
287 gtk_widget_set_can_default (close_button, FALSE);
289 /* Set the name to make the special rc style match. */
290 gtk_widget_set_name (close_button, "empathy-close-button");
292 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
294 gtk_container_add (GTK_CONTAINER (close_button), close_image);
296 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
298 g_signal_connect (close_button,
300 G_CALLBACK (chat_window_close_clicked_cb),
303 /* React to theme changes and also setup the size correctly. */
304 g_signal_connect (hbox,
306 G_CALLBACK (chat_tab_style_set_cb),
310 gtk_widget_show_all (hbox);
316 _submenu_notify_visible_changed_cb (GObject *object,
320 g_signal_handlers_disconnect_by_func (object,
321 _submenu_notify_visible_changed_cb,
323 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
327 chat_window_menu_context_update (EmpathyChatWindowPriv *priv,
332 gboolean is_connected;
335 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
336 first_page = (page_num == 0);
337 last_page = (page_num == (num_pages - 1));
338 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
340 DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
342 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
343 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
344 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
345 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
346 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
347 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
351 chat_window_conversation_menu_update (EmpathyChatWindowPriv *priv,
352 EmpathyChatWindow *self)
354 EmpathyTpChat *tp_chat;
355 TpConnection *connection;
357 gboolean sensitive = FALSE;
359 g_return_if_fail (priv->current_chat != NULL);
361 action = gtk_ui_manager_get_action (priv->ui_manager,
362 "/chats_menubar/menu_conv/menu_conv_invite_participant");
363 tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
365 if (tp_chat != NULL) {
366 connection = empathy_tp_chat_get_connection (tp_chat);
368 sensitive = empathy_tp_chat_can_add_contact (tp_chat) &&
369 (tp_connection_get_status (connection, NULL) ==
370 TP_CONNECTION_STATUS_CONNECTED);
373 gtk_action_set_sensitive (action, sensitive);
377 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
378 EmpathyChatWindow *window)
380 GtkWidget *menu, *submenu, *orig_submenu;
382 menu = gtk_ui_manager_get_widget (priv->ui_manager,
383 "/chats_menubar/menu_contact");
384 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
386 DEBUG ("Update window : Contact Menu");
388 if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
389 submenu = empathy_chat_get_contact_menu (priv->current_chat);
390 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
391 gtk_widget_show (menu);
393 empathy_signal_connect_weak (orig_submenu,
395 (GCallback)_submenu_notify_visible_changed_cb,
401 get_all_unread_messages (EmpathyChatWindowPriv *priv)
406 for (l = priv->chats_new_msg; l != NULL; l = g_list_next (l)) {
407 EmpathyChat *chat = l->data;
409 nb += empathy_chat_get_nb_unread_messages (chat);
416 get_window_title_name (EmpathyChatWindowPriv *priv)
418 const gchar *active_name;
420 guint current_unread_msgs;
422 nb_chats = g_list_length (priv->chats);
423 g_assert (nb_chats > 0);
425 active_name = empathy_chat_get_name (priv->current_chat);
427 current_unread_msgs = empathy_chat_get_nb_unread_messages (
432 if (current_unread_msgs == 0)
433 return g_strdup (active_name);
435 return g_strdup_printf (ngettext (
437 "%s (%d unread)", current_unread_msgs),
438 active_name, current_unread_msgs);
440 guint nb_others = nb_chats - 1;
441 guint all_unread_msgs;
443 all_unread_msgs = get_all_unread_messages (priv);
445 if (all_unread_msgs == 0) {
446 /* no unread message */
447 return g_strdup_printf (ngettext (
449 "%s (and %u others)", nb_others),
450 active_name, nb_others);
453 else if (all_unread_msgs == current_unread_msgs) {
454 /* unread messages are in the current tab */
455 return g_strdup_printf (ngettext (
457 "%s (%d unread)", current_unread_msgs),
458 active_name, current_unread_msgs);
461 else if (current_unread_msgs == 0) {
462 /* unread messages are in other tabs */
463 return g_strdup_printf (ngettext (
464 "%s (%d unread from others)",
465 "%s (%d unread from others)",
467 active_name, all_unread_msgs);
471 /* unread messages are in all the tabs */
472 return g_strdup_printf (ngettext (
473 "%s (%d unread from all)",
474 "%s (%d unread from all)",
476 active_name, all_unread_msgs);
482 chat_window_title_update (EmpathyChatWindowPriv *priv)
486 DEBUG ("Update window : Title");
488 name = get_window_title_name (priv);
489 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
494 chat_window_icon_update (EmpathyChatWindowPriv *priv)
497 EmpathyContact *remote_contact;
498 gboolean avatar_in_icon;
501 n_chats = g_list_length (priv->chats);
503 DEBUG ("Update window : Icon");
505 /* Update window icon */
506 if (priv->chats_new_msg) {
507 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
508 EMPATHY_IMAGE_MESSAGE);
510 empathy_conf_get_bool (empathy_conf_get (),
511 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
514 if (n_chats == 1 && avatar_in_icon) {
515 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
516 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
517 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
520 g_object_unref (icon);
523 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
529 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
533 GtkWidget *chat_close_button;
536 DEBUG ("Update window : Close Button");
538 if (num_pages == 1) {
539 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
540 chat_close_button = g_object_get_data (G_OBJECT (chat),
541 "chat-window-tab-close-button");
542 gtk_widget_hide (chat_close_button);
544 for (i=0; i<num_pages; i++) {
545 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
546 chat_close_button = g_object_get_data (G_OBJECT (chat),
547 "chat-window-tab-close-button");
548 gtk_widget_show (chat_close_button);
554 chat_window_update (EmpathyChatWindow *window)
556 EmpathyChatWindowPriv *priv = GET_PRIV (window);
559 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
561 DEBUG ("Update window");
563 /* Update Tab menu */
564 chat_window_menu_context_update (priv,
567 chat_window_conversation_menu_update (priv, window);
569 chat_window_contact_menu_update (priv,
572 chat_window_title_update (priv);
574 chat_window_icon_update (priv);
576 chat_window_close_button_update (priv,
581 append_markup_printf (GString *string,
588 va_start (args, format);
590 tmp = g_markup_vprintf_escaped (format, args);
591 g_string_append (string, tmp);
598 chat_window_update_chat_tab (EmpathyChat *chat)
600 EmpathyChatWindow *window;
601 EmpathyChatWindowPriv *priv;
602 EmpathyContact *remote_contact;
606 const gchar *subject;
607 const gchar *status = NULL;
611 const gchar *icon_name;
612 GtkWidget *tab_image;
613 GtkWidget *menu_image;
615 window = chat_window_find_chat (chat);
619 priv = GET_PRIV (window);
621 /* Get information */
622 name = empathy_chat_get_name (chat);
623 account = empathy_chat_get_account (chat);
624 subject = empathy_chat_get_subject (chat);
625 remote_contact = empathy_chat_get_remote_contact (chat);
627 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
628 name, tp_proxy_get_object_path (account), subject, remote_contact);
630 /* Update tab image */
631 if (empathy_chat_get_tp_chat (chat) == NULL) {
632 /* No TpChat, we are disconnected */
635 else if (g_list_find (priv->chats_new_msg, chat)) {
636 icon_name = EMPATHY_IMAGE_MESSAGE;
638 else if (g_list_find (priv->chats_composing, chat)) {
639 icon_name = EMPATHY_IMAGE_TYPING;
641 else if (remote_contact) {
642 icon_name = empathy_icon_name_for_contact (remote_contact);
644 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
647 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
648 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
649 if (icon_name != NULL) {
650 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
651 gtk_widget_show (tab_image);
652 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
653 gtk_widget_show (menu_image);
655 gtk_widget_hide (tab_image);
656 gtk_widget_hide (menu_image);
659 /* Update tab tooltip */
660 tooltip = g_string_new (NULL);
662 if (remote_contact) {
663 id = empathy_contact_get_id (remote_contact);
664 status = empathy_contact_get_presence_message (remote_contact);
669 append_markup_printf (tooltip,
670 "<b>%s</b><small> (%s)</small>",
672 tp_account_get_display_name (account));
674 if (!EMP_STR_EMPTY (status)) {
675 append_markup_printf (tooltip, "\n<i>%s</i>", status);
679 append_markup_printf (tooltip, "\n<b>%s</b> %s",
680 _("Topic:"), subject);
683 if (g_list_find (priv->chats_composing, chat)) {
684 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
687 markup = g_string_free (tooltip, FALSE);
688 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
689 gtk_widget_set_tooltip_markup (widget, markup);
690 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
691 gtk_widget_set_tooltip_markup (widget, markup);
694 /* Update tab and menu label */
695 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
696 gtk_label_set_text (GTK_LABEL (widget), name);
697 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
698 gtk_label_set_text (GTK_LABEL (widget), name);
700 /* Update the window if it's the current chat */
701 if (priv->current_chat == chat) {
702 chat_window_update (window);
707 chat_window_chat_notify_cb (EmpathyChat *chat)
709 EmpathyContact *old_remote_contact;
710 EmpathyContact *remote_contact = NULL;
712 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
713 remote_contact = empathy_chat_get_remote_contact (chat);
715 if (old_remote_contact != remote_contact) {
716 /* The remote-contact associated with the chat changed, we need
717 * to keep track of any change of that contact and update the
718 * window each time. */
719 if (remote_contact) {
720 g_signal_connect_swapped (remote_contact, "notify",
721 G_CALLBACK (chat_window_update_chat_tab),
724 if (old_remote_contact) {
725 g_signal_handlers_disconnect_by_func (old_remote_contact,
726 chat_window_update_chat_tab,
730 g_object_set_data_full (G_OBJECT (chat), "chat-window-remote-contact",
731 g_object_ref (remote_contact), (GDestroyNotify) g_object_unref);
734 chat_window_update_chat_tab (chat);
738 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
739 EmpathySmiley *smiley,
742 EmpathyChatWindowPriv *priv = GET_PRIV (window);
744 GtkTextBuffer *buffer;
747 chat = priv->current_chat;
749 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
750 gtk_text_buffer_get_end_iter (buffer, &iter);
751 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
755 chat_window_conv_activate_cb (GtkAction *action,
756 EmpathyChatWindow *window)
758 EmpathyChatWindowPriv *priv = GET_PRIV (window);
761 EmpathyContact *remote_contact = NULL;
763 /* Favorite room menu */
764 is_room = empathy_chat_is_room (priv->current_chat);
768 gboolean found = FALSE;
769 EmpathyChatroom *chatroom;
771 room = empathy_chat_get_id (priv->current_chat);
772 account = empathy_chat_get_account (priv->current_chat);
773 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
775 if (chatroom != NULL)
776 found = empathy_chatroom_is_favorite (chatroom);
778 DEBUG ("This room %s favorite", found ? "is" : "is not");
779 gtk_toggle_action_set_active (
780 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
782 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
784 /* Show contacts menu */
785 g_object_get (priv->current_chat,
786 "remote-contact", &remote_contact,
787 "show-contacts", &active,
789 if (remote_contact == NULL) {
790 gtk_toggle_action_set_active (
791 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
794 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
795 (remote_contact == NULL));
796 if (remote_contact != NULL) {
797 g_object_unref (remote_contact);
802 chat_window_clear_activate_cb (GtkAction *action,
803 EmpathyChatWindow *window)
805 EmpathyChatWindowPriv *priv = GET_PRIV (window);
807 empathy_chat_clear (priv->current_chat);
811 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
812 EmpathyChatWindow *window)
814 EmpathyChatWindowPriv *priv = GET_PRIV (window);
818 EmpathyChatroom *chatroom;
820 active = gtk_toggle_action_get_active (toggle_action);
821 account = empathy_chat_get_account (priv->current_chat);
822 room = empathy_chat_get_id (priv->current_chat);
824 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
827 if (chatroom == NULL) {
830 name = empathy_chat_get_name (priv->current_chat);
831 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
832 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
833 g_object_unref (chatroom);
836 empathy_chatroom_set_favorite (chatroom, active);
840 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
841 EmpathyChatWindow *window)
843 EmpathyChatWindowPriv *priv = GET_PRIV (window);
846 active = gtk_toggle_action_get_active (toggle_action);
848 empathy_chat_set_show_contacts (priv->current_chat, active);
852 got_contact_cb (EmpathyTpContactFactory *factory,
853 EmpathyContact *contact,
858 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (user_data);
861 DEBUG ("Failed: %s", error->message);
864 empathy_contact_list_add (EMPATHY_CONTACT_LIST (tp_chat),
865 contact, _("Inviting you to this room"));
870 chat_window_invite_participant_activate_cb (GtkAction *action,
871 EmpathyChatWindow *window)
873 EmpathyChatWindowPriv *priv;
875 EmpathyTpChat *tp_chat;
879 priv = GET_PRIV (window);
881 g_return_if_fail (priv->current_chat != NULL);
883 tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
884 channel = empathy_tp_chat_get_channel (tp_chat);
886 dialog = empathy_invite_participant_dialog_new (
887 GTK_WINDOW (priv->dialog));
888 gtk_widget_show (dialog);
890 response = gtk_dialog_run (GTK_DIALOG (dialog));
892 if (response == GTK_RESPONSE_ACCEPT) {
893 TpConnection *connection;
894 EmpathyTpContactFactory *factory;
897 id = empathy_contact_selector_dialog_get_selected (
898 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL);
899 if (EMP_STR_EMPTY (id)) goto out;
901 connection = tp_channel_borrow_connection (channel);
902 factory = empathy_tp_contact_factory_dup_singleton (connection);
904 empathy_tp_contact_factory_get_from_id (factory, id,
905 got_contact_cb, tp_chat, NULL, NULL);
907 g_object_unref (factory);
911 gtk_widget_destroy (dialog);
915 chat_window_close_activate_cb (GtkAction *action,
916 EmpathyChatWindow *window)
918 EmpathyChatWindowPriv *priv;
920 priv = GET_PRIV (window);
922 g_return_if_fail (priv->current_chat != NULL);
924 empathy_chat_window_remove_chat (window, priv->current_chat);
928 chat_window_edit_activate_cb (GtkAction *action,
929 EmpathyChatWindow *window)
931 EmpathyChatWindowPriv *priv;
932 GtkClipboard *clipboard;
933 GtkTextBuffer *buffer;
934 gboolean text_available;
936 priv = GET_PRIV (window);
938 g_return_if_fail (priv->current_chat != NULL);
940 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
941 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
942 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
943 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
947 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
948 if (gtk_text_buffer_get_has_selection (buffer)) {
949 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
950 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
954 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
956 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
957 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
960 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
961 text_available = gtk_clipboard_wait_is_text_available (clipboard);
962 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
966 chat_window_cut_activate_cb (GtkAction *action,
967 EmpathyChatWindow *window)
969 EmpathyChatWindowPriv *priv;
971 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
973 priv = GET_PRIV (window);
975 empathy_chat_cut (priv->current_chat);
979 chat_window_copy_activate_cb (GtkAction *action,
980 EmpathyChatWindow *window)
982 EmpathyChatWindowPriv *priv;
984 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
986 priv = GET_PRIV (window);
988 empathy_chat_copy (priv->current_chat);
992 chat_window_paste_activate_cb (GtkAction *action,
993 EmpathyChatWindow *window)
995 EmpathyChatWindowPriv *priv;
997 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
999 priv = GET_PRIV (window);
1001 empathy_chat_paste (priv->current_chat);
1005 chat_window_tabs_next_activate_cb (GtkAction *action,
1006 EmpathyChatWindow *window)
1008 EmpathyChatWindowPriv *priv;
1010 gint index_, numPages;
1012 priv = GET_PRIV (window);
1014 chat = priv->current_chat;
1015 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1016 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1018 if (index_ == (numPages - 1)) {
1019 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
1023 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
1027 chat_window_tabs_previous_activate_cb (GtkAction *action,
1028 EmpathyChatWindow *window)
1030 EmpathyChatWindowPriv *priv;
1032 gint index_, numPages;
1034 priv = GET_PRIV (window);
1036 chat = priv->current_chat;
1037 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1038 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1041 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
1045 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
1049 chat_window_tabs_left_activate_cb (GtkAction *action,
1050 EmpathyChatWindow *window)
1052 EmpathyChatWindowPriv *priv;
1056 priv = GET_PRIV (window);
1058 chat = priv->current_chat;
1059 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1064 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1070 chat_window_tabs_right_activate_cb (GtkAction *action,
1071 EmpathyChatWindow *window)
1073 EmpathyChatWindowPriv *priv;
1077 priv = GET_PRIV (window);
1079 chat = priv->current_chat;
1080 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1082 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1088 chat_window_detach_activate_cb (GtkAction *action,
1089 EmpathyChatWindow *window)
1091 EmpathyChatWindowPriv *priv;
1092 EmpathyChatWindow *new_window;
1095 priv = GET_PRIV (window);
1097 chat = priv->current_chat;
1098 new_window = empathy_chat_window_new ();
1100 empathy_chat_window_move_chat (window, new_window, chat);
1102 priv = GET_PRIV (new_window);
1103 gtk_widget_show (priv->dialog);
1107 chat_window_help_contents_activate_cb (GtkAction *action,
1108 EmpathyChatWindow *window)
1110 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1112 empathy_url_show (priv->dialog, "ghelp:empathy");
1116 chat_window_help_about_activate_cb (GtkAction *action,
1117 EmpathyChatWindow *window)
1119 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1121 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1125 chat_window_delete_event_cb (GtkWidget *dialog,
1127 EmpathyChatWindow *window)
1129 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1131 DEBUG ("Delete event received");
1133 g_object_ref (window);
1134 while (priv->chats) {
1135 empathy_chat_window_remove_chat (window, priv->chats->data);
1137 g_object_unref (window);
1143 chat_window_composing_cb (EmpathyChat *chat,
1144 gboolean is_composing,
1145 EmpathyChatWindow *window)
1147 EmpathyChatWindowPriv *priv;
1149 priv = GET_PRIV (window);
1151 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1152 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1154 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1157 chat_window_update_chat_tab (chat);
1161 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1164 EmpathyChatWindowPriv *priv;
1166 priv = GET_PRIV (window);
1168 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
1169 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1173 free_notification_data (NotificationData *data)
1175 g_object_unref (data->chat);
1176 g_slice_free (NotificationData, data);
1180 chat_window_notification_closed_cb (NotifyNotification *notify,
1181 NotificationData *cb_data)
1183 EmpathyNotificationClosedReason reason = 0;
1184 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1186 #ifdef notify_notification_get_closed_reason
1187 reason = notify_notification_get_closed_reason (notify);
1189 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1190 empathy_chat_window_present_chat (cb_data->chat);
1193 g_object_unref (notify);
1194 priv->notification = NULL;
1195 free_notification_data (cb_data);
1196 priv->notification_data = NULL;
1200 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1201 EmpathyMessage *message,
1204 EmpathyContact *sender;
1205 const gchar *header;
1209 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1212 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1215 empathy_conf_get_bool (empathy_conf_get (),
1216 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1222 sender = empathy_message_get_sender (message);
1223 header = empathy_contact_get_name (sender);
1224 body = empathy_message_get_body (message);
1225 escaped = g_markup_escape_text (body, -1);
1227 if (priv->notification != NULL) {
1228 notify_notification_update (priv->notification,
1229 header, escaped, NULL);
1231 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1233 cb_data->chat = g_object_ref (chat);
1234 cb_data->window = window;
1236 priv->notification_data = cb_data;
1237 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1238 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1240 g_signal_connect (priv->notification, "closed",
1241 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1244 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1245 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1247 if (pixbuf != NULL) {
1248 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1249 g_object_unref (pixbuf);
1252 notify_notification_show (priv->notification, NULL);
1258 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1263 if (!empathy_chat_is_room (chat))
1266 markup = g_markup_printf_escaped (
1267 "<span color=\"red\" weight=\"bold\">%s</span>",
1268 empathy_chat_get_name (chat));
1270 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1271 gtk_label_set_markup (GTK_LABEL (widget), markup);
1276 chat_window_new_message_cb (EmpathyChat *chat,
1277 EmpathyMessage *message,
1278 EmpathyChatWindow *window)
1280 EmpathyChatWindowPriv *priv;
1282 gboolean needs_urgency;
1283 EmpathyContact *sender;
1285 priv = GET_PRIV (window);
1287 has_focus = empathy_chat_window_has_focus (window);
1289 /* - if we're the sender, we play the sound if it's specified in the
1290 * preferences and we're not away.
1291 * - if we receive a message, we play the sound if it's specified in the
1292 * preferences and the window does not have focus on the chat receiving
1296 sender = empathy_message_get_sender (message);
1298 if (empathy_contact_is_user (sender)) {
1299 empathy_sound_play (GTK_WIDGET (priv->dialog),
1300 EMPATHY_SOUND_MESSAGE_OUTGOING);
1303 if (has_focus && priv->current_chat == chat) {
1304 /* window and tab are focused so consider the message to be read */
1305 empathy_chat_messages_read (chat);
1309 if (!g_list_find (priv->chats_new_msg, chat)) {
1310 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1311 chat_window_update_chat_tab (chat);
1314 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1315 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1316 * an unamed MUC (msn-like).
1317 * In case of a MUC, we set urgency only if the message contains our
1319 if (empathy_chat_is_room (chat) ||
1320 empathy_chat_get_remote_contact (chat) == NULL) {
1321 needs_urgency = empathy_message_should_highlight (message);
1323 needs_urgency = TRUE;
1326 if (needs_urgency) {
1328 chat_window_set_urgency_hint (window, TRUE);
1329 chat_window_set_highlight_room_tab_label (chat);
1332 empathy_sound_play (GTK_WIDGET (priv->dialog),
1333 EMPATHY_SOUND_MESSAGE_INCOMING);
1334 chat_window_show_or_update_notification (window, message, chat);
1337 /* update the number of unread messages */
1338 chat_window_title_update (priv);
1341 static GtkNotebook *
1342 chat_window_detach_hook (GtkNotebook *source,
1348 EmpathyChatWindowPriv *priv;
1349 EmpathyChatWindow *window, *new_window;
1352 chat = EMPATHY_CHAT (page);
1353 window = chat_window_find_chat (chat);
1355 new_window = empathy_chat_window_new ();
1356 priv = GET_PRIV (new_window);
1358 DEBUG ("Detach hook called");
1360 empathy_chat_window_move_chat (window, new_window, chat);
1362 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1363 gtk_widget_show (priv->dialog);
1369 chat_window_page_switched_cb (GtkNotebook *notebook,
1370 GtkNotebookPage *page,
1372 EmpathyChatWindow *window)
1374 EmpathyChatWindowPriv *priv;
1378 DEBUG ("Page switched");
1380 priv = GET_PRIV (window);
1382 child = gtk_notebook_get_nth_page (notebook, page_num);
1383 chat = EMPATHY_CHAT (child);
1385 if (priv->page_added) {
1386 priv->page_added = FALSE;
1387 empathy_chat_scroll_down (chat);
1389 else if (priv->current_chat == chat) {
1393 priv->current_chat = chat;
1394 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1395 empathy_chat_messages_read (chat);
1397 chat_window_update_chat_tab (chat);
1401 chat_window_page_added_cb (GtkNotebook *notebook,
1404 EmpathyChatWindow *window)
1406 EmpathyChatWindowPriv *priv;
1409 priv = GET_PRIV (window);
1411 /* If we just received DND to the same window, we don't want
1412 * to do anything here like removing the tab and then readding
1413 * it, so we return here and in "page-added".
1415 if (priv->dnd_same_window) {
1416 DEBUG ("Page added (back to the same window)");
1417 priv->dnd_same_window = FALSE;
1421 DEBUG ("Page added");
1423 /* Get chat object */
1424 chat = EMPATHY_CHAT (child);
1426 /* Connect chat signals for this window */
1427 g_signal_connect (chat, "composing",
1428 G_CALLBACK (chat_window_composing_cb),
1430 g_signal_connect (chat, "new-message",
1431 G_CALLBACK (chat_window_new_message_cb),
1433 g_signal_connect (chat, "notify::tp-chat",
1434 G_CALLBACK (chat_window_update_chat_tab),
1437 /* Set flag so we know to perform some special operations on
1438 * switch page due to the new page being added.
1440 priv->page_added = TRUE;
1442 /* Get list of chats up to date */
1443 priv->chats = g_list_append (priv->chats, chat);
1445 chat_window_update_chat_tab (chat);
1449 chat_window_page_removed_cb (GtkNotebook *notebook,
1452 EmpathyChatWindow *window)
1454 EmpathyChatWindowPriv *priv;
1457 priv = GET_PRIV (window);
1459 /* If we just received DND to the same window, we don't want
1460 * to do anything here like removing the tab and then readding
1461 * it, so we return here and in "page-added".
1463 if (priv->dnd_same_window) {
1464 DEBUG ("Page removed (and will be readded to same window)");
1468 DEBUG ("Page removed");
1470 /* Get chat object */
1471 chat = EMPATHY_CHAT (child);
1473 /* Disconnect all signal handlers for this chat and this window */
1474 g_signal_handlers_disconnect_by_func (chat,
1475 G_CALLBACK (chat_window_composing_cb),
1477 g_signal_handlers_disconnect_by_func (chat,
1478 G_CALLBACK (chat_window_new_message_cb),
1480 g_signal_handlers_disconnect_by_func (chat,
1481 G_CALLBACK (chat_window_update_chat_tab),
1484 /* Keep list of chats up to date */
1485 priv->chats = g_list_remove (priv->chats, chat);
1486 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1487 empathy_chat_messages_read (chat);
1488 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1490 if (priv->chats == NULL) {
1491 g_object_unref (window);
1493 chat_window_update (window);
1498 chat_window_focus_in_event_cb (GtkWidget *widget,
1500 EmpathyChatWindow *window)
1502 EmpathyChatWindowPriv *priv;
1504 DEBUG ("Focus in event, updating title");
1506 priv = GET_PRIV (window);
1508 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1509 empathy_chat_messages_read (priv->current_chat);
1511 chat_window_set_urgency_hint (window, FALSE);
1513 /* Update the title, since we now mark all unread messages as read. */
1514 chat_window_update_chat_tab (priv->current_chat);
1520 chat_window_drag_motion (GtkWidget *widget,
1521 GdkDragContext *context,
1525 EmpathyChatWindow *window)
1528 EmpathyChatWindowPriv *priv;
1529 GdkAtom dest_target;
1531 priv = GET_PRIV (window);
1533 target = gtk_drag_dest_find_target (widget, context, NULL);
1535 dest_target = gdk_atom_intern_static_string ("text/uri-list");
1536 if (target == dest_target) {
1537 /* This is a file drag. Ensure the contact is online and set the
1538 drag type to COPY. Note that it's possible that the tab will
1539 be switched by GTK+ after a timeout from drag_motion without
1540 getting another drag_motion to disable the drop. You have
1541 to hold your mouse really still.
1543 EmpathyContact *contact;
1545 priv = GET_PRIV (window);
1546 contact = empathy_chat_get_remote_contact (priv->current_chat);
1547 /* contact is NULL for multi-user chats. We don't do
1548 * file transfers to MUCs. We also don't send files
1549 * to offline contacts or contacts that don't support
1552 if ((contact == NULL) || !empathy_contact_is_online (contact)) {
1553 gdk_drag_status (context, 0, time_);
1556 if (!(empathy_contact_get_capabilities (contact)
1557 & EMPATHY_CAPABILITIES_FT)) {
1558 gdk_drag_status (context, 0, time_);
1561 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1565 dest_target = gdk_atom_intern_static_string ("text/contact-id");
1566 if (target == dest_target) {
1567 /* This is a drag of a contact from a contact list. Set to COPY.
1568 FIXME: If this drag is to a MUC window, it invites the user.
1569 Otherwise, it opens a chat. Should we use a different drag
1570 type for invites? Should we allow ASK?
1572 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1576 /* Otherwise, it must be a notebook tab drag. Set to MOVE. */
1577 gdk_drag_status (context, GDK_ACTION_MOVE, time_);
1582 chat_window_drag_data_received (GtkWidget *widget,
1583 GdkDragContext *context,
1586 GtkSelectionData *selection,
1589 EmpathyChatWindow *window)
1591 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1592 EmpathyChat *chat = NULL;
1593 EmpathyChatWindow *old_window;
1594 TpAccount *account = NULL;
1595 TpAccountManager *account_manager;
1598 const gchar *account_id;
1599 const gchar *contact_id;
1601 id = (const gchar*) gtk_selection_data_get_data (selection);
1603 /* FIXME: Perhaps should be sure that the account manager is
1604 * prepared before calling _ensure_account on it. */
1605 account_manager = tp_account_manager_dup ();
1607 DEBUG ("DND contact from roster with id:'%s'", id);
1609 strv = g_strsplit (id, ":", 2);
1610 if (g_strv_length (strv) == 2) {
1611 account_id = strv[0];
1612 contact_id = strv[1];
1614 tp_account_manager_ensure_account (account_manager, account_id);
1615 if (account != NULL)
1616 chat = empathy_chat_window_find_chat (account, contact_id);
1619 if (account == NULL) {
1621 gtk_drag_finish (context, FALSE, FALSE, time_);
1626 TpConnection *connection;
1628 connection = tp_account_get_connection (account);
1631 empathy_dispatcher_chat_with_contact_id (
1632 connection, contact_id, NULL, NULL);
1638 g_object_unref (account_manager);
1641 old_window = chat_window_find_chat (chat);
1643 if (old_window == window) {
1644 gtk_drag_finish (context, TRUE, FALSE, time_);
1648 empathy_chat_window_move_chat (old_window, window, chat);
1650 empathy_chat_window_add_chat (window, chat);
1653 /* Added to take care of any outstanding chat events */
1654 empathy_chat_window_present_chat (chat);
1656 /* We should return TRUE to remove the data when doing
1657 * GDK_ACTION_MOVE, but we don't here otherwise it has
1658 * weird consequences, and we handle that internally
1659 * anyway with add_chat () and remove_chat ().
1661 gtk_drag_finish (context, TRUE, FALSE, time_);
1663 else if (info == DND_DRAG_TYPE_URI_LIST) {
1664 EmpathyChatWindowPriv *priv;
1665 EmpathyContact *contact;
1668 priv = GET_PRIV (window);
1669 contact = empathy_chat_get_remote_contact (priv->current_chat);
1671 /* contact is NULL when current_chat is a multi-user chat.
1672 * We don't do file transfers to MUCs, so just cancel the drag.
1674 if (contact == NULL) {
1675 gtk_drag_finish (context, TRUE, FALSE, time_);
1679 data = (const gchar *) gtk_selection_data_get_data (selection);
1680 empathy_send_file_from_uri_list (contact, data);
1682 gtk_drag_finish (context, TRUE, FALSE, time_);
1684 else if (info == DND_DRAG_TYPE_TAB) {
1686 EmpathyChatWindow *old_window = NULL;
1690 chat = (void *) gtk_selection_data_get_data (selection);
1691 old_window = chat_window_find_chat (*chat);
1694 EmpathyChatWindowPriv *priv;
1696 priv = GET_PRIV (window);
1698 if (old_window == window) {
1699 DEBUG ("DND tab (within same window)");
1700 priv->dnd_same_window = TRUE;
1701 gtk_drag_finish (context, TRUE, FALSE, time_);
1705 priv->dnd_same_window = FALSE;
1708 /* We should return TRUE to remove the data when doing
1709 * GDK_ACTION_MOVE, but we don't here otherwise it has
1710 * weird consequences, and we handle that internally
1711 * anyway with add_chat () and remove_chat ().
1713 gtk_drag_finish (context, TRUE, FALSE, time_);
1715 DEBUG ("DND from unknown source");
1716 gtk_drag_finish (context, FALSE, FALSE, time_);
1721 chat_window_finalize (GObject *object)
1723 EmpathyChatWindow *window;
1724 EmpathyChatWindowPriv *priv;
1726 window = EMPATHY_CHAT_WINDOW (object);
1727 priv = GET_PRIV (window);
1729 DEBUG ("Finalized: %p", object);
1731 g_object_unref (priv->ui_manager);
1732 g_object_unref (priv->chatroom_manager);
1733 g_object_unref (priv->notify_mgr);
1735 if (priv->notification != NULL) {
1736 notify_notification_close (priv->notification, NULL);
1737 g_object_unref (priv->notification);
1738 priv->notification = NULL;
1739 if (priv->notification_data != NULL)
1741 free_notification_data (priv->notification_data);
1742 priv->notification_data = NULL;
1746 if (priv->contact_targets) {
1747 gtk_target_list_unref (priv->contact_targets);
1749 if (priv->file_targets) {
1750 gtk_target_list_unref (priv->file_targets);
1753 chat_windows = g_list_remove (chat_windows, window);
1754 gtk_widget_destroy (priv->dialog);
1756 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1760 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1762 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1764 object_class->finalize = chat_window_finalize;
1766 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1768 /* Set up a style for the close button with no focus padding. */
1769 gtk_rc_parse_string (
1770 "style \"empathy-close-button-style\"\n"
1772 " GtkWidget::focus-padding = 0\n"
1776 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1778 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1782 empathy_chat_window_init (EmpathyChatWindow *window)
1785 GtkAccelGroup *accel_group;
1790 GtkWidget *chat_vbox;
1792 EmpathySmileyManager *smiley_manager;
1793 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1794 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1796 window->priv = priv;
1797 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1798 gui = empathy_builder_get_file (filename,
1799 "chat_window", &priv->dialog,
1800 "chat_vbox", &chat_vbox,
1801 "ui_manager", &priv->ui_manager,
1802 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1803 "menu_conv_favorite", &priv->menu_conv_favorite,
1804 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1805 "menu_edit_cut", &priv->menu_edit_cut,
1806 "menu_edit_copy", &priv->menu_edit_copy,
1807 "menu_edit_paste", &priv->menu_edit_paste,
1808 "menu_tabs_next", &priv->menu_tabs_next,
1809 "menu_tabs_prev", &priv->menu_tabs_prev,
1810 "menu_tabs_left", &priv->menu_tabs_left,
1811 "menu_tabs_right", &priv->menu_tabs_right,
1812 "menu_tabs_detach", &priv->menu_tabs_detach,
1816 empathy_builder_connect (gui, window,
1817 "menu_conv", "activate", chat_window_conv_activate_cb,
1818 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1819 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1820 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1821 "menu_conv_invite_participant", "activate", chat_window_invite_participant_activate_cb,
1822 "menu_conv_close", "activate", chat_window_close_activate_cb,
1823 "menu_edit", "activate", chat_window_edit_activate_cb,
1824 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1825 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1826 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1827 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1828 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1829 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1830 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1831 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1832 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1833 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1836 g_object_ref (priv->ui_manager);
1837 g_object_unref (gui);
1839 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1841 priv->notebook = gtk_notebook_new ();
1842 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1843 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1844 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1845 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1846 gtk_widget_show (priv->notebook);
1849 accel_group = gtk_accel_group_new ();
1850 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1852 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1853 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1856 gtk_accel_group_connect (accel_group,
1863 g_object_unref (accel_group);
1865 /* Set up drag target lists */
1866 priv->contact_targets = gtk_target_list_new (drag_types_dest_contact,
1867 G_N_ELEMENTS (drag_types_dest_contact));
1868 priv->file_targets = gtk_target_list_new (drag_types_dest_file,
1869 G_N_ELEMENTS (drag_types_dest_file));
1871 /* Set up smiley menu */
1872 smiley_manager = empathy_smiley_manager_dup_singleton ();
1873 submenu = empathy_smiley_menu_new (smiley_manager,
1874 chat_window_insert_smiley_activate_cb,
1876 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1877 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1878 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1879 g_object_unref (smiley_manager);
1881 /* Set up signals we can't do with ui file since we may need to
1882 * block/unblock them at some later stage.
1885 g_signal_connect (priv->dialog,
1887 G_CALLBACK (chat_window_delete_event_cb),
1889 g_signal_connect (priv->dialog,
1891 G_CALLBACK (chat_window_focus_in_event_cb),
1893 g_signal_connect_after (priv->notebook,
1895 G_CALLBACK (chat_window_page_switched_cb),
1897 g_signal_connect (priv->notebook,
1899 G_CALLBACK (chat_window_page_added_cb),
1901 g_signal_connect (priv->notebook,
1903 G_CALLBACK (chat_window_page_removed_cb),
1906 /* Set up drag and drop */
1907 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1908 GTK_DEST_DEFAULT_ALL,
1910 G_N_ELEMENTS (drag_types_dest),
1911 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1913 /* connect_after to allow GtkNotebook's built-in tab switching */
1914 g_signal_connect_after (priv->notebook,
1916 G_CALLBACK (chat_window_drag_motion),
1918 g_signal_connect (priv->notebook,
1919 "drag-data-received",
1920 G_CALLBACK (chat_window_drag_data_received),
1923 chat_windows = g_list_prepend (chat_windows, window);
1925 /* Set up private details */
1927 priv->chats_new_msg = NULL;
1928 priv->chats_composing = NULL;
1929 priv->current_chat = NULL;
1931 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1935 empathy_chat_window_new (void)
1937 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1940 /* Returns the window to open a new tab in if there is only one window
1941 * visble, otherwise, returns NULL indicating that a new window should
1945 empathy_chat_window_get_default (gboolean room)
1948 gboolean separate_windows = TRUE;
1950 empathy_conf_get_bool (empathy_conf_get (),
1951 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1954 if (separate_windows) {
1955 /* Always create a new window */
1959 for (l = chat_windows; l; l = l->next) {
1960 EmpathyChatWindowPriv *priv;
1961 EmpathyChatWindow *chat_window;
1964 chat_window = l->data;
1965 priv = GET_PRIV (chat_window);
1967 dialog = empathy_chat_window_get_dialog (chat_window);
1968 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1969 guint nb_rooms, nb_private;
1970 empathy_chat_window_get_nb_chats (chat_window, &nb_rooms, &nb_private);
1972 /* Skip the window if there aren't any rooms in it */
1973 if (room && nb_rooms == 0)
1976 /* Skip the window if there aren't any 1-1 chats in it */
1977 if (!room && nb_private == 0)
1980 /* Found a visible window on this desktop */
1989 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1991 EmpathyChatWindowPriv *priv;
1993 g_return_val_if_fail (window != NULL, NULL);
1995 priv = GET_PRIV (window);
1997 return priv->dialog;
2001 empathy_chat_window_add_chat (EmpathyChatWindow *window,
2004 EmpathyChatWindowPriv *priv;
2006 GtkWidget *popup_label;
2009 g_return_if_fail (window != NULL);
2010 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2012 priv = GET_PRIV (window);
2014 /* Reference the chat object */
2015 g_object_ref (chat);
2017 /* If this window has just been created, position it */
2018 if (priv->chats == NULL) {
2019 const gchar *name = "chat-window";
2020 gboolean separate_windows;
2022 empathy_conf_get_bool (empathy_conf_get (),
2023 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
2026 if (separate_windows) {
2027 name = empathy_chat_get_id (chat);
2029 else if (empathy_chat_is_room (chat)) {
2030 name = "room-window";
2033 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
2036 child = GTK_WIDGET (chat);
2037 label = chat_window_create_label (window, chat, TRUE);
2038 popup_label = chat_window_create_label (window, chat, FALSE);
2039 gtk_widget_show (child);
2041 g_signal_connect (chat, "notify::name",
2042 G_CALLBACK (chat_window_chat_notify_cb),
2044 g_signal_connect (chat, "notify::subject",
2045 G_CALLBACK (chat_window_chat_notify_cb),
2047 g_signal_connect (chat, "notify::remote-contact",
2048 G_CALLBACK (chat_window_chat_notify_cb),
2050 chat_window_chat_notify_cb (chat);
2052 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
2053 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2054 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2055 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
2056 TRUE, TRUE, GTK_PACK_START);
2058 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
2062 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
2065 EmpathyChatWindowPriv *priv;
2067 EmpathyContact *remote_contact;
2069 g_return_if_fail (window != NULL);
2070 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2072 priv = GET_PRIV (window);
2074 g_signal_handlers_disconnect_by_func (chat,
2075 chat_window_chat_notify_cb,
2077 remote_contact = g_object_get_data (G_OBJECT (chat),
2078 "chat-window-remote-contact");
2079 if (remote_contact) {
2080 g_signal_handlers_disconnect_by_func (remote_contact,
2081 chat_window_update_chat_tab,
2085 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2087 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
2089 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
2091 g_object_unref (chat);
2095 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
2096 EmpathyChatWindow *new_window,
2101 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
2102 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
2103 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2105 widget = GTK_WIDGET (chat);
2107 DEBUG ("Chat moving with widget:%p (%d references)", widget,
2108 G_OBJECT (widget)->ref_count);
2110 /* We reference here to make sure we don't loose the widget
2111 * and the EmpathyChat object during the move.
2113 g_object_ref (chat);
2114 g_object_ref (widget);
2116 empathy_chat_window_remove_chat (old_window, chat);
2117 empathy_chat_window_add_chat (new_window, chat);
2119 g_object_unref (widget);
2120 g_object_unref (chat);
2124 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
2127 EmpathyChatWindowPriv *priv;
2130 g_return_if_fail (window != NULL);
2131 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2133 priv = GET_PRIV (window);
2135 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2137 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
2142 empathy_chat_window_has_focus (EmpathyChatWindow *window)
2144 EmpathyChatWindowPriv *priv;
2147 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
2149 priv = GET_PRIV (window);
2151 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
2157 empathy_chat_window_find_chat (TpAccount *account,
2162 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
2164 for (l = chat_windows; l; l = l->next) {
2165 EmpathyChatWindowPriv *priv;
2166 EmpathyChatWindow *window;
2170 priv = GET_PRIV (window);
2172 for (ll = priv->chats; ll; ll = ll->next) {
2177 if (account == empathy_chat_get_account (chat) &&
2178 !tp_strdiff (id, empathy_chat_get_id (chat))) {
2188 empathy_chat_window_present_chat (EmpathyChat *chat)
2190 EmpathyChatWindow *window;
2191 EmpathyChatWindowPriv *priv;
2193 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2195 window = chat_window_find_chat (chat);
2197 /* If the chat has no window, create one */
2198 if (window == NULL) {
2199 window = empathy_chat_window_get_default (empathy_chat_is_room (chat));
2201 window = empathy_chat_window_new ();
2204 empathy_chat_window_add_chat (window, chat);
2207 priv = GET_PRIV (window);
2208 empathy_chat_window_switch_to_chat (window, chat);
2209 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
2211 gtk_widget_grab_focus (chat->input_text_view);
2215 empathy_chat_window_get_nb_chats (EmpathyChatWindow *self,
2219 EmpathyChatWindowPriv *priv = GET_PRIV (self);
2221 guint _nb_rooms = 0, _nb_private = 0;
2223 for (l = priv->chats; l != NULL; l = g_list_next (l)) {
2224 if (empathy_chat_is_room (EMPATHY_CHAT (l->data)))
2230 if (nb_rooms != NULL)
2231 *nb_rooms = _nb_rooms;
2232 if (nb_private != NULL)
2233 *nb_private = _nb_private;