]> git.0d.be Git - empathy.git/blob - src/empathy-chat-window.c
Fix crash in DND of chat tab.
[empathy.git] / src / empathy-chat-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
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  */
27
28 #include <config.h>
29
30 #include <string.h>
31
32 #include <gtk/gtk.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <glade/glade.h>
35 #include <glib/gi18n.h>
36
37 #include <libmissioncontrol/mission-control.h>
38
39 #include <libempathy/empathy-contact-factory.h>
40 #include <libempathy/empathy-contact.h>
41 #include <libempathy/empathy-message.h>
42 #include <libempathy/empathy-debug.h>
43 #include <libempathy/empathy-utils.h>
44
45 #include <libempathy-gtk/empathy-images.h>
46 #include <libempathy-gtk/empathy-conf.h>
47 #include <libempathy-gtk/empathy-contact-dialogs.h>
48 #include <libempathy-gtk/empathy-log-window.h>
49 #include <libempathy-gtk/empathy-geometry.h>
50 #include <libempathy-gtk/empathy-ui-utils.h>
51
52 #include "empathy-chat-window.h"
53 #include "empathy-about-dialog.h"
54
55 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CHAT_WINDOW, EmpathyChatWindowPriv))
56
57 #define DEBUG_DOMAIN "ChatWindow"
58
59 struct _EmpathyChatWindowPriv {
60         EmpathyChat *current_chat;
61         GList       *chats;
62         GList       *chats_new_msg;
63         GList       *chats_composing;
64         gboolean     page_added;
65         gboolean     dnd_same_window;
66         guint        save_geometry_id;
67         GtkWidget   *dialog;
68         GtkWidget   *notebook;
69
70         /* Menu items. */
71         GtkWidget   *menu_conv_clear;
72         GtkWidget   *menu_conv_insert_smiley;
73         GtkWidget   *menu_conv_contact;
74         GtkWidget   *menu_conv_close;
75
76         GtkWidget   *menu_edit_cut;
77         GtkWidget   *menu_edit_copy;
78         GtkWidget   *menu_edit_paste;
79
80         GtkWidget   *menu_tabs_next;
81         GtkWidget   *menu_tabs_prev;
82         GtkWidget   *menu_tabs_left;
83         GtkWidget   *menu_tabs_right;
84         GtkWidget   *menu_tabs_detach;
85         
86         GtkWidget   *menu_help_contents;
87         GtkWidget   *menu_help_about;
88 };
89
90 static void       empathy_chat_window_class_init (EmpathyChatWindowClass *klass);
91 static void       empathy_chat_window_init       (EmpathyChatWindow      *window);
92 static void       chat_window_finalize           (GObject                *object);
93
94 static GList *chat_windows = NULL;
95
96 static const guint tab_accel_keys[] = {
97         GDK_1, GDK_2, GDK_3, GDK_4, GDK_5,
98         GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
99 };
100
101 typedef enum {
102         DND_DRAG_TYPE_CONTACT_ID,
103         DND_DRAG_TYPE_TAB
104 } DndDragType;
105
106 static const GtkTargetEntry drag_types_dest[] = {
107         { "text/contact-id", 0, DND_DRAG_TYPE_CONTACT_ID },
108         { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, DND_DRAG_TYPE_TAB },
109 };
110
111 G_DEFINE_TYPE (EmpathyChatWindow, empathy_chat_window, G_TYPE_OBJECT);
112
113 static void
114 chat_window_accel_cb (GtkAccelGroup    *accelgroup,
115                       GObject          *object,
116                       guint             key,
117                       GdkModifierType   mod,
118                       EmpathyChatWindow *window)
119 {
120         EmpathyChatWindowPriv *priv;
121         gint                  num = -1;
122         gint                  i;
123
124         priv = GET_PRIV (window);
125
126         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
127                 if (tab_accel_keys[i] == key) {
128                         num = i;
129                         break;
130                 }
131         }
132
133         if (num != -1) {
134                 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), num);
135         }
136 }
137
138 static EmpathyChatWindow *
139 chat_window_find_chat (EmpathyChat *chat)
140 {
141         EmpathyChatWindowPriv *priv;
142         GList                 *l, *ll;
143
144         for (l = chat_windows; l; l = l->next) {
145                 priv = GET_PRIV (l->data);
146                 ll = g_list_find (priv->chats, chat);
147                 if (ll) {
148                         return l->data;
149                 }
150         }
151
152         return NULL;
153 }
154
155 static void
156 chat_window_close_clicked_cb (GtkWidget  *button,
157                               EmpathyChat *chat)
158 {
159         EmpathyChatWindow *window;
160
161         window = chat_window_find_chat (chat);
162         empathy_chat_window_remove_chat (window, chat);
163 }
164
165 static void
166 chat_window_close_button_style_set_cb (GtkWidget *button,
167                                        GtkStyle  *previous_style,
168                                        gpointer   user_data)
169 {
170         gint h, w;
171
172         gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (button),
173                                            GTK_ICON_SIZE_MENU, &w, &h);
174
175         gtk_widget_set_size_request (button, w, h);
176 }
177
178 static GtkWidget *
179 chat_window_create_label (EmpathyChatWindow *window,
180                           EmpathyChat       *chat)
181 {
182         EmpathyChatWindowPriv *priv;
183         GtkWidget            *hbox;
184         GtkWidget            *name_label;
185         GtkWidget            *status_image;
186         GtkWidget            *close_button;
187         GtkWidget            *close_image;
188         GtkWidget            *event_box;
189         GtkWidget            *event_box_hbox;
190         PangoAttrList        *attr_list;
191         PangoAttribute       *attr;
192
193         priv = GET_PRIV (window);
194
195         /* The spacing between the button and the label. */
196         hbox = gtk_hbox_new (FALSE, 0);
197
198         event_box = gtk_event_box_new ();
199         gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
200
201         name_label = gtk_label_new (NULL);
202         gtk_label_set_ellipsize (GTK_LABEL (name_label), PANGO_ELLIPSIZE_END);
203
204         attr_list = pango_attr_list_new ();
205         attr = pango_attr_scale_new (1/1.2);
206         attr->start_index = 0;
207         attr->end_index = -1;
208         pango_attr_list_insert (attr_list, attr);
209         gtk_label_set_attributes (GTK_LABEL (name_label), attr_list);
210         pango_attr_list_unref (attr_list);
211
212         gtk_misc_set_padding (GTK_MISC (name_label), 2, 0);
213         gtk_misc_set_alignment (GTK_MISC (name_label), 0.0, 0.5);
214         g_object_set_data (G_OBJECT (chat), "chat-window-tab-label", name_label);
215
216         status_image = gtk_image_new ();
217
218         /* Spacing between the icon and label. */
219         event_box_hbox = gtk_hbox_new (FALSE, 0);
220
221         gtk_box_pack_start (GTK_BOX (event_box_hbox), status_image, FALSE, FALSE, 0);
222         gtk_box_pack_start (GTK_BOX (event_box_hbox), name_label, TRUE, TRUE, 0);
223
224         g_object_set_data (G_OBJECT (chat), "chat-window-tab-image", status_image);
225         g_object_set_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget", event_box);
226
227         close_button = gtk_button_new ();
228         gtk_button_set_relief (GTK_BUTTON (close_button), GTK_RELIEF_NONE);
229
230         /* We don't want focus/keynav for the button to avoid clutter, and
231          * Ctrl-W works anyway.
232          */
233         GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_FOCUS);
234         GTK_WIDGET_UNSET_FLAGS (close_button, GTK_CAN_DEFAULT);
235
236         /* Set the name to make the special rc style match. */
237         gtk_widget_set_name (close_button, "empathy-close-button");
238
239         close_image = gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
240
241         gtk_container_add (GTK_CONTAINER (close_button), close_image);
242
243         gtk_container_add (GTK_CONTAINER (event_box), event_box_hbox);
244         gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
245         gtk_box_pack_end (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);
246
247         /* React to theme changes and also used to setup the initial size
248          * correctly.
249          */
250         g_signal_connect (close_button,
251                           "style-set",
252                           G_CALLBACK (chat_window_close_button_style_set_cb),
253                           chat);
254
255         g_signal_connect (close_button,
256                           "clicked",
257                           G_CALLBACK (chat_window_close_clicked_cb),
258                           chat);
259
260         gtk_widget_show_all (hbox);
261
262         return hbox;
263 }
264
265 static const gchar *
266 chat_window_get_chat_name (EmpathyChat *chat)
267 {
268         EmpathyContact *remote_contact = NULL;
269         const gchar    *name = NULL;
270
271         name = empathy_chat_get_name (chat);
272         if (!name) {
273                 remote_contact = empathy_chat_get_remote_contact (chat);
274                 if (remote_contact) {
275                         name = empathy_contact_get_name (remote_contact);
276                 } else {
277                         name = _("Conversation");
278                 }
279         }
280
281         return name;
282 }
283
284 static void
285 chat_window_update (EmpathyChatWindow *window)
286 {
287         EmpathyChatWindowPriv *priv = GET_PRIV (window);
288         gboolean               first_page;
289         gboolean               last_page;
290         gboolean               is_connected;
291         gint                   num_pages;
292         gint                   page_num;
293         const gchar           *name;
294         guint                  n_chats;
295
296         /* Get information */
297         page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
298         num_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (priv->notebook));
299         first_page = (page_num == 0);
300         last_page = (page_num == (num_pages - 1));
301         is_connected = empathy_chat_get_tp_chat (priv->current_chat) != NULL;
302         name = chat_window_get_chat_name (priv->current_chat);
303         n_chats = g_list_length (priv->chats);
304
305         empathy_debug (DEBUG_DOMAIN, "Update window");
306
307         /* Update menu */
308         gtk_widget_set_sensitive (priv->menu_tabs_next, !last_page);
309         gtk_widget_set_sensitive (priv->menu_tabs_prev, !first_page);
310         gtk_widget_set_sensitive (priv->menu_tabs_detach, num_pages > 1);
311         gtk_widget_set_sensitive (priv->menu_tabs_left, !first_page);
312         gtk_widget_set_sensitive (priv->menu_tabs_right, !last_page);
313         gtk_widget_set_sensitive (priv->menu_conv_insert_smiley, is_connected);
314
315         /* Update window title */
316         if (n_chats == 1) {
317                 gtk_window_set_title (GTK_WINDOW (priv->dialog), name);
318         } else {
319                 gchar *title;
320
321                 title = g_strdup_printf (_("Conversations (%d)"), n_chats);
322                 gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
323                 g_free (title);
324         }
325
326         /* Update window icon */
327         if (priv->chats_new_msg) {
328                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog),
329                                           EMPATHY_IMAGE_MESSAGE);
330         } else {
331                 gtk_window_set_icon_name (GTK_WINDOW (priv->dialog), NULL);
332         }
333 }
334
335 static void
336 chat_window_update_chat_tab (EmpathyChat *chat)
337 {
338         EmpathyChatWindow     *window;
339         EmpathyChatWindowPriv *priv;
340         EmpathyContact        *remote_contact;
341         const gchar           *name;
342         const gchar           *subject;
343         GtkWidget             *widget;
344         GString               *tooltip;
345         gchar                 *str;
346         const gchar           *icon_name;
347
348         window = chat_window_find_chat (chat);
349         if (!window) {
350                 return;
351         }
352         priv = GET_PRIV (window);
353
354         /* Get information */
355         name = chat_window_get_chat_name (chat);
356         subject = empathy_chat_get_subject (chat);
357         remote_contact = empathy_chat_get_remote_contact (chat);
358
359         empathy_debug (DEBUG_DOMAIN, "Updating chat tab, name=%s, subject=%s, "
360                        "remote_contact=%p", name, subject, remote_contact);
361
362         /* Update tab image */
363         if (g_list_find (priv->chats_new_msg, chat)) {
364                 icon_name = EMPATHY_IMAGE_MESSAGE;
365         }
366         else if (g_list_find (priv->chats_composing, chat)) {
367                 icon_name = EMPATHY_IMAGE_TYPING;
368         }
369         else if (remote_contact) {
370                 icon_name = empathy_icon_name_for_contact (remote_contact);
371         } else {
372                 icon_name = EMPATHY_IMAGE_GROUP_MESSAGE;
373         }
374         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-image");
375         gtk_image_set_from_icon_name (GTK_IMAGE (widget), icon_name, GTK_ICON_SIZE_MENU);
376
377         /* Update tab tooltip */
378         tooltip = g_string_new (NULL);
379         if (remote_contact) {
380                 g_string_append_printf (tooltip, "%s\n%s",
381                                         empathy_contact_get_id (remote_contact),
382                                         empathy_contact_get_status (remote_contact));
383         }
384         else {
385                 g_string_append (tooltip, name);
386         }
387         if (subject) {
388                 g_string_append_printf (tooltip, "\n%s %s", _("Topic:"), subject);
389         }
390         if (g_list_find (priv->chats_composing, chat)) {
391                 g_string_append_printf (tooltip, "\n%s", _("Typing a message."));
392         }
393         str = g_string_free (tooltip, FALSE);
394         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-tooltip-widget");
395         gtk_widget_set_tooltip_text (widget, str);
396         g_free (str);
397
398         /* Update tab label */
399         widget = g_object_get_data (G_OBJECT (chat), "chat-window-tab-label");
400         gtk_label_set_text (GTK_LABEL (widget), name);
401
402         /* Update the window if it's the current chat */
403         if (priv->current_chat == chat) {
404                 chat_window_update (window);
405         }
406 }
407
408 static void
409 chat_window_chat_notify_cb (EmpathyChat *chat)
410 {
411         EmpathyContact *old_remote_contact;
412         EmpathyContact *remote_contact = NULL;
413
414         old_remote_contact = g_object_get_data (G_OBJECT (chat), "chat-window-remote-contact");
415         remote_contact = empathy_chat_get_remote_contact (chat);
416
417         if (old_remote_contact != remote_contact) {
418                 /* The remote-contact associated with the chat changed, we need
419                  * to keep track of any change of that contact and update the
420                  * window each time. */
421                 if (remote_contact) {
422                         g_signal_connect_swapped (remote_contact, "notify",
423                                                   G_CALLBACK (chat_window_update_chat_tab),
424                                                   chat);
425                 }
426                 if (old_remote_contact) {
427                         g_signal_handlers_disconnect_by_func (old_remote_contact,
428                                                               chat_window_update_chat_tab,
429                                                               chat);
430                 }
431
432                 g_object_set_data (G_OBJECT (chat), "chat-window-remote-contact",
433                                    remote_contact);
434         }
435
436         chat_window_update_chat_tab (chat);
437 }
438
439 static void
440 chat_window_insert_smiley_activate_cb (GtkWidget         *menuitem,
441                                        EmpathyChatWindow *window)
442 {
443         EmpathyChatWindowPriv *priv;
444         EmpathyChat           *chat;
445         GtkTextBuffer        *buffer;
446         GtkTextIter           iter;
447         const gchar          *smiley;
448
449         priv = GET_PRIV (window);
450
451         chat = priv->current_chat;
452
453         smiley = g_object_get_data (G_OBJECT (menuitem), "smiley_text");
454
455         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
456         gtk_text_buffer_get_end_iter (buffer, &iter);
457         gtk_text_buffer_insert (buffer, &iter,
458                                 smiley, -1);
459 }
460
461 static void
462 chat_window_clear_activate_cb (GtkWidget        *menuitem,
463                                EmpathyChatWindow *window)
464 {
465         EmpathyChatWindowPriv *priv;
466
467         priv = GET_PRIV (window);
468
469         empathy_chat_clear (priv->current_chat);
470 }
471
472 static const gchar *
473 chat_get_window_id_for_geometry (EmpathyChat *chat)
474 {
475         gboolean separate_windows;
476
477         empathy_conf_get_bool (empathy_conf_get (),
478                                EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
479                                &separate_windows);
480
481         if (separate_windows) {
482                 return empathy_chat_get_id (chat);
483         } else {
484                 return "chat-window";
485         }
486 }
487
488 static gboolean
489 chat_window_save_geometry_timeout_cb (EmpathyChatWindow *window)
490 {
491         EmpathyChatWindowPriv *priv;
492         gint                  x, y, w, h;
493
494         priv = GET_PRIV (window);
495
496         gtk_window_get_size (GTK_WINDOW (priv->dialog), &w, &h);
497         gtk_window_get_position (GTK_WINDOW (priv->dialog), &x, &y);
498
499         empathy_geometry_save (chat_get_window_id_for_geometry (priv->current_chat),
500                                x, y, w, h);
501
502         priv->save_geometry_id = 0;
503
504         return FALSE;
505 }
506
507 static gboolean
508 chat_window_configure_event_cb (GtkWidget         *widget,
509                                 GdkEventConfigure *event,
510                                 EmpathyChatWindow  *window)
511 {
512         EmpathyChatWindowPriv *priv;
513
514         priv = GET_PRIV (window);
515
516         if (priv->save_geometry_id != 0) {
517                 g_source_remove (priv->save_geometry_id);
518         }
519
520         priv->save_geometry_id =
521                 g_timeout_add_seconds (1,
522                                        (GSourceFunc) chat_window_save_geometry_timeout_cb,
523                                        window);
524
525         return FALSE;
526 }
527
528 static void
529 chat_window_close_activate_cb (GtkWidget        *menuitem,
530                                EmpathyChatWindow *window)
531 {
532         EmpathyChatWindowPriv *priv;
533
534         priv = GET_PRIV (window);
535
536         g_return_if_fail (priv->current_chat != NULL);
537
538         empathy_chat_window_remove_chat (window, priv->current_chat);
539 }
540
541 static void
542 chat_window_edit_activate_cb (GtkWidget        *menuitem,
543                               EmpathyChatWindow *window)
544 {
545         EmpathyChatWindowPriv *priv;
546         GtkClipboard         *clipboard;
547         GtkTextBuffer        *buffer;
548         gboolean              text_available;
549
550         priv = GET_PRIV (window);
551
552         g_return_if_fail (priv->current_chat != NULL);
553
554         if (!empathy_chat_get_tp_chat (priv->current_chat)) {
555                 gtk_widget_set_sensitive (priv->menu_edit_copy, FALSE);
556                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
557                 gtk_widget_set_sensitive (priv->menu_edit_paste, FALSE);
558                 return;
559         }
560
561         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->current_chat->input_text_view));
562         if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL)) {
563                 gtk_widget_set_sensitive (priv->menu_edit_copy, TRUE);
564                 gtk_widget_set_sensitive (priv->menu_edit_cut, TRUE);
565         } else {
566                 gboolean selection;
567
568                 selection = empathy_chat_view_get_selection_bounds (priv->current_chat->view, 
569                                                                    NULL, NULL);
570
571                 gtk_widget_set_sensitive (priv->menu_edit_cut, FALSE);
572                 gtk_widget_set_sensitive (priv->menu_edit_copy, selection);
573         }
574
575         clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
576         text_available = gtk_clipboard_wait_is_text_available (clipboard);
577         gtk_widget_set_sensitive (priv->menu_edit_paste, text_available);
578 }
579
580 static void
581 chat_window_cut_activate_cb (GtkWidget        *menuitem,
582                              EmpathyChatWindow *window)
583 {
584         EmpathyChatWindowPriv *priv;
585
586         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
587
588         priv = GET_PRIV (window);
589
590         empathy_chat_cut (priv->current_chat);
591 }
592
593 static void
594 chat_window_copy_activate_cb (GtkWidget        *menuitem,
595                               EmpathyChatWindow *window)
596 {
597         EmpathyChatWindowPriv *priv;
598
599         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
600
601         priv = GET_PRIV (window);
602
603         empathy_chat_copy (priv->current_chat);
604 }
605
606 static void
607 chat_window_paste_activate_cb (GtkWidget        *menuitem,
608                                EmpathyChatWindow *window)
609 {
610         EmpathyChatWindowPriv *priv;
611
612         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (window));
613
614         priv = GET_PRIV (window);
615
616         empathy_chat_paste (priv->current_chat);
617 }
618
619 static void
620 chat_window_tabs_left_activate_cb (GtkWidget        *menuitem,
621                                    EmpathyChatWindow *window)
622 {
623         EmpathyChatWindowPriv *priv;
624         EmpathyChat           *chat;
625         gint                  index;
626
627         priv = GET_PRIV (window);
628
629         chat = priv->current_chat;
630         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
631         if (index <= 0) {
632                 return;
633         }
634
635         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
636                                     GTK_WIDGET (chat),
637                                     index - 1);
638 }
639
640 static void
641 chat_window_tabs_right_activate_cb (GtkWidget        *menuitem,
642                                     EmpathyChatWindow *window)
643 {
644         EmpathyChatWindowPriv *priv;
645         EmpathyChat           *chat;
646         gint                  index;
647
648         priv = GET_PRIV (window);
649
650         chat = priv->current_chat;
651         index = gtk_notebook_get_current_page (GTK_NOTEBOOK (priv->notebook));
652
653         gtk_notebook_reorder_child (GTK_NOTEBOOK (priv->notebook),
654                                     GTK_WIDGET (chat),
655                                     index + 1);
656 }
657
658 static void
659 chat_window_detach_activate_cb (GtkWidget        *menuitem,
660                                 EmpathyChatWindow *window)
661 {
662         EmpathyChatWindowPriv *priv;
663         EmpathyChatWindow     *new_window;
664         EmpathyChat           *chat;
665
666         priv = GET_PRIV (window);
667
668         chat = priv->current_chat;
669         new_window = empathy_chat_window_new ();
670
671         empathy_chat_window_move_chat (window, new_window, chat);
672
673         priv = GET_PRIV (new_window);
674         gtk_widget_show (priv->dialog);
675 }
676
677 static void
678 chat_window_help_contents_cb (GtkWidget         *menuitem,
679                               EmpathyChatWindow *window)
680 {
681         //empathy_help_show ();
682 }
683
684 static void
685 chat_window_help_about_cb (GtkWidget         *menuitem,
686                            EmpathyChatWindow *window)
687 {
688         EmpathyChatWindowPriv *priv = GET_PRIV (window);
689
690         empathy_about_dialog_new (GTK_WINDOW (priv->dialog));
691 }
692
693 static gboolean
694 chat_window_delete_event_cb (GtkWidget        *dialog,
695                              GdkEvent         *event,
696                              EmpathyChatWindow *window)
697 {
698         EmpathyChatWindowPriv *priv;
699         GList                *list;
700         GList                *l;
701
702         priv = GET_PRIV (window);
703
704         empathy_debug (DEBUG_DOMAIN, "Delete event received");
705
706         list = g_list_copy (priv->chats);
707
708         for (l = list; l; l = l->next) {
709                 empathy_chat_window_remove_chat (window, l->data);
710         }
711
712         g_list_free (list);
713
714         return TRUE;
715 }
716
717 static void
718 chat_window_composing_cb (EmpathyChat       *chat,
719                           gboolean          is_composing,
720                           EmpathyChatWindow *window)
721 {
722         EmpathyChatWindowPriv *priv;
723
724         priv = GET_PRIV (window);
725
726         if (is_composing && !g_list_find (priv->chats_composing, chat)) {
727                 priv->chats_composing = g_list_prepend (priv->chats_composing, chat);
728         } else {
729                 priv->chats_composing = g_list_remove (priv->chats_composing, chat);
730         }
731
732         chat_window_update_chat_tab (chat);
733 }
734
735 static void
736 chat_window_set_urgency_hint (EmpathyChatWindow *window,
737                               gboolean          urgent)
738 {
739         EmpathyChatWindowPriv *priv;
740
741         priv = GET_PRIV (window);
742
743         empathy_debug (DEBUG_DOMAIN, "Turning %s urgency hint",
744                        urgent ? "on" : "off");
745         gtk_window_set_urgency_hint (GTK_WINDOW (priv->dialog), urgent);
746 }
747
748 static void
749 chat_window_new_message_cb (EmpathyChat       *chat,
750                             EmpathyMessage    *message,
751                             EmpathyChatWindow *window)
752 {
753         EmpathyChatWindowPriv *priv;
754         gboolean              has_focus;
755         gboolean              needs_urgency;
756
757         priv = GET_PRIV (window);
758
759         has_focus = empathy_chat_window_has_focus (window);
760         
761         if (has_focus && priv->current_chat == chat) {
762                 return;
763         }
764         
765         needs_urgency = empathy_message_should_highlight (message);
766
767         if (needs_urgency && !has_focus) {
768                 chat_window_set_urgency_hint (window, TRUE);
769         }
770
771         if (!g_list_find (priv->chats_new_msg, chat)) {
772                 priv->chats_new_msg = g_list_prepend (priv->chats_new_msg, chat);
773                 chat_window_update_chat_tab (chat);
774         }
775 }
776
777 static GtkNotebook *
778 chat_window_detach_hook (GtkNotebook *source,
779                          GtkWidget   *page,
780                          gint         x,
781                          gint         y,
782                          gpointer     user_data)
783 {
784         EmpathyChatWindowPriv *priv;
785         EmpathyChatWindow     *window, *new_window;
786         EmpathyChat           *chat;
787
788         chat = EMPATHY_CHAT (page);
789         window = chat_window_find_chat (chat);
790
791         new_window = empathy_chat_window_new ();
792         priv = GET_PRIV (new_window);
793
794         empathy_debug (DEBUG_DOMAIN, "Detach hook called");
795
796         empathy_chat_window_move_chat (window, new_window, chat);
797
798         gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
799         gtk_widget_show (priv->dialog);
800
801         return NULL;
802 }
803
804 static void
805 chat_window_page_switched_cb (GtkNotebook      *notebook,
806                               GtkNotebookPage  *page,
807                               gint              page_num,
808                               EmpathyChatWindow *window)
809 {
810         EmpathyChatWindowPriv *priv;
811         EmpathyChat           *chat;
812         GtkWidget            *child;
813
814         empathy_debug (DEBUG_DOMAIN, "Page switched");
815
816         priv = GET_PRIV (window);
817
818         child = gtk_notebook_get_nth_page (notebook, page_num);
819         chat = EMPATHY_CHAT (child);
820
821         if (priv->page_added) {
822                 priv->page_added = FALSE;
823                 empathy_chat_scroll_down (chat);
824         }
825         else if (priv->current_chat == chat) {
826                 return;
827         }
828
829         priv->current_chat = chat;
830         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
831
832         chat_window_update_chat_tab (chat);
833 }
834
835 static void
836 chat_window_page_added_cb (GtkNotebook      *notebook,
837                            GtkWidget        *child,
838                            guint             page_num,
839                            EmpathyChatWindow *window)
840 {
841         EmpathyChatWindowPriv *priv;
842         EmpathyChat           *chat;
843
844         priv = GET_PRIV (window);
845
846         /* If we just received DND to the same window, we don't want
847          * to do anything here like removing the tab and then readding
848          * it, so we return here and in "page-added".
849          */
850         if (priv->dnd_same_window) {
851                 empathy_debug (DEBUG_DOMAIN, "Page added (back to the same window)");
852                 priv->dnd_same_window = FALSE;
853                 return;
854         }
855
856         empathy_debug (DEBUG_DOMAIN, "Page added");
857
858         /* Get chat object */
859         chat = EMPATHY_CHAT (child);
860
861         /* Connect chat signals for this window */
862         g_signal_connect (chat, "composing",
863                           G_CALLBACK (chat_window_composing_cb),
864                           window);
865         g_signal_connect (chat, "new-message",
866                           G_CALLBACK (chat_window_new_message_cb),
867                           window);
868
869         /* Set flag so we know to perform some special operations on
870          * switch page due to the new page being added.
871          */
872         priv->page_added = TRUE;
873
874         /* Get list of chats up to date */
875         priv->chats = g_list_append (priv->chats, chat);
876
877         chat_window_update_chat_tab (chat);
878 }
879
880 static void
881 chat_window_page_removed_cb (GtkNotebook      *notebook,
882                              GtkWidget        *child,
883                              guint             page_num,
884                              EmpathyChatWindow *window)
885 {
886         EmpathyChatWindowPriv *priv;
887         EmpathyChat           *chat;
888
889         priv = GET_PRIV (window);
890
891         /* If we just received DND to the same window, we don't want
892          * to do anything here like removing the tab and then readding
893          * it, so we return here and in "page-added".
894          */
895         if (priv->dnd_same_window) {
896                 empathy_debug (DEBUG_DOMAIN, "Page removed (and will be readded to same window)");
897                 return;
898         }
899
900         empathy_debug (DEBUG_DOMAIN, "Page removed");
901
902         /* Get chat object */
903         chat = EMPATHY_CHAT (child);
904
905         /* Disconnect all signal handlers for this chat and this window */
906         g_signal_handlers_disconnect_by_func (chat,
907                                               G_CALLBACK (chat_window_composing_cb),
908                                               window);
909         g_signal_handlers_disconnect_by_func (chat,
910                                               G_CALLBACK (chat_window_new_message_cb),
911                                               window);
912
913         /* Keep list of chats up to date */
914         priv->chats = g_list_remove (priv->chats, chat);
915         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, chat);
916         priv->chats_composing = g_list_remove (priv->chats_composing, chat);
917
918         if (priv->chats == NULL) {
919                 g_object_unref (window);
920         } else {
921                 chat_window_update (window);
922         }
923 }
924
925 static gboolean
926 chat_window_focus_in_event_cb (GtkWidget        *widget,
927                                GdkEvent         *event,
928                                EmpathyChatWindow *window)
929 {
930         EmpathyChatWindowPriv *priv;
931
932         empathy_debug (DEBUG_DOMAIN, "Focus in event, updating title");
933
934         priv = GET_PRIV (window);
935
936         priv->chats_new_msg = g_list_remove (priv->chats_new_msg, priv->current_chat);
937
938         chat_window_set_urgency_hint (window, FALSE);
939         
940         /* Update the title, since we now mark all unread messages as read. */
941         chat_window_update_chat_tab (priv->current_chat);
942
943         return FALSE;
944 }
945
946 static void
947 chat_window_drag_data_received (GtkWidget        *widget,
948                                 GdkDragContext   *context,
949                                 int               x,
950                                 int               y,
951                                 GtkSelectionData *selection,
952                                 guint             info,
953                                 guint             time,
954                                 EmpathyChatWindow *window)
955 {
956         if (info == DND_DRAG_TYPE_CONTACT_ID) {
957                 EmpathyChat           *chat;
958                 EmpathyChatWindow     *old_window;
959                 McAccount             *account;
960                 const gchar           *id;
961                 gchar                **strv;
962
963                 id = (const gchar*) selection->data;
964
965                 empathy_debug (DEBUG_DOMAIN, "DND contact from roster with id:'%s'", id);
966                 
967                 strv = g_strsplit (id, "/", 2);
968                 account = mc_account_lookup (strv[0]);
969                 chat = empathy_chat_window_find_chat (account, strv[1]);
970
971                 if (!chat) {
972                         empathy_chat_with_contact_id (account, strv[2]);
973                         g_object_unref (account);
974                         g_strfreev (strv);
975                         return;
976                 }
977                 g_object_unref (account);
978                 g_strfreev (strv);
979
980                 old_window = chat_window_find_chat (chat);              
981                 if (old_window) {
982                         if (old_window == window) {
983                                 gtk_drag_finish (context, TRUE, FALSE, time);
984                                 return;
985                         }
986                         
987                         empathy_chat_window_move_chat (old_window, window, chat);
988                 } else {
989                         empathy_chat_window_add_chat (window, chat);
990                 }
991                 
992                 /* Added to take care of any outstanding chat events */
993                 empathy_chat_window_present_chat (chat);
994
995                 /* We should return TRUE to remove the data when doing
996                  * GDK_ACTION_MOVE, but we don't here otherwise it has
997                  * weird consequences, and we handle that internally
998                  * anyway with add_chat() and remove_chat().
999                  */
1000                 gtk_drag_finish (context, TRUE, FALSE, time);
1001         }
1002         else if (info == DND_DRAG_TYPE_TAB) {
1003                 EmpathyChat        **chat;
1004                 EmpathyChatWindow   *old_window = NULL;
1005
1006                 empathy_debug (DEBUG_DOMAIN, "DND tab");
1007
1008                 chat = (void*) selection->data;
1009                 old_window = chat_window_find_chat (*chat);
1010
1011                 if (old_window) {
1012                         EmpathyChatWindowPriv *priv;
1013
1014                         priv = GET_PRIV (window);
1015
1016                         if (old_window == window) {
1017                                 empathy_debug (DEBUG_DOMAIN, "DND tab (within same window)");
1018                                 priv->dnd_same_window = TRUE;
1019                                 gtk_drag_finish (context, TRUE, FALSE, time);
1020                                 return;
1021                         }
1022                         
1023                         priv->dnd_same_window = FALSE;
1024                 }
1025
1026                 /* We should return TRUE to remove the data when doing
1027                  * GDK_ACTION_MOVE, but we don't here otherwise it has
1028                  * weird consequences, and we handle that internally
1029                  * anyway with add_chat() and remove_chat().
1030                  */
1031                 gtk_drag_finish (context, TRUE, FALSE, time);
1032         } else {
1033                 empathy_debug (DEBUG_DOMAIN, "DND from unknown source");
1034                 gtk_drag_finish (context, FALSE, FALSE, time);
1035         }
1036 }
1037
1038 static void
1039 chat_window_finalize (GObject *object)
1040 {
1041         EmpathyChatWindow     *window;
1042         EmpathyChatWindowPriv *priv;
1043
1044         window = EMPATHY_CHAT_WINDOW (object);
1045         priv = GET_PRIV (window);
1046
1047         empathy_debug (DEBUG_DOMAIN, "Finalized: %p", object);
1048
1049         if (priv->save_geometry_id != 0) {
1050                 g_source_remove (priv->save_geometry_id);
1051         }
1052
1053         chat_windows = g_list_remove (chat_windows, window);
1054         gtk_widget_destroy (priv->dialog);
1055
1056         G_OBJECT_CLASS (empathy_chat_window_parent_class)->finalize (object);
1057 }
1058
1059 static void
1060 empathy_chat_window_class_init (EmpathyChatWindowClass *klass)
1061 {
1062         GObjectClass *object_class = G_OBJECT_CLASS (klass);
1063
1064         object_class->finalize = chat_window_finalize;
1065
1066         g_type_class_add_private (object_class, sizeof (EmpathyChatWindowPriv));
1067
1068         /* Set up a style for the close button with no focus padding. */
1069         gtk_rc_parse_string (
1070                 "style \"empathy-close-button-style\"\n"
1071                 "{\n"
1072                 "  GtkWidget::focus-padding = 0\n"
1073                 "  xthickness = 0\n"
1074                 "  ythickness = 0\n"
1075                 "}\n"
1076                 "widget \"*.empathy-close-button\" style \"empathy-close-button-style\"");
1077
1078         gtk_notebook_set_window_creation_hook (chat_window_detach_hook, NULL, NULL);
1079 }
1080
1081 static void
1082 empathy_chat_window_init (EmpathyChatWindow *window)
1083 {
1084         EmpathyChatWindowPriv *priv;
1085         GladeXML             *glade;
1086         GtkAccelGroup        *accel_group;
1087         GClosure             *closure;
1088         GtkWidget            *menu_conv;
1089         GtkWidget            *menu;
1090         gint                  i;
1091         GtkWidget            *chat_vbox;
1092         gchar                *filename;
1093
1094         priv = GET_PRIV (window);
1095
1096         filename = empathy_file_lookup ("empathy-chat-window.glade", "src");
1097         glade = empathy_glade_get_file (filename,
1098                                        "chat_window",
1099                                        NULL,
1100                                        "chat_window", &priv->dialog,
1101                                        "chat_vbox", &chat_vbox,
1102                                        "menu_conv", &menu_conv,
1103                                        "menu_conv_clear", &priv->menu_conv_clear,
1104                                        "menu_conv_insert_smiley", &priv->menu_conv_insert_smiley,
1105                                        "menu_conv_contact", &priv->menu_conv_contact,
1106                                        "menu_conv_close", &priv->menu_conv_close,
1107                                        "menu_edit_cut", &priv->menu_edit_cut,
1108                                        "menu_edit_copy", &priv->menu_edit_copy,
1109                                        "menu_edit_paste", &priv->menu_edit_paste,
1110                                        "menu_tabs_next", &priv->menu_tabs_next,
1111                                        "menu_tabs_prev", &priv->menu_tabs_prev,
1112                                        "menu_tabs_left", &priv->menu_tabs_left,
1113                                        "menu_tabs_right", &priv->menu_tabs_right,
1114                                        "menu_tabs_detach", &priv->menu_tabs_detach,
1115                                        "menu_help_contents", &priv->menu_help_contents,
1116                                        "menu_help_about", &priv->menu_help_about,
1117                                        NULL);
1118         g_free (filename);
1119
1120         empathy_glade_connect (glade,
1121                               window,
1122                               "chat_window", "configure-event", chat_window_configure_event_cb,
1123                               "menu_conv_clear", "activate", chat_window_clear_activate_cb,
1124                               "menu_conv_close", "activate", chat_window_close_activate_cb,
1125                               "menu_edit", "activate", chat_window_edit_activate_cb,
1126                               "menu_edit_cut", "activate", chat_window_cut_activate_cb,
1127                               "menu_edit_copy", "activate", chat_window_copy_activate_cb,
1128                               "menu_edit_paste", "activate", chat_window_paste_activate_cb,
1129                               "menu_tabs_left", "activate", chat_window_tabs_left_activate_cb,
1130                               "menu_tabs_right", "activate", chat_window_tabs_right_activate_cb,
1131                               "menu_tabs_detach", "activate", chat_window_detach_activate_cb,
1132                               "menu_help_contents", "activate", chat_window_help_contents_cb,
1133                               "menu_help_about", "activate", chat_window_help_about_cb,
1134                               NULL);
1135
1136         g_object_unref (glade);
1137
1138         priv->notebook = gtk_notebook_new ();
1139         gtk_notebook_set_group (GTK_NOTEBOOK (priv->notebook), "EmpathyChatWindow"); 
1140         gtk_box_pack_start (GTK_BOX (chat_vbox), priv->notebook, TRUE, TRUE, 0);
1141         gtk_widget_show (priv->notebook);
1142
1143         /* Set up accels */
1144         accel_group = gtk_accel_group_new ();
1145         gtk_window_add_accel_group (GTK_WINDOW (priv->dialog), accel_group);
1146
1147         for (i = 0; i < G_N_ELEMENTS (tab_accel_keys); i++) {
1148                 closure =  g_cclosure_new (G_CALLBACK (chat_window_accel_cb),
1149                                            window,
1150                                            NULL);
1151                 gtk_accel_group_connect (accel_group,
1152                                          tab_accel_keys[i],
1153                                          GDK_MOD1_MASK,
1154                                          0,
1155                                          closure);
1156         }
1157
1158         g_object_unref (accel_group);
1159
1160         /* Set up smiley menu */
1161         menu = empathy_chat_view_get_smiley_menu (
1162                 G_CALLBACK (chat_window_insert_smiley_activate_cb),
1163                 window);
1164         gtk_menu_item_set_submenu (GTK_MENU_ITEM (priv->menu_conv_insert_smiley), menu);
1165
1166         /* Set up signals we can't do with glade since we may need to
1167          * block/unblock them at some later stage.
1168          */
1169
1170         g_signal_connect (priv->dialog,
1171                           "delete_event",
1172                           G_CALLBACK (chat_window_delete_event_cb),
1173                           window);
1174
1175         g_signal_connect_swapped (priv->menu_tabs_prev,
1176                                   "activate",
1177                                   G_CALLBACK (gtk_notebook_prev_page),
1178                                   priv->notebook);
1179         g_signal_connect_swapped (priv->menu_tabs_next,
1180                                   "activate",
1181                                   G_CALLBACK (gtk_notebook_next_page),
1182                                   priv->notebook);
1183
1184         g_signal_connect (priv->dialog,
1185                           "focus_in_event",
1186                           G_CALLBACK (chat_window_focus_in_event_cb),
1187                           window);
1188         g_signal_connect_after (priv->notebook,
1189                                 "switch_page",
1190                                 G_CALLBACK (chat_window_page_switched_cb),
1191                                 window);
1192         g_signal_connect (priv->notebook,
1193                           "page_added",
1194                           G_CALLBACK (chat_window_page_added_cb),
1195                           window);
1196         g_signal_connect (priv->notebook,
1197                           "page_removed",
1198                           G_CALLBACK (chat_window_page_removed_cb),
1199                           window);
1200
1201         /* Set up drag and drop */
1202         gtk_drag_dest_set (GTK_WIDGET (priv->notebook),
1203                            GTK_DEST_DEFAULT_ALL,
1204                            drag_types_dest,
1205                            G_N_ELEMENTS (drag_types_dest),
1206                            GDK_ACTION_MOVE);
1207
1208         g_signal_connect (priv->notebook,
1209                           "drag-data-received",
1210                           G_CALLBACK (chat_window_drag_data_received),
1211                           window);
1212
1213         chat_windows = g_list_prepend (chat_windows, window);
1214
1215         /* Set up private details */
1216         priv->chats = NULL;
1217         priv->chats_new_msg = NULL;
1218         priv->chats_composing = NULL;
1219         priv->current_chat = NULL;
1220 }
1221
1222 EmpathyChatWindow *
1223 empathy_chat_window_new (void)
1224 {
1225         return EMPATHY_CHAT_WINDOW (g_object_new (EMPATHY_TYPE_CHAT_WINDOW, NULL));
1226 }
1227
1228 /* Returns the window to open a new tab in if there is only one window
1229  * visble, otherwise, returns NULL indicating that a new window should
1230  * be added.
1231  */
1232 EmpathyChatWindow *
1233 empathy_chat_window_get_default (void)
1234 {
1235         GList    *l;
1236         gboolean  separate_windows = TRUE;
1237
1238         empathy_conf_get_bool (empathy_conf_get (),
1239                               EMPATHY_PREFS_UI_SEPARATE_CHAT_WINDOWS,
1240                               &separate_windows);
1241
1242         if (separate_windows) {
1243                 /* Always create a new window */
1244                 return NULL;
1245         }
1246
1247         for (l = chat_windows; l; l = l->next) {
1248                 EmpathyChatWindow *chat_window;
1249                 GtkWidget         *dialog;
1250
1251                 chat_window = l->data;
1252
1253                 dialog = empathy_chat_window_get_dialog (chat_window);
1254                 if (empathy_window_get_is_visible (GTK_WINDOW (GTK_WINDOW (dialog)))) {
1255                         /* Found a visible window on this desktop */
1256                         return chat_window;
1257                 }
1258         }
1259
1260         return NULL;
1261 }
1262
1263 GtkWidget *
1264 empathy_chat_window_get_dialog (EmpathyChatWindow *window)
1265 {
1266         EmpathyChatWindowPriv *priv;
1267
1268         g_return_val_if_fail (window != NULL, NULL);
1269
1270         priv = GET_PRIV (window);
1271
1272         return priv->dialog;
1273 }
1274
1275 void
1276 empathy_chat_window_add_chat (EmpathyChatWindow *window,
1277                               EmpathyChat       *chat)
1278 {
1279         EmpathyChatWindowPriv *priv;
1280         GtkWidget             *label;
1281         GtkWidget             *child;
1282         gint                   x, y, w, h;
1283
1284         g_return_if_fail (window != NULL);
1285         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1286
1287         priv = GET_PRIV (window);
1288
1289         /* Reference the chat object */
1290         g_object_ref (chat);
1291
1292         empathy_geometry_load (chat_get_window_id_for_geometry (chat), &x, &y, &w, &h);
1293
1294         if (x >= 0 && y >= 0) {
1295                 /* Let the window manager position it if we don't have
1296                  * good x, y coordinates.
1297                  */
1298                 gtk_window_move (GTK_WINDOW (priv->dialog), x, y);
1299         }
1300
1301         if (w > 0 && h > 0) {
1302                 /* Use the defaults from the glade file if we don't have
1303                  * good w, h geometry.
1304                  */
1305                 gtk_window_resize (GTK_WINDOW (priv->dialog), w, h);
1306         }
1307
1308         child = GTK_WIDGET (chat);
1309         label = chat_window_create_label (window, chat); 
1310         gtk_widget_show (child);
1311
1312         g_signal_connect (chat, "notify::name",
1313                           G_CALLBACK (chat_window_chat_notify_cb),
1314                           NULL);
1315         g_signal_connect (chat, "notify::subject",
1316                           G_CALLBACK (chat_window_chat_notify_cb),
1317                           NULL);
1318         g_signal_connect (chat, "notify::remote-contact",
1319                           G_CALLBACK (chat_window_chat_notify_cb),
1320                           NULL);
1321         chat_window_chat_notify_cb (chat);
1322
1323         gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), child, label);
1324         gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1325         gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (priv->notebook), child, TRUE);
1326         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (priv->notebook), child,
1327                                             TRUE, TRUE, GTK_PACK_START); 
1328
1329         empathy_debug (DEBUG_DOMAIN, 
1330                       "Chat added (%d references)",
1331                       G_OBJECT (chat)->ref_count);
1332 }
1333
1334 void
1335 empathy_chat_window_remove_chat (EmpathyChatWindow *window,
1336                                  EmpathyChat       *chat)
1337 {
1338         EmpathyChatWindowPriv *priv;
1339         gint                   position;
1340         EmpathyContact        *remote_contact;
1341
1342         g_return_if_fail (window != NULL);
1343         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1344
1345         priv = GET_PRIV (window);
1346
1347         g_signal_handlers_disconnect_by_func (chat,
1348                                               chat_window_chat_notify_cb,
1349                                               NULL);
1350         remote_contact = g_object_get_data (G_OBJECT (chat),
1351                                             "chat-window-remote-contact");
1352         if (remote_contact) {
1353                 g_signal_handlers_disconnect_by_func (remote_contact,
1354                                                       chat_window_update_chat_tab,
1355                                                       chat);
1356         }
1357
1358         position = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1359                                           GTK_WIDGET (chat));
1360         gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), position);
1361
1362         empathy_debug (DEBUG_DOMAIN, 
1363                       "Chat removed (%d references)", 
1364                       G_OBJECT (chat)->ref_count - 1);
1365
1366         g_object_unref (chat);
1367 }
1368
1369 void
1370 empathy_chat_window_move_chat (EmpathyChatWindow *old_window,
1371                                EmpathyChatWindow *new_window,
1372                                EmpathyChat       *chat)
1373 {
1374         GtkWidget *widget;
1375
1376         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (old_window));
1377         g_return_if_fail (EMPATHY_IS_CHAT_WINDOW (new_window));
1378         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1379
1380         widget = GTK_WIDGET (chat);
1381
1382         empathy_debug (DEBUG_DOMAIN,
1383                       "Chat moving with widget:%p (%d references)", 
1384                       widget,
1385                       G_OBJECT (widget)->ref_count);
1386
1387         /* We reference here to make sure we don't loose the widget
1388          * and the EmpathyChat object during the move.
1389          */
1390         g_object_ref (chat);
1391         g_object_ref (widget);
1392
1393         empathy_chat_window_remove_chat (old_window, chat);
1394         empathy_chat_window_add_chat (new_window, chat);
1395
1396         g_object_unref (widget);
1397         g_object_unref (chat);
1398 }
1399
1400 void
1401 empathy_chat_window_switch_to_chat (EmpathyChatWindow *window,
1402                                     EmpathyChat       *chat)
1403 {
1404         EmpathyChatWindowPriv *priv;
1405         gint                  page_num;
1406
1407         g_return_if_fail (window != NULL);
1408         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1409
1410         priv = GET_PRIV (window);
1411
1412         page_num = gtk_notebook_page_num (GTK_NOTEBOOK (priv->notebook),
1413                                           GTK_WIDGET (chat));
1414         gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook),
1415                                        page_num);
1416 }
1417
1418 gboolean
1419 empathy_chat_window_has_focus (EmpathyChatWindow *window)
1420 {
1421         EmpathyChatWindowPriv *priv;
1422         gboolean              has_focus;
1423
1424         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1425
1426         priv = GET_PRIV (window);
1427
1428         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1429
1430         return has_focus;
1431 }
1432
1433 EmpathyChat *
1434 empathy_chat_window_find_chat (McAccount   *account,
1435                                const gchar *id)
1436 {
1437         GList *l;
1438
1439         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
1440         g_return_val_if_fail (!G_STR_EMPTY (id), NULL);
1441
1442         for (l = chat_windows; l; l = l->next) {
1443                 EmpathyChatWindowPriv *priv;
1444                 EmpathyChatWindow     *window;
1445                 GList                *ll;
1446
1447                 window = l->data;
1448                 priv = GET_PRIV (window);
1449
1450                 for (ll = priv->chats; ll; ll = ll->next) {
1451                         EmpathyChat *chat;
1452
1453                         chat = ll->data;
1454
1455                         if (empathy_account_equal (account, empathy_chat_get_account (chat)) &&
1456                             strcmp (id, empathy_chat_get_id (chat)) == 0) {
1457                                 return chat;
1458                         }
1459                 }
1460         }
1461
1462         return NULL;
1463 }
1464
1465 void
1466 empathy_chat_window_present_chat (EmpathyChat *chat)
1467 {
1468         EmpathyChatWindow     *window;
1469         EmpathyChatWindowPriv *priv;
1470
1471         g_return_if_fail (EMPATHY_IS_CHAT (chat));
1472
1473         window = chat_window_find_chat (chat);
1474
1475         /* If the chat has no window, create one */
1476         if (window == NULL) {
1477                 window = empathy_chat_window_get_default ();
1478                 if (!window) {
1479                         window = empathy_chat_window_new ();
1480                 }
1481
1482                 empathy_chat_window_add_chat (window, chat);
1483         }
1484
1485         priv = GET_PRIV (window);
1486         empathy_chat_window_switch_to_chat (window, chat);
1487         empathy_window_present (GTK_WINDOW (priv->dialog), TRUE);
1488
1489         gtk_widget_grab_focus (chat->input_text_view); 
1490 }
1491
1492 #if 0
1493 static gboolean
1494 chat_window_should_play_sound (EmpathyChatWindow *window)
1495 {
1496         EmpathyChatWindowPriv *priv = GET_PRIV (window);
1497         gboolean               has_focus = FALSE;
1498
1499         g_return_val_if_fail (EMPATHY_IS_CHAT_WINDOW (window), FALSE);
1500
1501         g_object_get (priv->dialog, "has-toplevel-focus", &has_focus, NULL);
1502
1503         return !has_focus;
1504 }
1505 #endif