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/util.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-account-manager.h>
45 #include <libempathy/empathy-utils.h>
47 #include <libempathy-gtk/empathy-images.h>
48 #include <libempathy-gtk/empathy-conf.h>
49 #include <libempathy-gtk/empathy-contact-dialogs.h>
50 #include <libempathy-gtk/empathy-log-window.h>
51 #include <libempathy-gtk/empathy-geometry.h>
52 #include <libempathy-gtk/empathy-smiley-manager.h>
53 #include <libempathy-gtk/empathy-sound.h>
54 #include <libempathy-gtk/empathy-ui-utils.h>
56 #include "empathy-chat-window.h"
57 #include "empathy-about-dialog.h"
58 #include "empathy-misc.h"
60 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
61 #include <libempathy/empathy-debug.h>
64 EmpathyChatWindow *window;
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
70 EmpathyChat *current_chat;
73 GList *chats_composing;
75 gboolean dnd_same_window;
76 guint save_geometry_id;
77 EmpathyChatroomManager *chatroom_manager;
80 NotifyNotification *notification;
81 NotificationData *notification_data;
84 GtkUIManager *ui_manager;
85 GtkAction *menu_conv_insert_smiley;
86 GtkAction *menu_conv_favorite;
87 GtkAction *menu_conv_toggle_contacts;
89 GtkAction *menu_edit_cut;
90 GtkAction *menu_edit_copy;
91 GtkAction *menu_edit_paste;
93 GtkAction *menu_tabs_next;
94 GtkAction *menu_tabs_prev;
95 GtkAction *menu_tabs_left;
96 GtkAction *menu_tabs_right;
97 GtkAction *menu_tabs_detach;
98 } EmpathyChatWindowPriv;
100 static GList *chat_windows = NULL;
102 static const guint tab_accel_keys[] = {
103 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
104 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
108 DND_DRAG_TYPE_CONTACT_ID,
112 static const GtkTargetEntry drag_types_dest[] = {
113 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
114 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
117 static void chat_window_update (EmpathyChatWindow *window);
119 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
122 chat_window_accel_cb (GtkAccelGroup *accelgroup,
126 EmpathyChatWindow *window)
128 EmpathyChatWindowPriv *priv;
132 priv = GET_PRIV (window);
134 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
135 if (tab_accel_keys[i] == key) {
142 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
146 static EmpathyChatWindow *
147 chat_window_find_chat (EmpathyChat *chat)
149 EmpathyChatWindowPriv *priv;
152 for (l = chat_windows; l; l = l->next) {
153 priv = GET_PRIV (l->data);
154 ll = g_list_find (priv->chats, chat);
164 chat_window_close_clicked_cb (GtkAction *action,
167 EmpathyChatWindow *window;
169 window = chat_window_find_chat (chat);
170 empathy_chat_window_remove_chat (window, chat);
174 chat_tab_style_set_cb (GtkWidget *hbox,
175 GtkStyle *previous_style,
179 int char_width, h, w;
180 PangoContext *context;
181 PangoFontMetrics *metrics;
183 button = g_object_get_data (G_OBJECT (user_data),
184 "chat-window-tab-close-button");
185 context = gtk_widget_get_pango_context (hbox);
187 metrics = pango_context_get_metrics (context, gtk_widget_get_style (hbox)->font_desc,
188 pango_context_get_language (context));
189 char_width = pango_font_metrics_get_approximate_char_width (metrics);
190 pango_font_metrics_unref (metrics);
192 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
193 GTK_ICON_SIZE_MENU, &w, &h);
195 /* Request at least about 12 chars width plus at least space for the status
196 * image and the close button */
197 gtk_widget_set_size_request (hbox,
198 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
200 gtk_widget_set_size_request (button, w, h);
204 chat_window_create_label (EmpathyChatWindow *window,
206 gboolean is_tab_label)
208 EmpathyChatWindowPriv *priv;
210 GtkWidget *name_label;
211 GtkWidget *status_image;
212 GtkWidget *close_button;
213 GtkWidget *close_image;
214 GtkWidget *event_box;
215 GtkWidget *event_box_hbox;
216 PangoAttrList *attr_list;
217 PangoAttribute *attr;
219 priv = GET_PRIV (window);
221 /* The spacing between the button and the label. */
222 hbox = gtk_hbox_new (FALSE, 0);
224 event_box = gtk_event_box_new ();
225 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
227 name_label = gtk_label_new (NULL);
229 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
231 attr_list = pango_attr_list_new ();
232 attr = pango_attr_scale_new (1/1.2);
233 attr->start_index = 0;
234 attr->end_index = -1;
235 pango_attr_list_insert (attr_list, attr);
236 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
237 pango_attr_list_unref (attr_list);
239 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
240 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
241 g_object_set_data (G_OBJECT (chat),
242 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
245 status_image = gtk_image_new ();
247 /* Spacing between the icon and label. */
248 event_box_hbox = gtk_hbox_new (FALSE, 0);
250 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
251 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
253 g_object_set_data (G_OBJECT (chat),
254 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
256 g_object_set_data (G_OBJECT (chat),
257 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
260 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
261 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
264 close_button = gtk_button_new ();
265 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
266 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
268 /* We don't want focus/keynav for the button to avoid clutter, and
269 * Ctrl-W works anyway.
271 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
272 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
274 /* Set the name to make the special rc style match. */
275 gtk_widget_set_name (close_button, "empathy-close-button");
277 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
279 gtk_container_add (GTK_CONTAINER (close_button), close_image);
281 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
283 g_signal_connect (close_button,
285 G_CALLBACK (chat_window_close_clicked_cb),
288 /* React to theme changes and also setup the size correctly. */
289 g_signal_connect (hbox,
291 G_CALLBACK (chat_tab_style_set_cb),
295 gtk_widget_show_all (hbox);
301 _submenu_notify_visible_changed_cb (GObject *object,
305 g_signal_handlers_disconnect_by_func (object,
306 _submenu_notify_visible_changed_cb,
308 chat_window_update (EMPATHY_CHAT_WINDOW (userdata));
312 chat_window_update (EmpathyChatWindow *window)
314 EmpathyChatWindowPriv *priv = GET_PRIV (window);
317 gboolean is_connected;
324 EmpathyContact *remote_contact;
325 gboolean avatar_in_icon;
327 GtkWidget *chat_close_button;
328 GtkWidget *menu, *submenu, *orig_submenu;
330 /* Get information */
331 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
332 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
333 first_page = (page_num == 0);
334 last_page = (page_num == (num_pages - 1));
335 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
336 name = empathy_chat_get_name (priv->current_chat);
337 n_chats = g_list_length (priv->chats);
339 DEBUG ("Update window");
342 gtk_action_set_sensitive (priv->menu_tabs_next, TRUE);
343 gtk_action_set_sensitive (priv->menu_tabs_prev, TRUE);
344 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
345 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
346 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
347 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
349 /* Update Contact menu */
350 menu = gtk_ui_manager_get_widget (priv->ui_manager,
351 "/chats_menubar/menu_contact");
352 orig_submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (menu));
353 if (orig_submenu == NULL || !GTK_WIDGET_VISIBLE (orig_submenu)) {
354 submenu = empathy_chat_get_contact_menu (priv->current_chat);
355 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
356 gtk_widget_show (menu);
358 empathy_signal_connect_weak (orig_submenu,
360 (GCallback)_submenu_notify_visible_changed_cb,
364 /* Update window title */
366 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
370 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
371 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
375 /* Update window icon */
376 if (priv->chats_new_msg) {
377 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
378 EMPATHY_IMAGE_MESSAGE);
380 empathy_conf_get_bool (empathy_conf_get (),
381 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
384 if (n_chats == 1 && avatar_in_icon) {
385 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
386 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
387 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
390 g_object_unref (icon);
393 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
397 if (num_pages == 1) {
398 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
399 chat_close_button = g_object_get_data (G_OBJECT (chat),
400 "chat-window-tab-close-button");
401 gtk_widget_hide (chat_close_button);
403 for (i=0; i<num_pages; i++) {
404 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
405 chat_close_button = g_object_get_data (G_OBJECT (chat),
406 "chat-window-tab-close-button");
407 gtk_widget_show (chat_close_button);
413 append_markup_printf (GString *string,
420 va_start (args, format);
422 tmp = g_markup_vprintf_escaped (format, args);
423 g_string_append (string, tmp);
430 chat_window_update_chat_tab (EmpathyChat *chat)
432 EmpathyChatWindow *window;
433 EmpathyChatWindowPriv *priv;
434 EmpathyContact *remote_contact;
437 EmpathyAccount *account;
438 const gchar *subject;
439 const gchar *status = NULL;
443 const gchar *icon_name;
444 GtkWidget *tab_image;
445 GtkWidget *menu_image;
447 window = chat_window_find_chat (chat);
451 priv = GET_PRIV (window);
453 /* Get information */
454 name = empathy_chat_get_name (chat);
455 account = empathy_chat_get_account (chat);
456 subject = empathy_chat_get_subject (chat);
457 remote_contact = empathy_chat_get_remote_contact (chat);
459 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
460 name, empathy_account_get_unique_name (account), subject, remote_contact);
462 /* Update tab image */
463 if (empathy_chat_get_tp_chat (chat) == NULL) {
464 /* No TpChat, we are disconnected */
467 else if (g_list_find (priv->chats_new_msg, chat)) {
468 icon_name = EMPATHY_IMAGE_MESSAGE;
470 else if (g_list_find (priv->chats_composing, chat)) {
471 icon_name = EMPATHY_IMAGE_TYPING;
473 else if (remote_contact) {
474 icon_name = empathy_icon_name_for_contact (remote_contact);
476 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
479 tab_image = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
480 menu_image = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
481 if (icon_name != NULL) {
482 gtk_image_set_from_icon_name (GTK_IMAGE (tab_image), icon_name, GTK_ICON_SIZE_MENU);
483 gtk_widget_show (tab_image);
484 gtk_image_set_from_icon_name (GTK_IMAGE (menu_image), icon_name, GTK_ICON_SIZE_MENU);
485 gtk_widget_show (menu_image);
487 gtk_widget_hide (tab_image);
488 gtk_widget_hide (menu_image);
491 /* Update tab tooltip */
492 tooltip = g_string_new (NULL);
494 if (remote_contact) {
495 id = empathy_contact_get_id (remote_contact);
496 status = empathy_contact_get_presence_message (remote_contact);
501 append_markup_printf (tooltip,
502 "<b>%s</b><small> (%s)</small>",
504 empathy_account_get_display_name (account));
506 if (!EMP_STR_EMPTY (status)) {
507 append_markup_printf (tooltip, "\n<i>%s</i>", status);
511 append_markup_printf (tooltip, "\n<b>%s</b> %s",
512 _("Topic:"), subject);
515 if (g_list_find (priv->chats_composing, chat)) {
516 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
519 markup = g_string_free (tooltip, FALSE);
520 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
521 gtk_widget_set_tooltip_markup (widget, markup);
522 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
523 gtk_widget_set_tooltip_markup (widget, markup);
526 /* Update tab and menu label */
527 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
528 gtk_label_set_text (GTK_LABEL (widget), name);
529 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
530 gtk_label_set_text (GTK_LABEL (widget), name);
532 /* Update the window if it's the current chat */
533 if (priv->current_chat == chat) {
534 chat_window_update (window);
539 chat_window_chat_notify_cb (EmpathyChat *chat)
541 EmpathyContact *old_remote_contact;
542 EmpathyContact *remote_contact = NULL;
544 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
545 remote_contact = empathy_chat_get_remote_contact (chat);
547 if (old_remote_contact != remote_contact) {
548 /* The remote-contact associated with the chat changed, we need
549 * to keep track of any change of that contact and update the
550 * window each time. */
551 if (remote_contact) {
552 g_signal_connect_swapped (remote_contact, "notify",
553 G_CALLBACK (chat_window_update_chat_tab),
556 if (old_remote_contact) {
557 g_signal_handlers_disconnect_by_func (old_remote_contact,
558 chat_window_update_chat_tab,
562 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
566 chat_window_update_chat_tab (chat);
570 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
571 EmpathySmiley *smiley,
574 EmpathyChatWindowPriv *priv = GET_PRIV (window);
576 GtkTextBuffer *buffer;
579 chat = priv->current_chat;
581 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
582 gtk_text_buffer_get_end_iter (buffer, &iter);
583 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
587 chat_window_conv_activate_cb (GtkAction *action,
588 EmpathyChatWindow *window)
590 EmpathyChatWindowPriv *priv = GET_PRIV (window);
593 EmpathyContact *remote_contact = NULL;
595 /* Favorite room menu */
596 is_room = empathy_chat_is_room (priv->current_chat);
599 EmpathyAccount *account;
600 gboolean found = FALSE;
601 EmpathyChatroom *chatroom;
603 room = empathy_chat_get_id (priv->current_chat);
604 account = empathy_chat_get_account (priv->current_chat);
605 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
607 if (chatroom != NULL)
608 found = empathy_chatroom_is_favorite (chatroom);
610 DEBUG ("This room %s favorite", found ? "is" : "is not");
611 gtk_toggle_action_set_active (
612 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
614 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
616 /* Show contacts menu */
617 g_object_get (priv->current_chat,
618 "remote-contact", &remote_contact,
619 "show-contacts", &active,
621 if (remote_contact == NULL) {
622 gtk_toggle_action_set_active (
623 GTK_TOGGLE_ACTION (priv->menu_conv_toggle_contacts),
626 gtk_action_set_visible (priv->menu_conv_toggle_contacts,
627 (remote_contact == NULL));
628 if (remote_contact != NULL) {
629 g_object_unref (remote_contact);
634 chat_window_clear_activate_cb (GtkAction *action,
635 EmpathyChatWindow *window)
637 EmpathyChatWindowPriv *priv = GET_PRIV (window);
639 empathy_chat_clear (priv->current_chat);
643 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
644 EmpathyChatWindow *window)
646 EmpathyChatWindowPriv *priv = GET_PRIV (window);
648 EmpathyAccount *account;
650 EmpathyChatroom *chatroom;
652 active = gtk_toggle_action_get_active (toggle_action);
653 account = empathy_chat_get_account (priv->current_chat);
654 room = empathy_chat_get_id (priv->current_chat);
656 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
659 if (chatroom == NULL) {
662 name = empathy_chat_get_name (priv->current_chat);
663 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
664 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
665 g_object_unref (chatroom);
668 empathy_chatroom_set_favorite (chatroom, active);
672 chat_window_contacts_toggled_cb (GtkToggleAction *toggle_action,
673 EmpathyChatWindow *window)
675 EmpathyChatWindowPriv *priv = GET_PRIV (window);
678 active = gtk_toggle_action_get_active (toggle_action);
680 empathy_chat_set_show_contacts (priv->current_chat, active);
684 chat_get_window_id_for_geometry (EmpathyChat *chat)
686 const gchar *res = NULL;
687 gboolean separate_windows;
689 empathy_conf_get_bool (empathy_conf_get (),
690 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
693 if (separate_windows) {
694 res = empathy_chat_get_id (chat);
697 return res ? res : "chat-window";
701 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
703 EmpathyChatWindowPriv *priv;
706 priv = GET_PRIV (window);
708 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
709 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
711 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
714 priv->save_geometry_id = 0;
720 chat_window_configure_event_cb (GtkWidget *widget,
721 GdkEventConfigure *event,
722 EmpathyChatWindow *window)
724 EmpathyChatWindowPriv *priv;
726 priv = GET_PRIV (window);
728 if (priv->save_geometry_id != 0) {
729 g_source_remove (priv->save_geometry_id);
732 priv->save_geometry_id =
733 g_timeout_add_seconds (1,
734 (GSourceFunc) chat_window_save_geometry_timeout_cb,
741 chat_window_close_activate_cb (GtkAction *action,
742 EmpathyChatWindow *window)
744 EmpathyChatWindowPriv *priv;
746 priv = GET_PRIV (window);
748 g_return_if_fail (priv->current_chat != NULL);
750 empathy_chat_window_remove_chat (window, priv->current_chat);
754 chat_window_edit_activate_cb (GtkAction *action,
755 EmpathyChatWindow *window)
757 EmpathyChatWindowPriv *priv;
758 GtkClipboard *clipboard;
759 GtkTextBuffer *buffer;
760 gboolean text_available;
762 priv = GET_PRIV (window);
764 g_return_if_fail (priv->current_chat != NULL);
766 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
767 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
768 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
769 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
773 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
774 if (gtk_text_buffer_get_has_selection (buffer)) {
775 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
776 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
780 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
782 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
783 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
786 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
787 text_available = gtk_clipboard_wait_is_text_available (clipboard);
788 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
792 chat_window_cut_activate_cb (GtkAction *action,
793 EmpathyChatWindow *window)
795 EmpathyChatWindowPriv *priv;
797 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
799 priv = GET_PRIV (window);
801 empathy_chat_cut (priv->current_chat);
805 chat_window_copy_activate_cb (GtkAction *action,
806 EmpathyChatWindow *window)
808 EmpathyChatWindowPriv *priv;
810 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
812 priv = GET_PRIV (window);
814 empathy_chat_copy (priv->current_chat);
818 chat_window_paste_activate_cb (GtkAction *action,
819 EmpathyChatWindow *window)
821 EmpathyChatWindowPriv *priv;
823 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
825 priv = GET_PRIV (window);
827 empathy_chat_paste (priv->current_chat);
831 chat_window_tabs_next_activate_cb (GtkAction *action,
832 EmpathyChatWindow *window)
834 EmpathyChatWindowPriv *priv;
836 gint index_, numPages;
838 priv = GET_PRIV (window);
840 chat = priv->current_chat;
841 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
842 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
844 if (index_ == (numPages - 1)) {
845 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), 0);
849 gtk_notebook_next_page (GTK_NOTEBOOK (priv->notebook));
853 chat_window_tabs_previous_activate_cb (GtkAction *action,
854 EmpathyChatWindow *window)
856 EmpathyChatWindowPriv *priv;
858 gint index_, numPages;
860 priv = GET_PRIV (window);
862 chat = priv->current_chat;
863 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
864 numPages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
867 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), numPages - 1);
871 gtk_notebook_prev_page (GTK_NOTEBOOK (priv->notebook));
875 chat_window_tabs_left_activate_cb (GtkAction *action,
876 EmpathyChatWindow *window)
878 EmpathyChatWindowPriv *priv;
882 priv = GET_PRIV (window);
884 chat = priv->current_chat;
885 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
890 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
896 chat_window_tabs_right_activate_cb (GtkAction *action,
897 EmpathyChatWindow *window)
899 EmpathyChatWindowPriv *priv;
903 priv = GET_PRIV (window);
905 chat = priv->current_chat;
906 index_ = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
908 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
914 chat_window_detach_activate_cb (GtkAction *action,
915 EmpathyChatWindow *window)
917 EmpathyChatWindowPriv *priv;
918 EmpathyChatWindow *new_window;
921 priv = GET_PRIV (window);
923 chat = priv->current_chat;
924 new_window = empathy_chat_window_new ();
926 empathy_chat_window_move_chat (window, new_window, chat);
928 priv = GET_PRIV (new_window);
929 gtk_widget_show (priv->dialog);
933 chat_window_help_contents_activate_cb (GtkAction *action,
934 EmpathyChatWindow *window)
936 EmpathyChatWindowPriv *priv = GET_PRIV (window);
938 empathy_url_show (priv->dialog, "ghelp:empathy");
942 chat_window_help_about_activate_cb (GtkAction *action,
943 EmpathyChatWindow *window)
945 EmpathyChatWindowPriv *priv = GET_PRIV (window);
947 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
951 chat_window_delete_event_cb (GtkWidget *dialog,
953 EmpathyChatWindow *window)
955 EmpathyChatWindowPriv *priv = GET_PRIV (window);
957 DEBUG ("Delete event received");
959 g_object_ref (window);
960 while (priv->chats) {
961 empathy_chat_window_remove_chat (window, priv->chats->data);
963 g_object_unref (window);
969 chat_window_composing_cb (EmpathyChat *chat,
970 gboolean is_composing,
971 EmpathyChatWindow *window)
973 EmpathyChatWindowPriv *priv;
975 priv = GET_PRIV (window);
977 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
978 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
980 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
983 chat_window_update_chat_tab (chat);
987 chat_window_set_urgency_hint (EmpathyChatWindow *window,
990 EmpathyChatWindowPriv *priv;
992 priv = GET_PRIV (window);
994 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
995 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
999 free_notification_data (NotificationData *data)
1001 g_object_unref (data->chat);
1002 g_slice_free (NotificationData, data);
1006 chat_window_notification_closed_cb (NotifyNotification *notify,
1007 NotificationData *cb_data)
1009 EmpathyNotificationClosedReason reason = 0;
1010 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
1012 #ifdef notify_notification_get_closed_reason
1013 reason = notify_notification_get_closed_reason (notify);
1015 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
1016 empathy_chat_window_present_chat (cb_data->chat);
1019 g_object_unref (notify);
1020 priv->notification = NULL;
1021 free_notification_data (cb_data);
1022 priv->notification_data = NULL;
1026 chat_window_show_or_update_notification (EmpathyChatWindow *window,
1027 EmpathyMessage *message,
1030 EmpathyContact *sender;
1031 const gchar *header;
1035 EmpathyChatWindowPriv *priv = GET_PRIV (window);
1038 if (!empathy_notification_is_enabled ()) {
1041 empathy_conf_get_bool (empathy_conf_get (),
1042 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
1048 sender = empathy_message_get_sender (message);
1049 header = empathy_contact_get_name (sender);
1050 body = empathy_message_get_body (message);
1051 escaped = g_markup_escape_text (body, -1);
1053 if (priv->notification != NULL) {
1054 notify_notification_update (priv->notification,
1055 header, escaped, NULL);
1057 NotificationData *cb_data = cb_data = g_slice_new0 (NotificationData);
1059 cb_data->chat = g_object_ref (chat);
1060 cb_data->window = window;
1062 priv->notification_data = cb_data;
1063 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
1064 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
1066 g_signal_connect (priv->notification, "closed",
1067 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
1070 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
1072 if (pixbuf != NULL) {
1073 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
1074 g_object_unref (pixbuf);
1077 notify_notification_show (priv->notification, NULL);
1083 chat_window_set_highlight_room_tab_label (EmpathyChat *chat)
1088 if (!empathy_chat_is_room (chat))
1091 markup = g_markup_printf_escaped (
1092 "<span color=\"red\" weight=\"bold\">%s</span>",
1093 empathy_chat_get_name (chat));
1095 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
1096 gtk_label_set_markup (GTK_LABEL (widget), markup);
1101 chat_window_new_message_cb (EmpathyChat *chat,
1102 EmpathyMessage *message,
1103 EmpathyChatWindow *window)
1105 EmpathyChatWindowPriv *priv;
1107 gboolean needs_urgency;
1108 EmpathyContact *sender;
1110 priv = GET_PRIV (window);
1112 has_focus = empathy_chat_window_has_focus (window);
1114 /* - if we're the sender, we play the sound if it's specified in the
1115 * preferences and we're not away.
1116 * - if we receive a message, we play the sound if it's specified in the
1117 * preferences and the window does not have focus on the chat receiving
1121 sender = empathy_message_get_sender (message);
1123 if (empathy_contact_is_user (sender)) {
1124 empathy_sound_play (GTK_WIDGET (priv->dialog),
1125 EMPATHY_SOUND_MESSAGE_OUTGOING);
1128 if (has_focus && priv->current_chat == chat) {
1132 if (!g_list_find (priv->chats_new_msg, chat)) {
1133 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1134 chat_window_update_chat_tab (chat);
1137 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
1138 * If empathy_chat_get_remote_contact () returns NULL, that means it's
1139 * an unamed MUC (msn-like).
1140 * In case of a MUC, we set urgency only if the message contains our
1142 if (empathy_chat_is_room (chat) ||
1143 empathy_chat_get_remote_contact (chat) == NULL) {
1144 needs_urgency = empathy_message_should_highlight (message);
1146 needs_urgency = TRUE;
1149 if (needs_urgency) {
1151 chat_window_set_urgency_hint (window, TRUE);
1152 chat_window_set_highlight_room_tab_label (chat);
1155 empathy_sound_play (GTK_WIDGET (priv->dialog),
1156 EMPATHY_SOUND_MESSAGE_INCOMING);
1157 chat_window_show_or_update_notification (window, message, chat);
1161 static GtkNotebook *
1162 chat_window_detach_hook (GtkNotebook *source,
1168 EmpathyChatWindowPriv *priv;
1169 EmpathyChatWindow *window, *new_window;
1172 chat = EMPATHY_CHAT (page);
1173 window = chat_window_find_chat (chat);
1175 new_window = empathy_chat_window_new ();
1176 priv = GET_PRIV (new_window);
1178 DEBUG ("Detach hook called");
1180 empathy_chat_window_move_chat (window, new_window, chat);
1182 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1183 gtk_widget_show (priv->dialog);
1189 chat_window_page_switched_cb (GtkNotebook *notebook,
1190 GtkNotebookPage *page,
1192 EmpathyChatWindow *window)
1194 EmpathyChatWindowPriv *priv;
1198 DEBUG ("Page switched");
1200 priv = GET_PRIV (window);
1202 child = gtk_notebook_get_nth_page (notebook, page_num);
1203 chat = EMPATHY_CHAT (child);
1205 if (priv->page_added) {
1206 priv->page_added = FALSE;
1207 empathy_chat_scroll_down (chat);
1209 else if (priv->current_chat == chat) {
1213 priv->current_chat = chat;
1214 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1216 chat_window_update_chat_tab (chat);
1220 chat_window_page_added_cb (GtkNotebook *notebook,
1223 EmpathyChatWindow *window)
1225 EmpathyChatWindowPriv *priv;
1228 priv = GET_PRIV (window);
1230 /* If we just received DND to the same window, we don't want
1231 * to do anything here like removing the tab and then readding
1232 * it, so we return here and in "page-added".
1234 if (priv->dnd_same_window) {
1235 DEBUG ("Page added (back to the same window)");
1236 priv->dnd_same_window = FALSE;
1240 DEBUG ("Page added");
1242 /* Get chat object */
1243 chat = EMPATHY_CHAT (child);
1245 /* Connect chat signals for this window */
1246 g_signal_connect (chat, "composing",
1247 G_CALLBACK (chat_window_composing_cb),
1249 g_signal_connect (chat, "new-message",
1250 G_CALLBACK (chat_window_new_message_cb),
1252 g_signal_connect (chat, "notify::tp-chat",
1253 G_CALLBACK (chat_window_update_chat_tab),
1256 /* Set flag so we know to perform some special operations on
1257 * switch page due to the new page being added.
1259 priv->page_added = TRUE;
1261 /* Get list of chats up to date */
1262 priv->chats = g_list_append (priv->chats, chat);
1264 chat_window_update_chat_tab (chat);
1268 chat_window_page_removed_cb (GtkNotebook *notebook,
1271 EmpathyChatWindow *window)
1273 EmpathyChatWindowPriv *priv;
1276 priv = GET_PRIV (window);
1278 /* If we just received DND to the same window, we don't want
1279 * to do anything here like removing the tab and then readding
1280 * it, so we return here and in "page-added".
1282 if (priv->dnd_same_window) {
1283 DEBUG ("Page removed (and will be readded to same window)");
1287 DEBUG ("Page removed");
1289 /* Get chat object */
1290 chat = EMPATHY_CHAT (child);
1292 /* Disconnect all signal handlers for this chat and this window */
1293 g_signal_handlers_disconnect_by_func (chat,
1294 G_CALLBACK (chat_window_composing_cb),
1296 g_signal_handlers_disconnect_by_func (chat,
1297 G_CALLBACK (chat_window_new_message_cb),
1299 g_signal_handlers_disconnect_by_func (chat,
1300 G_CALLBACK (chat_window_update_chat_tab),
1303 /* Keep list of chats up to date */
1304 priv->chats = g_list_remove (priv->chats, chat);
1305 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1306 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1308 if (priv->chats == NULL) {
1309 g_object_unref (window);
1311 chat_window_update (window);
1316 chat_window_focus_in_event_cb (GtkWidget *widget,
1318 EmpathyChatWindow *window)
1320 EmpathyChatWindowPriv *priv;
1322 DEBUG ("Focus in event, updating title");
1324 priv = GET_PRIV (window);
1326 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1328 chat_window_set_urgency_hint (window, FALSE);
1330 /* Update the title, since we now mark all unread messages as read. */
1331 chat_window_update_chat_tab (priv->current_chat);
1337 chat_window_drag_data_received (GtkWidget *widget,
1338 GdkDragContext *context,
1341 GtkSelectionData *selection,
1344 EmpathyChatWindow *window)
1346 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1347 EmpathyChat *chat = NULL;
1348 EmpathyChatWindow *old_window;
1349 EmpathyAccount *account = NULL;
1350 EmpathyAccountManager *account_manager;
1353 const gchar *account_id;
1354 const gchar *contact_id;
1356 id = (const gchar*) gtk_selection_data_get_data (selection);
1357 account_manager = empathy_account_manager_dup_singleton ();
1359 DEBUG ("DND contact from roster with id:'%s'", id);
1361 strv = g_strsplit (id, ":", 2);
1362 if (g_strv_length (strv) == 2) {
1363 account_id = strv[0];
1364 contact_id = strv[1];
1366 empathy_account_manager_get_account (account_manager, account_id);
1367 if (account != NULL)
1368 chat = empathy_chat_window_find_chat (account, contact_id);
1371 if (account == NULL) {
1373 gtk_drag_finish (context, FALSE, FALSE, time_);
1378 TpConnection *connection;
1380 connection = empathy_account_get_connection (account);
1383 empathy_dispatcher_chat_with_contact_id (
1384 connection, contact_id, NULL, NULL);
1390 g_object_unref (account_manager);
1393 old_window = chat_window_find_chat (chat);
1395 if (old_window == window) {
1396 gtk_drag_finish (context, TRUE, FALSE, time_);
1400 empathy_chat_window_move_chat (old_window, window, chat);
1402 empathy_chat_window_add_chat (window, chat);
1405 /* Added to take care of any outstanding chat events */
1406 empathy_chat_window_present_chat (chat);
1408 /* We should return TRUE to remove the data when doing
1409 * GDK_ACTION_MOVE, but we don't here otherwise it has
1410 * weird consequences, and we handle that internally
1411 * anyway with add_chat () and remove_chat ().
1413 gtk_drag_finish (context, TRUE, FALSE, time_);
1415 else if (info == DND_DRAG_TYPE_TAB) {
1417 EmpathyChatWindow *old_window = NULL;
1421 chat = (void *) gtk_selection_data_get_data (selection);
1422 old_window = chat_window_find_chat (*chat);
1425 EmpathyChatWindowPriv *priv;
1427 priv = GET_PRIV (window);
1429 if (old_window == window) {
1430 DEBUG ("DND tab (within same window)");
1431 priv->dnd_same_window = TRUE;
1432 gtk_drag_finish (context, TRUE, FALSE, time_);
1436 priv->dnd_same_window = FALSE;
1439 /* We should return TRUE to remove the data when doing
1440 * GDK_ACTION_MOVE, but we don't here otherwise it has
1441 * weird consequences, and we handle that internally
1442 * anyway with add_chat () and remove_chat ().
1444 gtk_drag_finish (context, TRUE, FALSE, time_);
1446 DEBUG ("DND from unknown source");
1447 gtk_drag_finish (context, FALSE, FALSE, time_);
1452 chat_window_finalize (GObject *object)
1454 EmpathyChatWindow *window;
1455 EmpathyChatWindowPriv *priv;
1457 window = EMPATHY_CHAT_WINDOW (object);
1458 priv = GET_PRIV (window);
1460 DEBUG ("Finalized: %p", object);
1462 g_object_unref (priv->ui_manager);
1463 g_object_unref (priv->chatroom_manager);
1464 if (priv->save_geometry_id != 0) {
1465 g_source_remove (priv->save_geometry_id);
1468 if (priv->notification != NULL) {
1469 notify_notification_close (priv->notification, NULL);
1470 g_object_unref (priv->notification);
1471 priv->notification = NULL;
1472 if (priv->notification_data != NULL)
1474 free_notification_data (priv->notification_data);
1475 priv->notification_data = NULL;
1479 chat_windows = g_list_remove (chat_windows, window);
1480 gtk_widget_destroy (priv->dialog);
1482 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1486 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1488 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1490 object_class->finalize = chat_window_finalize;
1492 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1494 /* Set up a style for the close button with no focus padding. */
1495 gtk_rc_parse_string (
1496 "style \"empathy-close-button-style\"\n"
1498 " GtkWidget::focus-padding = 0\n"
1502 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1504 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1508 empathy_chat_window_init (EmpathyChatWindow *window)
1511 GtkAccelGroup *accel_group;
1516 GtkWidget *chat_vbox;
1518 EmpathySmileyManager *smiley_manager;
1519 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1520 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1522 window->priv = priv;
1523 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1524 gui = empathy_builder_get_file (filename,
1525 "chat_window", &priv->dialog,
1526 "chat_vbox", &chat_vbox,
1527 "ui_manager", &priv->ui_manager,
1528 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1529 "menu_conv_favorite", &priv->menu_conv_favorite,
1530 "menu_conv_toggle_contacts", &priv->menu_conv_toggle_contacts,
1531 "menu_edit_cut", &priv->menu_edit_cut,
1532 "menu_edit_copy", &priv->menu_edit_copy,
1533 "menu_edit_paste", &priv->menu_edit_paste,
1534 "menu_tabs_next", &priv->menu_tabs_next,
1535 "menu_tabs_prev", &priv->menu_tabs_prev,
1536 "menu_tabs_left", &priv->menu_tabs_left,
1537 "menu_tabs_right", &priv->menu_tabs_right,
1538 "menu_tabs_detach", &priv->menu_tabs_detach,
1542 empathy_builder_connect (gui, window,
1543 "chat_window", "configure-event", chat_window_configure_event_cb,
1544 "menu_conv", "activate", chat_window_conv_activate_cb,
1545 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1546 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1547 "menu_conv_toggle_contacts", "toggled", chat_window_contacts_toggled_cb,
1548 "menu_conv_close", "activate", chat_window_close_activate_cb,
1549 "menu_edit", "activate", chat_window_edit_activate_cb,
1550 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1551 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1552 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1553 "menu_tabs_next", "activate", chat_window_tabs_next_activate_cb,
1554 "menu_tabs_prev", "activate", chat_window_tabs_previous_activate_cb,
1555 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1556 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1557 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1558 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1559 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1562 g_object_ref (priv->ui_manager);
1563 g_object_unref (gui);
1565 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1567 priv->notebook = gtk_notebook_new ();
1568 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1569 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1570 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1571 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1572 gtk_widget_show (priv->notebook);
1575 accel_group = gtk_accel_group_new ();
1576 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1578 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1579 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1582 gtk_accel_group_connect (accel_group,
1589 g_object_unref (accel_group);
1591 /* Set up smiley menu */
1592 smiley_manager = empathy_smiley_manager_dup_singleton ();
1593 submenu = empathy_smiley_menu_new (smiley_manager,
1594 chat_window_insert_smiley_activate_cb,
1596 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1597 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1598 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1599 g_object_unref (smiley_manager);
1601 /* Set up signals we can't do with ui file since we may need to
1602 * block/unblock them at some later stage.
1605 g_signal_connect (priv->dialog,
1607 G_CALLBACK (chat_window_delete_event_cb),
1609 g_signal_connect (priv->dialog,
1611 G_CALLBACK (chat_window_focus_in_event_cb),
1613 g_signal_connect_after (priv->notebook,
1615 G_CALLBACK (chat_window_page_switched_cb),
1617 g_signal_connect (priv->notebook,
1619 G_CALLBACK (chat_window_page_added_cb),
1621 g_signal_connect (priv->notebook,
1623 G_CALLBACK (chat_window_page_removed_cb),
1626 /* Set up drag and drop */
1627 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1628 GTK_DEST_DEFAULT_ALL,
1630 G_N_ELEMENTS (drag_types_dest),
1633 g_signal_connect (priv->notebook,
1634 "drag-data-received",
1635 G_CALLBACK (chat_window_drag_data_received),
1638 chat_windows = g_list_prepend (chat_windows, window);
1640 /* Set up private details */
1642 priv->chats_new_msg = NULL;
1643 priv->chats_composing = NULL;
1644 priv->current_chat = NULL;
1648 empathy_chat_window_new (void)
1650 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1653 /* Returns the window to open a new tab in if there is only one window
1654 * visble, otherwise, returns NULL indicating that a new window should
1658 empathy_chat_window_get_default (void)
1661 gboolean separate_windows = TRUE;
1663 empathy_conf_get_bool (empathy_conf_get (),
1664 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1667 if (separate_windows) {
1668 /* Always create a new window */
1672 for (l = chat_windows; l; l = l->next) {
1673 EmpathyChatWindow *chat_window;
1676 chat_window = l->data;
1678 dialog = empathy_chat_window_get_dialog (chat_window);
1679 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1680 /* Found a visible window on this desktop */
1689 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1691 EmpathyChatWindowPriv *priv;
1693 g_return_val_if_fail (window != NULL, NULL);
1695 priv = GET_PRIV (window);
1697 return priv->dialog;
1701 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1704 EmpathyChatWindowPriv *priv;
1706 GtkWidget *popup_label;
1710 g_return_if_fail (window != NULL);
1711 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1713 priv = GET_PRIV (window);
1715 /* Reference the chat object */
1716 g_object_ref (chat);
1718 /* If this window has just been created, position it */
1719 if (priv->chats == NULL) {
1720 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1722 if (x >= 0 && y >= 0) {
1723 /* Let the window manager position it if we don't have
1724 * good x, y coordinates.
1726 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1729 if (w > 0 && h > 0) {
1730 /* Use the defaults from the ui file if we don't have
1731 * good w, h geometry.
1733 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1737 child = GTK_WIDGET (chat);
1738 label = chat_window_create_label (window, chat, TRUE);
1739 popup_label = chat_window_create_label (window, chat, FALSE);
1740 gtk_widget_show (child);
1742 g_signal_connect (chat, "notify::name",
1743 G_CALLBACK (chat_window_chat_notify_cb),
1745 g_signal_connect (chat, "notify::subject",
1746 G_CALLBACK (chat_window_chat_notify_cb),
1748 g_signal_connect (chat, "notify::remote-contact",
1749 G_CALLBACK (chat_window_chat_notify_cb),
1751 chat_window_chat_notify_cb (chat);
1753 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1754 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1755 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1756 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1757 TRUE, TRUE, GTK_PACK_START);
1759 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1763 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1766 EmpathyChatWindowPriv *priv;
1768 EmpathyContact *remote_contact;
1770 g_return_if_fail (window != NULL);
1771 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1773 priv = GET_PRIV (window);
1775 g_signal_handlers_disconnect_by_func (chat,
1776 chat_window_chat_notify_cb,
1778 remote_contact = g_object_get_data (G_OBJECT (chat),
1779 "chat-window-remote-contact");
1780 if (remote_contact) {
1781 g_signal_handlers_disconnect_by_func (remote_contact,
1782 chat_window_update_chat_tab,
1786 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1788 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1790 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1792 g_object_unref (chat);
1796 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1797 EmpathyChatWindow *new_window,
1802 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1803 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1804 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1806 widget = GTK_WIDGET (chat);
1808 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1809 G_OBJECT (widget)->ref_count);
1811 /* We reference here to make sure we don't loose the widget
1812 * and the EmpathyChat object during the move.
1814 g_object_ref (chat);
1815 g_object_ref (widget);
1817 empathy_chat_window_remove_chat (old_window, chat);
1818 empathy_chat_window_add_chat (new_window, chat);
1820 g_object_unref (widget);
1821 g_object_unref (chat);
1825 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1828 EmpathyChatWindowPriv *priv;
1831 g_return_if_fail (window != NULL);
1832 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1834 priv = GET_PRIV (window);
1836 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1838 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1843 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1845 EmpathyChatWindowPriv *priv;
1848 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1850 priv = GET_PRIV (window);
1852 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1858 empathy_chat_window_find_chat (EmpathyAccount *account,
1863 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1865 for (l = chat_windows; l; l = l->next) {
1866 EmpathyChatWindowPriv *priv;
1867 EmpathyChatWindow *window;
1871 priv = GET_PRIV (window);
1873 for (ll = priv->chats; ll; ll = ll->next) {
1878 if (account == empathy_chat_get_account (chat) &&
1879 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1889 empathy_chat_window_present_chat (EmpathyChat *chat)
1891 EmpathyChatWindow *window;
1892 EmpathyChatWindowPriv *priv;
1894 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1896 window = chat_window_find_chat (chat);
1898 /* If the chat has no window, create one */
1899 if (window == NULL) {
1900 window = empathy_chat_window_get_default ();
1902 window = empathy_chat_window_new ();
1905 empathy_chat_window_add_chat (window, chat);
1908 priv = GET_PRIV (window);
1909 empathy_chat_window_switch_to_chat (window, chat);
1910 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1912 gtk_widget_grab_focus (chat->input_text_view);