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-dispatcher.h>
43 #include <libempathy/empathy-chatroom-manager.h>
44 #include <libempathy/empathy-utils.h>
46 #include <libempathy-gtk/empathy-images.h>
47 #include <libempathy-gtk/empathy-conf.h>
48 #include <libempathy-gtk/empathy-contact-dialogs.h>
49 #include <libempathy-gtk/empathy-log-window.h>
50 #include <libempathy-gtk/empathy-geometry.h>
51 #include <libempathy-gtk/empathy-smiley-manager.h>
52 #include <libempathy-gtk/empathy-sound.h>
53 #include <libempathy-gtk/empathy-ui-utils.h>
54 #include <libempathy-gtk/empathy-notify-manager.h>
56 #include <extensions/extensions.h>
58 #include "empathy-chat-window.h"
59 #include "empathy-about-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 (G_OBJECT (chat),
256 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
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 (G_OBJECT (chat),
268 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
270 g_object_set_data (G_OBJECT (chat),
271 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
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 (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
282 /* We don't want focus/keynav for the button to avoid clutter, and
283 * Ctrl-W works anyway.
285 gtk_widget_set_can_focus (close_button, FALSE);
286 gtk_widget_set_can_default (close_button, FALSE);
288 /* Set the name to make the special rc style match. */
289 gtk_widget_set_name (close_button, "empathy-close-button");
291 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
293 gtk_container_add (GTK_CONTAINER (close_button), close_image);
295 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
297 g_signal_connect (close_button,
299 G_CALLBACK (chat_window_close_clicked_cb),
302 /* React to theme changes and also setup the size correctly. */
303 g_signal_connect (hbox,
305 G_CALLBACK (chat_tab_style_set_cb),
309 gtk_widget_show_all (hbox);
315 _submenu_notify_visible_changed_cb (GObject *object,
319 g_signal_handlers_disconnect_by_func (object,
320 _submenu_notify_visible_changed_cb,
322 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
326 chat_window_menu_context_update (EmpathyChatWindowPriv *priv,
331 gboolean is_connected;
334 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
335 first_page = (page_num == 0);
336 last_page = (page_num == (num_pages - 1));
337 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
339 DEBUG ("Update window : Menu Contexts (Tabs & Conv)");
341 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
342 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
343 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
344 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
345 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
346 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
350 chat_window_contact_menu_update (EmpathyChatWindowPriv *priv,
351 EmpathyChatWindow *window)
353 GtkWidget *menu, *submenu, *orig_submenu;
355 menu = gtk_ui_manager_get_widget (priv->ui_manager,
356 "/chats_menubar/menu_contact");
357 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
359 DEBUG ("Update window : Contact Menu");
361 if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
362 submenu = empathy_chat_get_contact_menu (priv->current_chat);
363 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
364 gtk_widget_show (menu);
366 empathy_signal_connect_weak (orig_submenu,
368 (GCallback)_submenu_notify_visible_changed_cb,
374 get_all_unread_messages (EmpathyChatWindowPriv *priv)
379 for (l = priv->chats_new_msg; l != NULL; l = g_list_next (l)) {
380 EmpathyChat *chat = l->data;
382 nb += empathy_chat_get_nb_unread_messages (chat);
389 get_window_title_name (EmpathyChatWindowPriv *priv)
391 const gchar *active_name;
393 guint current_unread_msgs;
395 nb_chats = g_list_length (priv->chats);
396 g_assert (nb_chats > 0);
398 active_name = empathy_chat_get_name (priv->current_chat);
400 current_unread_msgs = empathy_chat_get_nb_unread_messages (
405 if (current_unread_msgs == 0)
406 return g_strdup (active_name);
408 return g_strdup_printf (ngettext (
410 "%s (%d unread)", current_unread_msgs),
411 active_name, current_unread_msgs);
413 guint nb_others = nb_chats - 1;
414 guint all_unread_msgs;
416 all_unread_msgs = get_all_unread_messages (priv);
418 if (all_unread_msgs == 0) {
419 /* no unread message */
420 return g_strdup_printf (ngettext (
422 "%s (and %u others)", nb_others),
423 active_name, nb_others);
426 else if (all_unread_msgs == current_unread_msgs) {
427 /* unread messages are in the current tab */
428 return g_strdup_printf (ngettext (
430 "%s (%d unread)", current_unread_msgs),
431 active_name, current_unread_msgs);
434 else if (current_unread_msgs == 0) {
435 /* unread messages are in other tabs */
436 return g_strdup_printf (ngettext (
437 "%s (%d unread from others)",
438 "%s (%d unread from others)",
440 active_name, all_unread_msgs);
444 /* unread messages are in all the tabs */
445 return g_strdup_printf (ngettext (
446 "%s (%d unread from all)",
447 "%s (%d unread from all)",
449 active_name, all_unread_msgs);
455 chat_window_title_update (EmpathyChatWindowPriv *priv)
459 DEBUG ("Update window : Title");
461 name = get_window_title_name (priv);
462 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
467 chat_window_icon_update (EmpathyChatWindowPriv *priv)
470 EmpathyContact *remote_contact;
471 gboolean avatar_in_icon;
474 n_chats = g_list_length (priv->chats);
476 DEBUG ("Update window : Icon");
478 /* Update window icon */
479 if (priv->chats_new_msg) {
480 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
481 EMPATHY_IMAGE_MESSAGE);
483 empathy_conf_get_bool (empathy_conf_get (),
484 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
487 if (n_chats == 1 && avatar_in_icon) {
488 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
489 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
490 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
493 g_object_unref (icon);
496 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
502 chat_window_close_button_update (EmpathyChatWindowPriv *priv,
506 GtkWidget *chat_close_button;
509 DEBUG ("Update window : Close Button");
511 if (num_pages == 1) {
512 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
513 chat_close_button = g_object_get_data (G_OBJECT (chat),
514 "chat-window-tab-close-button");
515 gtk_widget_hide (chat_close_button);
517 for (i=0; i<num_pages; i++) {
518 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
519 chat_close_button = g_object_get_data (G_OBJECT (chat),
520 "chat-window-tab-close-button");
521 gtk_widget_show (chat_close_button);
527 chat_window_update (EmpathyChatWindow *window)
529 EmpathyChatWindowPriv *priv = GET_PRIV (window);
532 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
534 DEBUG ("Update window");
536 /* Update Tab menu */
537 chat_window_menu_context_update (priv,
540 chat_window_contact_menu_update (priv,
543 chat_window_title_update (priv);
545 chat_window_icon_update (priv);
547 chat_window_close_button_update (priv,
552 append_markup_printf (GString *string,
559 va_start (args, format);
561 tmp = g_markup_vprintf_escaped (format, args);
562 g_string_append (string, tmp);
569 chat_window_update_chat_tab (EmpathyChat *chat)
571 EmpathyChatWindow *window;
572 EmpathyChatWindowPriv *priv;
573 EmpathyContact *remote_contact;
577 const gchar *subject;
578 const gchar *status = NULL;
582 const gchar *icon_name;
583 GtkWidget *tab_image;
584 GtkWidget *menu_image;
586 window = chat_window_find_chat (chat);
590 priv = GET_PRIV (window);
592 /* Get information */
593 name = empathy_chat_get_name (chat);
594 account = empathy_chat_get_account (chat);
595 subject = empathy_chat_get_subject (chat);
596 remote_contact = empathy_chat_get_remote_contact (chat);
598 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
599 name, tp_proxy_get_object_path (account), subject, remote_contact);
601 /* Update tab image */
602 if (empathy_chat_get_tp_chat (chat) == NULL) {
603 /* No TpChat, we are disconnected */
606 else if (g_list_find (priv->chats_new_msg, chat)) {
607 icon_name = EMPATHY_IMAGE_MESSAGE;
609 else if (g_list_find (priv->chats_composing, chat)) {
610 icon_name = EMPATHY_IMAGE_TYPING;
612 else if (remote_contact) {
613 icon_name = empathy_icon_name_for_contact (remote_contact);
615 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
618 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
619 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
620 if (icon_name != NULL) {
621 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
622 gtk_widget_show (tab_image);
623 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
624 gtk_widget_show (menu_image);
626 gtk_widget_hide (tab_image);
627 gtk_widget_hide (menu_image);
630 /* Update tab tooltip */
631 tooltip = g_string_new (NULL);
633 if (remote_contact) {
634 id = empathy_contact_get_id (remote_contact);
635 status = empathy_contact_get_presence_message (remote_contact);
640 append_markup_printf (tooltip,
641 "<b>%s</b><small> (%s)</small>",
643 tp_account_get_display_name (account));
645 if (!EMP_STR_EMPTY (status)) {
646 append_markup_printf (tooltip, "\n<i>%s</i>", status);
650 append_markup_printf (tooltip, "\n<b>%s</b> %s",
651 _("Topic:"), subject);
654 if (g_list_find (priv->chats_composing, chat)) {
655 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
658 markup = g_string_free (tooltip, FALSE);
659 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
660 gtk_widget_set_tooltip_markup (widget, markup);
661 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
662 gtk_widget_set_tooltip_markup (widget, markup);
665 /* Update tab and menu label */
666 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
667 gtk_label_set_text (GTK_LABEL (widget), name);
668 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
669 gtk_label_set_text (GTK_LABEL (widget), name);
671 /* Update the window if it's the current chat */
672 if (priv->current_chat == chat) {
673 chat_window_update (window);
678 chat_window_chat_notify_cb (EmpathyChat *chat)
680 EmpathyContact *old_remote_contact;
681 EmpathyContact *remote_contact = NULL;
683 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
684 remote_contact = empathy_chat_get_remote_contact (chat);
686 if (old_remote_contact != remote_contact) {
687 /* The remote-contact associated with the chat changed, we need
688 * to keep track of any change of that contact and update the
689 * window each time. */
690 if (remote_contact) {
691 g_signal_connect_swapped (remote_contact, "notify",
692 G_CALLBACK (chat_window_update_chat_tab),
695 if (old_remote_contact) {
696 g_signal_handlers_disconnect_by_func (old_remote_contact,
697 chat_window_update_chat_tab,
701 g_object_set_data_full (G_OBJECT (chat), "chat-window-remote-contact",
702 g_object_ref (remote_contact), (GDestroyNotify) g_object_unref);
705 chat_window_update_chat_tab (chat);
709 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
710 EmpathySmiley *smiley,
713 EmpathyChatWindowPriv *priv = GET_PRIV (window);
715 GtkTextBuffer *buffer;
718 chat = priv->current_chat;
720 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
721 gtk_text_buffer_get_end_iter (buffer, &iter);
722 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
726 chat_window_conv_activate_cb (GtkAction *action,
727 EmpathyChatWindow *window)
729 EmpathyChatWindowPriv *priv = GET_PRIV (window);
732 EmpathyContact *remote_contact = NULL;
734 /* Favorite room menu */
735 is_room = empathy_chat_is_room (priv->current_chat);
739 gboolean found = FALSE;
740 EmpathyChatroom *chatroom;
742 room = empathy_chat_get_id (priv->current_chat);
743 account = empathy_chat_get_account (priv->current_chat);
744 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
746 if (chatroom != NULL)
747 found = empathy_chatroom_is_favorite (chatroom);
749 DEBUG ("This room %s favorite", found ? "is" : "is not");
750 gtk_toggle_action_set_active (
751 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
753 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
755 /* Show contacts menu */
756 g_object_get (priv->current_chat,
757 "remote-contact", &remote_contact,
758 "show-contacts", &active,
760 if (remote_contact == NULL) {
761 gtk_toggle_action_set_active (
762 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
765 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
766 (remote_contact == NULL));
767 if (remote_contact != NULL) {
768 g_object_unref (remote_contact);
773 chat_window_clear_activate_cb (GtkAction *action,
774 EmpathyChatWindow *window)
776 EmpathyChatWindowPriv *priv = GET_PRIV (window);
778 empathy_chat_clear (priv->current_chat);
782 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
783 EmpathyChatWindow *window)
785 EmpathyChatWindowPriv *priv = GET_PRIV (window);
789 EmpathyChatroom *chatroom;
791 active = gtk_toggle_action_get_active (toggle_action);
792 account = empathy_chat_get_account (priv->current_chat);
793 room = empathy_chat_get_id (priv->current_chat);
795 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
798 if (chatroom == NULL) {
801 name = empathy_chat_get_name (priv->current_chat);
802 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
803 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
804 g_object_unref (chatroom);
807 empathy_chatroom_set_favorite (chatroom, active);
811 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
812 EmpathyChatWindow *window)
814 EmpathyChatWindowPriv *priv = GET_PRIV (window);
817 active = gtk_toggle_action_get_active (toggle_action);
819 empathy_chat_set_show_contacts (priv->current_chat, active);
823 upgrade_to_muc_cb (TpConnection *connection,
825 const char *object_path,
826 GHashTable *properties,
833 g_critical ("%s", error->message);
837 g_print ("GOT CHANNEL! %s\n", object_path);
841 chat_window_invite_participant_activate_cb (GtkAction *action,
842 EmpathyChatWindow *window)
844 EmpathyChatWindowPriv *priv;
845 EmpathyTpChat *tp_chat;
846 TpConnection *connection;
850 char *invitees[3] = { NULL, };
852 g_print ("INVITE PARTICIPANT\n");
854 priv = GET_PRIV (window);
856 g_return_if_fail (priv->current_chat != NULL);
858 /* FIXME: this is for upgrading a 1-to-1 channel to a MUC, inviting
859 * a user to a MUC is much easier, and needs to be written */
861 tp_chat = empathy_chat_get_tp_chat (priv->current_chat);
862 connection = empathy_tp_chat_get_connection (tp_chat);
863 channel = empathy_tp_chat_get_channel (tp_chat);
865 /* Ensure a MUC channel */
866 channels = g_ptr_array_sized_new (1);
867 g_ptr_array_add (channels, (char *) tp_proxy_get_object_path (channel));
869 invitees[0] = (char *) tp_channel_get_identifier (channel);
870 // invitees[1] = /* FIXME: ask for this */
873 TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
874 TP_IFACE_CHANNEL_TYPE_TEXT,
875 TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT,
877 EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialChannels",
878 TP_ARRAY_TYPE_OBJECT_PATH_LIST, channels,
879 EMP_IFACE_CHANNEL_INTERFACE_CONFERENCE ".InitialInviteeIDs",
880 G_TYPE_STRV, invitees,
881 /* FIXME: InvitationMessage ? */
884 /* FIXME: this probably needs to go through EmpathyDispatcher */
885 tp_cli_connection_interface_requests_call_ensure_channel (
886 connection, -1, props, upgrade_to_muc_cb, NULL, NULL,
889 g_hash_table_destroy (props);
890 g_ptr_array_free (channels, TRUE);
894 chat_window_close_activate_cb (GtkAction *action,
895 EmpathyChatWindow *window)
897 EmpathyChatWindowPriv *priv;
899 priv = GET_PRIV (window);
901 g_return_if_fail (priv->current_chat != NULL);
903 empathy_chat_window_remove_chat (window, priv->current_chat);
907 chat_window_edit_activate_cb (GtkAction *action,
908 EmpathyChatWindow *window)
910 EmpathyChatWindowPriv *priv;
911 GtkClipboard *clipboard;
912 GtkTextBuffer *buffer;
913 gboolean text_available;
915 priv = GET_PRIV (window);
917 g_return_if_fail (priv->current_chat != NULL);
919 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
920 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
921 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
922 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
926 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
927 if (gtk_text_buffer_get_has_selection (buffer)) {
928 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
929 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
933 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
935 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
936 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
939 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
940 text_available = gtk_clipboard_wait_is_text_available (clipboard);
941 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
945 chat_window_cut_activate_cb (GtkAction *action,
946 EmpathyChatWindow *window)
948 EmpathyChatWindowPriv *priv;
950 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
952 priv = GET_PRIV (window);
954 empathy_chat_cut (priv->current_chat);
958 chat_window_copy_activate_cb (GtkAction *action,
959 EmpathyChatWindow *window)
961 EmpathyChatWindowPriv *priv;
963 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
965 priv = GET_PRIV (window);
967 empathy_chat_copy (priv->current_chat);
971 chat_window_paste_activate_cb (GtkAction *action,
972 EmpathyChatWindow *window)
974 EmpathyChatWindowPriv *priv;
976 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
978 priv = GET_PRIV (window);
980 empathy_chat_paste (priv->current_chat);
984 chat_window_tabs_next_activate_cb (GtkAction *action,
985 EmpathyChatWindow *window)
987 EmpathyChatWindowPriv *priv;
989 gint index_, numPages;
991 priv = GET_PRIV (window);
993 chat = priv->current_chat;
994 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
995 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
997 if (index_ == (numPages - 1)) {
998 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
1002 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
1006 chat_window_tabs_previous_activate_cb (GtkAction *action,
1007 EmpathyChatWindow *window)
1009 EmpathyChatWindowPriv *priv;
1011 gint index_, numPages;
1013 priv = GET_PRIV (window);
1015 chat = priv->current_chat;
1016 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1017 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1020 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
1024 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
1028 chat_window_tabs_left_activate_cb (GtkAction *action,
1029 EmpathyChatWindow *window)
1031 EmpathyChatWindowPriv *priv;
1035 priv = GET_PRIV (window);
1037 chat = priv->current_chat;
1038 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1043 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1049 chat_window_tabs_right_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));
1061 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1067 chat_window_detach_activate_cb (GtkAction *action,
1068 EmpathyChatWindow *window)
1070 EmpathyChatWindowPriv *priv;
1071 EmpathyChatWindow *new_window;
1074 priv = GET_PRIV (window);
1076 chat = priv->current_chat;
1077 new_window = empathy_chat_window_new ();
1079 empathy_chat_window_move_chat (window, new_window, chat);
1081 priv = GET_PRIV (new_window);
1082 gtk_widget_show (priv->dialog);
1086 chat_window_help_contents_activate_cb (GtkAction *action,
1087 EmpathyChatWindow *window)
1089 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1091 empathy_url_show (priv->dialog, "ghelp:empathy");
1095 chat_window_help_about_activate_cb (GtkAction *action,
1096 EmpathyChatWindow *window)
1098 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1100 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1104 chat_window_delete_event_cb (GtkWidget *dialog,
1106 EmpathyChatWindow *window)
1108 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1110 DEBUG ("Delete event received");
1112 g_object_ref (window);
1113 while (priv->chats) {
1114 empathy_chat_window_remove_chat (window, priv->chats->data);
1116 g_object_unref (window);
1122 chat_window_composing_cb (EmpathyChat *chat,
1123 gboolean is_composing,
1124 EmpathyChatWindow *window)
1126 EmpathyChatWindowPriv *priv;
1128 priv = GET_PRIV (window);
1130 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1131 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1133 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1136 chat_window_update_chat_tab (chat);
1140 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1143 EmpathyChatWindowPriv *priv;
1145 priv = GET_PRIV (window);
1147 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
1148 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1152 free_notification_data (NotificationData *data)
1154 g_object_unref (data->chat);
1155 g_slice_free (NotificationData, data);
1159 chat_window_notification_closed_cb (NotifyNotification *notify,
1160 NotificationData *cb_data)
1162 EmpathyNotificationClosedReason reason = 0;
1163 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1165 #ifdef notify_notification_get_closed_reason
1166 reason = notify_notification_get_closed_reason (notify);
1168 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1169 empathy_chat_window_present_chat (cb_data->chat);
1172 g_object_unref (notify);
1173 priv->notification = NULL;
1174 free_notification_data (cb_data);
1175 priv->notification_data = NULL;
1179 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1180 EmpathyMessage *message,
1183 EmpathyContact *sender;
1184 const gchar *header;
1188 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1191 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1194 empathy_conf_get_bool (empathy_conf_get (),
1195 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1201 sender = empathy_message_get_sender (message);
1202 header = empathy_contact_get_name (sender);
1203 body = empathy_message_get_body (message);
1204 escaped = g_markup_escape_text (body, -1);
1206 if (priv->notification != NULL) {
1207 notify_notification_update (priv->notification,
1208 header, escaped, NULL);
1210 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1212 cb_data->chat = g_object_ref (chat);
1213 cb_data->window = window;
1215 priv->notification_data = cb_data;
1216 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1217 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1219 g_signal_connect (priv->notification, "closed",
1220 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1223 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1224 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1226 if (pixbuf != NULL) {
1227 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1228 g_object_unref (pixbuf);
1231 notify_notification_show (priv->notification, NULL);
1237 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1242 if (!empathy_chat_is_room (chat))
1245 markup = g_markup_printf_escaped (
1246 "<span color=\"red\" weight=\"bold\">%s</span>",
1247 empathy_chat_get_name (chat));
1249 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1250 gtk_label_set_markup (GTK_LABEL (widget), markup);
1255 chat_window_new_message_cb (EmpathyChat *chat,
1256 EmpathyMessage *message,
1257 EmpathyChatWindow *window)
1259 EmpathyChatWindowPriv *priv;
1261 gboolean needs_urgency;
1262 EmpathyContact *sender;
1264 priv = GET_PRIV (window);
1266 has_focus = empathy_chat_window_has_focus (window);
1268 /* - if we're the sender, we play the sound if it's specified in the
1269 * preferences and we're not away.
1270 * - if we receive a message, we play the sound if it's specified in the
1271 * preferences and the window does not have focus on the chat receiving
1275 sender = empathy_message_get_sender (message);
1277 if (empathy_contact_is_user (sender)) {
1278 empathy_sound_play (GTK_WIDGET (priv->dialog),
1279 EMPATHY_SOUND_MESSAGE_OUTGOING);
1282 if (has_focus && priv->current_chat == chat) {
1283 /* window and tab are focused so consider the message to be read */
1284 empathy_chat_messages_read (chat);
1288 if (!g_list_find (priv->chats_new_msg, chat)) {
1289 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1290 chat_window_update_chat_tab (chat);
1293 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1294 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1295 * an unamed MUC (msn-like).
1296 * In case of a MUC, we set urgency only if the message contains our
1298 if (empathy_chat_is_room (chat) ||
1299 empathy_chat_get_remote_contact (chat) == NULL) {
1300 needs_urgency = empathy_message_should_highlight (message);
1302 needs_urgency = TRUE;
1305 if (needs_urgency) {
1307 chat_window_set_urgency_hint (window, TRUE);
1308 chat_window_set_highlight_room_tab_label (chat);
1311 empathy_sound_play (GTK_WIDGET (priv->dialog),
1312 EMPATHY_SOUND_MESSAGE_INCOMING);
1313 chat_window_show_or_update_notification (window, message, chat);
1316 /* update the number of unread messages */
1317 chat_window_title_update (priv);
1320 static GtkNotebook *
1321 chat_window_detach_hook (GtkNotebook *source,
1327 EmpathyChatWindowPriv *priv;
1328 EmpathyChatWindow *window, *new_window;
1331 chat = EMPATHY_CHAT (page);
1332 window = chat_window_find_chat (chat);
1334 new_window = empathy_chat_window_new ();
1335 priv = GET_PRIV (new_window);
1337 DEBUG ("Detach hook called");
1339 empathy_chat_window_move_chat (window, new_window, chat);
1341 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1342 gtk_widget_show (priv->dialog);
1348 chat_window_page_switched_cb (GtkNotebook *notebook,
1349 GtkNotebookPage *page,
1351 EmpathyChatWindow *window)
1353 EmpathyChatWindowPriv *priv;
1357 DEBUG ("Page switched");
1359 priv = GET_PRIV (window);
1361 child = gtk_notebook_get_nth_page (notebook, page_num);
1362 chat = EMPATHY_CHAT (child);
1364 if (priv->page_added) {
1365 priv->page_added = FALSE;
1366 empathy_chat_scroll_down (chat);
1368 else if (priv->current_chat == chat) {
1372 priv->current_chat = chat;
1373 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1374 empathy_chat_messages_read (chat);
1376 chat_window_update_chat_tab (chat);
1380 chat_window_page_added_cb (GtkNotebook *notebook,
1383 EmpathyChatWindow *window)
1385 EmpathyChatWindowPriv *priv;
1388 priv = GET_PRIV (window);
1390 /* If we just received DND to the same window, we don't want
1391 * to do anything here like removing the tab and then readding
1392 * it, so we return here and in "page-added".
1394 if (priv->dnd_same_window) {
1395 DEBUG ("Page added (back to the same window)");
1396 priv->dnd_same_window = FALSE;
1400 DEBUG ("Page added");
1402 /* Get chat object */
1403 chat = EMPATHY_CHAT (child);
1405 /* Connect chat signals for this window */
1406 g_signal_connect (chat, "composing",
1407 G_CALLBACK (chat_window_composing_cb),
1409 g_signal_connect (chat, "new-message",
1410 G_CALLBACK (chat_window_new_message_cb),
1412 g_signal_connect (chat, "notify::tp-chat",
1413 G_CALLBACK (chat_window_update_chat_tab),
1416 /* Set flag so we know to perform some special operations on
1417 * switch page due to the new page being added.
1419 priv->page_added = TRUE;
1421 /* Get list of chats up to date */
1422 priv->chats = g_list_append (priv->chats, chat);
1424 chat_window_update_chat_tab (chat);
1428 chat_window_page_removed_cb (GtkNotebook *notebook,
1431 EmpathyChatWindow *window)
1433 EmpathyChatWindowPriv *priv;
1436 priv = GET_PRIV (window);
1438 /* If we just received DND to the same window, we don't want
1439 * to do anything here like removing the tab and then readding
1440 * it, so we return here and in "page-added".
1442 if (priv->dnd_same_window) {
1443 DEBUG ("Page removed (and will be readded to same window)");
1447 DEBUG ("Page removed");
1449 /* Get chat object */
1450 chat = EMPATHY_CHAT (child);
1452 /* Disconnect all signal handlers for this chat and this window */
1453 g_signal_handlers_disconnect_by_func (chat,
1454 G_CALLBACK (chat_window_composing_cb),
1456 g_signal_handlers_disconnect_by_func (chat,
1457 G_CALLBACK (chat_window_new_message_cb),
1459 g_signal_handlers_disconnect_by_func (chat,
1460 G_CALLBACK (chat_window_update_chat_tab),
1463 /* Keep list of chats up to date */
1464 priv->chats = g_list_remove (priv->chats, chat);
1465 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1466 empathy_chat_messages_read (chat);
1467 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1469 if (priv->chats == NULL) {
1470 g_object_unref (window);
1472 chat_window_update (window);
1477 chat_window_focus_in_event_cb (GtkWidget *widget,
1479 EmpathyChatWindow *window)
1481 EmpathyChatWindowPriv *priv;
1483 DEBUG ("Focus in event, updating title");
1485 priv = GET_PRIV (window);
1487 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1488 empathy_chat_messages_read (priv->current_chat);
1490 chat_window_set_urgency_hint (window, FALSE);
1492 /* Update the title, since we now mark all unread messages as read. */
1493 chat_window_update_chat_tab (priv->current_chat);
1499 chat_window_drag_motion (GtkWidget *widget,
1500 GdkDragContext *context,
1504 EmpathyChatWindow *window)
1507 EmpathyChatWindowPriv *priv;
1508 GdkAtom dest_target;
1510 priv = GET_PRIV (window);
1512 target = gtk_drag_dest_find_target (widget, context, NULL);
1514 dest_target = gdk_atom_intern_static_string ("text/uri-list");
1515 if (target == dest_target) {
1516 /* This is a file drag. Ensure the contact is online and set the
1517 drag type to COPY. Note that it's possible that the tab will
1518 be switched by GTK+ after a timeout from drag_motion without
1519 getting another drag_motion to disable the drop. You have
1520 to hold your mouse really still.
1522 EmpathyContact *contact;
1524 priv = GET_PRIV (window);
1525 contact = empathy_chat_get_remote_contact (priv->current_chat);
1526 /* contact is NULL for multi-user chats. We don't do
1527 * file transfers to MUCs. We also don't send files
1528 * to offline contacts or contacts that don't support
1531 if ((contact == NULL) || !empathy_contact_is_online (contact)) {
1532 gdk_drag_status (context, 0, time_);
1535 if (!(empathy_contact_get_capabilities (contact)
1536 & EMPATHY_CAPABILITIES_FT)) {
1537 gdk_drag_status (context, 0, time_);
1540 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1544 dest_target = gdk_atom_intern_static_string ("text/contact-id");
1545 if (target == dest_target) {
1546 /* This is a drag of a contact from a contact list. Set to COPY.
1547 FIXME: If this drag is to a MUC window, it invites the user.
1548 Otherwise, it opens a chat. Should we use a different drag
1549 type for invites? Should we allow ASK?
1551 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1555 /* Otherwise, it must be a notebook tab drag. Set to MOVE. */
1556 gdk_drag_status (context, GDK_ACTION_MOVE, time_);
1561 chat_window_drag_data_received (GtkWidget *widget,
1562 GdkDragContext *context,
1565 GtkSelectionData *selection,
1568 EmpathyChatWindow *window)
1570 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1571 EmpathyChat *chat = NULL;
1572 EmpathyChatWindow *old_window;
1573 TpAccount *account = NULL;
1574 TpAccountManager *account_manager;
1577 const gchar *account_id;
1578 const gchar *contact_id;
1580 id = (const gchar*) gtk_selection_data_get_data (selection);
1582 /* FIXME: Perhaps should be sure that the account manager is
1583 * prepared before calling _ensure_account on it. */
1584 account_manager = tp_account_manager_dup ();
1586 DEBUG ("DND contact from roster with id:'%s'", id);
1588 strv = g_strsplit (id, ":", 2);
1589 if (g_strv_length (strv) == 2) {
1590 account_id = strv[0];
1591 contact_id = strv[1];
1593 tp_account_manager_ensure_account (account_manager, account_id);
1594 if (account != NULL)
1595 chat = empathy_chat_window_find_chat (account, contact_id);
1598 if (account == NULL) {
1600 gtk_drag_finish (context, FALSE, FALSE, time_);
1605 TpConnection *connection;
1607 connection = tp_account_get_connection (account);
1610 empathy_dispatcher_chat_with_contact_id (
1611 connection, contact_id, NULL, NULL);
1617 g_object_unref (account_manager);
1620 old_window = chat_window_find_chat (chat);
1622 if (old_window == window) {
1623 gtk_drag_finish (context, TRUE, FALSE, time_);
1627 empathy_chat_window_move_chat (old_window, window, chat);
1629 empathy_chat_window_add_chat (window, chat);
1632 /* Added to take care of any outstanding chat events */
1633 empathy_chat_window_present_chat (chat);
1635 /* We should return TRUE to remove the data when doing
1636 * GDK_ACTION_MOVE, but we don't here otherwise it has
1637 * weird consequences, and we handle that internally
1638 * anyway with add_chat () and remove_chat ().
1640 gtk_drag_finish (context, TRUE, FALSE, time_);
1642 else if (info == DND_DRAG_TYPE_URI_LIST) {
1643 EmpathyChatWindowPriv *priv;
1644 EmpathyContact *contact;
1647 priv = GET_PRIV (window);
1648 contact = empathy_chat_get_remote_contact (priv->current_chat);
1650 /* contact is NULL when current_chat is a multi-user chat.
1651 * We don't do file transfers to MUCs, so just cancel the drag.
1653 if (contact == NULL) {
1654 gtk_drag_finish (context, TRUE, FALSE, time_);
1658 data = (const gchar *) gtk_selection_data_get_data (selection);
1659 empathy_send_file_from_uri_list (contact, data);
1661 gtk_drag_finish (context, TRUE, FALSE, time_);
1663 else if (info == DND_DRAG_TYPE_TAB) {
1665 EmpathyChatWindow *old_window = NULL;
1669 chat = (void *) gtk_selection_data_get_data (selection);
1670 old_window = chat_window_find_chat (*chat);
1673 EmpathyChatWindowPriv *priv;
1675 priv = GET_PRIV (window);
1677 if (old_window == window) {
1678 DEBUG ("DND tab (within same window)");
1679 priv->dnd_same_window = TRUE;
1680 gtk_drag_finish (context, TRUE, FALSE, time_);
1684 priv->dnd_same_window = FALSE;
1687 /* We should return TRUE to remove the data when doing
1688 * GDK_ACTION_MOVE, but we don't here otherwise it has
1689 * weird consequences, and we handle that internally
1690 * anyway with add_chat () and remove_chat ().
1692 gtk_drag_finish (context, TRUE, FALSE, time_);
1694 DEBUG ("DND from unknown source");
1695 gtk_drag_finish (context, FALSE, FALSE, time_);
1700 chat_window_finalize (GObject *object)
1702 EmpathyChatWindow *window;
1703 EmpathyChatWindowPriv *priv;
1705 window = EMPATHY_CHAT_WINDOW (object);
1706 priv = GET_PRIV (window);
1708 DEBUG ("Finalized: %p", object);
1710 g_object_unref (priv->ui_manager);
1711 g_object_unref (priv->chatroom_manager);
1712 g_object_unref (priv->notify_mgr);
1714 if (priv->notification != NULL) {
1715 notify_notification_close (priv->notification, NULL);
1716 g_object_unref (priv->notification);
1717 priv->notification = NULL;
1718 if (priv->notification_data != NULL)
1720 free_notification_data (priv->notification_data);
1721 priv->notification_data = NULL;
1725 if (priv->contact_targets) {
1726 gtk_target_list_unref (priv->contact_targets);
1728 if (priv->file_targets) {
1729 gtk_target_list_unref (priv->file_targets);
1732 chat_windows = g_list_remove (chat_windows, window);
1733 gtk_widget_destroy (priv->dialog);
1735 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1739 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1741 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1743 object_class->finalize = chat_window_finalize;
1745 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1747 /* Set up a style for the close button with no focus padding. */
1748 gtk_rc_parse_string (
1749 "style \"empathy-close-button-style\"\n"
1751 " GtkWidget::focus-padding = 0\n"
1755 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1757 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1761 empathy_chat_window_init (EmpathyChatWindow *window)
1764 GtkAccelGroup *accel_group;
1769 GtkWidget *chat_vbox;
1771 EmpathySmileyManager *smiley_manager;
1772 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1773 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1775 window->priv = priv;
1776 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1777 gui = empathy_builder_get_file (filename,
1778 "chat_window", &priv->dialog,
1779 "chat_vbox", &chat_vbox,
1780 "ui_manager", &priv->ui_manager,
1781 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1782 "menu_conv_favorite", &priv->menu_conv_favorite,
1783 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1784 "menu_edit_cut", &priv->menu_edit_cut,
1785 "menu_edit_copy", &priv->menu_edit_copy,
1786 "menu_edit_paste", &priv->menu_edit_paste,
1787 "menu_tabs_next", &priv->menu_tabs_next,
1788 "menu_tabs_prev", &priv->menu_tabs_prev,
1789 "menu_tabs_left", &priv->menu_tabs_left,
1790 "menu_tabs_right", &priv->menu_tabs_right,
1791 "menu_tabs_detach", &priv->menu_tabs_detach,
1795 empathy_builder_connect (gui, window,
1796 "menu_conv", "activate", chat_window_conv_activate_cb,
1797 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1798 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1799 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1800 "menu_conv_invite_participant", "activate", chat_window_invite_participant_activate_cb,
1801 "menu_conv_close", "activate", chat_window_close_activate_cb,
1802 "menu_edit", "activate", chat_window_edit_activate_cb,
1803 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1804 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1805 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1806 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1807 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1808 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1809 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1810 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1811 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1812 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1815 g_object_ref (priv->ui_manager);
1816 g_object_unref (gui);
1818 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1820 priv->notebook = gtk_notebook_new ();
1821 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1822 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1823 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1824 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1825 gtk_widget_show (priv->notebook);
1828 accel_group = gtk_accel_group_new ();
1829 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1831 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1832 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1835 gtk_accel_group_connect (accel_group,
1842 g_object_unref (accel_group);
1844 /* Set up drag target lists */
1845 priv->contact_targets = gtk_target_list_new (drag_types_dest_contact,
1846 G_N_ELEMENTS (drag_types_dest_contact));
1847 priv->file_targets = gtk_target_list_new (drag_types_dest_file,
1848 G_N_ELEMENTS (drag_types_dest_file));
1850 /* Set up smiley menu */
1851 smiley_manager = empathy_smiley_manager_dup_singleton ();
1852 submenu = empathy_smiley_menu_new (smiley_manager,
1853 chat_window_insert_smiley_activate_cb,
1855 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1856 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1857 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1858 g_object_unref (smiley_manager);
1860 /* Set up signals we can't do with ui file since we may need to
1861 * block/unblock them at some later stage.
1864 g_signal_connect (priv->dialog,
1866 G_CALLBACK (chat_window_delete_event_cb),
1868 g_signal_connect (priv->dialog,
1870 G_CALLBACK (chat_window_focus_in_event_cb),
1872 g_signal_connect_after (priv->notebook,
1874 G_CALLBACK (chat_window_page_switched_cb),
1876 g_signal_connect (priv->notebook,
1878 G_CALLBACK (chat_window_page_added_cb),
1880 g_signal_connect (priv->notebook,
1882 G_CALLBACK (chat_window_page_removed_cb),
1885 /* Set up drag and drop */
1886 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1887 GTK_DEST_DEFAULT_ALL,
1889 G_N_ELEMENTS (drag_types_dest),
1890 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1892 /* connect_after to allow GtkNotebook's built-in tab switching */
1893 g_signal_connect_after (priv->notebook,
1895 G_CALLBACK (chat_window_drag_motion),
1897 g_signal_connect (priv->notebook,
1898 "drag-data-received",
1899 G_CALLBACK (chat_window_drag_data_received),
1902 chat_windows = g_list_prepend (chat_windows, window);
1904 /* Set up private details */
1906 priv->chats_new_msg = NULL;
1907 priv->chats_composing = NULL;
1908 priv->current_chat = NULL;
1910 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1914 empathy_chat_window_new (void)
1916 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1919 /* Returns the window to open a new tab in if there is only one window
1920 * visble, otherwise, returns NULL indicating that a new window should
1924 empathy_chat_window_get_default (gboolean room)
1927 gboolean separate_windows = TRUE;
1929 empathy_conf_get_bool (empathy_conf_get (),
1930 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1933 if (separate_windows) {
1934 /* Always create a new window */
1938 for (l = chat_windows; l; l = l->next) {
1939 EmpathyChatWindowPriv *priv;
1940 EmpathyChatWindow *chat_window;
1943 chat_window = l->data;
1944 priv = GET_PRIV (chat_window);
1946 dialog = empathy_chat_window_get_dialog (chat_window);
1947 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1948 guint nb_rooms, nb_private;
1949 empathy_chat_window_get_nb_chats (chat_window, &nb_rooms, &nb_private);
1951 /* Skip the window if there aren't any rooms in it */
1952 if (room && nb_rooms == 0)
1955 /* Skip the window if there aren't any 1-1 chats in it */
1956 if (!room && nb_private == 0)
1959 /* Found a visible window on this desktop */
1968 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1970 EmpathyChatWindowPriv *priv;
1972 g_return_val_if_fail (window != NULL, NULL);
1974 priv = GET_PRIV (window);
1976 return priv->dialog;
1980 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1983 EmpathyChatWindowPriv *priv;
1985 GtkWidget *popup_label;
1988 g_return_if_fail (window != NULL);
1989 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1991 priv = GET_PRIV (window);
1993 /* Reference the chat object */
1994 g_object_ref (chat);
1996 /* If this window has just been created, position it */
1997 if (priv->chats == NULL) {
1998 const gchar *name = "chat-window";
1999 gboolean separate_windows;
2001 empathy_conf_get_bool (empathy_conf_get (),
2002 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
2005 if (separate_windows) {
2006 name = empathy_chat_get_id (chat);
2008 else if (empathy_chat_is_room (chat)) {
2009 name = "room-window";
2012 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
2015 child = GTK_WIDGET (chat);
2016 label = chat_window_create_label (window, chat, TRUE);
2017 popup_label = chat_window_create_label (window, chat, FALSE);
2018 gtk_widget_show (child);
2020 g_signal_connect (chat, "notify::name",
2021 G_CALLBACK (chat_window_chat_notify_cb),
2023 g_signal_connect (chat, "notify::subject",
2024 G_CALLBACK (chat_window_chat_notify_cb),
2026 g_signal_connect (chat, "notify::remote-contact",
2027 G_CALLBACK (chat_window_chat_notify_cb),
2029 chat_window_chat_notify_cb (chat);
2031 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
2032 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2033 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2034 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
2035 TRUE, TRUE, GTK_PACK_START);
2037 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
2041 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
2044 EmpathyChatWindowPriv *priv;
2046 EmpathyContact *remote_contact;
2048 g_return_if_fail (window != NULL);
2049 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2051 priv = GET_PRIV (window);
2053 g_signal_handlers_disconnect_by_func (chat,
2054 chat_window_chat_notify_cb,
2056 remote_contact = g_object_get_data (G_OBJECT (chat),
2057 "chat-window-remote-contact");
2058 if (remote_contact) {
2059 g_signal_handlers_disconnect_by_func (remote_contact,
2060 chat_window_update_chat_tab,
2064 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2066 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
2068 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
2070 g_object_unref (chat);
2074 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
2075 EmpathyChatWindow *new_window,
2080 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
2081 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
2082 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2084 widget = GTK_WIDGET (chat);
2086 DEBUG ("Chat moving with widget:%p (%d references)", widget,
2087 G_OBJECT (widget)->ref_count);
2089 /* We reference here to make sure we don't loose the widget
2090 * and the EmpathyChat object during the move.
2092 g_object_ref (chat);
2093 g_object_ref (widget);
2095 empathy_chat_window_remove_chat (old_window, chat);
2096 empathy_chat_window_add_chat (new_window, chat);
2098 g_object_unref (widget);
2099 g_object_unref (chat);
2103 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
2106 EmpathyChatWindowPriv *priv;
2109 g_return_if_fail (window != NULL);
2110 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2112 priv = GET_PRIV (window);
2114 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2116 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
2121 empathy_chat_window_has_focus (EmpathyChatWindow *window)
2123 EmpathyChatWindowPriv *priv;
2126 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
2128 priv = GET_PRIV (window);
2130 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
2136 empathy_chat_window_find_chat (TpAccount *account,
2141 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
2143 for (l = chat_windows; l; l = l->next) {
2144 EmpathyChatWindowPriv *priv;
2145 EmpathyChatWindow *window;
2149 priv = GET_PRIV (window);
2151 for (ll = priv->chats; ll; ll = ll->next) {
2156 if (account == empathy_chat_get_account (chat) &&
2157 !tp_strdiff (id, empathy_chat_get_id (chat))) {
2167 empathy_chat_window_present_chat (EmpathyChat *chat)
2169 EmpathyChatWindow *window;
2170 EmpathyChatWindowPriv *priv;
2172 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2174 window = chat_window_find_chat (chat);
2176 /* If the chat has no window, create one */
2177 if (window == NULL) {
2178 window = empathy_chat_window_get_default (empathy_chat_is_room (chat));
2180 window = empathy_chat_window_new ();
2183 empathy_chat_window_add_chat (window, chat);
2186 priv = GET_PRIV (window);
2187 empathy_chat_window_switch_to_chat (window, chat);
2188 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
2190 gtk_widget_grab_focus (chat->input_text_view);
2194 empathy_chat_window_get_nb_chats (EmpathyChatWindow *self,
2198 EmpathyChatWindowPriv *priv = GET_PRIV (self);
2200 guint _nb_rooms = 0, _nb_private = 0;
2202 for (l = priv->chats; l != NULL; l = g_list_next (l)) {
2203 if (empathy_chat_is_room (EMPATHY_CHAT (l->data)))
2209 if (nb_rooms != NULL)
2210 *nb_rooms = _nb_rooms;
2211 if (nb_private != NULL)
2212 *nb_private = _nb_private;