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);
893 chat_window_close_activate_cb (GtkAction *action,
894 EmpathyChatWindow *window)
896 EmpathyChatWindowPriv *priv;
898 priv = GET_PRIV (window);
900 g_return_if_fail (priv->current_chat != NULL);
902 empathy_chat_window_remove_chat (window, priv->current_chat);
906 chat_window_edit_activate_cb (GtkAction *action,
907 EmpathyChatWindow *window)
909 EmpathyChatWindowPriv *priv;
910 GtkClipboard *clipboard;
911 GtkTextBuffer *buffer;
912 gboolean text_available;
914 priv = GET_PRIV (window);
916 g_return_if_fail (priv->current_chat != NULL);
918 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
919 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
920 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
921 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
925 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
926 if (gtk_text_buffer_get_has_selection (buffer)) {
927 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
928 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
932 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
934 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
935 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
938 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
939 text_available = gtk_clipboard_wait_is_text_available (clipboard);
940 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
944 chat_window_cut_activate_cb (GtkAction *action,
945 EmpathyChatWindow *window)
947 EmpathyChatWindowPriv *priv;
949 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
951 priv = GET_PRIV (window);
953 empathy_chat_cut (priv->current_chat);
957 chat_window_copy_activate_cb (GtkAction *action,
958 EmpathyChatWindow *window)
960 EmpathyChatWindowPriv *priv;
962 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
964 priv = GET_PRIV (window);
966 empathy_chat_copy (priv->current_chat);
970 chat_window_paste_activate_cb (GtkAction *action,
971 EmpathyChatWindow *window)
973 EmpathyChatWindowPriv *priv;
975 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
977 priv = GET_PRIV (window);
979 empathy_chat_paste (priv->current_chat);
983 chat_window_tabs_next_activate_cb (GtkAction *action,
984 EmpathyChatWindow *window)
986 EmpathyChatWindowPriv *priv;
988 gint index_, numPages;
990 priv = GET_PRIV (window);
992 chat = priv->current_chat;
993 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
994 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
996 if (index_ == (numPages - 1)) {
997 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
1001 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
1005 chat_window_tabs_previous_activate_cb (GtkAction *action,
1006 EmpathyChatWindow *window)
1008 EmpathyChatWindowPriv *priv;
1010 gint index_, numPages;
1012 priv = GET_PRIV (window);
1014 chat = priv->current_chat;
1015 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1016 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
1019 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
1023 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
1027 chat_window_tabs_left_activate_cb (GtkAction *action,
1028 EmpathyChatWindow *window)
1030 EmpathyChatWindowPriv *priv;
1034 priv = GET_PRIV (window);
1036 chat = priv->current_chat;
1037 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1042 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1048 chat_window_tabs_right_activate_cb (GtkAction *action,
1049 EmpathyChatWindow *window)
1051 EmpathyChatWindowPriv *priv;
1055 priv = GET_PRIV (window);
1057 chat = priv->current_chat;
1058 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
1060 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
1066 chat_window_detach_activate_cb (GtkAction *action,
1067 EmpathyChatWindow *window)
1069 EmpathyChatWindowPriv *priv;
1070 EmpathyChatWindow *new_window;
1073 priv = GET_PRIV (window);
1075 chat = priv->current_chat;
1076 new_window = empathy_chat_window_new ();
1078 empathy_chat_window_move_chat (window, new_window, chat);
1080 priv = GET_PRIV (new_window);
1081 gtk_widget_show (priv->dialog);
1085 chat_window_help_contents_activate_cb (GtkAction *action,
1086 EmpathyChatWindow *window)
1088 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1090 empathy_url_show (priv->dialog, "ghelp:empathy");
1094 chat_window_help_about_activate_cb (GtkAction *action,
1095 EmpathyChatWindow *window)
1097 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1099 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
1103 chat_window_delete_event_cb (GtkWidget *dialog,
1105 EmpathyChatWindow *window)
1107 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1109 DEBUG ("Delete event received");
1111 g_object_ref (window);
1112 while (priv->chats) {
1113 empathy_chat_window_remove_chat (window, priv->chats->data);
1115 g_object_unref (window);
1121 chat_window_composing_cb (EmpathyChat *chat,
1122 gboolean is_composing,
1123 EmpathyChatWindow *window)
1125 EmpathyChatWindowPriv *priv;
1127 priv = GET_PRIV (window);
1129 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
1130 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
1132 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1135 chat_window_update_chat_tab (chat);
1139 chat_window_set_urgency_hint (EmpathyChatWindow *window,
1142 EmpathyChatWindowPriv *priv;
1144 priv = GET_PRIV (window);
1146 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
1147 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
1151 free_notification_data (NotificationData *data)
1153 g_object_unref (data->chat);
1154 g_slice_free (NotificationData, data);
1158 chat_window_notification_closed_cb (NotifyNotification *notify,
1159 NotificationData *cb_data)
1161 EmpathyNotificationClosedReason reason = 0;
1162 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1164 #ifdef notify_notification_get_closed_reason
1165 reason = notify_notification_get_closed_reason (notify);
1167 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1168 empathy_chat_window_present_chat (cb_data->chat);
1171 g_object_unref (notify);
1172 priv->notification = NULL;
1173 free_notification_data (cb_data);
1174 priv->notification_data = NULL;
1178 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1179 EmpathyMessage *message,
1182 EmpathyContact *sender;
1183 const gchar *header;
1187 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1190 if (!empathy_notify_manager_notification_is_enabled (priv->notify_mgr)) {
1193 empathy_conf_get_bool (empathy_conf_get (),
1194 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1200 sender = empathy_message_get_sender (message);
1201 header = empathy_contact_get_name (sender);
1202 body = empathy_message_get_body (message);
1203 escaped = g_markup_escape_text (body, -1);
1205 if (priv->notification != NULL) {
1206 notify_notification_update (priv->notification,
1207 header, escaped, NULL);
1209 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1211 cb_data->chat = g_object_ref (chat);
1212 cb_data->window = window;
1214 priv->notification_data = cb_data;
1215 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1216 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1218 g_signal_connect (priv->notification, "closed",
1219 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1222 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (priv->notify_mgr,
1223 sender, EMPATHY_IMAGE_NEW_MESSAGE);
1225 if (pixbuf != NULL) {
1226 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1227 g_object_unref (pixbuf);
1230 notify_notification_show (priv->notification, NULL);
1236 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1241 if (!empathy_chat_is_room (chat))
1244 markup = g_markup_printf_escaped (
1245 "<span color=\"red\" weight=\"bold\">%s</span>",
1246 empathy_chat_get_name (chat));
1248 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1249 gtk_label_set_markup (GTK_LABEL (widget), markup);
1254 chat_window_new_message_cb (EmpathyChat *chat,
1255 EmpathyMessage *message,
1256 EmpathyChatWindow *window)
1258 EmpathyChatWindowPriv *priv;
1260 gboolean needs_urgency;
1261 EmpathyContact *sender;
1263 priv = GET_PRIV (window);
1265 has_focus = empathy_chat_window_has_focus (window);
1267 /* - if we're the sender, we play the sound if it's specified in the
1268 * preferences and we're not away.
1269 * - if we receive a message, we play the sound if it's specified in the
1270 * preferences and the window does not have focus on the chat receiving
1274 sender = empathy_message_get_sender (message);
1276 if (empathy_contact_is_user (sender)) {
1277 empathy_sound_play (GTK_WIDGET (priv->dialog),
1278 EMPATHY_SOUND_MESSAGE_OUTGOING);
1281 if (has_focus && priv->current_chat == chat) {
1282 /* window and tab are focused so consider the message to be read */
1283 empathy_chat_messages_read (chat);
1287 if (!g_list_find (priv->chats_new_msg, chat)) {
1288 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1289 chat_window_update_chat_tab (chat);
1292 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1293 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1294 * an unamed MUC (msn-like).
1295 * In case of a MUC, we set urgency only if the message contains our
1297 if (empathy_chat_is_room (chat) ||
1298 empathy_chat_get_remote_contact (chat) == NULL) {
1299 needs_urgency = empathy_message_should_highlight (message);
1301 needs_urgency = TRUE;
1304 if (needs_urgency) {
1306 chat_window_set_urgency_hint (window, TRUE);
1307 chat_window_set_highlight_room_tab_label (chat);
1310 empathy_sound_play (GTK_WIDGET (priv->dialog),
1311 EMPATHY_SOUND_MESSAGE_INCOMING);
1312 chat_window_show_or_update_notification (window, message, chat);
1315 /* update the number of unread messages */
1316 chat_window_title_update (priv);
1319 static GtkNotebook *
1320 chat_window_detach_hook (GtkNotebook *source,
1326 EmpathyChatWindowPriv *priv;
1327 EmpathyChatWindow *window, *new_window;
1330 chat = EMPATHY_CHAT (page);
1331 window = chat_window_find_chat (chat);
1333 new_window = empathy_chat_window_new ();
1334 priv = GET_PRIV (new_window);
1336 DEBUG ("Detach hook called");
1338 empathy_chat_window_move_chat (window, new_window, chat);
1340 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1341 gtk_widget_show (priv->dialog);
1347 chat_window_page_switched_cb (GtkNotebook *notebook,
1348 GtkNotebookPage *page,
1350 EmpathyChatWindow *window)
1352 EmpathyChatWindowPriv *priv;
1356 DEBUG ("Page switched");
1358 priv = GET_PRIV (window);
1360 child = gtk_notebook_get_nth_page (notebook, page_num);
1361 chat = EMPATHY_CHAT (child);
1363 if (priv->page_added) {
1364 priv->page_added = FALSE;
1365 empathy_chat_scroll_down (chat);
1367 else if (priv->current_chat == chat) {
1371 priv->current_chat = chat;
1372 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1373 empathy_chat_messages_read (chat);
1375 chat_window_update_chat_tab (chat);
1379 chat_window_page_added_cb (GtkNotebook *notebook,
1382 EmpathyChatWindow *window)
1384 EmpathyChatWindowPriv *priv;
1387 priv = GET_PRIV (window);
1389 /* If we just received DND to the same window, we don't want
1390 * to do anything here like removing the tab and then readding
1391 * it, so we return here and in "page-added".
1393 if (priv->dnd_same_window) {
1394 DEBUG ("Page added (back to the same window)");
1395 priv->dnd_same_window = FALSE;
1399 DEBUG ("Page added");
1401 /* Get chat object */
1402 chat = EMPATHY_CHAT (child);
1404 /* Connect chat signals for this window */
1405 g_signal_connect (chat, "composing",
1406 G_CALLBACK (chat_window_composing_cb),
1408 g_signal_connect (chat, "new-message",
1409 G_CALLBACK (chat_window_new_message_cb),
1411 g_signal_connect (chat, "notify::tp-chat",
1412 G_CALLBACK (chat_window_update_chat_tab),
1415 /* Set flag so we know to perform some special operations on
1416 * switch page due to the new page being added.
1418 priv->page_added = TRUE;
1420 /* Get list of chats up to date */
1421 priv->chats = g_list_append (priv->chats, chat);
1423 chat_window_update_chat_tab (chat);
1427 chat_window_page_removed_cb (GtkNotebook *notebook,
1430 EmpathyChatWindow *window)
1432 EmpathyChatWindowPriv *priv;
1435 priv = GET_PRIV (window);
1437 /* If we just received DND to the same window, we don't want
1438 * to do anything here like removing the tab and then readding
1439 * it, so we return here and in "page-added".
1441 if (priv->dnd_same_window) {
1442 DEBUG ("Page removed (and will be readded to same window)");
1446 DEBUG ("Page removed");
1448 /* Get chat object */
1449 chat = EMPATHY_CHAT (child);
1451 /* Disconnect all signal handlers for this chat and this window */
1452 g_signal_handlers_disconnect_by_func (chat,
1453 G_CALLBACK (chat_window_composing_cb),
1455 g_signal_handlers_disconnect_by_func (chat,
1456 G_CALLBACK (chat_window_new_message_cb),
1458 g_signal_handlers_disconnect_by_func (chat,
1459 G_CALLBACK (chat_window_update_chat_tab),
1462 /* Keep list of chats up to date */
1463 priv->chats = g_list_remove (priv->chats, chat);
1464 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1465 empathy_chat_messages_read (chat);
1466 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1468 if (priv->chats == NULL) {
1469 g_object_unref (window);
1471 chat_window_update (window);
1476 chat_window_focus_in_event_cb (GtkWidget *widget,
1478 EmpathyChatWindow *window)
1480 EmpathyChatWindowPriv *priv;
1482 DEBUG ("Focus in event, updating title");
1484 priv = GET_PRIV (window);
1486 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1487 empathy_chat_messages_read (priv->current_chat);
1489 chat_window_set_urgency_hint (window, FALSE);
1491 /* Update the title, since we now mark all unread messages as read. */
1492 chat_window_update_chat_tab (priv->current_chat);
1498 chat_window_drag_motion (GtkWidget *widget,
1499 GdkDragContext *context,
1503 EmpathyChatWindow *window)
1506 EmpathyChatWindowPriv *priv;
1507 GdkAtom dest_target;
1509 priv = GET_PRIV (window);
1511 target = gtk_drag_dest_find_target (widget, context, NULL);
1513 dest_target = gdk_atom_intern_static_string ("text/uri-list");
1514 if (target == dest_target) {
1515 /* This is a file drag. Ensure the contact is online and set the
1516 drag type to COPY. Note that it's possible that the tab will
1517 be switched by GTK+ after a timeout from drag_motion without
1518 getting another drag_motion to disable the drop. You have
1519 to hold your mouse really still.
1521 EmpathyContact *contact;
1523 priv = GET_PRIV (window);
1524 contact = empathy_chat_get_remote_contact (priv->current_chat);
1525 /* contact is NULL for multi-user chats. We don't do
1526 * file transfers to MUCs. We also don't send files
1527 * to offline contacts or contacts that don't support
1530 if ((contact == NULL) || !empathy_contact_is_online (contact)) {
1531 gdk_drag_status (context, 0, time_);
1534 if (!(empathy_contact_get_capabilities (contact)
1535 & EMPATHY_CAPABILITIES_FT)) {
1536 gdk_drag_status (context, 0, time_);
1539 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1543 dest_target = gdk_atom_intern_static_string ("text/contact-id");
1544 if (target == dest_target) {
1545 /* This is a drag of a contact from a contact list. Set to COPY.
1546 FIXME: If this drag is to a MUC window, it invites the user.
1547 Otherwise, it opens a chat. Should we use a different drag
1548 type for invites? Should we allow ASK?
1550 gdk_drag_status (context, GDK_ACTION_COPY, time_);
1554 /* Otherwise, it must be a notebook tab drag. Set to MOVE. */
1555 gdk_drag_status (context, GDK_ACTION_MOVE, time_);
1560 chat_window_drag_data_received (GtkWidget *widget,
1561 GdkDragContext *context,
1564 GtkSelectionData *selection,
1567 EmpathyChatWindow *window)
1569 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1570 EmpathyChat *chat = NULL;
1571 EmpathyChatWindow *old_window;
1572 TpAccount *account = NULL;
1573 TpAccountManager *account_manager;
1576 const gchar *account_id;
1577 const gchar *contact_id;
1579 id = (const gchar*) gtk_selection_data_get_data (selection);
1581 /* FIXME: Perhaps should be sure that the account manager is
1582 * prepared before calling _ensure_account on it. */
1583 account_manager = tp_account_manager_dup ();
1585 DEBUG ("DND contact from roster with id:'%s'", id);
1587 strv = g_strsplit (id, ":", 2);
1588 if (g_strv_length (strv) == 2) {
1589 account_id = strv[0];
1590 contact_id = strv[1];
1592 tp_account_manager_ensure_account (account_manager, account_id);
1593 if (account != NULL)
1594 chat = empathy_chat_window_find_chat (account, contact_id);
1597 if (account == NULL) {
1599 gtk_drag_finish (context, FALSE, FALSE, time_);
1604 TpConnection *connection;
1606 connection = tp_account_get_connection (account);
1609 empathy_dispatcher_chat_with_contact_id (
1610 connection, contact_id, NULL, NULL);
1616 g_object_unref (account_manager);
1619 old_window = chat_window_find_chat (chat);
1621 if (old_window == window) {
1622 gtk_drag_finish (context, TRUE, FALSE, time_);
1626 empathy_chat_window_move_chat (old_window, window, chat);
1628 empathy_chat_window_add_chat (window, chat);
1631 /* Added to take care of any outstanding chat events */
1632 empathy_chat_window_present_chat (chat);
1634 /* We should return TRUE to remove the data when doing
1635 * GDK_ACTION_MOVE, but we don't here otherwise it has
1636 * weird consequences, and we handle that internally
1637 * anyway with add_chat () and remove_chat ().
1639 gtk_drag_finish (context, TRUE, FALSE, time_);
1641 else if (info == DND_DRAG_TYPE_URI_LIST) {
1642 EmpathyChatWindowPriv *priv;
1643 EmpathyContact *contact;
1646 priv = GET_PRIV (window);
1647 contact = empathy_chat_get_remote_contact (priv->current_chat);
1649 /* contact is NULL when current_chat is a multi-user chat.
1650 * We don't do file transfers to MUCs, so just cancel the drag.
1652 if (contact == NULL) {
1653 gtk_drag_finish (context, TRUE, FALSE, time_);
1657 data = (const gchar *) gtk_selection_data_get_data (selection);
1658 empathy_send_file_from_uri_list (contact, data);
1660 gtk_drag_finish (context, TRUE, FALSE, time_);
1662 else if (info == DND_DRAG_TYPE_TAB) {
1664 EmpathyChatWindow *old_window = NULL;
1668 chat = (void *) gtk_selection_data_get_data (selection);
1669 old_window = chat_window_find_chat (*chat);
1672 EmpathyChatWindowPriv *priv;
1674 priv = GET_PRIV (window);
1676 if (old_window == window) {
1677 DEBUG ("DND tab (within same window)");
1678 priv->dnd_same_window = TRUE;
1679 gtk_drag_finish (context, TRUE, FALSE, time_);
1683 priv->dnd_same_window = FALSE;
1686 /* We should return TRUE to remove the data when doing
1687 * GDK_ACTION_MOVE, but we don't here otherwise it has
1688 * weird consequences, and we handle that internally
1689 * anyway with add_chat () and remove_chat ().
1691 gtk_drag_finish (context, TRUE, FALSE, time_);
1693 DEBUG ("DND from unknown source");
1694 gtk_drag_finish (context, FALSE, FALSE, time_);
1699 chat_window_finalize (GObject *object)
1701 EmpathyChatWindow *window;
1702 EmpathyChatWindowPriv *priv;
1704 window = EMPATHY_CHAT_WINDOW (object);
1705 priv = GET_PRIV (window);
1707 DEBUG ("Finalized: %p", object);
1709 g_object_unref (priv->ui_manager);
1710 g_object_unref (priv->chatroom_manager);
1711 g_object_unref (priv->notify_mgr);
1713 if (priv->notification != NULL) {
1714 notify_notification_close (priv->notification, NULL);
1715 g_object_unref (priv->notification);
1716 priv->notification = NULL;
1717 if (priv->notification_data != NULL)
1719 free_notification_data (priv->notification_data);
1720 priv->notification_data = NULL;
1724 if (priv->contact_targets) {
1725 gtk_target_list_unref (priv->contact_targets);
1727 if (priv->file_targets) {
1728 gtk_target_list_unref (priv->file_targets);
1731 chat_windows = g_list_remove (chat_windows, window);
1732 gtk_widget_destroy (priv->dialog);
1734 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1738 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1740 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1742 object_class->finalize = chat_window_finalize;
1744 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1746 /* Set up a style for the close button with no focus padding. */
1747 gtk_rc_parse_string (
1748 "style \"empathy-close-button-style\"\n"
1750 " GtkWidget::focus-padding = 0\n"
1754 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1756 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1760 empathy_chat_window_init (EmpathyChatWindow *window)
1763 GtkAccelGroup *accel_group;
1768 GtkWidget *chat_vbox;
1770 EmpathySmileyManager *smiley_manager;
1771 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1772 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1774 window->priv = priv;
1775 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1776 gui = empathy_builder_get_file (filename,
1777 "chat_window", &priv->dialog,
1778 "chat_vbox", &chat_vbox,
1779 "ui_manager", &priv->ui_manager,
1780 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1781 "menu_conv_favorite", &priv->menu_conv_favorite,
1782 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1783 "menu_edit_cut", &priv->menu_edit_cut,
1784 "menu_edit_copy", &priv->menu_edit_copy,
1785 "menu_edit_paste", &priv->menu_edit_paste,
1786 "menu_tabs_next", &priv->menu_tabs_next,
1787 "menu_tabs_prev", &priv->menu_tabs_prev,
1788 "menu_tabs_left", &priv->menu_tabs_left,
1789 "menu_tabs_right", &priv->menu_tabs_right,
1790 "menu_tabs_detach", &priv->menu_tabs_detach,
1794 empathy_builder_connect (gui, window,
1795 "menu_conv", "activate", chat_window_conv_activate_cb,
1796 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1797 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1798 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1799 "menu_conv_invite_participant", "activate", chat_window_invite_participant_activate_cb,
1800 "menu_conv_close", "activate", chat_window_close_activate_cb,
1801 "menu_edit", "activate", chat_window_edit_activate_cb,
1802 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1803 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1804 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1805 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1806 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1807 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1808 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1809 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1810 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1811 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1814 g_object_ref (priv->ui_manager);
1815 g_object_unref (gui);
1817 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1819 priv->notebook = gtk_notebook_new ();
1820 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1821 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1822 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1823 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1824 gtk_widget_show (priv->notebook);
1827 accel_group = gtk_accel_group_new ();
1828 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1830 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1831 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1834 gtk_accel_group_connect (accel_group,
1841 g_object_unref (accel_group);
1843 /* Set up drag target lists */
1844 priv->contact_targets = gtk_target_list_new (drag_types_dest_contact,
1845 G_N_ELEMENTS (drag_types_dest_contact));
1846 priv->file_targets = gtk_target_list_new (drag_types_dest_file,
1847 G_N_ELEMENTS (drag_types_dest_file));
1849 /* Set up smiley menu */
1850 smiley_manager = empathy_smiley_manager_dup_singleton ();
1851 submenu = empathy_smiley_menu_new (smiley_manager,
1852 chat_window_insert_smiley_activate_cb,
1854 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1855 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1856 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1857 g_object_unref (smiley_manager);
1859 /* Set up signals we can't do with ui file since we may need to
1860 * block/unblock them at some later stage.
1863 g_signal_connect (priv->dialog,
1865 G_CALLBACK (chat_window_delete_event_cb),
1867 g_signal_connect (priv->dialog,
1869 G_CALLBACK (chat_window_focus_in_event_cb),
1871 g_signal_connect_after (priv->notebook,
1873 G_CALLBACK (chat_window_page_switched_cb),
1875 g_signal_connect (priv->notebook,
1877 G_CALLBACK (chat_window_page_added_cb),
1879 g_signal_connect (priv->notebook,
1881 G_CALLBACK (chat_window_page_removed_cb),
1884 /* Set up drag and drop */
1885 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1886 GTK_DEST_DEFAULT_ALL,
1888 G_N_ELEMENTS (drag_types_dest),
1889 GDK_ACTION_MOVE | GDK_ACTION_COPY);
1891 /* connect_after to allow GtkNotebook's built-in tab switching */
1892 g_signal_connect_after (priv->notebook,
1894 G_CALLBACK (chat_window_drag_motion),
1896 g_signal_connect (priv->notebook,
1897 "drag-data-received",
1898 G_CALLBACK (chat_window_drag_data_received),
1901 chat_windows = g_list_prepend (chat_windows, window);
1903 /* Set up private details */
1905 priv->chats_new_msg = NULL;
1906 priv->chats_composing = NULL;
1907 priv->current_chat = NULL;
1909 priv->notify_mgr = empathy_notify_manager_dup_singleton ();
1913 empathy_chat_window_new (void)
1915 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1918 /* Returns the window to open a new tab in if there is only one window
1919 * visble, otherwise, returns NULL indicating that a new window should
1923 empathy_chat_window_get_default (gboolean room)
1926 gboolean separate_windows = TRUE;
1928 empathy_conf_get_bool (empathy_conf_get (),
1929 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1932 if (separate_windows) {
1933 /* Always create a new window */
1937 for (l = chat_windows; l; l = l->next) {
1938 EmpathyChatWindowPriv *priv;
1939 EmpathyChatWindow *chat_window;
1942 chat_window = l->data;
1943 priv = GET_PRIV (chat_window);
1945 dialog = empathy_chat_window_get_dialog (chat_window);
1946 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1947 guint nb_rooms, nb_private;
1948 empathy_chat_window_get_nb_chats (chat_window, &nb_rooms, &nb_private);
1950 /* Skip the window if there aren't any rooms in it */
1951 if (room && nb_rooms == 0)
1954 /* Skip the window if there aren't any 1-1 chats in it */
1955 if (!room && nb_private == 0)
1958 /* Found a visible window on this desktop */
1967 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1969 EmpathyChatWindowPriv *priv;
1971 g_return_val_if_fail (window != NULL, NULL);
1973 priv = GET_PRIV (window);
1975 return priv->dialog;
1979 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1982 EmpathyChatWindowPriv *priv;
1984 GtkWidget *popup_label;
1987 g_return_if_fail (window != NULL);
1988 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1990 priv = GET_PRIV (window);
1992 /* Reference the chat object */
1993 g_object_ref (chat);
1995 /* If this window has just been created, position it */
1996 if (priv->chats == NULL) {
1997 const gchar *name = "chat-window";
1998 gboolean separate_windows;
2000 empathy_conf_get_bool (empathy_conf_get (),
2001 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
2004 if (separate_windows) {
2005 name = empathy_chat_get_id (chat);
2007 else if (empathy_chat_is_room (chat)) {
2008 name = "room-window";
2011 empathy_geometry_bind (GTK_WINDOW (priv->dialog), name);
2014 child = GTK_WIDGET (chat);
2015 label = chat_window_create_label (window, chat, TRUE);
2016 popup_label = chat_window_create_label (window, chat, FALSE);
2017 gtk_widget_show (child);
2019 g_signal_connect (chat, "notify::name",
2020 G_CALLBACK (chat_window_chat_notify_cb),
2022 g_signal_connect (chat, "notify::subject",
2023 G_CALLBACK (chat_window_chat_notify_cb),
2025 g_signal_connect (chat, "notify::remote-contact",
2026 G_CALLBACK (chat_window_chat_notify_cb),
2028 chat_window_chat_notify_cb (chat);
2030 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
2031 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2032 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
2033 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
2034 TRUE, TRUE, GTK_PACK_START);
2036 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
2040 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
2043 EmpathyChatWindowPriv *priv;
2045 EmpathyContact *remote_contact;
2047 g_return_if_fail (window != NULL);
2048 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2050 priv = GET_PRIV (window);
2052 g_signal_handlers_disconnect_by_func (chat,
2053 chat_window_chat_notify_cb,
2055 remote_contact = g_object_get_data (G_OBJECT (chat),
2056 "chat-window-remote-contact");
2057 if (remote_contact) {
2058 g_signal_handlers_disconnect_by_func (remote_contact,
2059 chat_window_update_chat_tab,
2063 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2065 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
2067 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
2069 g_object_unref (chat);
2073 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
2074 EmpathyChatWindow *new_window,
2079 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
2080 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
2081 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2083 widget = GTK_WIDGET (chat);
2085 DEBUG ("Chat moving with widget:%p (%d references)", widget,
2086 G_OBJECT (widget)->ref_count);
2088 /* We reference here to make sure we don't loose the widget
2089 * and the EmpathyChat object during the move.
2091 g_object_ref (chat);
2092 g_object_ref (widget);
2094 empathy_chat_window_remove_chat (old_window, chat);
2095 empathy_chat_window_add_chat (new_window, chat);
2097 g_object_unref (widget);
2098 g_object_unref (chat);
2102 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
2105 EmpathyChatWindowPriv *priv;
2108 g_return_if_fail (window != NULL);
2109 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2111 priv = GET_PRIV (window);
2113 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
2115 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
2120 empathy_chat_window_has_focus (EmpathyChatWindow *window)
2122 EmpathyChatWindowPriv *priv;
2125 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
2127 priv = GET_PRIV (window);
2129 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
2135 empathy_chat_window_find_chat (TpAccount *account,
2140 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
2142 for (l = chat_windows; l; l = l->next) {
2143 EmpathyChatWindowPriv *priv;
2144 EmpathyChatWindow *window;
2148 priv = GET_PRIV (window);
2150 for (ll = priv->chats; ll; ll = ll->next) {
2155 if (account == empathy_chat_get_account (chat) &&
2156 !tp_strdiff (id, empathy_chat_get_id (chat))) {
2166 empathy_chat_window_present_chat (EmpathyChat *chat)
2168 EmpathyChatWindow *window;
2169 EmpathyChatWindowPriv *priv;
2171 g_return_if_fail (EMPATHY_IS_CHAT (chat));
2173 window = chat_window_find_chat (chat);
2175 /* If the chat has no window, create one */
2176 if (window == NULL) {
2177 window = empathy_chat_window_get_default (empathy_chat_is_room (chat));
2179 window = empathy_chat_window_new ();
2182 empathy_chat_window_add_chat (window, chat);
2185 priv = GET_PRIV (window);
2186 empathy_chat_window_switch_to_chat (window, chat);
2187 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
2189 gtk_widget_grab_focus (chat->input_text_view);
2193 empathy_chat_window_get_nb_chats (EmpathyChatWindow *self,
2197 EmpathyChatWindowPriv *priv = GET_PRIV (self);
2199 guint _nb_rooms = 0, _nb_private = 0;
2201 for (l = priv->chats; l != NULL; l = g_list_next (l)) {
2202 if (empathy_chat_is_room (EMPATHY_CHAT (l->data)))
2208 if (nb_rooms != NULL)
2209 *nb_rooms = _nb_rooms;
2210 if (nb_private != NULL)
2211 *nb_private = _nb_private;