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>
33 #include <gdk/gdkkeysyms.h>
34 #include <glib/gi18n.h>
35 #include <libnotify/notification.h>
37 #include <telepathy-glib/util.h>
38 #include <libmissioncontrol/mission-control.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-ui-utils.h>
55 #include "empathy-chat-window.h"
56 #include "empathy-about-dialog.h"
57 #include "empathy-misc.h"
59 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
60 #include <libempathy/empathy-debug.h>
62 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatWindow)
64 EmpathyChat *current_chat;
67 GList *chats_composing;
69 gboolean dnd_same_window;
70 guint save_geometry_id;
71 EmpathyChatroomManager *chatroom_manager;
74 NotifyNotification *notification;
77 GtkUIManager *ui_manager;
78 GtkAction *menu_conv_insert_smiley;
79 GtkAction *menu_conv_favorite;
81 GtkAction *menu_edit_cut;
82 GtkAction *menu_edit_copy;
83 GtkAction *menu_edit_paste;
85 GtkAction *menu_tabs_next;
86 GtkAction *menu_tabs_prev;
87 GtkAction *menu_tabs_left;
88 GtkAction *menu_tabs_right;
89 GtkAction *menu_tabs_detach;
90 } EmpathyChatWindowPriv;
92 static GList *chat_windows = NULL;
94 static const guint tab_accel_keys[] = {
95 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
96 GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
100 DND_DRAG_TYPE_CONTACT_ID,
104 static const GtkTargetEntry drag_types_dest[] = {
105 { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
106 { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
109 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
112 chat_window_accel_cb (GtkAccelGroup *accelgroup,
116 EmpathyChatWindow *window)
118 EmpathyChatWindowPriv *priv;
122 priv = GET_PRIV (window);
124 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
125 if (tab_accel_keys[i] == key) {
132 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
136 static EmpathyChatWindow *
137 chat_window_find_chat (EmpathyChat *chat)
139 EmpathyChatWindowPriv *priv;
142 for (l = chat_windows; l; l = l->next) {
143 priv = GET_PRIV (l->data);
144 ll = g_list_find (priv->chats, chat);
154 chat_window_close_clicked_cb (GtkAction *action,
157 EmpathyChatWindow *window;
159 window = chat_window_find_chat (chat);
160 empathy_chat_window_remove_chat (window, chat);
164 chat_tab_style_set_cb (GtkWidget *hbox,
165 GtkStyle *previous_style,
169 int char_width, h, w;
170 PangoContext *context;
171 PangoFontMetrics *metrics;
173 button = g_object_get_data (G_OBJECT (user_data),
174 "chat-window-tab-close-button");
175 context = gtk_widget_get_pango_context (hbox);
177 metrics = pango_context_get_metrics (context, hbox->style->font_desc,
178 pango_context_get_language (context));
179 char_width = pango_font_metrics_get_approximate_char_width (metrics);
180 pango_font_metrics_unref (metrics);
182 gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
183 GTK_ICON_SIZE_MENU, &w, &h);
185 /* Request at least about 12 chars width plus at least space for the status
186 * image and the close button */
187 gtk_widget_set_size_request (hbox,
188 12 * PANGO_PIXELS (char_width) + 2 * w, -1);
190 gtk_widget_set_size_request (button, w, h);
194 chat_window_create_label (EmpathyChatWindow *window,
196 gboolean is_tab_label)
198 EmpathyChatWindowPriv *priv;
200 GtkWidget *name_label;
201 GtkWidget *status_image;
202 GtkWidget *close_button;
203 GtkWidget *close_image;
204 GtkWidget *event_box;
205 GtkWidget *event_box_hbox;
206 PangoAttrList *attr_list;
207 PangoAttribute *attr;
209 priv = GET_PRIV (window);
211 /* The spacing between the button and the label. */
212 hbox = gtk_hbox_new (FALSE, 0);
214 event_box = gtk_event_box_new ();
215 gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
217 name_label = gtk_label_new (NULL);
219 gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
221 attr_list = pango_attr_list_new ();
222 attr = pango_attr_scale_new (1/1.2);
223 attr->start_index = 0;
224 attr->end_index = -1;
225 pango_attr_list_insert (attr_list, attr);
226 gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
227 pango_attr_list_unref (attr_list);
229 gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
230 gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
231 g_object_set_data (G_OBJECT (chat),
232 is_tab_label ? "chat-window-tab-label" : "chat-window-menu-label",
235 status_image = gtk_image_new ();
237 /* Spacing between the icon and label. */
238 event_box_hbox = gtk_hbox_new (FALSE, 0);
240 gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
241 gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
243 g_object_set_data (G_OBJECT (chat),
244 is_tab_label ? "chat-window-tab-image" : "chat-window-menu-image",
246 g_object_set_data (G_OBJECT (chat),
247 is_tab_label ? "chat-window-tab-tooltip-widget" : "chat-window-menu-tooltip-widget",
250 gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
251 gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
254 close_button = gtk_button_new ();
255 gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
256 g_object_set_data (G_OBJECT (chat), "chat-window-tab-close-button", close_button);
258 /* We don't want focus/keynav for the button to avoid clutter, and
259 * Ctrl-W works anyway.
261 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
262 GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
264 /* Set the name to make the special rc style match. */
265 gtk_widget_set_name (close_button, "empathy-close-button");
267 close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
269 gtk_container_add (GTK_CONTAINER (close_button), close_image);
271 gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
273 g_signal_connect (close_button,
275 G_CALLBACK (chat_window_close_clicked_cb),
278 /* React to theme changes and also setup the size correctly. */
279 g_signal_connect (hbox,
281 G_CALLBACK (chat_tab_style_set_cb),
285 gtk_widget_show_all (hbox);
291 chat_window_update (EmpathyChatWindow *window)
293 EmpathyChatWindowPriv *priv = GET_PRIV (window);
296 gboolean is_connected;
303 EmpathyContact *remote_contact;
304 gboolean avatar_in_icon;
306 GtkWidget *chat_close_button;
310 /* Get information */
311 page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
312 num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
313 first_page = (page_num == 0);
314 last_page = (page_num == (num_pages - 1));
315 is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
316 name = empathy_chat_get_name (priv->current_chat);
317 n_chats = g_list_length (priv->chats);
319 DEBUG ("Update window");
322 gtk_action_set_sensitive (priv->menu_tabs_next, !last_page);
323 gtk_action_set_sensitive (priv->menu_tabs_prev, !first_page);
324 gtk_action_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
325 gtk_action_set_sensitive (priv->menu_tabs_left, !first_page);
326 gtk_action_set_sensitive (priv->menu_tabs_right, !last_page);
327 gtk_action_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
329 /* Update Contact menu */
330 menu = gtk_ui_manager_get_widget (priv->ui_manager,
331 "/chats_menubar/menu_contact");
332 submenu = empathy_chat_get_contact_menu (priv->current_chat);
333 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
334 gtk_widget_show (menu);
336 /* Update window title */
338 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
342 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
343 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
347 /* Update window icon */
348 if (priv->chats_new_msg) {
349 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
350 EMPATHY_IMAGE_MESSAGE);
352 empathy_conf_get_bool (empathy_conf_get (),
353 EMPATHY_PREFS_CHAT_AVATAR_IN_ICON,
356 if (n_chats == 1 && avatar_in_icon) {
357 remote_contact = empathy_chat_get_remote_contact (priv->current_chat);
358 icon = empathy_pixbuf_avatar_from_contact_scaled (remote_contact, 0, 0);
359 gtk_window_set_icon (GTK_WINDOW (priv->dialog), icon);
362 g_object_unref (icon);
365 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
369 if (num_pages == 1) {
370 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), 0);
371 chat_close_button = g_object_get_data (G_OBJECT (chat),
372 "chat-window-tab-close-button");
373 gtk_widget_hide (chat_close_button);
375 for (i=0; i<num_pages; i++) {
376 chat = gtk_notebook_get_nth_page (GTK_NOTEBOOK (priv->notebook), i);
377 chat_close_button = g_object_get_data (G_OBJECT (chat),
378 "chat-window-tab-close-button");
379 gtk_widget_show (chat_close_button);
385 append_markup_printf (GString *string,
392 va_start (args, format);
394 tmp = g_markup_vprintf_escaped (format, args);
395 g_string_append (string, tmp);
402 chat_window_update_chat_tab (EmpathyChat *chat)
404 EmpathyChatWindow *window;
405 EmpathyChatWindowPriv *priv;
406 EmpathyContact *remote_contact;
410 const gchar *subject;
411 const gchar *status = NULL;
415 const gchar *icon_name;
417 window = chat_window_find_chat (chat);
421 priv = GET_PRIV (window);
423 /* Get information */
424 name = empathy_chat_get_name (chat);
425 account = empathy_chat_get_account (chat);
426 subject = empathy_chat_get_subject (chat);
427 remote_contact = empathy_chat_get_remote_contact (chat);
429 DEBUG ("Updating chat tab, name=%s, account=%s, subject=%s, remote_contact=%p",
430 name, mc_account_get_unique_name (account), subject, remote_contact);
432 /* Update tab image */
433 if (g_list_find (priv->chats_new_msg, chat)) {
434 icon_name = EMPATHY_IMAGE_MESSAGE;
436 else if (g_list_find (priv->chats_composing, chat)) {
437 icon_name = EMPATHY_IMAGE_TYPING;
439 else if (remote_contact) {
440 icon_name = empathy_icon_name_for_contact (remote_contact);
442 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
444 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
445 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
446 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-image");
447 gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
449 /* Update tab tooltip */
450 tooltip = g_string_new (NULL);
452 if (remote_contact) {
453 id = empathy_contact_get_id (remote_contact);
454 status = empathy_contact_get_presence_message (remote_contact);
459 append_markup_printf (tooltip,
460 "<b>%s</b><small> (%s)</small>",
462 mc_account_get_display_name (account));
464 if (!EMP_STR_EMPTY (status)) {
465 append_markup_printf (tooltip, "\n<i>%s</i>", status);
469 append_markup_printf (tooltip, "\n<b>%s</b> %s",
470 _("Topic:"), subject);
473 if (g_list_find (priv->chats_composing, chat)) {
474 append_markup_printf (tooltip, "\n%s", _("Typing a message."));
477 markup = g_string_free (tooltip, FALSE);
478 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
479 gtk_widget_set_tooltip_markup (widget, markup);
480 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-tooltip-widget");
481 gtk_widget_set_tooltip_markup (widget, markup);
484 /* Update tab and menu label */
485 widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
486 gtk_label_set_text (GTK_LABEL (widget), name);
487 widget = g_object_get_data (G_OBJECT (chat), "chat-window-menu-label");
488 gtk_label_set_text (GTK_LABEL (widget), name);
490 /* Update the window if it's the current chat */
491 if (priv->current_chat == chat) {
492 chat_window_update (window);
497 chat_window_chat_notify_cb (EmpathyChat *chat)
499 EmpathyContact *old_remote_contact;
500 EmpathyContact *remote_contact = NULL;
502 old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
503 remote_contact = empathy_chat_get_remote_contact (chat);
505 if (old_remote_contact != remote_contact) {
506 /* The remote-contact associated with the chat changed, we need
507 * to keep track of any change of that contact and update the
508 * window each time. */
509 if (remote_contact) {
510 g_signal_connect_swapped (remote_contact, "notify",
511 G_CALLBACK (chat_window_update_chat_tab),
514 if (old_remote_contact) {
515 g_signal_handlers_disconnect_by_func (old_remote_contact,
516 chat_window_update_chat_tab,
520 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
524 chat_window_update_chat_tab (chat);
528 chat_window_insert_smiley_activate_cb (EmpathySmileyManager *manager,
529 EmpathySmiley *smiley,
532 EmpathyChatWindowPriv *priv = GET_PRIV (window);
534 GtkTextBuffer *buffer;
537 chat = priv->current_chat;
539 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
540 gtk_text_buffer_get_end_iter (buffer, &iter);
541 gtk_text_buffer_insert (buffer, &iter, smiley->str, -1);
545 chat_window_conv_activate_cb (GtkAction *action,
546 EmpathyChatWindow *window)
548 EmpathyChatWindowPriv *priv = GET_PRIV (window);
551 /* Favorite room menu */
552 is_room = empathy_chat_is_room (priv->current_chat);
558 room = empathy_chat_get_id (priv->current_chat);
559 account = empathy_chat_get_account (priv->current_chat);
560 found = empathy_chatroom_manager_find (priv->chatroom_manager,
561 account, room) != NULL;
563 DEBUG ("This room %s favorite", found ? "is" : "is not");
564 gtk_toggle_action_set_active (
565 GTK_TOGGLE_ACTION (priv->menu_conv_favorite), found);
567 gtk_action_set_visible (priv->menu_conv_favorite, is_room);
571 chat_window_clear_activate_cb (GtkAction *action,
572 EmpathyChatWindow *window)
574 EmpathyChatWindowPriv *priv = GET_PRIV (window);
576 empathy_chat_clear (priv->current_chat);
580 chat_window_favorite_toggled_cb (GtkToggleAction *toggle_action,
581 EmpathyChatWindow *window)
583 EmpathyChatWindowPriv *priv = GET_PRIV (window);
587 EmpathyChatroom *chatroom;
589 active = gtk_toggle_action_get_active (toggle_action);
590 account = empathy_chat_get_account (priv->current_chat);
591 room = empathy_chat_get_id (priv->current_chat);
593 chatroom = empathy_chatroom_manager_find (priv->chatroom_manager,
596 if (active && !chatroom) {
599 name = empathy_chat_get_name (priv->current_chat);
600 chatroom = empathy_chatroom_new_full (account, room, name, FALSE);
601 empathy_chatroom_manager_add (priv->chatroom_manager, chatroom);
602 g_object_unref (chatroom);
606 if (!active && chatroom) {
607 empathy_chatroom_manager_remove (priv->chatroom_manager, chatroom);
612 chat_get_window_id_for_geometry (EmpathyChat *chat)
614 const gchar *res = NULL;
615 gboolean separate_windows;
617 empathy_conf_get_bool (empathy_conf_get (),
618 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
621 if (separate_windows) {
622 res = empathy_chat_get_id (chat);
625 return res ? res : "chat-window";
629 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
631 EmpathyChatWindowPriv *priv;
634 priv = GET_PRIV (window);
636 gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
637 gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
639 empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
642 priv->save_geometry_id = 0;
648 chat_window_configure_event_cb (GtkWidget *widget,
649 GdkEventConfigure *event,
650 EmpathyChatWindow *window)
652 EmpathyChatWindowPriv *priv;
654 priv = GET_PRIV (window);
656 if (priv->save_geometry_id != 0) {
657 g_source_remove (priv->save_geometry_id);
660 priv->save_geometry_id =
661 g_timeout_add_seconds (1,
662 (GSourceFunc) chat_window_save_geometry_timeout_cb,
669 chat_window_close_activate_cb (GtkAction *action,
670 EmpathyChatWindow *window)
672 EmpathyChatWindowPriv *priv;
674 priv = GET_PRIV (window);
676 g_return_if_fail (priv->current_chat != NULL);
678 empathy_chat_window_remove_chat (window, priv->current_chat);
682 chat_window_edit_activate_cb (GtkAction *action,
683 EmpathyChatWindow *window)
685 EmpathyChatWindowPriv *priv;
686 GtkClipboard *clipboard;
687 GtkTextBuffer *buffer;
688 gboolean text_available;
690 priv = GET_PRIV (window);
692 g_return_if_fail (priv->current_chat != NULL);
694 if (!empathy_chat_get_tp_chat (priv->current_chat)) {
695 gtk_action_set_sensitive (priv->menu_edit_copy, FALSE);
696 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
697 gtk_action_set_sensitive (priv->menu_edit_paste, FALSE);
701 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
702 if (gtk_text_buffer_get_has_selection (buffer)) {
703 gtk_action_set_sensitive (priv->menu_edit_copy, TRUE);
704 gtk_action_set_sensitive (priv->menu_edit_cut, TRUE);
708 selection = empathy_chat_view_get_has_selection (priv->current_chat->view);
710 gtk_action_set_sensitive (priv->menu_edit_cut, FALSE);
711 gtk_action_set_sensitive (priv->menu_edit_copy, selection);
714 clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
715 text_available = gtk_clipboard_wait_is_text_available (clipboard);
716 gtk_action_set_sensitive (priv->menu_edit_paste, text_available);
720 chat_window_cut_activate_cb (GtkAction *action,
721 EmpathyChatWindow *window)
723 EmpathyChatWindowPriv *priv;
725 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
727 priv = GET_PRIV (window);
729 empathy_chat_cut (priv->current_chat);
733 chat_window_copy_activate_cb (GtkAction *action,
734 EmpathyChatWindow *window)
736 EmpathyChatWindowPriv *priv;
738 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
740 priv = GET_PRIV (window);
742 empathy_chat_copy (priv->current_chat);
746 chat_window_paste_activate_cb (GtkAction *action,
747 EmpathyChatWindow *window)
749 EmpathyChatWindowPriv *priv;
751 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
753 priv = GET_PRIV (window);
755 empathy_chat_paste (priv->current_chat);
759 chat_window_tabs_left_activate_cb (GtkAction *action,
760 EmpathyChatWindow *window)
762 EmpathyChatWindowPriv *priv;
766 priv = GET_PRIV (window);
768 chat = priv->current_chat;
769 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
774 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
780 chat_window_tabs_right_activate_cb (GtkAction *action,
781 EmpathyChatWindow *window)
783 EmpathyChatWindowPriv *priv;
787 priv = GET_PRIV (window);
789 chat = priv->current_chat;
790 index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
792 gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
798 chat_window_detach_activate_cb (GtkAction *action,
799 EmpathyChatWindow *window)
801 EmpathyChatWindowPriv *priv;
802 EmpathyChatWindow *new_window;
805 priv = GET_PRIV (window);
807 chat = priv->current_chat;
808 new_window = empathy_chat_window_new ();
810 empathy_chat_window_move_chat (window, new_window, chat);
812 priv = GET_PRIV (new_window);
813 gtk_widget_show (priv->dialog);
817 chat_window_help_contents_activate_cb (GtkAction *action,
818 EmpathyChatWindow *window)
820 EmpathyChatWindowPriv *priv = GET_PRIV (window);
822 empathy_url_show (priv->dialog, "ghelp:empathy?chat");
826 chat_window_help_about_activate_cb (GtkAction *action,
827 EmpathyChatWindow *window)
829 EmpathyChatWindowPriv *priv = GET_PRIV (window);
831 empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
835 chat_window_delete_event_cb (GtkWidget *dialog,
837 EmpathyChatWindow *window)
839 EmpathyChatWindowPriv *priv = GET_PRIV (window);
841 DEBUG ("Delete event received");
843 g_object_ref (window);
844 while (priv->chats) {
845 empathy_chat_window_remove_chat (window, priv->chats->data);
847 g_object_unref (window);
853 chat_window_composing_cb (EmpathyChat *chat,
854 gboolean is_composing,
855 EmpathyChatWindow *window)
857 EmpathyChatWindowPriv *priv;
859 priv = GET_PRIV (window);
861 if (is_composing && !g_list_find (priv->chats_composing, chat)) {
862 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
864 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
867 chat_window_update_chat_tab (chat);
871 chat_window_set_urgency_hint (EmpathyChatWindow *window,
874 EmpathyChatWindowPriv *priv;
876 priv = GET_PRIV (window);
878 DEBUG ("Turning %s urgency hint", urgent ? "on" : "off");
879 gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
883 EmpathyChatWindow *window;
888 chat_window_notification_closed_cb (NotifyNotification *notify,
889 NotificationData *cb_data)
891 EmpathyNotificationClosedReason reason = 0;
892 EmpathyChatWindowPriv *priv = GET_PRIV (cb_data->window);
894 #ifdef notify_notification_get_closed_reason
895 reason = notify_notification_get_closed_reason (notify);
897 if (reason == EMPATHY_NOTIFICATION_CLOSED_DISMISSED) {
898 empathy_chat_window_present_chat (cb_data->chat);
901 g_object_unref (notify);
902 priv->notification = NULL;
903 g_object_unref (cb_data->chat);
904 g_slice_free (NotificationData, cb_data);
908 chat_window_show_or_update_notification (EmpathyChatWindow *window,
909 EmpathyMessage *message,
912 EmpathyContact *sender;
917 NotificationData *cb_data;
918 EmpathyChatWindowPriv *priv = GET_PRIV (window);
921 if (!empathy_notification_is_enabled ()) {
924 empathy_conf_get_bool (empathy_conf_get (),
925 EMPATHY_PREFS_NOTIFICATIONS_FOCUS, &res);
931 cb_data = g_slice_new0 (NotificationData);
932 cb_data->chat = g_object_ref (chat);
933 cb_data->window = window;
935 sender = empathy_message_get_sender (message);
936 header = empathy_contact_get_name (sender);
937 body = empathy_message_get_body (message);
938 escaped = g_markup_escape_text (body, -1);
940 pixbuf = empathy_misc_get_pixbuf_for_notification (sender, EMPATHY_IMAGE_NEW_MESSAGE);
942 if (priv->notification != NULL) {
943 notify_notification_update (priv->notification,
944 header, escaped, NULL);
945 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
947 priv->notification = notify_notification_new (header, escaped, NULL, NULL);
948 notify_notification_set_timeout (priv->notification, NOTIFY_EXPIRES_DEFAULT);
949 notify_notification_set_icon_from_pixbuf (priv->notification, pixbuf);
951 g_signal_connect (priv->notification, "closed",
952 G_CALLBACK (chat_window_notification_closed_cb), cb_data);
955 notify_notification_show (priv->notification, NULL);
957 g_object_unref (pixbuf);
962 chat_window_new_message_cb (EmpathyChat *chat,
963 EmpathyMessage *message,
964 EmpathyChatWindow *window)
966 EmpathyChatWindowPriv *priv;
968 gboolean needs_urgency;
969 EmpathyContact *sender;
971 priv = GET_PRIV (window);
973 has_focus = empathy_chat_window_has_focus (window);
975 /* - if we're the sender, we play the sound if it's specified in the
976 * preferences and we're not away.
977 * - if we receive a message, we play the sound if it's specified in the
978 * preferences and the window does not have focus on the chat receiving
982 sender = empathy_message_get_sender (message);
984 if (empathy_contact_is_user (sender)) {
985 empathy_sound_play (GTK_WIDGET (priv->dialog),
986 EMPATHY_SOUND_MESSAGE_OUTGOING);
989 if (has_focus && priv->current_chat == chat) {
993 /* If empathy_chat_is_room () returns TRUE, that means it's a named MUC.
994 * If empathy_chat_get_remote_contact () returns NULL, that means it's
995 * an unamed MUC (msn-like).
996 * In case of a MUC, we set urgency only if the message contains our
998 if (empathy_chat_is_room (chat) ||
999 empathy_chat_get_remote_contact (chat) == NULL) {
1000 needs_urgency = empathy_message_should_highlight (message);
1002 needs_urgency = TRUE;
1005 if (needs_urgency) {
1007 chat_window_set_urgency_hint (window, TRUE);
1009 empathy_sound_play (GTK_WIDGET (priv->dialog),
1010 EMPATHY_SOUND_MESSAGE_INCOMING);
1011 chat_window_show_or_update_notification (window, message, chat);
1014 if (!g_list_find (priv->chats_new_msg, chat)) {
1015 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
1016 chat_window_update_chat_tab (chat);
1020 static GtkNotebook *
1021 chat_window_detach_hook (GtkNotebook *source,
1027 EmpathyChatWindowPriv *priv;
1028 EmpathyChatWindow *window, *new_window;
1031 chat = EMPATHY_CHAT (page);
1032 window = chat_window_find_chat (chat);
1034 new_window = empathy_chat_window_new ();
1035 priv = GET_PRIV (new_window);
1037 DEBUG ("Detach hook called");
1039 empathy_chat_window_move_chat (window, new_window, chat);
1041 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1042 gtk_widget_show (priv->dialog);
1048 chat_window_page_switched_cb (GtkNotebook *notebook,
1049 GtkNotebookPage *page,
1051 EmpathyChatWindow *window)
1053 EmpathyChatWindowPriv *priv;
1057 DEBUG ("Page switched");
1059 priv = GET_PRIV (window);
1061 child = gtk_notebook_get_nth_page (notebook, page_num);
1062 chat = EMPATHY_CHAT (child);
1064 if (priv->page_added) {
1065 priv->page_added = FALSE;
1066 empathy_chat_scroll_down (chat);
1068 else if (priv->current_chat == chat) {
1072 priv->current_chat = chat;
1073 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1075 chat_window_update_chat_tab (chat);
1079 chat_window_page_added_cb (GtkNotebook *notebook,
1082 EmpathyChatWindow *window)
1084 EmpathyChatWindowPriv *priv;
1087 priv = GET_PRIV (window);
1089 /* If we just received DND to the same window, we don't want
1090 * to do anything here like removing the tab and then readding
1091 * it, so we return here and in "page-added".
1093 if (priv->dnd_same_window) {
1094 DEBUG ("Page added (back to the same window)");
1095 priv->dnd_same_window = FALSE;
1099 DEBUG ("Page added");
1101 /* Get chat object */
1102 chat = EMPATHY_CHAT (child);
1104 /* Connect chat signals for this window */
1105 g_signal_connect (chat, "composing",
1106 G_CALLBACK (chat_window_composing_cb),
1108 g_signal_connect (chat, "new-message",
1109 G_CALLBACK (chat_window_new_message_cb),
1112 /* Set flag so we know to perform some special operations on
1113 * switch page due to the new page being added.
1115 priv->page_added = TRUE;
1117 /* Get list of chats up to date */
1118 priv->chats = g_list_append (priv->chats, chat);
1120 chat_window_update_chat_tab (chat);
1124 chat_window_page_removed_cb (GtkNotebook *notebook,
1127 EmpathyChatWindow *window)
1129 EmpathyChatWindowPriv *priv;
1132 priv = GET_PRIV (window);
1134 /* If we just received DND to the same window, we don't want
1135 * to do anything here like removing the tab and then readding
1136 * it, so we return here and in "page-added".
1138 if (priv->dnd_same_window) {
1139 DEBUG ("Page removed (and will be readded to same window)");
1143 DEBUG ("Page removed");
1145 /* Get chat object */
1146 chat = EMPATHY_CHAT (child);
1148 /* Disconnect all signal handlers for this chat and this window */
1149 g_signal_handlers_disconnect_by_func (chat,
1150 G_CALLBACK (chat_window_composing_cb),
1152 g_signal_handlers_disconnect_by_func (chat,
1153 G_CALLBACK (chat_window_new_message_cb),
1156 /* Keep list of chats up to date */
1157 priv->chats = g_list_remove (priv->chats, chat);
1158 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
1159 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
1161 if (priv->chats == NULL) {
1162 g_object_unref (window);
1164 chat_window_update (window);
1169 chat_window_focus_in_event_cb (GtkWidget *widget,
1171 EmpathyChatWindow *window)
1173 EmpathyChatWindowPriv *priv;
1175 DEBUG ("Focus in event, updating title");
1177 priv = GET_PRIV (window);
1179 priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
1181 chat_window_set_urgency_hint (window, FALSE);
1183 /* Update the title, since we now mark all unread messages as read. */
1184 chat_window_update_chat_tab (priv->current_chat);
1190 chat_window_drag_data_received (GtkWidget *widget,
1191 GdkDragContext *context,
1194 GtkSelectionData *selection,
1197 EmpathyChatWindow *window)
1199 if (info == DND_DRAG_TYPE_CONTACT_ID) {
1201 EmpathyChatWindow *old_window;
1205 const gchar *account_id;
1206 const gchar *contact_id;
1208 id = (const gchar*) selection->data;
1210 DEBUG ("DND contact from roster with id:'%s'", id);
1212 strv = g_strsplit (id, "/", 2);
1213 account_id = strv[0];
1214 contact_id = strv[1];
1215 account = mc_account_lookup (account_id);
1216 chat = empathy_chat_window_find_chat (account, contact_id);
1219 EmpathyAccountManager *account_manager;
1220 TpConnection *connection;
1222 account_manager = empathy_account_manager_dup_singleton ();
1223 connection = empathy_account_manager_get_connection (
1224 account_manager, account);
1227 empathy_dispatcher_chat_with_contact_id (
1228 connection, contact_id, NULL, NULL);
1231 g_object_unref (account_manager);
1232 g_object_unref (account);
1236 g_object_unref (account);
1239 old_window = chat_window_find_chat (chat);
1241 if (old_window == window) {
1242 gtk_drag_finish (context, TRUE, FALSE, time);
1246 empathy_chat_window_move_chat (old_window, window, chat);
1248 empathy_chat_window_add_chat (window, chat);
1251 /* Added to take care of any outstanding chat events */
1252 empathy_chat_window_present_chat (chat);
1254 /* We should return TRUE to remove the data when doing
1255 * GDK_ACTION_MOVE, but we don't here otherwise it has
1256 * weird consequences, and we handle that internally
1257 * anyway with add_chat () and remove_chat ().
1259 gtk_drag_finish (context, TRUE, FALSE, time);
1261 else if (info == DND_DRAG_TYPE_TAB) {
1263 EmpathyChatWindow *old_window = NULL;
1267 chat = (void *) selection->data;
1268 old_window = chat_window_find_chat (*chat);
1271 EmpathyChatWindowPriv *priv;
1273 priv = GET_PRIV (window);
1275 if (old_window == window) {
1276 DEBUG ("DND tab (within same window)");
1277 priv->dnd_same_window = TRUE;
1278 gtk_drag_finish (context, TRUE, FALSE, time);
1282 priv->dnd_same_window = FALSE;
1285 /* We should return TRUE to remove the data when doing
1286 * GDK_ACTION_MOVE, but we don't here otherwise it has
1287 * weird consequences, and we handle that internally
1288 * anyway with add_chat () and remove_chat ().
1290 gtk_drag_finish (context, TRUE, FALSE, time);
1292 DEBUG ("DND from unknown source");
1293 gtk_drag_finish (context, FALSE, FALSE, time);
1298 chat_window_finalize (GObject *object)
1300 EmpathyChatWindow *window;
1301 EmpathyChatWindowPriv *priv;
1303 window = EMPATHY_CHAT_WINDOW (object);
1304 priv = GET_PRIV (window);
1306 DEBUG ("Finalized: %p", object);
1308 g_object_unref (priv->ui_manager);
1309 g_object_unref (priv->chatroom_manager);
1310 if (priv->save_geometry_id != 0) {
1311 g_source_remove (priv->save_geometry_id);
1314 if (priv->notification != NULL) {
1315 notify_notification_close (priv->notification, NULL);
1316 g_object_unref (priv->notification);
1317 priv->notification = NULL;
1320 chat_windows = g_list_remove (chat_windows, window);
1321 gtk_widget_destroy (priv->dialog);
1323 G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1327 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1329 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1331 object_class->finalize = chat_window_finalize;
1333 g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1335 /* Set up a style for the close button with no focus padding. */
1336 gtk_rc_parse_string (
1337 "style \"empathy-close-button-style\"\n"
1339 " GtkWidget::focus-padding = 0\n"
1343 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1345 gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1349 empathy_chat_window_init (EmpathyChatWindow *window)
1352 GtkAccelGroup *accel_group;
1357 GtkWidget *chat_vbox;
1359 EmpathySmileyManager *smiley_manager;
1360 EmpathyChatWindowPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (window,
1361 EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv);
1363 window->priv = priv;
1364 filename = empathy_file_lookup ("empathy-chat-window.ui", "src");
1365 gui = empathy_builder_get_file (filename,
1366 "chat_window", &priv->dialog,
1367 "chat_vbox", &chat_vbox,
1368 "ui_manager", &priv->ui_manager,
1369 "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1370 "menu_conv_favorite", &priv->menu_conv_favorite,
1371 "menu_edit_cut", &priv->menu_edit_cut,
1372 "menu_edit_copy", &priv->menu_edit_copy,
1373 "menu_edit_paste", &priv->menu_edit_paste,
1374 "menu_tabs_next", &priv->menu_tabs_next,
1375 "menu_tabs_prev", &priv->menu_tabs_prev,
1376 "menu_tabs_left", &priv->menu_tabs_left,
1377 "menu_tabs_right", &priv->menu_tabs_right,
1378 "menu_tabs_detach", &priv->menu_tabs_detach,
1382 empathy_builder_connect (gui, window,
1383 "chat_window", "configure-event", chat_window_configure_event_cb,
1384 "menu_conv", "activate", chat_window_conv_activate_cb,
1385 "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1386 "menu_conv_favorite", "toggled", chat_window_favorite_toggled_cb,
1387 "menu_conv_close", "activate", chat_window_close_activate_cb,
1388 "menu_edit", "activate", chat_window_edit_activate_cb,
1389 "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1390 "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1391 "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1392 "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1393 "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1394 "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1395 "menu_help_contents", "activate", chat_window_help_contents_activate_cb,
1396 "menu_help_about", "activate", chat_window_help_about_activate_cb,
1399 g_object_ref (priv->ui_manager);
1400 g_object_unref (gui);
1402 priv->chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
1404 priv->notebook = gtk_notebook_new ();
1405 gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow");
1406 gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
1407 gtk_notebook_popup_enable (GTK_NOTEBOOK (priv->notebook));
1408 gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1409 gtk_widget_show (priv->notebook);
1412 accel_group = gtk_accel_group_new ();
1413 gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1415 for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1416 closure = g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1419 gtk_accel_group_connect (accel_group,
1426 g_object_unref (accel_group);
1428 /* Set up smiley menu */
1429 smiley_manager = empathy_smiley_manager_dup_singleton ();
1430 submenu = empathy_smiley_menu_new (smiley_manager,
1431 chat_window_insert_smiley_activate_cb,
1433 menu = gtk_ui_manager_get_widget (priv->ui_manager,
1434 "/chats_menubar/menu_conv/menu_conv_insert_smiley");
1435 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu), submenu);
1436 g_object_unref (smiley_manager);
1438 /* Set up signals we can't do with ui file since we may need to
1439 * block/unblock them at some later stage.
1442 g_signal_connect (priv->dialog,
1444 G_CALLBACK (chat_window_delete_event_cb),
1447 g_signal_connect_swapped (priv->menu_tabs_prev,
1449 G_CALLBACK (gtk_notebook_prev_page),
1451 g_signal_connect_swapped (priv->menu_tabs_next,
1453 G_CALLBACK (gtk_notebook_next_page),
1456 g_signal_connect (priv->dialog,
1458 G_CALLBACK (chat_window_focus_in_event_cb),
1460 g_signal_connect_after (priv->notebook,
1462 G_CALLBACK (chat_window_page_switched_cb),
1464 g_signal_connect (priv->notebook,
1466 G_CALLBACK (chat_window_page_added_cb),
1468 g_signal_connect (priv->notebook,
1470 G_CALLBACK (chat_window_page_removed_cb),
1473 /* Set up drag and drop */
1474 gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1475 GTK_DEST_DEFAULT_ALL,
1477 G_N_ELEMENTS (drag_types_dest),
1480 g_signal_connect (priv->notebook,
1481 "drag-data-received",
1482 G_CALLBACK (chat_window_drag_data_received),
1485 chat_windows = g_list_prepend (chat_windows, window);
1487 /* Set up private details */
1489 priv->chats_new_msg = NULL;
1490 priv->chats_composing = NULL;
1491 priv->current_chat = NULL;
1495 empathy_chat_window_new (void)
1497 return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1500 /* Returns the window to open a new tab in if there is only one window
1501 * visble, otherwise, returns NULL indicating that a new window should
1505 empathy_chat_window_get_default (void)
1508 gboolean separate_windows = TRUE;
1510 empathy_conf_get_bool (empathy_conf_get (),
1511 EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1514 if (separate_windows) {
1515 /* Always create a new window */
1519 for (l = chat_windows; l; l = l->next) {
1520 EmpathyChatWindow *chat_window;
1523 chat_window = l->data;
1525 dialog = empathy_chat_window_get_dialog (chat_window);
1526 if (empathy_window_get_is_visible (GTK_WINDOW (dialog))) {
1527 /* Found a visible window on this desktop */
1536 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1538 EmpathyChatWindowPriv *priv;
1540 g_return_val_if_fail (window != NULL, NULL);
1542 priv = GET_PRIV (window);
1544 return priv->dialog;
1548 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1551 EmpathyChatWindowPriv *priv;
1553 GtkWidget *popup_label;
1557 g_return_if_fail (window != NULL);
1558 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1560 priv = GET_PRIV (window);
1562 /* Reference the chat object */
1563 g_object_ref (chat);
1565 /* If this window has just been created, position it */
1566 if (priv->chats == NULL) {
1567 empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1569 if (x >= 0 && y >= 0) {
1570 /* Let the window manager position it if we don't have
1571 * good x, y coordinates.
1573 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1576 if (w > 0 && h > 0) {
1577 /* Use the defaults from the ui file if we don't have
1578 * good w, h geometry.
1580 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1584 child = GTK_WIDGET (chat);
1585 label = chat_window_create_label (window, chat, TRUE);
1586 popup_label = chat_window_create_label (window, chat, FALSE);
1587 gtk_widget_show (child);
1589 g_signal_connect (chat, "notify::name",
1590 G_CALLBACK (chat_window_chat_notify_cb),
1592 g_signal_connect (chat, "notify::subject",
1593 G_CALLBACK (chat_window_chat_notify_cb),
1595 g_signal_connect (chat, "notify::remote-contact",
1596 G_CALLBACK (chat_window_chat_notify_cb),
1598 chat_window_chat_notify_cb (chat);
1600 gtk_notebook_append_page_menu (GTK_NOTEBOOK (priv->notebook), child, label, popup_label);
1601 gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1602 gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1603 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1604 TRUE, TRUE, GTK_PACK_START);
1606 DEBUG ("Chat added (%d references)", G_OBJECT (chat)->ref_count);
1610 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1613 EmpathyChatWindowPriv *priv;
1615 EmpathyContact *remote_contact;
1617 g_return_if_fail (window != NULL);
1618 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1620 priv = GET_PRIV (window);
1622 g_signal_handlers_disconnect_by_func (chat,
1623 chat_window_chat_notify_cb,
1625 remote_contact = g_object_get_data (G_OBJECT (chat),
1626 "chat-window-remote-contact");
1627 if (remote_contact) {
1628 g_signal_handlers_disconnect_by_func (remote_contact,
1629 chat_window_update_chat_tab,
1633 position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1635 gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1637 DEBUG ("Chat removed (%d references)", G_OBJECT (chat)->ref_count - 1);
1639 g_object_unref (chat);
1643 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1644 EmpathyChatWindow *new_window,
1649 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1650 g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1651 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1653 widget = GTK_WIDGET (chat);
1655 DEBUG ("Chat moving with widget:%p (%d references)", widget,
1656 G_OBJECT (widget)->ref_count);
1658 /* We reference here to make sure we don't loose the widget
1659 * and the EmpathyChat object during the move.
1661 g_object_ref (chat);
1662 g_object_ref (widget);
1664 empathy_chat_window_remove_chat (old_window, chat);
1665 empathy_chat_window_add_chat (new_window, chat);
1667 g_object_unref (widget);
1668 g_object_unref (chat);
1672 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1675 EmpathyChatWindowPriv *priv;
1678 g_return_if_fail (window != NULL);
1679 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1681 priv = GET_PRIV (window);
1683 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1685 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1690 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1692 EmpathyChatWindowPriv *priv;
1695 g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1697 priv = GET_PRIV (window);
1699 g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1705 empathy_chat_window_find_chat (McAccount *account,
1710 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1711 g_return_val_if_fail (!EMP_STR_EMPTY (id), NULL);
1713 for (l = chat_windows; l; l = l->next) {
1714 EmpathyChatWindowPriv *priv;
1715 EmpathyChatWindow *window;
1719 priv = GET_PRIV (window);
1721 for (ll = priv->chats; ll; ll = ll->next) {
1726 if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1727 !tp_strdiff (id, empathy_chat_get_id (chat))) {
1737 empathy_chat_window_present_chat (EmpathyChat *chat)
1739 EmpathyChatWindow *window;
1740 EmpathyChatWindowPriv *priv;
1742 g_return_if_fail (EMPATHY_IS_CHAT (chat));
1744 window = chat_window_find_chat (chat);
1746 /* If the chat has no window, create one */
1747 if (window == NULL) {
1748 window = empathy_chat_window_get_default ();
1750 window = empathy_chat_window_new ();
1753 empathy_chat_window_add_chat (window, chat);
1756 priv = GET_PRIV (window);
1757 empathy_chat_window_switch_to_chat (window, chat);
1758 empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1760 gtk_widget_grab_focus (chat->input_text_view);