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