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;
96 GtkAction *menu_edit_find;
98 GtkAction *menu_tabs_next;
99 GtkAction *menu_tabs_prev;
100 GtkAction *menu_tabs_left;
101 GtkAction *menu_tabs_right;
102 GtkAction *menu_tabs_detach;
103 } EmpathyChatWindowPriv;
105 static GList *chat_windows = NULL;
107 static const guint tab_accel_keys[] = {
108 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
109 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
113 DND_DRAG_TYPE_CONTACT_ID,
114 DND_DRAG_TYPE_URI_LIST,
118 static const GtkTargetEntry drag_types_dest[] = {
119 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
120 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
121 { "text/uri-list", 0, DND_DRAG_TYPE_URI_LIST },
122 { "text/path-list", 0, DND_DRAG_TYPE_URI_LIST },
125 static const GtkTargetEntry drag_types_dest_contact[] = {
126 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
129 static const GtkTargetEntry drag_types_dest_file[] = {
130 /* must be first to be prioritized, in order to receive the
131 * note's file path from Tomboy instead of an URI */
132 { "text/path-list", 0, DND_DRAG_TYPE_URI_LIST },
133 { "text/uri-list", 0, DND_DRAG_TYPE_URI_LIST },
136 static void chat_window_update (EmpathyChatWindow *window);
138 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
141 chat_window_accel_cb (GtkAccelGroup *accelgroup,
145 EmpathyChatWindow *window)
147 EmpathyChatWindowPriv *priv;
151 priv = GET_PRIV (window);
153 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
154 if (tab_accel_keys[i] == key) {
161 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
165 static EmpathyChatWindow *
166 chat_window_find_chat (EmpathyChat *chat)
168 EmpathyChatWindowPriv *priv;
171 for (l = chat_windows; l; l = l->next) {
172 priv = GET_PRIV (l->data);
173 ll = g_list_find (priv->chats, chat);
183 chat_window_close_clicked_cb (GtkAction *action,
186 EmpathyChatWindow *window;
188 window = chat_window_find_chat (chat);
189 empathy_chat_window_remove_chat (window, chat);
193 chat_tab_style_set_cb (GtkWidget *hbox,
194 GtkStyle *previous_style,
198 int char_width, h, w;
199 PangoContext *context;
200 PangoFontMetrics *metrics;
202 button = g_object_get_data (G_OBJECT (user_data),
203 "chat-window-tab-close-button");
204 context = gtk_widget_get_pango_context (hbox);
206 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
207 pango_context_get_language (context));
208 char_width = pango_font_metrics_get_approximate_char_width (metrics);
209 pango_font_metrics_unref (metrics);
211 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
212 GTK_ICON_SIZE_MENU, &w, &h);
214 /* Request at least about 12 chars width plus at least space for the status
215 * image and the close button */
216 gtk_widget_set_size_request (hbox,
217 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
219 gtk_widget_set_size_request (button, w, h);
223 chat_window_create_label (EmpathyChatWindow *window,
225 gboolean is_tab_label)
227 EmpathyChatWindowPriv *priv;
229 GtkWidget *name_label;
230 GtkWidget *status_image;
231 GtkWidget *close_button;
232 GtkWidget *close_image;
233 GtkWidget *event_box;
234 GtkWidget *event_box_hbox;
235 PangoAttrList *attr_list;
236 PangoAttribute *attr;
238 priv = GET_PRIV (window);
240 /* The spacing between the button and the label. */
241 hbox = gtk_hbox_new (FALSE, 0);
243 event_box = gtk_event_box_new ();
244 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
246 name_label = gtk_label_new (NULL);
248 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
250 attr_list = pango_attr_list_new ();
251 attr = pango_attr_scale_new (1/1.2);
252 attr->start_index = 0;
253 attr->end_index = -1;
254 pango_attr_list_insert (attr_list, attr);
255 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
256 pango_attr_list_unref (attr_list);
258 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
259 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
260 g_object_set_data (G_OBJECT (chat),
261 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
264 status_image = gtk_image_new ();
266 /* Spacing between the icon and label. */
267 event_box_hbox = gtk_hbox_new (FALSE, 0);
269 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
270 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
272 g_object_set_data (G_OBJECT (chat),
273 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
275 g_object_set_data (G_OBJECT (chat),
276 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
279 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
280 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
283 close_button = gtk_button_new ();
284 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
285 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
287 /* We don't want focus/keynav for the button to avoid clutter, and
288 * Ctrl-W works anyway.
290 gtk_widget_set_can_focus (close_button, FALSE);
291 gtk_widget_set_can_default (close_button, FALSE);
293 /* Set the name to make the special rc style match. */
294 gtk_widget_set_name (close_button, "empathy-close-button");
296 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
298 gtk_container_add (GTK_CONTAINER (close_button), close_image);
300 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
302 g_signal_connect (close_button,
304 G_CALLBACK (chat_window_close_clicked_cb),
307 /* React to theme changes and also setup the size correctly. */
308 g_signal_connect (hbox,
310 G_CALLBACK (chat_tab_style_set_cb),
314 gtk_widget_show_all (hbox);
320 _submenu_notify_visible_changed_cb (GObject *object,
324 g_signal_handlers_disconnect_by_func (object,
325 _submenu_notify_visible_changed_cb,
327 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
331 chat_window_menu_context_update (EmpathyChatWindowPriv *priv,
336 gboolean is_connected;
339 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
340 first_page = (page_num == 0);
341 last_page = (page_num == (num_pages - 1));
342 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
344 DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
346 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
347 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
348 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
349 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
350 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
351 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
355 chat_window_conversation_menu_update (EmpathyChatWindowPriv *priv,
356 EmpathyChatWindow *self)
358 EmpathyTpChat *tp_chat;
359 TpConnection *connection;
361 gboolean sensitive = FALSE;
363 g_return_if_fail (priv->current_chat != NULL);
365 action = gtk_ui_manager_get_action (priv->ui_manager,
366 "/chats_menubar/menu_conv/menu_conv_invite_participant");
367 tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
369 if (tp_chat != NULL) {
370 connection = empathy_tp_chat_get_connection (tp_chat);
372 sensitive = empathy_tp_chat_can_add_contact (tp_chat) &&
373 (tp_connection_get_status (connection, NULL) ==
374 TP_CONNECTION_STATUS_CONNECTED);
377 gtk_action_set_sensitive (action, sensitive);
381 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
382 EmpathyChatWindow *window)
384 GtkWidget *menu, *submenu, *orig_submenu;
386 menu = gtk_ui_manager_get_widget (priv->ui_manager,
387 "/chats_menubar/menu_contact");
388 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
390 DEBUG ("Update window : Contact Menu");
392 if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
393 submenu = empathy_chat_get_contact_menu (priv->current_chat);
394 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
395 gtk_widget_show (menu);
397 empathy_signal_connect_weak (orig_submenu,
399 (GCallback)_submenu_notify_visible_changed_cb,
405 get_all_unread_messages (EmpathyChatWindowPriv *priv)
410 for (l = priv->chats_new_msg; l != NULL; l = g_list_next (l)) {
411 EmpathyChat *chat = l->data;
413 nb += empathy_chat_get_nb_unread_messages (chat);
420 get_window_title_name (EmpathyChatWindowPriv *priv)
422 const gchar *active_name;
424 guint current_unread_msgs;
426 nb_chats = g_list_length (priv->chats);
427 g_assert (nb_chats > 0);
429 active_name = empathy_chat_get_name (priv->current_chat);
431 current_unread_msgs = empathy_chat_get_nb_unread_messages (
436 if (current_unread_msgs == 0)
437 return g_strdup (active_name);
439 return g_strdup_printf (ngettext (
441 "%s (%d unread)", current_unread_msgs),
442 active_name, current_unread_msgs);
444 guint nb_others = nb_chats - 1;
445 guint all_unread_msgs;
447 all_unread_msgs = get_all_unread_messages (priv);
449 if (all_unread_msgs == 0) {
450 /* no unread message */
451 return g_strdup_printf (ngettext (
453 "%s (and %u others)", nb_others),
454 active_name, nb_others);
457 else if (all_unread_msgs == current_unread_msgs) {
458 /* unread messages are in the current tab */
459 return g_strdup_printf (ngettext (
461 "%s (%d unread)", current_unread_msgs),
462 active_name, current_unread_msgs);
465 else if (current_unread_msgs == 0) {
466 /* unread messages are in other tabs */
467 return g_strdup_printf (ngettext (
468 "%s (%d unread from others)",
469 "%s (%d unread from others)",
471 active_name, all_unread_msgs);
475 /* unread messages are in all the tabs */
476 return g_strdup_printf (ngettext (
477 "%s (%d unread from all)",
478 "%s (%d unread from all)",
480 active_name, all_unread_msgs);
486 chat_window_title_update (EmpathyChatWindowPriv *priv)
490 DEBUG ("Update window : Title");
492 name = get_window_title_name (priv);
493 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
498 chat_window_icon_update (EmpathyChatWindowPriv *priv)
501 EmpathyContact *remote_contact;
502 gboolean avatar_in_icon;
505 n_chats = g_list_length (priv->chats);
507 DEBUG ("Update window : Icon");
509 /* Update window icon */
510 if (priv->chats_new_msg) {
511 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
512 EMPATHY_IMAGE_MESSAGE);
514 empathy_conf_get_bool (empathy_conf_get (),
515 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
518 if (n_chats == 1 && avatar_in_icon) {
519 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
520 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
521 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
524 g_object_unref (icon);
527 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
533 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
537 GtkWidget *chat_close_button;
540 DEBUG ("Update window : Close Button");
542 if (num_pages == 1) {
543 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
544 chat_close_button = g_object_get_data (G_OBJECT (chat),
545 "chat-window-tab-close-button");
546 gtk_widget_hide (chat_close_button);
548 for (i=0; i<num_pages; i++) {
549 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
550 chat_close_button = g_object_get_data (G_OBJECT (chat),
551 "chat-window-tab-close-button");
552 gtk_widget_show (chat_close_button);
558 chat_window_update (EmpathyChatWindow *window)
560 EmpathyChatWindowPriv *priv = GET_PRIV (window);
563 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
565 DEBUG ("Update window");
567 /* Update Tab menu */
568 chat_window_menu_context_update (priv,
571 chat_window_conversation_menu_update (priv, window);
573 chat_window_contact_menu_update (priv,
576 chat_window_title_update (priv);
578 chat_window_icon_update (priv);
580 chat_window_close_button_update (priv,
585 append_markup_printf (GString *string,
592 va_start (args, format);
594 tmp = g_markup_vprintf_escaped (format, args);
595 g_string_append (string, tmp);
602 chat_window_update_chat_tab (EmpathyChat *chat)
604 EmpathyChatWindow *window;
605 EmpathyChatWindowPriv *priv;
606 EmpathyContact *remote_contact;
610 const gchar *subject;
611 const gchar *status = NULL;
615 const gchar *icon_name;
616 GtkWidget *tab_image;
617 GtkWidget *menu_image;
619 window = chat_window_find_chat (chat);
623 priv = GET_PRIV (window);
625 /* Get information */
626 name = empathy_chat_get_name (chat);
627 account = empathy_chat_get_account (chat);
628 subject = empathy_chat_get_subject (chat);
629 remote_contact = empathy_chat_get_remote_contact (chat);
631 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
632 name, tp_proxy_get_object_path (account), subject, remote_contact);
634 /* Update tab image */
635 if (empathy_chat_get_tp_chat (chat) == NULL) {
636 /* No TpChat, we are disconnected */
639 else if (g_list_find (priv->chats_new_msg, chat)) {
640 icon_name = EMPATHY_IMAGE_MESSAGE;
642 else if (g_list_find (priv->chats_composing, chat)) {
643 icon_name = EMPATHY_IMAGE_TYPING;
645 else if (remote_contact) {
646 icon_name = empathy_icon_name_for_contact (remote_contact);
648 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
651 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
652 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
653 if (icon_name != NULL) {
654 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
655 gtk_widget_show (tab_image);
656 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
657 gtk_widget_show (menu_image);
659 gtk_widget_hide (tab_image);
660 gtk_widget_hide (menu_image);
663 /* Update tab tooltip */
664 tooltip = g_string_new (NULL);
666 if (remote_contact) {
667 id = empathy_contact_get_id (remote_contact);
668 status = empathy_contact_get_presence_message (remote_contact);
673 append_markup_printf (tooltip,
674 "<b>%s</b><small> (%s)</small>",
676 tp_account_get_display_name (account));
678 if (!EMP_STR_EMPTY (status)) {
679 append_markup_printf (tooltip, "\n<i>%s</i>", status);
683 append_markup_printf (tooltip, "\n<b>%s</b> %s",
684 _("Topic:"), subject);
687 if (g_list_find (priv->chats_composing, chat)) {
688 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
691 markup = g_string_free (tooltip, FALSE);
692 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
693 gtk_widget_set_tooltip_markup (widget, markup);
694 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
695 gtk_widget_set_tooltip_markup (widget, markup);
698 /* Update tab and menu label */
699 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
700 gtk_label_set_text (GTK_LABEL (widget), name);
701 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
702 gtk_label_set_text (GTK_LABEL (widget), name);
704 /* Update the window if it's the current chat */
705 if (priv->current_chat == chat) {
706 chat_window_update (window);
711 chat_window_chat_notify_cb (EmpathyChat *chat)
713 EmpathyContact *old_remote_contact;
714 EmpathyContact *remote_contact = NULL;
716 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
717 remote_contact = empathy_chat_get_remote_contact (chat);
719 if (old_remote_contact != remote_contact) {
720 /* The remote-contact associated with the chat changed, we need
721 * to keep track of any change of that contact and update the
722 * window each time. */
723 if (remote_contact) {
724 g_signal_connect_swapped (remote_contact, "notify",
725 G_CALLBACK (chat_window_update_chat_tab),
728 if (old_remote_contact) {
729 g_signal_handlers_disconnect_by_func (old_remote_contact,
730 chat_window_update_chat_tab,
734 g_object_set_data_full (G_OBJECT (chat), "chat-window-remote-contact",
735 g_object_ref (remote_contact), (GDestroyNotify) g_object_unref);
738 chat_window_update_chat_tab (chat);
742 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
743 EmpathySmiley *smiley,
746 EmpathyChatWindowPriv *priv = GET_PRIV (window);
748 GtkTextBuffer *buffer;
751 chat = priv->current_chat;
753 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
754 gtk_text_buffer_get_end_iter (buffer, &iter);
755 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
759 chat_window_conv_activate_cb (GtkAction *action,
760 EmpathyChatWindow *window)
762 EmpathyChatWindowPriv *priv = GET_PRIV (window);
765 EmpathyContact *remote_contact = NULL;
767 /* Favorite room menu */
768 is_room = empathy_chat_is_room (priv->current_chat);
772 gboolean found = FALSE;
773 EmpathyChatroom *chatroom;
775 room = empathy_chat_get_id (priv->current_chat);
776 account = empathy_chat_get_account (priv->current_chat);
777 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
779 if (chatroom != NULL)
780 found = empathy_chatroom_is_favorite (chatroom);
782 DEBUG ("This room %s favorite", found ? "is" : "is not");
783 gtk_toggle_action_set_active (
784 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
786 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
788 /* Show contacts menu */
789 g_object_get (priv->current_chat,
790 "remote-contact", &remote_contact,
791 "show-contacts", &active,
793 if (remote_contact == NULL) {
794 gtk_toggle_action_set_active (
795 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
798 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
799 (remote_contact == NULL));
800 if (remote_contact != NULL) {
801 g_object_unref (remote_contact);
806 chat_window_clear_activate_cb (GtkAction *action,
807 EmpathyChatWindow *window)
809 EmpathyChatWindowPriv *priv = GET_PRIV (window);
811 empathy_chat_clear (priv->current_chat);
815 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
816 EmpathyChatWindow *window)
818 EmpathyChatWindowPriv *priv = GET_PRIV (window);
822 EmpathyChatroom *chatroom;
824 active = gtk_toggle_action_get_active (toggle_action);
825 account = empathy_chat_get_account (priv->current_chat);
826 room = empathy_chat_get_id (priv->current_chat);
828 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
831 if (chatroom == NULL) {
834 name = empathy_chat_get_name (priv->current_chat);
835 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
836 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
837 g_object_unref (chatroom);
840 empathy_chatroom_set_favorite (chatroom, active);
844 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
845 EmpathyChatWindow *window)
847 EmpathyChatWindowPriv *priv = GET_PRIV (window);
850 active = gtk_toggle_action_get_active (toggle_action);
852 empathy_chat_set_show_contacts (priv->current_chat, active);
856 got_contact_cb (EmpathyTpContactFactory *factory,
857 EmpathyContact *contact,
862 EmpathyTpChat *tp_chat = EMPATHY_TP_CHAT (user_data);
865 DEBUG ("Failed: %s", error->message);
868 empathy_contact_list_add (EMPATHY_CONTACT_LIST (tp_chat),
869 contact, _("Inviting you to this room"));
874 chat_window_invite_participant_activate_cb (GtkAction *action,
875 EmpathyChatWindow *window)
877 EmpathyChatWindowPriv *priv;
879 EmpathyTpChat *tp_chat;
884 priv = GET_PRIV (window);
886 g_return_if_fail (priv->current_chat != NULL);
888 tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
889 channel = empathy_tp_chat_get_channel (tp_chat);
890 account = empathy_chat_get_account (priv->current_chat);
892 dialog = empathy_invite_participant_dialog_new (
893 GTK_WINDOW (priv->dialog), account);
894 gtk_widget_show (dialog);
896 response = gtk_dialog_run (GTK_DIALOG (dialog));
898 if (response == GTK_RESPONSE_ACCEPT) {
899 TpConnection *connection;
900 EmpathyTpContactFactory *factory;
903 id = empathy_contact_selector_dialog_get_selected (
904 EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL);
905 if (EMP_STR_EMPTY (id)) goto out;
907 connection = tp_channel_borrow_connection (channel);
908 factory = empathy_tp_contact_factory_dup_singleton (connection);
910 empathy_tp_contact_factory_get_from_id (factory, id,
911 got_contact_cb, tp_chat, NULL, NULL);
913 g_object_unref (factory);
917 gtk_widget_destroy (dialog);
921 chat_window_close_activate_cb (GtkAction *action,
922 EmpathyChatWindow *window)
924 EmpathyChatWindowPriv *priv;
926 priv = GET_PRIV (window);
928 g_return_if_fail (priv->current_chat != NULL);
930 empathy_chat_window_remove_chat (window, priv->current_chat);
934 chat_window_edit_activate_cb (GtkAction *action,
935 EmpathyChatWindow *window)
937 EmpathyChatWindowPriv *priv;
938 GtkClipboard *clipboard;
939 GtkTextBuffer *buffer;
940 gboolean text_available;
942 priv = GET_PRIV (window);
944 g_return_if_fail (priv->current_chat != NULL);
946 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
947 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
948 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
949 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
953 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
954 if (gtk_text_buffer_get_has_selection (buffer)) {
955 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
956 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
960 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
962 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
963 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
966 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
967 text_available = gtk_clipboard_wait_is_text_available (clipboard);
968 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
972 chat_window_cut_activate_cb (GtkAction *action,
973 EmpathyChatWindow *window)
975 EmpathyChatWindowPriv *priv;
977 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
979 priv = GET_PRIV (window);
981 empathy_chat_cut (priv->current_chat);
985 chat_window_copy_activate_cb (GtkAction *action,
986 EmpathyChatWindow *window)
988 EmpathyChatWindowPriv *priv;
990 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
992 priv = GET_PRIV (window);
994 empathy_chat_copy (priv->current_chat);
998 chat_window_paste_activate_cb (GtkAction *action,
999 EmpathyChatWindow *window)
1001 EmpathyChatWindowPriv *priv;
1003 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
1005 priv = GET_PRIV (window);
1007 empathy_chat_paste (priv->current_chat);
1011 chat_window_find_activate_cb (GtkAction *action,
1012 EmpathyChatWindow *window)
1014 EmpathyChatWindowPriv *priv;
1016 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
1018 priv = GET_PRIV (window);
1020 empathy_chat_find (priv->current_chat);
1024 chat_window_tabs_next_activate_cb (GtkAction *action,
1025 EmpathyChatWindow *window)
1027 EmpathyChatWindowPriv *priv;
1029 gint index_, numPages;
1031 priv = GET_PRIV (window);
1033 chat = priv->current_chat;
1034 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1035 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1037 if (index_ == (numPages - 1)) {
1038 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
1042 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
1046 chat_window_tabs_previous_activate_cb (GtkAction *action,
1047 EmpathyChatWindow *window)
1049 EmpathyChatWindowPriv *priv;
1051 gint index_, numPages;
1053 priv = GET_PRIV (window);
1055 chat = priv->current_chat;
1056 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1057 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1060 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
1064 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
1068 chat_window_tabs_left_activate_cb (GtkAction *action,
1069 EmpathyChatWindow *window)
1071 EmpathyChatWindowPriv *priv;
1075 priv = GET_PRIV (window);
1077 chat = priv->current_chat;
1078 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1083 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1089 chat_window_tabs_right_activate_cb (GtkAction *action,
1090 EmpathyChatWindow *window)
1092 EmpathyChatWindowPriv *priv;
1096 priv = GET_PRIV (window);
1098 chat = priv->current_chat;
1099 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1101 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1107 chat_window_detach_activate_cb (GtkAction *action,
1108 EmpathyChatWindow *window)
1110 EmpathyChatWindowPriv *priv;
1111 EmpathyChatWindow *new_window;
1114 priv = GET_PRIV (window);
1116 chat = priv->current_chat;
1117 new_window = empathy_chat_window_new ();
1119 empathy_chat_window_move_chat (window, new_window, chat);
1121 priv = GET_PRIV (new_window);
1122 gtk_widget_show (priv->dialog);
1126 chat_window_help_contents_activate_cb (GtkAction *action,
1127 EmpathyChatWindow *window)
1129 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1131 empathy_url_show (priv->dialog, "ghelp:empathy");
1135 chat_window_help_about_activate_cb (GtkAction *action,
1136 EmpathyChatWindow *window)
1138 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1140 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1144 chat_window_delete_event_cb (GtkWidget *dialog,
1146 EmpathyChatWindow *window)
1148 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1150 DEBUG ("Delete event received");
1152 g_object_ref (window);
1153 while (priv->chats) {
1154 empathy_chat_window_remove_chat (window, priv->chats->data);
1156 g_object_unref (window);
1162 chat_window_composing_cb (EmpathyChat *chat,
1163 gboolean is_composing,
1164 EmpathyChatWindow *window)
1166 EmpathyChatWindowPriv *priv;
1168 priv = GET_PRIV (window);
1170 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1171 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1173 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1176 chat_window_update_chat_tab (chat);
1180 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1183 EmpathyChatWindowPriv *priv;
1185 priv = GET_PRIV (window);
1187 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
1188 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1192 free_notification_data (NotificationData *data)
1194 g_object_unref (data->chat);
1195 g_slice_free (NotificationData, data);
1199 chat_window_notification_closed_cb (NotifyNotification *notify,
1200 NotificationData *cb_data)
1202 EmpathyNotificationClosedReason reason = 0;
1203 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1205 #ifdef notify_notification_get_closed_reason
1206 reason = notify_notification_get_closed_reason (notify);
1208 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1209 empathy_chat_window_present_chat (cb_data->chat);
1212 g_object_unref (notify);
1213 priv->notification = NULL;
1214 free_notification_data (cb_data);
1215 priv->notification_data = NULL;
1219 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1220 EmpathyMessage *message,
1223 EmpathyContact *sender;
1224 const gchar *header;
1228 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1231 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1234 empathy_conf_get_bool (empathy_conf_get (),
1235 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1241 sender = empathy_message_get_sender (message);
1242 header = empathy_contact_get_name (sender);
1243 body = empathy_message_get_body (message);
1244 escaped = g_markup_escape_text (body, -1);
1246 if (priv->notification != NULL) {
1247 notify_notification_update (priv->notification,
1248 header, escaped, NULL);
1250 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1252 cb_data->chat = g_object_ref (chat);
1253 cb_data->window = window;
1255 priv->notification_data = cb_data;
1256 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1257 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1259 g_signal_connect (priv->notification, "closed",
1260 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1263 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1264 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1266 if (pixbuf != NULL) {
1267 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1268 g_object_unref (pixbuf);
1271 notify_notification_show (priv->notification, NULL);
1277 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1282 if (!empathy_chat_is_room (chat))
1285 markup = g_markup_printf_escaped (
1286 "<span color=\"red\" weight=\"bold\">%s</span>",
1287 empathy_chat_get_name (chat));
1289 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1290 gtk_label_set_markup (GTK_LABEL (widget), markup);
1295 chat_window_new_message_cb (EmpathyChat *chat,
1296 EmpathyMessage *message,
1297 EmpathyChatWindow *window)
1299 EmpathyChatWindowPriv *priv;
1301 gboolean needs_urgency;
1302 EmpathyContact *sender;
1304 priv = GET_PRIV (window);
1306 has_focus = empathy_chat_window_has_focus (window);
1308 /* - if we're the sender, we play the sound if it's specified in the
1309 * preferences and we're not away.
1310 * - if we receive a message, we play the sound if it's specified in the
1311 * preferences and the window does not have focus on the chat receiving
1315 sender = empathy_message_get_sender (message);
1317 if (empathy_contact_is_user (sender)) {
1318 empathy_sound_play (GTK_WIDGET (priv->dialog),
1319 EMPATHY_SOUND_MESSAGE_OUTGOING);
1322 if (has_focus && priv->current_chat == chat) {
1323 /* window and tab are focused so consider the message to be read */
1325 /* FIXME: see Bug#610994 and coments about it in EmpathyChatPriv */
1326 empathy_chat_messages_read (chat);
1330 if (!g_list_find (priv->chats_new_msg, chat)) {
1331 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1332 chat_window_update_chat_tab (chat);
1335 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1336 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1337 * an unamed MUC (msn-like).
1338 * In case of a MUC, we set urgency only if the message contains our
1340 if (empathy_chat_is_room (chat) ||
1341 empathy_chat_get_remote_contact (chat) == NULL) {
1342 needs_urgency = empathy_message_should_highlight (message);
1344 needs_urgency = TRUE;
1347 if (needs_urgency) {
1349 chat_window_set_urgency_hint (window, TRUE);
1350 chat_window_set_highlight_room_tab_label (chat);
1353 empathy_sound_play (GTK_WIDGET (priv->dialog),
1354 EMPATHY_SOUND_MESSAGE_INCOMING);
1355 chat_window_show_or_update_notification (window, message, chat);
1358 /* update the number of unread messages */
1359 chat_window_title_update (priv);
1362 static GtkNotebook *
1363 chat_window_detach_hook (GtkNotebook *source,
1369 EmpathyChatWindowPriv *priv;
1370 EmpathyChatWindow *window, *new_window;
1373 chat = EMPATHY_CHAT (page);
1374 window = chat_window_find_chat (chat);
1376 new_window = empathy_chat_window_new ();
1377 priv = GET_PRIV (new_window);
1379 DEBUG ("Detach hook called");
1381 empathy_chat_window_move_chat (window, new_window, chat);
1383 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1384 gtk_widget_show (priv->dialog);
1390 chat_window_page_switched_cb (GtkNotebook *notebook,
1391 GtkNotebookPage *page,
1393 EmpathyChatWindow *window)
1395 EmpathyChatWindowPriv *priv;
1399 DEBUG ("Page switched");
1401 priv = GET_PRIV (window);
1403 child = gtk_notebook_get_nth_page (notebook, page_num);
1404 chat = EMPATHY_CHAT (child);
1406 if (priv->page_added) {
1407 priv->page_added = FALSE;
1408 empathy_chat_scroll_down (chat);
1410 else if (priv->current_chat == chat) {
1414 priv->current_chat = chat;
1415 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1416 empathy_chat_messages_read (chat);
1418 chat_window_update_chat_tab (chat);
1422 chat_window_page_added_cb (GtkNotebook *notebook,
1425 EmpathyChatWindow *window)
1427 EmpathyChatWindowPriv *priv;
1430 priv = GET_PRIV (window);
1432 /* If we just received DND to the same window, we don't want
1433 * to do anything here like removing the tab and then readding
1434 * it, so we return here and in "page-added".
1436 if (priv->dnd_same_window) {
1437 DEBUG ("Page added (back to the same window)");
1438 priv->dnd_same_window = FALSE;
1442 DEBUG ("Page added");
1444 /* Get chat object */
1445 chat = EMPATHY_CHAT (child);
1447 /* Connect chat signals for this window */
1448 g_signal_connect (chat, "composing",
1449 G_CALLBACK (chat_window_composing_cb),
1451 g_signal_connect (chat, "new-message",
1452 G_CALLBACK (chat_window_new_message_cb),
1454 g_signal_connect (chat, "notify::tp-chat",
1455 G_CALLBACK (chat_window_update_chat_tab),
1458 /* Set flag so we know to perform some special operations on
1459 * switch page due to the new page being added.
1461 priv->page_added = TRUE;
1463 /* Get list of chats up to date */
1464 priv->chats = g_list_append (priv->chats, chat);
1466 chat_window_update_chat_tab (chat);
1470 chat_window_page_removed_cb (GtkNotebook *notebook,
1473 EmpathyChatWindow *window)
1475 EmpathyChatWindowPriv *priv;
1478 priv = GET_PRIV (window);
1480 /* If we just received DND to the same window, we don't want
1481 * to do anything here like removing the tab and then readding
1482 * it, so we return here and in "page-added".
1484 if (priv->dnd_same_window) {
1485 DEBUG ("Page removed (and will be readded to same window)");
1489 DEBUG ("Page removed");
1491 /* Get chat object */
1492 chat = EMPATHY_CHAT (child);
1494 /* Disconnect all signal handlers for this chat and this window */
1495 g_signal_handlers_disconnect_by_func (chat,
1496 G_CALLBACK (chat_window_composing_cb),
1498 g_signal_handlers_disconnect_by_func (chat,
1499 G_CALLBACK (chat_window_new_message_cb),
1501 g_signal_handlers_disconnect_by_func (chat,
1502 G_CALLBACK (chat_window_update_chat_tab),
1505 /* Keep list of chats up to date */
1506 priv->chats = g_list_remove (priv->chats, chat);
1507 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1508 empathy_chat_messages_read (chat);
1509 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1511 if (priv->chats == NULL) {
1512 g_object_unref (window);
1514 chat_window_update (window);
1519 chat_window_focus_in_event_cb (GtkWidget *widget,
1521 EmpathyChatWindow *window)
1523 EmpathyChatWindowPriv *priv;
1525 DEBUG ("Focus in event, updating title");
1527 priv = GET_PRIV (window);
1529 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1530 empathy_chat_messages_read (priv->current_chat);
1532 chat_window_set_urgency_hint (window, FALSE);
1534 /* Update the title, since we now mark all unread messages as read. */
1535 chat_window_update_chat_tab (priv->current_chat);
1541 chat_window_drag_drop (GtkWidget *widget,
1542 GdkDragContext *context,
1546 EmpathyChatWindow *window)
1549 EmpathyChatWindowPriv *priv;
1551 priv = GET_PRIV (window);
1553 target = gtk_drag_dest_find_target (widget, context, priv->file_targets);
1554 if (target == GDK_NONE)
1555 target = gtk_drag_dest_find_target (widget, context, priv->contact_targets);
1557 if (target != GDK_NONE) {
1558 gtk_drag_get_data (widget, context, target, time_);
1566 chat_window_drag_motion (GtkWidget *widget,
1567 GdkDragContext *context,
1571 EmpathyChatWindow *window)
1574 EmpathyChatWindowPriv *priv;
1576 priv = GET_PRIV (window);
1578 target = gtk_drag_dest_find_target (widget, context, priv->file_targets);
1579 if (target != GDK_NONE) {
1580 /* This is a file drag. Ensure the contact is online and set the
1581 drag type to COPY. Note that it's possible that the tab will
1582 be switched by GTK+ after a timeout from drag_motion without
1583 getting another drag_motion to disable the drop. You have
1584 to hold your mouse really still.
1586 EmpathyContact *contact;
1588 priv = GET_PRIV (window);
1589 contact = empathy_chat_get_remote_contact (priv->current_chat);
1590 /* contact is NULL for multi-user chats. We don't do
1591 * file transfers to MUCs. We also don't send files
1592 * to offline contacts or contacts that don't support
1595 if ((contact == NULL) || !empathy_contact_is_online (contact)) {
1596 gdk_drag_status (context, 0, time_);
1599 if (!(empathy_contact_get_capabilities (contact)
1600 & EMPATHY_CAPABILITIES_FT)) {
1601 gdk_drag_status (context, 0, time_);
1604 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1608 target = gtk_drag_dest_find_target (widget, context, priv->contact_targets);
1609 if (target != GDK_NONE) {
1610 /* This is a drag of a contact from a contact list. Set to COPY.
1611 FIXME: If this drag is to a MUC window, it invites the user.
1612 Otherwise, it opens a chat. Should we use a different drag
1613 type for invites? Should we allow ASK?
1615 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1623 chat_window_drag_data_received (GtkWidget *widget,
1624 GdkDragContext *context,
1627 GtkSelectionData *selection,
1630 EmpathyChatWindow *window)
1632 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1633 EmpathyChat *chat = NULL;
1634 EmpathyChatWindow *old_window;
1635 TpAccount *account = NULL;
1636 TpAccountManager *account_manager;
1639 const gchar *account_id;
1640 const gchar *contact_id;
1642 id = (const gchar*) gtk_selection_data_get_data (selection);
1644 /* FIXME: Perhaps should be sure that the account manager is
1645 * prepared before calling _ensure_account on it. */
1646 account_manager = tp_account_manager_dup ();
1648 DEBUG ("DND contact from roster with id:'%s'", id);
1650 strv = g_strsplit (id, ":", 2);
1651 if (g_strv_length (strv) == 2) {
1652 account_id = strv[0];
1653 contact_id = strv[1];
1655 tp_account_manager_ensure_account (account_manager, account_id);
1656 if (account != NULL)
1657 chat = empathy_chat_window_find_chat (account, contact_id);
1660 if (account == NULL) {
1662 gtk_drag_finish (context, FALSE, FALSE, time_);
1667 TpConnection *connection;
1669 connection = tp_account_get_connection (account);
1672 empathy_dispatcher_chat_with_contact_id (
1673 connection, contact_id, NULL, NULL);
1679 g_object_unref (account_manager);
1682 old_window = chat_window_find_chat (chat);
1684 if (old_window == window) {
1685 gtk_drag_finish (context, TRUE, FALSE, time_);
1689 empathy_chat_window_move_chat (old_window, window, chat);
1691 empathy_chat_window_add_chat (window, chat);
1694 /* Added to take care of any outstanding chat events */
1695 empathy_chat_window_present_chat (chat);
1697 /* We should return TRUE to remove the data when doing
1698 * GDK_ACTION_MOVE, but we don't here otherwise it has
1699 * weird consequences, and we handle that internally
1700 * anyway with add_chat () and remove_chat ().
1702 gtk_drag_finish (context, TRUE, FALSE, time_);
1704 else if (info == DND_DRAG_TYPE_URI_LIST) {
1705 EmpathyChatWindowPriv *priv;
1706 EmpathyContact *contact;
1709 priv = GET_PRIV (window);
1710 contact = empathy_chat_get_remote_contact (priv->current_chat);
1712 /* contact is NULL when current_chat is a multi-user chat.
1713 * We don't do file transfers to MUCs, so just cancel the drag.
1715 if (contact == NULL) {
1716 gtk_drag_finish (context, TRUE, FALSE, time_);
1720 data = (const gchar *) gtk_selection_data_get_data (selection);
1721 empathy_send_file_from_uri_list (contact, data);
1723 gtk_drag_finish (context, TRUE, FALSE, time_);
1725 else if (info == DND_DRAG_TYPE_TAB) {
1727 EmpathyChatWindow *old_window = NULL;
1731 chat = (void *) gtk_selection_data_get_data (selection);
1732 old_window = chat_window_find_chat (*chat);
1735 EmpathyChatWindowPriv *priv;
1737 priv = GET_PRIV (window);
1738 priv->dnd_same_window = (old_window == window);
1739 DEBUG ("DND tab (within same window: %s)",
1740 priv->dnd_same_window ? "Yes" : "No");
1743 DEBUG ("DND from unknown source");
1744 gtk_drag_finish (context, FALSE, FALSE, time_);
1749 chat_window_finalize (GObject *object)
1751 EmpathyChatWindow *window;
1752 EmpathyChatWindowPriv *priv;
1754 window = EMPATHY_CHAT_WINDOW (object);
1755 priv = GET_PRIV (window);
1757 DEBUG ("Finalized: %p", object);
1759 g_object_unref (priv->ui_manager);
1760 g_object_unref (priv->chatroom_manager);
1761 g_object_unref (priv->notify_mgr);
1763 if (priv->notification != NULL) {
1764 notify_notification_close (priv->notification, NULL);
1765 g_object_unref (priv->notification);
1766 priv->notification = NULL;
1767 if (priv->notification_data != NULL)
1769 free_notification_data (priv->notification_data);
1770 priv->notification_data = NULL;
1774 if (priv->contact_targets) {
1775 gtk_target_list_unref (priv->contact_targets);
1777 if (priv->file_targets) {
1778 gtk_target_list_unref (priv->file_targets);
1781 chat_windows = g_list_remove (chat_windows, window);
1782 gtk_widget_destroy (priv->dialog);
1784 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1788 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1790 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1792 object_class->finalize = chat_window_finalize;
1794 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1796 /* Set up a style for the close button with no focus padding. */
1797 gtk_rc_parse_string (
1798 "style \"empathy-close-button-style\"\n"
1800 " GtkWidget::focus-padding = 0\n"
1804 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1806 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1810 empathy_chat_window_init (EmpathyChatWindow *window)
1813 GtkAccelGroup *accel_group;
1818 GtkWidget *chat_vbox;
1820 EmpathySmileyManager *smiley_manager;
1821 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1822 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1824 window->priv = priv;
1825 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1826 gui = empathy_builder_get_file (filename,
1827 "chat_window", &priv->dialog,
1828 "chat_vbox", &chat_vbox,
1829 "ui_manager", &priv->ui_manager,
1830 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1831 "menu_conv_favorite", &priv->menu_conv_favorite,
1832 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1833 "menu_edit_cut", &priv->menu_edit_cut,
1834 "menu_edit_copy", &priv->menu_edit_copy,
1835 "menu_edit_paste", &priv->menu_edit_paste,
1836 "menu_edit_find", &priv->menu_edit_find,
1837 "menu_tabs_next", &priv->menu_tabs_next,
1838 "menu_tabs_prev", &priv->menu_tabs_prev,
1839 "menu_tabs_left", &priv->menu_tabs_left,
1840 "menu_tabs_right", &priv->menu_tabs_right,
1841 "menu_tabs_detach", &priv->menu_tabs_detach,
1845 empathy_builder_connect (gui, window,
1846 "menu_conv", "activate", chat_window_conv_activate_cb,
1847 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1848 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1849 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1850 "menu_conv_invite_participant", "activate", chat_window_invite_participant_activate_cb,
1851 "menu_conv_close", "activate", chat_window_close_activate_cb,
1852 "menu_edit", "activate", chat_window_edit_activate_cb,
1853 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1854 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1855 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1856 "menu_edit_find", "activate", chat_window_find_activate_cb,
1857 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1858 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1859 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1860 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1861 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1862 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1863 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1866 g_object_ref (priv->ui_manager);
1867 g_object_unref (gui);
1869 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1871 priv->notebook = gtk_notebook_new ();
1872 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1873 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1874 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1875 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1876 gtk_widget_show (priv->notebook);
1879 accel_group = gtk_accel_group_new ();
1880 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1882 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1883 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1886 gtk_accel_group_connect (accel_group,
1893 g_object_unref (accel_group);
1895 /* Set up drag target lists */
1896 priv->contact_targets = gtk_target_list_new (drag_types_dest_contact,
1897 G_N_ELEMENTS (drag_types_dest_contact));
1898 priv->file_targets = gtk_target_list_new (drag_types_dest_file,
1899 G_N_ELEMENTS (drag_types_dest_file));
1901 /* Set up smiley menu */
1902 smiley_manager = empathy_smiley_manager_dup_singleton ();
1903 submenu = empathy_smiley_menu_new (smiley_manager,
1904 chat_window_insert_smiley_activate_cb,
1906 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1907 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1908 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1909 g_object_unref (smiley_manager);
1911 /* Set up signals we can't do with ui file since we may need to
1912 * block/unblock them at some later stage.
1915 g_signal_connect (priv->dialog,
1917 G_CALLBACK (chat_window_delete_event_cb),
1919 g_signal_connect (priv->dialog,
1921 G_CALLBACK (chat_window_focus_in_event_cb),
1923 g_signal_connect_after (priv->notebook,
1925 G_CALLBACK (chat_window_page_switched_cb),
1927 g_signal_connect (priv->notebook,
1929 G_CALLBACK (chat_window_page_added_cb),
1931 g_signal_connect (priv->notebook,
1933 G_CALLBACK (chat_window_page_removed_cb),
1936 /* Set up drag and drop */
1937 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1938 GTK_DEST_DEFAULT_HIGHLIGHT,
1940 G_N_ELEMENTS (drag_types_dest),
1941 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1943 /* connect_after to allow GtkNotebook's built-in tab switching */
1944 g_signal_connect_after (priv->notebook,
1946 G_CALLBACK (chat_window_drag_motion),
1948 g_signal_connect (priv->notebook,
1949 "drag-data-received",
1950 G_CALLBACK (chat_window_drag_data_received),
1952 g_signal_connect (priv->notebook,
1954 G_CALLBACK (chat_window_drag_drop),
1957 chat_windows = g_list_prepend (chat_windows, window);
1959 /* Set up private details */
1961 priv->chats_new_msg = NULL;
1962 priv->chats_composing = NULL;
1963 priv->current_chat = NULL;
1965 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1969 empathy_chat_window_new (void)
1971 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1974 /* Returns the window to open a new tab in if there is only one window
1975 * visble, otherwise, returns NULL indicating that a new window should
1979 empathy_chat_window_get_default (gboolean room)
1982 gboolean separate_windows = TRUE;
1984 empathy_conf_get_bool (empathy_conf_get (),
1985 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1988 if (separate_windows) {
1989 /* Always create a new window */
1993 for (l = chat_windows; l; l = l->next) {
1994 EmpathyChatWindowPriv *priv;
1995 EmpathyChatWindow *chat_window;
1998 chat_window = l->data;
1999 priv = GET_PRIV (chat_window);
2001 dialog = empathy_chat_window_get_dialog (chat_window);
2002 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
2003 guint nb_rooms, nb_private;
2004 empathy_chat_window_get_nb_chats (chat_window, &nb_rooms, &nb_private);
2006 /* Skip the window if there aren't any rooms in it */
2007 if (room && nb_rooms == 0)
2010 /* Skip the window if there aren't any 1-1 chats in it */
2011 if (!room && nb_private == 0)
2014 /* Found a visible window on this desktop */
2023 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
2025 EmpathyChatWindowPriv *priv;
2027 g_return_val_if_fail (window != NULL, NULL);
2029 priv = GET_PRIV (window);
2031 return priv->dialog;
2035 empathy_chat_window_add_chat (EmpathyChatWindow *window,
2038 EmpathyChatWindowPriv *priv;
2040 GtkWidget *popup_label;
2042 GValue value = { 0, };
2044 g_return_if_fail (window != NULL);
2045 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2047 priv = GET_PRIV (window);
2049 /* Reference the chat object */
2050 g_object_ref (chat);
2052 /* If this window has just been created, position it */
2053 if (priv->chats == NULL) {
2054 const gchar *name = "chat-window";
2055 gboolean separate_windows;
2057 empathy_conf_get_bool (empathy_conf_get (),
2058 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
2061 if (separate_windows) {
2062 name = empathy_chat_get_id (chat);
2064 else if (empathy_chat_is_room (chat)) {
2065 name = "room-window";
2068 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
2071 child = GTK_WIDGET (chat);
2072 label = chat_window_create_label (window, chat, TRUE);
2073 popup_label = chat_window_create_label (window, chat, FALSE);
2074 gtk_widget_show (child);
2076 g_signal_connect (chat, "notify::name",
2077 G_CALLBACK (chat_window_chat_notify_cb),
2079 g_signal_connect (chat, "notify::subject",
2080 G_CALLBACK (chat_window_chat_notify_cb),
2082 g_signal_connect (chat, "notify::remote-contact",
2083 G_CALLBACK (chat_window_chat_notify_cb),
2085 chat_window_chat_notify_cb (chat);
2087 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
2088 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2089 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2090 g_value_init (&value, G_TYPE_BOOLEAN);
2091 g_value_set_boolean (&value, TRUE);
2092 gtk_container_child_set_property (GTK_CONTAINER (priv->notebook),
2093 child, "tab-expand" , &value);
2094 gtk_container_child_set_property (GTK_CONTAINER (priv->notebook),
2095 child, "tab-fill" , &value);
2096 g_value_unset (&value);
2098 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
2102 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
2105 EmpathyChatWindowPriv *priv;
2107 EmpathyContact *remote_contact;
2109 g_return_if_fail (window != NULL);
2110 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2112 priv = GET_PRIV (window);
2114 g_signal_handlers_disconnect_by_func (chat,
2115 chat_window_chat_notify_cb,
2117 remote_contact = g_object_get_data (G_OBJECT (chat),
2118 "chat-window-remote-contact");
2119 if (remote_contact) {
2120 g_signal_handlers_disconnect_by_func (remote_contact,
2121 chat_window_update_chat_tab,
2125 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2127 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
2129 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
2131 g_object_unref (chat);
2135 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
2136 EmpathyChatWindow *new_window,
2141 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
2142 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
2143 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2145 widget = GTK_WIDGET (chat);
2147 DEBUG ("Chat moving with widget:%p (%d references)", widget,
2148 G_OBJECT (widget)->ref_count);
2150 /* We reference here to make sure we don't loose the widget
2151 * and the EmpathyChat object during the move.
2153 g_object_ref (chat);
2154 g_object_ref (widget);
2156 empathy_chat_window_remove_chat (old_window, chat);
2157 empathy_chat_window_add_chat (new_window, chat);
2159 g_object_unref (widget);
2160 g_object_unref (chat);
2164 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
2167 EmpathyChatWindowPriv *priv;
2170 g_return_if_fail (window != NULL);
2171 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2173 priv = GET_PRIV (window);
2175 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2177 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
2182 empathy_chat_window_has_focus (EmpathyChatWindow *window)
2184 EmpathyChatWindowPriv *priv;
2187 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
2189 priv = GET_PRIV (window);
2191 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
2197 empathy_chat_window_find_chat (TpAccount *account,
2202 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
2204 for (l = chat_windows; l; l = l->next) {
2205 EmpathyChatWindowPriv *priv;
2206 EmpathyChatWindow *window;
2210 priv = GET_PRIV (window);
2212 for (ll = priv->chats; ll; ll = ll->next) {
2217 if (account == empathy_chat_get_account (chat) &&
2218 !tp_strdiff (id, empathy_chat_get_id (chat))) {
2228 empathy_chat_window_present_chat (EmpathyChat *chat)
2230 EmpathyChatWindow *window;
2231 EmpathyChatWindowPriv *priv;
2233 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2235 window = chat_window_find_chat (chat);
2237 /* If the chat has no window, create one */
2238 if (window == NULL) {
2239 window = empathy_chat_window_get_default (empathy_chat_is_room (chat));
2241 window = empathy_chat_window_new ();
2244 empathy_chat_window_add_chat (window, chat);
2247 priv = GET_PRIV (window);
2248 empathy_chat_window_switch_to_chat (window, chat);
2249 empathy_window_present (GTK_WINDOW (priv->dialog));
2251 gtk_widget_grab_focus (chat->input_text_view);
2255 empathy_chat_window_get_nb_chats (EmpathyChatWindow *self,
2259 EmpathyChatWindowPriv *priv = GET_PRIV (self);
2261 guint _nb_rooms = 0, _nb_private = 0;
2263 for (l = priv->chats; l != NULL; l = g_list_next (l)) {
2264 if (empathy_chat_is_room (EMPATHY_CHAT (l->data)))
2270 if (nb_rooms != NULL)
2271 *nb_rooms = _nb_rooms;
2272 if (nb_private != NULL)
2273 *nb_private = _nb_private;